@blocksuite/store
Advanced tools
Comparing version 0.3.0-20221228081200-60d8fd8 to 0.3.0-20221228162706-9576a3a
import { Map as YMap } from 'yjs'; | ||
function initialize(object, yMap) { | ||
yMap.forEach((value, key) => { | ||
object[key] = value; | ||
}); | ||
} | ||
function subscribe(object, yMap) { | ||
yMap.observe(event => { | ||
event.keysChanged.forEach(key => { | ||
const type = event.changes.keys.get(key); | ||
if (!type) { | ||
console.error('no possible'); | ||
return; | ||
} | ||
if (type.action === 'delete') { | ||
delete object[key]; | ||
} | ||
else if (type.action === 'add') { | ||
object[key] = yMap.get(key); | ||
} | ||
else if (type.action === 'update') { | ||
object[key] = yMap.get(key); | ||
} | ||
}); | ||
}); | ||
} | ||
export function createYMapProxy(yMap, config = {}) { | ||
const { readonly = false, initializer } = config; | ||
const object = {}; | ||
if (!(yMap instanceof YMap)) { | ||
throw new TypeError(); | ||
} | ||
return new Proxy(yMap, { | ||
initialize(object, yMap); | ||
subscribe(object, yMap); | ||
return new Proxy(object, { | ||
has: (target, p) => { | ||
const has = Reflect.get(target, 'has'); | ||
return Reflect.apply(has, target, [p]); | ||
return Reflect.has(target, p); | ||
}, | ||
@@ -17,21 +44,17 @@ set: (target, p, value, receiver) => { | ||
else { | ||
const set = Reflect.get(target, 'set', receiver); | ||
return Reflect.apply(set, target, [p, value]); | ||
return Reflect.set(target, p, value, receiver); | ||
} | ||
}, | ||
get: (target, p, receiver) => { | ||
const get = Reflect.get(target, 'get', receiver); | ||
const has = Reflect.get(target, 'has'); | ||
if (!Reflect.apply(has, target, [p])) { | ||
if (typeof p === 'string' && typeof initializer?.[p] === 'function') { | ||
const set = Reflect.get(target, 'set', receiver); | ||
const defaultValue = initializer[p](); | ||
Reflect.apply(set, target, [p, defaultValue]); | ||
} | ||
if (typeof p === 'string' && | ||
!yMap.has(p) && | ||
typeof initializer?.[p] === 'function') { | ||
const value = initializer[p](); | ||
yMap.set(p, value); | ||
return value; | ||
} | ||
return Reflect.apply(get, target, [p]); | ||
return Reflect.get(target, p, receiver); | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
}); | ||
} | ||
//# sourceMappingURL=proxy.js.map |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.3.0-20221228081200-60d8fd8", | ||
"version": "0.3.0-20221228162706-9576a3a", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -14,2 +14,27 @@ import { Map as YMap } from 'yjs'; | ||
function initialize(object: Record<string, unknown>, yMap: YMap<unknown>) { | ||
yMap.forEach((value, key) => { | ||
object[key] = value; | ||
}); | ||
} | ||
function subscribe(object: Record<string, unknown>, yMap: YMap<unknown>) { | ||
yMap.observe(event => { | ||
event.keysChanged.forEach(key => { | ||
const type = event.changes.keys.get(key); | ||
if (!type) { | ||
console.error('no possible'); | ||
return; | ||
} | ||
if (type.action === 'delete') { | ||
delete object[key]; | ||
} else if (type.action === 'add') { | ||
object[key] = yMap.get(key); | ||
} else if (type.action === 'update') { | ||
object[key] = yMap.get(key); | ||
} | ||
}); | ||
}); | ||
} | ||
export function createYMapProxy<Data extends Record<string, unknown>>( | ||
@@ -20,9 +45,11 @@ yMap: YMap<unknown>, | ||
const { readonly = false, initializer } = config; | ||
const object = {} as Data; | ||
if (!(yMap instanceof YMap)) { | ||
throw new TypeError(); | ||
} | ||
return new Proxy(yMap, { | ||
initialize(object, yMap); | ||
subscribe(object, yMap); | ||
return new Proxy(object, { | ||
has: (target, p) => { | ||
const has = Reflect.get(target, 'has'); | ||
return Reflect.apply(has, target, [p]); | ||
return Reflect.has(target, p); | ||
}, | ||
@@ -33,20 +60,18 @@ set: (target, p, value, receiver) => { | ||
} else { | ||
const set = Reflect.get(target, 'set', receiver); | ||
return Reflect.apply(set, target, [p, value]); | ||
return Reflect.set(target, p, value, receiver); | ||
} | ||
}, | ||
get: (target, p, receiver) => { | ||
const get = Reflect.get(target, 'get', receiver); | ||
const has = Reflect.get(target, 'has'); | ||
if (!Reflect.apply(has, target, [p])) { | ||
if (typeof p === 'string' && typeof initializer?.[p] === 'function') { | ||
const set = Reflect.get(target, 'set', receiver); | ||
const defaultValue = (initializer[p] as () => unknown)(); | ||
Reflect.apply(set, target, [p, defaultValue]); | ||
} | ||
if ( | ||
typeof p === 'string' && | ||
!yMap.has(p) && | ||
typeof initializer?.[p] === 'function' | ||
) { | ||
const value = (initializer[p] as () => unknown)(); | ||
yMap.set(p, value); | ||
return value; | ||
} | ||
return Reflect.apply(get, target, [p]); | ||
return Reflect.get(target, p, receiver); | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
}) as any; | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
418567
7001