diff --git a/src/IttyDurable.ts b/src/IttyDurable.ts index 7e76bbf..4f774e3 100644 --- a/src/IttyDurable.ts +++ b/src/IttyDurable.ts @@ -7,22 +7,30 @@ export class IttyDurable extends DurableObject { #persistLoaded: undefined | true // public attributes - persisted: undefined | true + persisted: any constructor(...args: any) { // @ts-ignore super(...args) return new Proxy(this, { - // @ts-ignore - get: (obj, prop, receiver, target = obj[prop]) => + get: ( + obj, + prop, + receiver, // @ts-ignore - (typeof target == 'function' && this.persist(prop)) - || target?.bind?.(obj) || target + target = obj[prop], + fn = target?.bind?.(obj), + ) => { + if (!fn) return target + this.persist() + return async (...args: any) => + this.#syncPersisted().then(() => fn(args)) + } }) } - async isLoaded() { + async #syncPersisted() { if (this.#persistLoaded) return true this.persisted = await this.ctx.storage.get('value') return this.#persistLoaded = true