MCPcopy
hub / github.com/YousefED/SyncedStore / arrayImplementation

Function arrayImplementation

packages/core/src/array.ts:19–149  ·  view source on GitHub ↗
(arr: Y.Array<T>)

Source from the content-addressed store, hash-verified

17} & T[]; // TODO: should return ArrayImplementation<T> on getter
18
19function arrayImplementation<T>(arr: Y.Array<T>) {
20 const slice = function slice() {
21 let ic = this[$reactiveproxy]?.implicitObserver;
22 (arr as any)._implicitObserver = ic;
23
24 const items = arr.slice.bind(arr).apply(arr, arguments);
25 return items.map((item) => {
26 const ret = parseYjsReturnValue(item, ic);
27 if (ic && typeof ret === "object") {
28 // when using Reactive, we should make sure the returned
29 // object is made reactive with the implicit observer ic
30 return reactive(ret, ic);
31 } else {
32 return ret;
33 }
34 });
35 } as T[]["slice"];
36
37 const wrapItems = function wrapItems(items) {
38 return items.map((item) => {
39 const wrapped = crdtValue(item as any); // TODO
40 let valueToSet = getYjsValue(wrapped) || wrapped;
41 if (valueToSet instanceof Box) {
42 valueToSet = valueToSet.value;
43 }
44 if (valueToSet instanceof Y.AbstractType && valueToSet.parent) {
45 throw new Error("Not supported: reassigning object that already occurs in the tree.");
46 }
47 return valueToSet;
48 });
49 };
50
51 const findIndex = function findIndex() {
52 return [].findIndex.apply(slice.apply(this), arguments);
53 } as T[]["find"];
54
55 const methods = {
56 // get length() {
57 // return arr.length;
58 // },
59 // set length(val: number) {
60 // throw new Error("set length of yjs array is unsupported");
61 // },
62 slice,
63 unshift: (...items: T[]) => {
64 arr.unshift(wrapItems(items));
65 return (arr as any).lengthUntracked;
66 },
67
68 push: (...items: T[]) => {
69 arr.push(wrapItems(items));
70 return (arr as any).lengthUntracked;
71 },
72
73 insert: arr.insert.bind(arr) as Y.Array<T>["insert"],
74 toJSON: arr.toJSON.bind(arr) as Y.Array<T>["toJSON"],
75
76 forEach: function () {

Callers 1

crdtArrayFunction · 0.85

Calls 3

parseYjsReturnValueFunction · 0.90
wrapItemsFunction · 0.85
areSameFunction · 0.85

Tested by

no test coverage detected