-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
localCopy()
for use outside of classes (#29)
* Support localCopy outside classes * Types
- Loading branch information
1 parent
a42bbe0
commit a788615
Showing
3 changed files
with
177 additions
and
0 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,63 @@ | ||
import { describe, test, assert } from "vitest"; | ||
import { localCopy } from "../src/local-copy.ts"; | ||
import { Signal } from "signal-polyfill"; | ||
import { assertReactivelySettled, assertStable } from "./helpers.ts"; | ||
|
||
describe("localCopy()", () => { | ||
test("it works as a Signal", () => { | ||
let remote = new Signal.State(123); | ||
|
||
let local = localCopy(() => remote.get()); | ||
|
||
assert.strictEqual(local.get(), 123, "defaults to the remote value"); | ||
|
||
assertReactivelySettled({ | ||
access: () => local.get(), | ||
change: () => local.set(456), | ||
}); | ||
|
||
assert.strictEqual(local.get(), 456, "local value updates correctly"); | ||
assert.strictEqual(remote.get(), 123, "remote value does not update"); | ||
|
||
remote.set(789); | ||
|
||
assert.strictEqual( | ||
local.get(), | ||
789, | ||
"local value updates to new remote value", | ||
); | ||
assert.strictEqual(remote.get(), 789, "remote value is updated"); | ||
|
||
assertStable(() => local.get()); | ||
}); | ||
|
||
test("it works as a Signal in a class", () => { | ||
class State { | ||
remote = new Signal.State(123); | ||
local = localCopy(() => this.remote.get()); | ||
} | ||
|
||
let state = new State(); | ||
|
||
assert.strictEqual(state.local.get(), 123, "defaults to the remote value"); | ||
|
||
assertReactivelySettled({ | ||
access: () => state.local.get(), | ||
change: () => state.local.set(456), | ||
}); | ||
|
||
assert.strictEqual(state.local.get(), 456, "local value updates correctly"); | ||
assert.strictEqual(state.remote.get(), 123, "remote value does not update"); | ||
|
||
state.remote.set(789); | ||
|
||
assert.strictEqual( | ||
state.local.get(), | ||
789, | ||
"local value updates to new remote value", | ||
); | ||
assert.strictEqual(state.remote.get(), 789, "remote value is updated"); | ||
|
||
assertStable(() => state.local.get()); | ||
}); | ||
}); |