observable
use proxy
to return a proxy object, the proxy object can be monitored by observe
.
1 | import { observable } from "dob" |
Usage
1 | // es5 |
Features
observable
support native object
、array
、Map
、WeakMap
、Set
、WeakSet
, support undefined variables, dynamic conditional branches.
Object
1 | const obj = observable({ |
Array
Only when the variables used are modified, the action will be triggered again.
1 | let arr = observable([1, 2, 3, 4, 5]) |
Because of JS achieve
shift
splice
by divided into multiple steps, so useAction
can be aggregated into an action.
.length
will trigger action when any change, which will change array’s length:
1 | let arr = observable([1, 2, 3, 4, 5]) |
Map
listen size:
1 | const map = observable(new Map()) |
listen single key:
1 | const map = observable(new Map()) |
WeakMap
Same with Map.
Set
listen size:
1 | const set = observable(new Set()) |
WeakSet
Same with Set.
Expand thinking
All of the basic type as shown above can be used in object:
1 | const obj = observable({ |
Dynamic binding
For uninitialized variables, can also be tracked.
1 | const obj = observable({}) |
Conditional branch
1 | const objCon = observable({ |