-
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.
- Loading branch information
Showing
10 changed files
with
463 additions
and
544 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,5 @@ | ||
import { configure } from 'mobx'; | ||
|
||
configure({ | ||
enforceActions: "never", | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,36 +1,49 @@ | ||
import { observable, untracked, computed as mobxComputed } from 'mobx'; | ||
import { observable, untracked, computed as mobxComputed, comparer } from 'mobx'; | ||
import { reaction, sync } from './reaction'; | ||
|
||
export interface ReadonlySignal<T> { | ||
get value(): T; | ||
get(): T; | ||
subscribe(listener: (current: T, prev: T) =>void): () => void; | ||
sync(listener: (current: T, prev: T | undefined) =>void): () => void; | ||
} | ||
|
||
export interface Signal<T> extends ReadonlySignal<T> { | ||
(value: T): T, | ||
update(fn: (value: T) => T); | ||
} | ||
|
||
export function signalFactory<T>(value: T, equals?: (a: T, b: T) => boolean): Signal<T> { | ||
const box = observable.box(value, { equals }); | ||
const set = box.set.bind(box); | ||
export function signal<T>(value: T): Signal<T> { | ||
const box = observable.box(value, { equals: comparer.default }); | ||
const get = box.get.bind(box); | ||
const set = box.set.bind(box); | ||
return make(get, set); | ||
} | ||
|
||
Object.defineProperty(set, 'value', { get }); | ||
set.get = get; | ||
set.update = (fn: (value: T) => T) => set(fn(untracked(set.get))); | ||
|
||
return set; | ||
export function wrap<T>(readfn: () => T): ReadonlySignal<T>; | ||
export function wrap<T>(readfn: () => T, writefn: (value: T) => void): Signal<T>; | ||
export function wrap<T>(readfn: () => T, writefn?: (value: T) => void): (ReadonlySignal<T> | Signal<T>) { | ||
const box = mobxComputed(readfn, { equals: comparer.shallow }); | ||
const get = box.get.bind(box); | ||
return make(get, writefn); | ||
} | ||
|
||
export function computedFactory<T>(fn: () => T, equals?: (a: T, b: T) => boolean): ReadonlySignal<T> { | ||
const box = mobxComputed(fn, { equals }); | ||
const get = box.get.bind(box); | ||
const h = { | ||
function make<T>(readfn: () => T): ReadonlySignal<T>; | ||
function make<T>(readfn: () => T, writefn: (value: T) => void): Signal<T>; | ||
function make<T>(readfn: () => T, writefn?: (value: T) => void): (ReadonlySignal<T> | Signal<T>) { | ||
const get = readfn; | ||
const h = writefn || {}; | ||
Object.defineProperty(h, 'value', { get }) | ||
Object.assign(h, { | ||
get, | ||
get value() { | ||
return get(); | ||
} | ||
}; | ||
subscribe: (listener) => reaction(get, listener), | ||
sync: (listener) => sync(get, listener) | ||
}); | ||
if (writefn) { | ||
Object.assign(h, { | ||
update: (fn: (value: T) => T) => writefn(fn(untracked(get))), | ||
}) | ||
} | ||
|
||
return h; | ||
return h as any; | ||
} |
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