Skip to content

Commit

Permalink
feat(atoms)!: remove createdAt timestamp tracking (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
bowheart authored Jan 7, 2025
1 parent 681af78 commit 28102e4
Show file tree
Hide file tree
Showing 13 changed files with 9 additions and 113 deletions.
1 change: 0 additions & 1 deletion packages/atoms/src/classes/GraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ export class ExternalNode extends GraphNode {
this.i = source

addEdge(this, source, {
createdAt: this.e._idGenerator.now(),
flags,
operation: operation,
})
Expand Down
21 changes: 2 additions & 19 deletions packages/atoms/src/classes/IdGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export class IdGenerator {
* Generate an id that is guaranteed to be unique in this ecosystem and
* pretty-much-guaranteed to be unique globally.
*
* This method and `IdGenerator#now()` are the only methods in Zedux that
* produce random values.
* This method is the only method in Zedux that produces random values.
*
* Override these when testing to create reproducible graphs/dehydrations that
* Override this when testing to create reproducible graphs/dehydrations that
* can be used easily in snapshot testing. See our setup in the Zedux repo at
* `<repo root>/packages/react/test/utils/ecosystem.ts` for an example.
*/
Expand Down Expand Up @@ -69,22 +68,6 @@ export class IdGenerator {
})
}

/**
* Generate a timestamp. Pass true to make it a high res timestamp if possible
*
* This method and `IdGenerator#generateId()` are the only methods in Zedux
* that produce random values.
*
* Override these when testing to create reproducible graphs/dehydrations that
* can be used easily in snapshot testing. See our setup in the Zedux repo at
* `<repo root>/packages/react/test/utils/ecosystem.ts` for an example.
*/
public now(highRes?: boolean) {
return highRes && typeof performance !== 'undefined'
? performance.now()
: Date.now()
}

private cacheClass(instance: { new (): any }) {
let id = this.weakCache.get(instance)
if (id) return id
Expand Down
2 changes: 0 additions & 2 deletions packages/atoms/src/classes/instances/AtomInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export class AtomInstance<
* @see Signal.c
*/
public c?: Cleanup
public _createdAt: number
public _injectors?: InjectorDescriptor[]
public _isEvaluating?: boolean
public _nextInjectors?: InjectorDescriptor[]
Expand All @@ -204,7 +203,6 @@ export class AtomInstance<
public readonly p: G['Params']
) {
super(e, id, undefined) // TODO NOW: fix this undefined
this._createdAt = e._idGenerator.now()
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/atoms/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ export interface GraphEdgeConfig {

// TODO: optimize this internal object to use single-letter properties
export interface GraphEdge {
createdAt: number
flags: number // calculated from the EdgeFlags
operation: string

Expand Down
14 changes: 7 additions & 7 deletions packages/atoms/src/utils/evaluationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface EvaluationContext {
*/
let evaluationContext: EvaluationContext = {}

const perf = typeof performance === 'undefined' ? Date : performance

/**
* In the current buffer, draw a new edge between the currently evaluating graph
* node and the passed node. This is how automatic dependencies are created
Expand All @@ -47,14 +49,13 @@ export const bufferEdge = (
operation: string,
flags: number
) => {
const { e, s } = evaluationContext.n!
const { s } = evaluationContext.n!
const existingEdge = s.get(source)

if (existingEdge) {
if (flags < (existingEdge.p || OutOfRange)) existingEdge.p = flags
} else {
s.set(source, {
createdAt: e._idGenerator.now(),
flags: OutOfRange, // set to an out-of-range flag to indicate a new edge
p: flags,
operation,
Expand Down Expand Up @@ -86,12 +87,11 @@ export const destroyBuffer = (
}

const finishBuffer = (previousNode?: GraphNode, previousStartTime?: number) => {
const { _idGenerator, _mods, modBus } = evaluationContext.n!.e
const { _mods, modBus } = evaluationContext.n!.e

if (_mods.evaluationFinished) {
const time = evaluationContext.s
? _idGenerator.now(true) - evaluationContext.s
: 0
const time = evaluationContext.s ? perf.now() - evaluationContext.s : 0

const action: ActionFactoryPayloadType<
typeof pluginActions.evaluationFinished
> = { node: evaluationContext.n!, time }
Expand Down Expand Up @@ -174,6 +174,6 @@ export const startBuffer = (node: GraphNode) => {
evaluationContext.n = node

if (node.e._mods.evaluationFinished) {
evaluationContext.s = node.e._idGenerator.now(true)
evaluationContext.s = perf.now()
}
}
6 changes: 0 additions & 6 deletions packages/react/test/__snapshots__/types.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ exports[`react types signals 1`] = `
"className": "Signal",
"observers": {
"no-1": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
"no-2": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
"no-3": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -31,7 +28,6 @@ exports[`react types signals 1`] = `
"observers": {},
"sources": {
"@signal-0": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -45,7 +41,6 @@ exports[`react types signals 1`] = `
"observers": {},
"sources": {
"@signal-0": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -59,7 +54,6 @@ exports[`react types signals 1`] = `
"observers": {},
"sources": {
"@signal-0": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"no-1": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
},
"sources": {
"@@selector-common-name-0": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
Expand All @@ -27,15 +25,13 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "SelectorInstance",
"observers": {
"1": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
},
},
"sources": {
"root": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -50,7 +46,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"observers": {},
"sources": {
"1": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -63,7 +58,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"@@selector-common-name-0": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -83,14 +77,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"no-3": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
},
"sources": {
"@@selector-common-name-2": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
Expand All @@ -104,15 +96,13 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "SelectorInstance",
"observers": {
"2": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
},
},
"sources": {
"root": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -127,7 +117,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"observers": {},
"sources": {
"2": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -140,7 +129,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"@@selector-common-name-2": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -160,14 +148,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"no-4": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
},
"sources": {
"@@selector-common-name-0": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
Expand All @@ -181,14 +167,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"no-3": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
},
"sources": {
"@@selector-common-name-2": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
Expand All @@ -202,15 +186,13 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "SelectorInstance",
"observers": {
"1": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
},
},
"sources": {
"root": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -224,15 +206,13 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "SelectorInstance",
"observers": {
"2": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
},
},
"sources": {
"root": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -247,7 +227,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"observers": {},
"sources": {
"2": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -261,7 +240,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"observers": {},
"sources": {
"1": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -274,13 +252,11 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"@@selector-common-name-0": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
},
"@@selector-common-name-2": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -300,14 +276,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"no-4": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
},
"sources": {
"@@selector-common-name-0": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
Expand All @@ -321,15 +295,13 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "SelectorInstance",
"observers": {
"1": {
"createdAt": 123456789,
"flags": 0,
"operation": "select",
"p": undefined,
},
},
"sources": {
"root": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand All @@ -344,7 +316,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"observers": {},
"sources": {
"1": {
"createdAt": 123456789,
"flags": 3,
"operation": "on",
},
Expand All @@ -357,7 +328,6 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"className": "AtomInstance",
"observers": {
"@@selector-common-name-0": {
"createdAt": 123456789,
"flags": 0,
"operation": "get",
"p": undefined,
Expand Down
Loading

0 comments on commit 28102e4

Please # to comment.