(initializer: T, map = new Y.Map<any>())
| 18 | }; |
| 19 | |
| 20 | export function crdtObject<T extends ObjectSchemaType>(initializer: T, map = new Y.Map<any>()) { |
| 21 | if (map[$reactive]) { |
| 22 | throw new Error("unexpected"); |
| 23 | // map = map[$reactive].raw; |
| 24 | } |
| 25 | |
| 26 | const proxy = new Proxy({} as any as CRDTObject<T>, { |
| 27 | set: (target, p, value) => { |
| 28 | if (typeof p !== "string") { |
| 29 | throw new Error(); |
| 30 | } |
| 31 | const wrapped = crdtValue(value); // TODO: maybe set cache |
| 32 | let valueToSet = getYjsValue(wrapped) || wrapped; |
| 33 | |
| 34 | if (valueToSet instanceof Box) { |
| 35 | valueToSet = valueToSet.value; |
| 36 | } |
| 37 | |
| 38 | if (valueToSet instanceof Y.AbstractType && valueToSet.parent) { |
| 39 | throw new Error("Not supported: reassigning object that already occurs in the tree."); |
| 40 | } |
| 41 | |
| 42 | map.set(p, valueToSet); |
| 43 | |
| 44 | return true; |
| 45 | }, |
| 46 | get: (target, p, receiver) => { |
| 47 | if (p === INTERNAL_SYMBOL) { |
| 48 | return map; |
| 49 | } |
| 50 | if (typeof p !== "string") { |
| 51 | return Reflect.get(target, p); |
| 52 | // throw new Error("get non string parameter"); |
| 53 | } |
| 54 | let ic: any; |
| 55 | if (receiver && receiver[$reactiveproxy]) { |
| 56 | ic = receiver[$reactiveproxy]?.implicitObserver; |
| 57 | (map as any)._implicitObserver = ic; |
| 58 | } else { |
| 59 | // console.warn("no receiver getting property", p); |
| 60 | } |
| 61 | let ret = map.get(p); |
| 62 | ret = parseYjsReturnValue(ret, ic); |
| 63 | return ret; |
| 64 | }, |
| 65 | deleteProperty: (target, p) => { |
| 66 | if (typeof p !== "string") { |
| 67 | throw new Error(); |
| 68 | } |
| 69 | if (map.has(p)) { |
| 70 | map.delete(p); |
| 71 | return true; |
| 72 | } else { |
| 73 | return false; |
| 74 | } |
| 75 | }, |
| 76 | has: (target, p) => { |
| 77 | if (typeof p === "string" && map.has(p)) { |
no test coverage detected