UNPKG

kr-observable

Version:
79 lines (78 loc) 2.36 kB
import { Utils } from './Utils.js'; import { Global } from './global.js'; const executor = Global.executor; export class ProxyHandler { adm; types = {}; receiver; factory; constructor(adm, factory) { this.adm = adm; this.factory = factory; } #report(property, value) { executor.report(this.adm, property, true); this.adm.report(property, value); } #batch(property) { if (this.types[property] !== Utils.IGNORED) { executor.report(this.adm, property); } if (Global.action) return; if (!this.adm.changes.size) return; if (this.adm.changes.has(property)) { this.adm.batch(true); } } get(target, property) { if (property === Utils.AdmKey) return this.adm; this.#batch(property); return target[property]; } set(target, property, value) { if (!(property in this.types)) { this.types[property] = Utils.WRITABLE; this.#report(property, value); } if (this.types[property] === Utils.ACCESSOR) { return Reflect.set(target, property, value); } if (target[property] !== value) { this.#report(property, value); } if (Utils.isPrimitive(value)) { target[property] = value; } else { target[property] = this.factory.object(property, value, this); } return true; } defineProperty(target, property, desc) { return Reflect.defineProperty(target, property, this.factory.descriptor(property, desc, this)); } deleteProperty(target, property) { if (!(property in target)) return false; const res = Reflect.deleteProperty(target, property); this.#report(property, undefined); return res; } setPrototypeOf(target, proto) { const adm = Utils.getAdm(proto); if (adm) Object.assign(adm, this.adm); return Reflect.setPrototypeOf(target, proto); } has(target, property) { this.#batch(property); return property in target; } getOwnPropertyDescriptor(target, property) { this.#batch(property); return Reflect.getOwnPropertyDescriptor(target, property); } }