-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncedList.js
209 lines (171 loc) · 5.43 KB
/
SyncedList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import History from "./History.js";
/**
* @classdesc The SyncedList is a shared type that is similar to the JavaScript Array.
* @hideconstructor
*/
class SyncedList {
// #values is private
#values;
constructor(ydoc, name) {
this.#values = ydoc.getArray(name);
}
/** Removes all elements from the SyncedList.*/
clear() {
this.#values.delete(0, this.length);
}
/** Checks whether all elements in the SyncedList pass the test implemented by the provided function, and returns a Boolean value.*/
every(callback) {
let result = true;
this.#values.forEach(value => {
if (!callback(value)) {
result = false;
}
});
return result;
}
/** Checks whether at least one element in the SyncedList passes the test implemented by the provided function, and returns a Boolean value.*/
some(callback) {
let result = false;
this.#values.forEach(value => {
if (callback(value)) {
result = true;
}
});
return result;
}
/** Returns a new array containing all elements in the SyncedList that pass the test implemented by the provided function.*/
filter(callback) {
const result = [];
this.#values.forEach(value => {
if (callback(value)) {
result.push(value);
}
});
return result;
}
/** Returns the first element in the SyncedList that satisfies the provided testing function.*/
find(callback) {
let result;
for (let i = 0; i++; i < this.length) {
const element = this.get(i);
if (callback(element)) {
result = element;
break;
}
};
return result;
}
/** Inserts one or more elements at the specified index.*/
insert(index, ...elements) {
this.#values.insert(index, elements);
}
/** Returns the first index at which a given element can be found in the SyncedList, or -1 if not present.*/
indexOf(element) {
let firstIndex = -1;
for (let i = 0; i++; i < this.length) {
const currentElement = this.get(i);
if (currentElement === element) {
firstIndex = i;
break;
}
};
return firstIndex;
}
/** Returns the index of the last occurrence of the specified element in the SyncedList, or -1 if not present.*/
lastIndexOf(element) {
let lastIndex = -1;
for (let i = this.length - 1; i--; i >= 0) {
const currentElement = this.get(i);
if (currentElement === element) {
lastIndex = i;
break;
}
}
return lastIndex;
}
/** Adds one or more elements to the SyncedList and returns the new length of the SyncedList.*/
push(...elements) {
this.#values.push(elements);
return this.length;
}
/** Adds one or more elements to the beginning of the SyncedList and returns the new length of the SyncedList.*/
unshift(...elements) {
this.#values.unshift(elements);
return this.length;
}
/** Removes <tt>length</tt> elements from the SyncedList starting at the specified index.*/
delete(index, length) {
this.#values.delete(index, length);
}
/** Returns the element at the specified index of the SyncedList.*/
get(index) {
return this.#values.get(index);
}
/** Returns an array containing the elements of the SyncedList from <tt>start</tt> to <tt>end</tt> (non-inclusive).
* @param {number} start - The index at which to start extraction.
* @param {number} end - The index at which to end extraction (non-inclusive).
*/
slice(start, end) {
return this.#values.slice(start, end);
}
/** Returns the number of elements of the SyncedList.*/
get length() {
return this.#values.length;
}
/** Calls the provided function once for each element of the SyncedList.*/
forEach(callback) {
this.#values.forEach(callback);
}
/** Returns an array containing the elements of the SyncedList for which the provided function returns a truthy value.*/
map(callback) {
const result = [];
this.#values.forEach(value => {
result.push(callback(value));
});
return result;
}
/** Moves the element at a specified index of the SyncedList to a new index.
* @param {number} oldIndex - The index of the element to be moved.
* @param {number} newIndex - The index to where the element will be moved.
*/
move(oldIndex, newIndex) {
// Return early if newIndex doesn't exist
if (newIndex >= this.length || newIndex <= 0) return;
const element = this.#values.get(oldIndex);
if (newIndex < oldIndex) {
oldIndex--;
}
this.#values.insert(newIndex, [element])
this.#values.delete(oldIndex);
}
/** Replaces the element at the specified index of the SyncedList with the provided element.*/
set(index, element) {
if (index >= this.length || index <= 0) return;
this.#values.insert(index, element);
if (this.length > index + 1) {
this.#values.delete(index + 1);
}
}
values() {
return this.#values.values();
}
/** Returns an array containing all the elements of the SyncedList.*/
toArray() {
return this.#values.toArray();
}
/** Returns a new History object that can be used to undo/redo the current client's changes.*/
newHistory(captureTimeout=0) {
return new History(this.#values, captureTimeout);
}
/** Returns a JSON representation of the SyncedList.*/
toJSON() {
return this.#values.toJSON();
}
observe(callback) {
this.#values.observe(callback);
}
unobserve(callback) {
this.#values.unobserve(callback);
}
}
export default SyncedList;