@isograph/pico 0.0.0-main-5f205c00
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-MIT +7 -0
- package/README.md +152 -0
- package/dist/index.cjs +1154 -0
- package/dist/index.d.cts +176 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1139 -0
- package/dist/index.mjs.map +1 -0
- package/docs/contracts.md +45 -0
- package/docs/correctness.md +233 -0
- package/docs/design.md +97 -0
- package/docs/disposing-nodes.md +171 -0
- package/docs/lifetimes.md +25 -0
- package/docs/performance.md +85 -0
- package/package.json +49 -0
- package/src/batch/batch.ts +133 -0
- package/src/batch/flush.ts +30 -0
- package/src/batch/notifications.ts +39 -0
- package/src/disposal/age-drain.ts +124 -0
- package/src/disposal/cleanup.ts +171 -0
- package/src/disposal/older-than-updates.ts +132 -0
- package/src/disposal/schedule.ts +78 -0
- package/src/disposal/take.ts +52 -0
- package/src/disposal/update-counter.ts +9 -0
- package/src/graph/edges.ts +167 -0
- package/src/graph/marking.ts +310 -0
- package/src/graph/nodes.ts +196 -0
- package/src/graph/read.ts +150 -0
- package/src/graph/recompute.ts +127 -0
- package/src/graph/refresh.ts +232 -0
- package/src/index.ts +18 -0
- package/src/retention/retain.ts +133 -0
- package/src/state.ts +39 -0
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 Isograph Labs
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
---
|
|
2
|
+
slug: /
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# pico-js
|
|
6
|
+
|
|
7
|
+
The TypeScript package `@isograph/pico` caches values computed from changing inputs. Each computation records the nodes it reads. When a source changes, pico-js uses those dependencies to decide which results it needs to recompute.
|
|
8
|
+
|
|
9
|
+
## Priorities
|
|
10
|
+
|
|
11
|
+
pico-js prioritizes speed over convenience. The caller decides when a source value has changed and writes the new value. A computation can return its previous result when the new result is equivalent, letting pico-js skip dependent work with a `===` check. The [contracts](#contracts) describe what callers must provide. [Puchi](../puchi/README.md) wraps Pico with callable signals, callback-style batches, and error handling.
|
|
12
|
+
|
|
13
|
+
## Example
|
|
14
|
+
|
|
15
|
+
`doubled` returns twice the value of `number`. A retain keeps the computed value cached between reads.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const number = createSourceNode(2);
|
|
19
|
+
const doubled = createDerivedNode(
|
|
20
|
+
(callingNode) => 2 * readSource(callingNode, number),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const retain = retainDerivedNode(doubled); // Computes 4 for the first time.
|
|
24
|
+
readDerivedOutsideComputation(doubled); // 4. The computation did not run again.
|
|
25
|
+
readDerivedOutsideComputation(doubled); // 4. The computation did not run again.
|
|
26
|
+
|
|
27
|
+
writeSource(number, 3); // Marks doubled for recomputation.
|
|
28
|
+
readDerivedOutsideComputation(doubled); // 6. The computation ran again.
|
|
29
|
+
readDerivedOutsideComputation(doubled); // 6. The computation did not run again.
|
|
30
|
+
releaseRetain(retain);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`number.value` returns the most recently committed value. Inside the computation, `readSource(callingNode, number)` also records the dependency on `number`.
|
|
34
|
+
|
|
35
|
+
A retain can also register a callback. With the source still at 3, creating this retain brings `doubled` up to date. The callback first runs when the result changes.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const subscription = retainDerivedNode(doubled, (value) => console.log(value));
|
|
39
|
+
writeSource(number, 4); // Recomputes doubled and logs 8 before the write returns.
|
|
40
|
+
readDerivedOutsideComputation(doubled); // 8. The computation did not run again.
|
|
41
|
+
releaseRetain(subscription);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
- `createSourceNode(value)` creates a source node.
|
|
47
|
+
- `writeSource(source, value)` updates a source.
|
|
48
|
+
- `createDerivedNode(computation)` creates a derived node. `computation(callingNode, previous)` returns its value. `previous` has the type `TValue | undefined`. It is `undefined` the first time the node computes, and again the first time it computes after pico-js has disposed it.
|
|
49
|
+
- `readSource(callingNode, source)` and `readDerived(callingNode, node)` read a node from inside a computation.
|
|
50
|
+
- `readDerivedOutsideComputation(node)` reads a derived node from outside a computation.
|
|
51
|
+
- `retainDerivedNode(node, onChange?)` computes the node if it is not up to date, keeps it cached between reads, and returns a retain. `releaseRetain(retain)` releases the retain.
|
|
52
|
+
- `startWriteBatch()` and `endWriteBatch()` group updates.
|
|
53
|
+
- [Disposing nodes](./docs/disposing-nodes.md) describes when pico-js releases unused cached values and how to customize cleanup.
|
|
54
|
+
- `SourceNode<T>.value` gives the current value without recording a dependency. `DerivedNode` and `Retain` are opaque handles. You pass them to the functions above. `CallingNode` is the type of the `callingNode` a computation receives.
|
|
55
|
+
|
|
56
|
+
## Advanced usage
|
|
57
|
+
|
|
58
|
+
### Returning the previous value
|
|
59
|
+
|
|
60
|
+
A computation receives its previous result as its second argument. Returning that same value lets dependents reuse their cached results unless another dependency changed. This matters for operations such as `filter`, which allocate a new array even if all the elements stay the same.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const numbers = createSourceNode([1, 2, 3, 4]);
|
|
64
|
+
|
|
65
|
+
const evens = createDerivedNode<ReadonlyArray<number>>(
|
|
66
|
+
(callingNode, previous) => {
|
|
67
|
+
const next = readSource(callingNode, numbers).filter((n) => n % 2 === 0);
|
|
68
|
+
if (
|
|
69
|
+
previous?.length === next.length &&
|
|
70
|
+
next.every((n, i) => n === previous[i])
|
|
71
|
+
) {
|
|
72
|
+
return previous;
|
|
73
|
+
}
|
|
74
|
+
return next;
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`filter` returns a new array every time, so without the check every update to `numbers` would give `evens` a new value. With it, updating `numbers` to `[1, 2, 5, 4]` leaves `evens` as the same `[2, 4]` array, and a node that reads only `evens` does not recompute.
|
|
80
|
+
|
|
81
|
+
### Changing dependencies
|
|
82
|
+
|
|
83
|
+
A computation records only the nodes it reads on its most recent run. In this example, `selected` initially depends on `useLeft` and `left`. After the switch, writes to `left` no longer invalidate it.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const useLeft = createSourceNode(true);
|
|
87
|
+
const left = createSourceNode(10);
|
|
88
|
+
const right = createSourceNode(20);
|
|
89
|
+
const selected = createDerivedNode((callingNode) =>
|
|
90
|
+
readSource(callingNode, useLeft)
|
|
91
|
+
? readSource(callingNode, left)
|
|
92
|
+
: readSource(callingNode, right),
|
|
93
|
+
);
|
|
94
|
+
const selection = retainDerivedNode(selected);
|
|
95
|
+
writeSource(useLeft, false);
|
|
96
|
+
readDerivedOutsideComputation(selected); // 20. The computation ran and now depends on useLeft and right.
|
|
97
|
+
writeSource(left, 11);
|
|
98
|
+
readDerivedOutsideComputation(selected); // 20. The computation did not run again.
|
|
99
|
+
releaseRetain(selection);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Nodes with parameters
|
|
103
|
+
|
|
104
|
+
A computation receives only `callingNode` and `previous`. For a value that depends on a parameter, create one derived node per parameter value, and look each node up however suits your app. For example, with a map:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const multipleNodes = new Map<number, DerivedNode<number>>();
|
|
108
|
+
|
|
109
|
+
function multipleOfNumber(factor: number): DerivedNode<number> {
|
|
110
|
+
let node = multipleNodes.get(factor);
|
|
111
|
+
if (node === undefined) {
|
|
112
|
+
node = createDerivedNode(
|
|
113
|
+
(callingNode) => readSource(callingNode, number) * factor,
|
|
114
|
+
);
|
|
115
|
+
multipleNodes.set(factor, node);
|
|
116
|
+
}
|
|
117
|
+
return node;
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Retaining nodes
|
|
122
|
+
|
|
123
|
+
A retain keeps a derived result cached while a caller needs it. A component typically retains on mount and releases on unmount. [Node lifetimes and SSR](./docs/lifetimes.md) covers request-scoped state and cleanup.
|
|
124
|
+
|
|
125
|
+
### Reacting to changes
|
|
126
|
+
|
|
127
|
+
The flush runs in two phases. First, pico-js refreshes the affected nodes that have callbacks. Then it calls `onChange` once for each retain whose node changed value. Each callback can read the final value of any derived node. Outside a batch, the flush finishes before `writeSource` returns. Inside a batch, `endWriteBatch` runs the flush. Releasing a node’s last callback retain makes the node lazy again.
|
|
128
|
+
|
|
129
|
+
### Grouping updates
|
|
130
|
+
|
|
131
|
+
A batch publishes all of its writes together. A second write to the same source replaces the pending value. Change callbacks run before `endWriteBatch()` returns.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
startWriteBatch();
|
|
135
|
+
try {
|
|
136
|
+
writeSource(number, 5);
|
|
137
|
+
writeSource(number, 6);
|
|
138
|
+
} finally {
|
|
139
|
+
endWriteBatch();
|
|
140
|
+
}
|
|
141
|
+
readDerivedOutsideComputation(doubled); // 12
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Contracts
|
|
145
|
+
|
|
146
|
+
Open one synchronous write batch at a time. Inside it, write sources and release retains. Read nodes and create retains after `endWriteBatch()` returns. The [contracts](./docs/contracts.md) cover computations, callbacks, equality, and cleanup.
|
|
147
|
+
|
|
148
|
+
## Guarantees
|
|
149
|
+
|
|
150
|
+
Under the correctness contracts, every tracked computation, top-level read, and change callback receives current values. A cached node reruns when it needs a fresh result and one of its direct sources received a write or one of its derived inputs changed value. Writing the same value to a source still counts as a write. A change callback runs at most once per update or batch, after all affected nodes with callbacks are up to date.
|
|
151
|
+
|
|
152
|
+
The [design](./docs/design.md) explains invalidation and recomputation. The [performance notes](./docs/performance.md) describe the cost of those operations, and the [correctness proof](./docs/correctness.md) establishes when reads return current values.
|