-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.2.0' into released
- Loading branch information
Showing
10 changed files
with
385 additions
and
108 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
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
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,23 +1,42 @@ | ||
import { Watcher } from '../Watcher' | ||
import { LiveSelector } from '../LiveSelector' | ||
|
||
export class ValueRef<T> extends Watcher<T> { | ||
type Fn<T> = (newVal: T, oldVal: T) => void | ||
export class ValueRef<T> { | ||
/** Get current value of a ValueRef */ | ||
get value() { | ||
return this._value | ||
} | ||
set value(v: T) { | ||
this._value = v | ||
this.watcherCallback() | ||
/** Set current value of a ValueRef */ | ||
set value(newVal: T) { | ||
const oldVal = this._value | ||
this._value = newVal | ||
for (const fn of this.watcher.keys()) { | ||
try { | ||
fn(newVal, oldVal) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
} | ||
constructor(private _value: T) { | ||
super(new LiveSelector().replace(() => [this._value])) | ||
this.startWatch() | ||
/** All watchers */ | ||
private watcher = new Map<Fn<T>, boolean>() | ||
constructor(private _value: T) {} | ||
/** | ||
* Add a listener. This will return a remover. | ||
* Use it like: useEffect(() => ref.addListener(() => {...})) | ||
*/ | ||
addListener(fn: Fn<T>) { | ||
this.watcher.set(fn, true) | ||
return () => this.removeListener(fn) | ||
} | ||
startWatch() { | ||
this.watching = true | ||
return this | ||
/** | ||
* Remove a listener | ||
*/ | ||
removeListener(fn: Fn<T>) { | ||
this.watcher.delete(fn) | ||
} | ||
stopWatch() { | ||
this.watching = false | ||
/** | ||
* Remove all listeners | ||
*/ | ||
removeAllListener() { | ||
this.watcher = new Map() | ||
} | ||
} |
Oops, something went wrong.