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(parentAdapton = null) {
const result = await this._compute(parentAdapton)
if (parentAdapton) {
addEdge(parentAdapton, this)
}
return result
}
async _compute() {
if (this._isClean) {
return await this._promise
}
for (const childAdapton of new Set(this._childAdaptons)) {
removeEdge(this, childAdapton)
}
this._isClean = true
this._promise = this._thunk()
return this.compute()
}
}