-
Notifications
You must be signed in to change notification settings - Fork 2
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
8 changed files
with
203 additions
and
2 deletions.
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,50 @@ | ||
/** | ||
* Unlike an effect, a cause does not mutate but extracts state from the world. | ||
* By providing a cause identifier the registered cause handler gets called and | ||
* must return the requested state. A cause can be anything, e.g. the current | ||
* state of a `signal`, the current time or a browsers window dimensions, just | ||
* to give some examples. | ||
* | ||
* @module cause | ||
*/ | ||
|
||
/** Holds the registered causings. */ | ||
let registry = new Map(); | ||
|
||
/** | ||
* Registers a cause handler identified by `causeId`. | ||
* | ||
* @param {string} causeId A cause identifier. | ||
* @param {function} handlerFn A function which gets passed the arguments from | ||
* the call to `cause`. | ||
*/ | ||
export function causing(causeId, handlerFn) { | ||
if (registry.has(causeId)) { | ||
console.warn("overwriting causing for", causeId); | ||
} | ||
registry.set(causeId, handlerFn); | ||
} | ||
|
||
/** | ||
* Calls the cause handler identified by `causeId` with the provided `args`. | ||
* | ||
* Note: for any given call to `cause` there must be a previous call to | ||
* `causing`, registering a handler function for `causeId`. | ||
* | ||
* @param {string} causeId The cause identifier. | ||
* @param {...any} args Arguments passed to the cause handler. | ||
*/ | ||
export function cause(causeId, ...args) { | ||
let handlerFn = registry.get(causeId); | ||
if (handlerFn) { | ||
return handlerFn(...args); | ||
} | ||
console.warn("no causing registered for:", causeId); | ||
} | ||
|
||
/** | ||
* Clears all registered causings. | ||
*/ | ||
export function clearCausings() { | ||
registry.clear(); | ||
} |
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,34 @@ | ||
import { cause, causing, clearCausings } from "./cause"; | ||
|
||
/* global global */ | ||
|
||
beforeEach(() => { | ||
global.console.warn = jest.fn(); | ||
clearCausings(); | ||
}); | ||
|
||
describe("cause", () => { | ||
it("handles causings", () => { | ||
causing("foo", value => value + "baz"); | ||
let result = cause("foo", "bar"); | ||
expect(result).toEqual("barbaz"); | ||
}); | ||
it("logs a warning for unknown causings", () => { | ||
cause("foo", "bar"); | ||
expect(global.console.warn).toHaveBeenCalledWith( | ||
"no causing registered for:", | ||
"foo", | ||
); | ||
}); | ||
}); | ||
|
||
describe("causing", () => { | ||
it("logs a warning when overwriting an existing causing", () => { | ||
causing("foo", () => {}); | ||
causing("foo", () => {}); | ||
expect(global.console.warn).toHaveBeenCalledWith( | ||
"overwriting causing for", | ||
"foo", | ||
); | ||
}); | ||
}); |
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,6 @@ | ||
import { signal } from "./signal"; | ||
|
||
/** | ||
* A signal storing the state of a database. | ||
*/ | ||
export const db = signal({}); |
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