Skip to content

Commit

Permalink
race conditions introduced?
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed May 1, 2024
1 parent 9339d26 commit 21d4008
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/IttyDurable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21d4008

Please sign in to comment.