-
-
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
4 changed files
with
107 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,53 @@ | ||
import { useSignal } from './signal' | ||
|
||
describe('signal', () => { | ||
it('should signal', async () => { | ||
let copy: any // copy of "flag" | ||
let count = 1 // counter for "name" | ||
|
||
const [flag, setFlag] = useSignal(true, v => copy = v) | ||
const name = useSignal('Henry') | ||
const object = useSignal({ | ||
a: 1, | ||
b: 2, | ||
}) | ||
|
||
const off = name.on(v => ++count) | ||
|
||
// Get values | ||
expect(flag()).toEqual(true) | ||
expect(name.get()).toEqual('Henry') | ||
expect(copy).toEqual(undefined) | ||
expect(object.get()).toEqual({ | ||
a: 1, | ||
b: 2, | ||
}) | ||
|
||
// Change values | ||
setFlag(false) | ||
name.set('Anna') | ||
object.set({ | ||
a: 11, | ||
b: 22, | ||
}) | ||
|
||
// Check changed values | ||
expect(flag()).toEqual(false) | ||
expect(name.get()).toEqual('Anna') | ||
expect(copy).toEqual(false) | ||
expect(object.get()).toEqual({ | ||
a: 11, | ||
b: 22, | ||
}) | ||
|
||
// Skip setting same value | ||
expect(count).toBe(2) | ||
name.set('Anna') | ||
expect(count).toBe(2) | ||
name.set('Cloe') | ||
expect(count).toBe(3) | ||
off() | ||
name.set('Diego') | ||
expect(count).toBe(3) | ||
}) | ||
}) |
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,52 @@ | ||
import { arrayRemoveElement } from './array' | ||
import { objectPlain } from './object' | ||
|
||
export type SignalWatcher<T> = (value: T, oldValue: T) => void | ||
|
||
export type Signal<T> = [ | ||
() => T, | ||
(value: T) => void, | ||
(fn: SignalWatcher<T>) => () => void, | ||
(fn: SignalWatcher<T>) => void, | ||
] & { | ||
get: () => T | ||
set: (value: T) => void | ||
on: (fn: SignalWatcher<T>) => () => void | ||
off: (fn: SignalWatcher<T>) => void | ||
} | ||
|
||
/** Super simple signal implementation */ | ||
export function useSignal<T = any>(value: T, onChange?: SignalWatcher<T>): Signal<T> { | ||
let signal = structuredClone(value) | ||
|
||
const watchers: SignalWatcher<T>[] = [] | ||
|
||
function off(fn: SignalWatcher<T>) { | ||
arrayRemoveElement(watchers, fn) | ||
} | ||
|
||
function on(fn: SignalWatcher<T>) { | ||
watchers.push(fn) | ||
return () => off(fn) | ||
} | ||
|
||
if (onChange) | ||
watchers.push(onChange) | ||
|
||
const get = () => structuredClone(signal) | ||
|
||
const set = (value: T) => { | ||
if (value !== signal) { | ||
const oldValue = signal | ||
signal = value | ||
watchers.forEach(fn => fn(value, oldValue)) | ||
} | ||
} | ||
|
||
const obj: any = [get, set, on, off] | ||
obj.get = get | ||
obj.set = set | ||
obj.on = on | ||
obj.off = off | ||
return obj | ||
} |