-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
165 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
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,80 @@ | ||
import { ValAgent } from "./agent"; | ||
import type { ReadonlyVal, ValConfig, ValDisposer } from "./typings"; | ||
import { invoke } from "./utils"; | ||
import { ValImpl } from "./val"; | ||
|
||
export type SignalEffectCollector<TValue = any> = ( | ||
val$: ReadonlyVal<TValue> | ||
) => TValue; | ||
|
||
/** | ||
* Create a signal val that subscribes to other vals dynamically. | ||
* | ||
* The `get` function passed to the effect callback can be used to get the current value of a Val and subscribe to it. | ||
* The effect callback will be re-evaluated whenever the dependencies change. | ||
* Stale dependencies are unsubscribed automatically. | ||
* | ||
* @param effect - The effect function which will be called immediately and whenever the dependencies change. | ||
* @param config - Optional val config. | ||
* @returns A signal val. | ||
* | ||
* @example | ||
* ```ts | ||
* import { signal, val } from "value-enhancer"; | ||
* | ||
* const a$ = val(1); | ||
* const b$ = val("b"); | ||
* const c$ = val("c"); | ||
* const s$ = signal(get => (get(a$) % 2 === 0 ? get(b$) : get(c$))); | ||
* ``` | ||
*/ | ||
export const signal = <TValue = any>( | ||
effect: (get: SignalEffectCollector) => TValue, | ||
config?: ValConfig<TValue> | ||
) => { | ||
let scopeLevel = 0; | ||
|
||
let currentDisposers = new Map<ReadonlyVal, ValDisposer>(); | ||
let oldDisposers = new Map<ReadonlyVal, ValDisposer>(); | ||
|
||
const get: SignalEffectCollector<TValue> = val$ => { | ||
if (!currentDisposers.has(val$)) { | ||
let disposer = oldDisposers.get(val$); | ||
if (disposer) { | ||
oldDisposers.delete(val$); | ||
} else { | ||
disposer = val$.$valCompute(agent.notify_); | ||
} | ||
currentDisposers.set(val$, disposer); | ||
} | ||
return val$.value; | ||
}; | ||
|
||
const agent = new ValAgent( | ||
() => { | ||
if (++scopeLevel === 1) { | ||
const tmp = currentDisposers; | ||
currentDisposers = oldDisposers; | ||
oldDisposers = tmp; | ||
} | ||
|
||
const value = effect(get); | ||
|
||
if (--scopeLevel === 0 && oldDisposers.size) { | ||
oldDisposers.forEach(invoke); | ||
oldDisposers.clear(); | ||
} | ||
|
||
return value; | ||
}, | ||
config, | ||
() => () => { | ||
if (currentDisposers.size) { | ||
currentDisposers.forEach(invoke); | ||
currentDisposers.clear(); | ||
} | ||
} | ||
); | ||
|
||
return new ValImpl(agent); | ||
}; |
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,49 @@ | ||
import { describe, expect, it, jest } from "@jest/globals"; | ||
import { nextTick, signal, val } from "../src"; | ||
|
||
describe("signal", () => { | ||
it("should collect effect dynamically", async () => { | ||
const a$ = val(1); | ||
const b$ = val("b"); | ||
const c$ = val("c"); | ||
const s$ = signal(get => (get(a$) % 2 === 0 ? get(b$) : get(c$))); | ||
|
||
expect(s$.value).toBe("c"); | ||
|
||
a$.set(2); | ||
|
||
expect(s$.value).toBe("b"); | ||
|
||
const spySubscribe = jest.fn(); | ||
s$.subscribe(spySubscribe); | ||
|
||
expect(spySubscribe).toBeCalledTimes(1); | ||
expect(spySubscribe).lastCalledWith("b"); | ||
|
||
spySubscribe.mockClear(); | ||
|
||
b$.set("bb"); | ||
|
||
await nextTick(); | ||
|
||
expect(spySubscribe).toBeCalledTimes(1); | ||
expect(spySubscribe).lastCalledWith("bb"); | ||
|
||
spySubscribe.mockClear(); | ||
|
||
c$.set("cc"); | ||
|
||
await nextTick(); | ||
|
||
expect(spySubscribe).toBeCalledTimes(0); | ||
|
||
a$.set(3); | ||
|
||
await nextTick(); | ||
|
||
expect(spySubscribe).toBeCalledTimes(1); | ||
expect(spySubscribe).lastCalledWith("cc"); | ||
|
||
s$.dispose(); | ||
}); | ||
}); |