Skip to content

Commit

Permalink
Implement lastCompute (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
hatmatty authored Feb 1, 2025
1 parent 234d607 commit 16a1619
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 3 deletions.
13 changes: 12 additions & 1 deletion docs/api-reference/graph/types/graphobject.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export type GraphObject = ScopedObject & {
dependencySet: {[GraphObject]: unknown},
dependentSet: {[GraphObject]: unknown},
lastChange: number?,
lastCompute: number?,
timeliness: "lazy" | "eager",
validity: "valid" | "invalid" | "busy",
_evaluate: (GraphObject, lastChange: number?) -> boolean
_evaluate: (GraphObject) -> boolean
}
```

Expand Down Expand Up @@ -75,6 +76,16 @@ object.
The `os.clock()` time of this object's most recent meaningful change, or `nil`
if the object is newly created.

<h3 markdown>
lastCompute
<span class="fusiondoc-api-type">
: number?
</span>
</h3>

The `os.clock()` time of when this object was most recently computed, or `nil`
if the object is newly created.

<h3 markdown>
timeliness
<span class="fusiondoc-api-type">
Expand Down
1 change: 1 addition & 0 deletions src/Animation/ExternalTime.luau
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ local function ExternalTime(
createdAt = createdAt,
dependentSet = {},
lastChange = nil,
lastCompute = nil,
scope = scope,
validity = "invalid"
},
Expand Down
1 change: 1 addition & 0 deletions src/Animation/Spring.luau
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ local function Spring<T>(
dependencySet = {},
dependentSet = {},
lastChange = nil,
lastCompute = nil,
scope = scope,
validity = "invalid",
_activeDamping = -1,
Expand Down
1 change: 1 addition & 0 deletions src/Animation/Stopwatch.luau
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ local function Stopwatch(
dependencySet = {},
dependentSet = {},
lastChange = nil,
lastCompute = nil,
scope = scope,
validity = "invalid",
_EXTREMELY_DANGEROUS_usedAsValue = 0,
Expand Down
1 change: 1 addition & 0 deletions src/Animation/Tween.luau
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ local function Tween<T>(
dependencySet = {},
dependentSet = {},
lastChange = nil,
lastCompute = nil,
scope = scope,
validity = "invalid",
_activeDuration = nil,
Expand Down
5 changes: 3 additions & 2 deletions src/Graph/evaluate.luau
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ local function evaluate(
if target.validity == "busy" then
return External.logError("infiniteLoop")
end
local firstEvaluation = target.lastChange == nil
local firstEvaluation = target.lastCompute == nil or target.lastChange == nil
local isInvalid = target.validity == "invalid"
if firstEvaluation or isInvalid or forceComputation then
local needsComputation = firstEvaluation or forceComputation
if not needsComputation then
for dependency in target.dependencySet do
evaluate(dependency, false)
if dependency.lastChange > target.lastChange then
if dependency.lastChange > target.lastCompute then
needsComputation = true
break
end
Expand All @@ -42,6 +42,7 @@ local function evaluate(
end
target.validity = "busy"
targetMeaningfullyChanged = target:_evaluate() or firstEvaluation
target.lastCompute = os.clock()
end
if targetMeaningfullyChanged then
target.lastChange = os.clock()
Expand Down
1 change: 1 addition & 0 deletions src/State/Computed.luau
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ local function Computed<T, S>(
dependencySet = {},
dependentSet = {},
lastChange = nil,
lastCompute = nil,
scope = scope,
validity = "invalid",
_EXTREMELY_DANGEROUS_usedAsValue = nil,
Expand Down
1 change: 1 addition & 0 deletions src/State/Value.luau
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local function Value<T>(
createdAt = createdAt,
dependentSet = {},
lastChange = os.clock(),
lastCompute = os.clock(),
scope = scope,
validity = "valid",
_EXTREMELY_DANGEROUS_usedAsValue = initialValue
Expand Down
1 change: 1 addition & 0 deletions src/Types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type GraphObject = ScopedObject & {
dependencySet: {[GraphObject]: unknown},
dependentSet: {[GraphObject]: unknown},
lastChange: number?,
lastCompute: number?,
timeliness: "lazy" | "eager",
validity: "valid" | "invalid" | "busy",
_evaluate: (GraphObject) -> boolean
Expand Down
1 change: 1 addition & 0 deletions test/Spec/Graph/evaluate.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ return function()
local seen = {}
for _, searchTarget in searchNow do
searchTarget.lastChange = -depth
searchTarget.lastCompute = -depth
searchTarget.validity = if depth == 0 then "valid" else "invalid"
for dependent in searchTarget.dependentSet do
if seen[dependent] then
Expand Down
1 change: 1 addition & 0 deletions test/Util/Graphs.luau
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ function Graphs.make(
dependencySet = {},
dependentSet = {},
lastChange = nil,
lastCompute = nil,
timeliness = "lazy" :: "lazy",
validity = "valid" :: "valid",
_evaluate = function()
Expand Down

0 comments on commit 16a1619

Please sign in to comment.