class Adapton {
constructor(thunk) {
this._thunk = thunk
this._promise = Promise.resolve(null)
this._isClean = false
this._childAdaptons = new Set()
this._parentAdaptons = new Set()
}
dirty() {
if (this._isClean) {
this._isClean = false
for (const parentAdapton of this._parentAdaptons) {
parentAdapton.dirty()
}
}
}
async compute() {
if (this._isClean) {
return await this._promise
}
this._isClean = true
this._promise = this._thunk()
return this.compute()
}
}