forked from proposal-signals/signal-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces a new `Signal.Volatile` function. It follows the initial proposal here: tc39/proposal-signals#237 Volatile sources bring outside data into the computation graph.
- Loading branch information
1 parent
e8f5a43
commit 4d054ad
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {REACTIVE_NODE, type ReactiveNode, producerAccessed} from './graph'; | ||
|
||
/** | ||
* Volatile functions read from external sources. They can change at any time | ||
* without notifying the graph. If the source supports it, optionally we can | ||
* subscribe to changes while observed. | ||
* | ||
* Unless the external source is actively being observed, we have to assume | ||
* it's stale and bust the cache of everything downstream. | ||
*/ | ||
export function createVolatile<T>(getSnapshot: () => T): VolatileNode<T> { | ||
const node: VolatileNode<T> = Object.create(REACTIVE_NODE); | ||
node.getSnapshot = getSnapshot; | ||
|
||
return node; | ||
} | ||
|
||
export function volatileGetFn<T>(this: VolatileNode<T>): T { | ||
producerAccessed(this); | ||
|
||
// TODO: | ||
// - Cache when live. | ||
// - Handle errors in live snapshots. | ||
// - Throw if dependencies are used in the snapshot. | ||
// - Bust downstream caches when not live. | ||
return this.getSnapshot(); | ||
} | ||
|
||
export interface VolatileNode<T> extends ReactiveNode { | ||
/** Read state from the outside world. May be expensive. */ | ||
getSnapshot: () => T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {describe, expect, it} from 'vitest'; | ||
import {Signal} from '../../src/wrapper.js'; | ||
|
||
describe('Signal.Volatile', () => { | ||
it('reads the value using the given function', () => { | ||
const volatile = new Signal.Volatile(() => 'value'); | ||
|
||
expect(volatile.get()).toBe('value'); | ||
}); | ||
}); |