Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lastCompute #398

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -84,6 +84,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 @@ -70,6 +70,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 @@ -60,6 +60,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 @@ -42,6 +42,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