Skip to content

Commit

Permalink
CORE: Various reactivity & race condition fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pckool committed Jul 11, 2022
1 parent 7265630 commit 99581b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": "1.2.0",
"npmClient": "yarn",
"useWorkspaces": true,
"packages": [
Expand All @@ -16,6 +17,5 @@
],
"registry": "https://registry.npmjs.org/"
}
},
"version": "1.2.0"
}
}
7 changes: 6 additions & 1 deletion packages/plexus-core/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class ComputedStateInstance<ValueType extends PlexusStateType = any> exte
// utilizing maps because it allows us to preform a lookup in O(1)
_depsDestroyers: Map<Dependency, ReturnType<WatchableMutable<any>["watch"]>>
_deps: Set<WatchableMutable<any>>
_ready: boolean
}
private instance: () => PlexusInstance
private computeFn: () => ValueType
Expand Down Expand Up @@ -41,6 +42,7 @@ export class ComputedStateInstance<ValueType extends PlexusStateType = any> exte
// utilizing maps because it allows us to preform a lookup in O(1)
_depsDestroyers: new Map<Dependency, ReturnType<WatchableMutable["watch"]>>(),
_deps: new Set(deps),
_ready: false,
}
this.refreshDeps()

Expand All @@ -51,8 +53,11 @@ export class ComputedStateInstance<ValueType extends PlexusStateType = any> exte
if (!this.instance()._computedStates.has(this)) {
this.instance().runtime.log("info", `Hoisting computed state ${this.id} with value`, this.value, ` to instance`)
this.instance()._computedStates.add(this)
// this.instance().storage?.sync()
this.instance().storage?.sync()
}
if (this._internalStore._ready) return
this._internalStore._ready = true
this.instance().runtime.log("info", `Computed state ${this.id} is ready`)
}
/**
* Internal Helper Function; for each dependency, add a watcher to the state that will update the computed state when a dependency changes
Expand Down
25 changes: 14 additions & 11 deletions packages/plexus-core/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export class StateInstance<StateValue extends PlexusStateType> extends Watchable
this.instance().storage?.sync()
}
}
if (this._internalStore._ready) return
this._internalStore._ready = true
this.instance().runtime.log("info", `State ${this.id} is ready`)
// this.instance().runtime.broadcast(this.id, this._watchableStore._value)
}
/**
* Set the value of the state
Expand Down Expand Up @@ -99,9 +103,9 @@ export class StateInstance<StateValue extends PlexusStateType> extends Watchable
// update the runtime conductor
// if (this._internalStore._persist) this.instance().storage?.set(this._internalStore._name, this._internalStore._value)
// this.instance().runtime.broadcast(this._internalStore._internalId, value)
this.mount()
super.set(value)
if (this._internalStore._persist) this.instance().storage?.set(this._internalStore._name, this._watchableStore._value)
// if (!this._internalStore._ready) this.mount()
// this.instance().runtime.broadcast(this.id, value)

return this
Expand Down Expand Up @@ -236,16 +240,15 @@ export class StateInstance<StateValue extends PlexusStateType> extends Watchable
this.instance().runtime.log("debug", `Accessing Stateful value ${this.instanceId}${this._internalStore._persist ? "; Persist Enabled" : ""}`)
this.mount()

if (this._internalStore._persist) {
const val = this.instance().storage?.get(this._internalStore._name)
if (super.value && isEqual(val, super.value)) {
this.instance().runtime.log("debug", `Stateful value ${this.instanceId} is equal to persisted value`)
}
else{
this.instance().runtime.log("debug", `Stateful value ${this.instanceId} is not equal to persisted value`)
this.set(val)
}
}
// if (this._internalStore._persist) {
// const val = this.instance().storage?.get(this._internalStore._name)
// if (super.value && isEqual(val, super.value)) {
// this.instance().runtime.log("debug", `Stateful value ${this.instanceId} is equal to persisted value`)
// } else {
// this.instance().runtime.log("debug", `Stateful value ${this.instanceId} is not equal to persisted value`)
// this.set(val)
// }
// }

// return deepClone(this._internalStore._value)
// return this._internalStore._publicValue
Expand Down

0 comments on commit 99581b1

Please sign in to comment.