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

Function crdtArray

packages/core/src/array.ts:162–276  ·  view source on GitHub ↗
(initializer: T[], arr = new Y.Array<T>())

Source from the content-addressed store, hash-verified

160}
161
162export function crdtArray<T>(initializer: T[], arr = new Y.Array<T>()) {
163 if (arr[$reactive]) {
164 throw new Error("unexpected");
165 // arr = arr[$reactive].raw;
166 }
167 const implementation = arrayImplementation(arr);
168
169 const proxy = new Proxy(implementation as any as CRDTArray<T>, {
170 set: (target, pArg, value) => {
171 const p = propertyToNumber(pArg);
172 if (typeof p !== "number") {
173 throw new Error();
174 }
175 // TODO map.set(p, smartValue(value));
176 throw new Error("array assignment is not implemented / supported");
177 },
178 get: (target, pArg, receiver) => {
179 const p = propertyToNumber(pArg);
180
181 if (p === INTERNAL_SYMBOL) {
182 return arr;
183 }
184
185 if (typeof p === "number") {
186 let ic: any;
187 if (receiver && receiver[$reactiveproxy]) {
188 ic = receiver[$reactiveproxy]?.implicitObserver;
189 (arr as any)._implicitObserver = ic;
190 } else {
191 // console.warn("no receiver getting property", p);
192 }
193 let ret = arr.get(p) as any;
194 ret = parseYjsReturnValue(ret, ic);
195 return ret;
196 }
197
198 if (p === Symbol.toStringTag) {
199 return "Array";
200 }
201
202 if (p === Symbol.iterator) {
203 const values = arr.slice();
204 return Reflect.get(values, p);
205 }
206
207 if (p === "length") {
208 return arr.length;
209 }
210 // forward to arrayimplementation
211 const ret = Reflect.get(target, p, receiver);
212 return ret;
213 },
214 // getOwnPropertyDescriptor: (target, pArg) => {
215 // const p = propertyToNumber(pArg);
216 // if (typeof p === "number" && p < arr.length && p >= 0) {
217 // return { configurable: true, enumerable: true, value: arr.get(p) };
218 // } else {
219 // return undefined;

Callers 1

crdtValueFunction · 0.90

Calls 3

parseYjsReturnValueFunction · 0.90
arrayImplementationFunction · 0.85
propertyToNumberFunction · 0.85

Tested by

no test coverage detected