2 ms·
I created a class compatible with Redux time travel and debugging tools that works like this (in typescript): interface FooState { myNumber: number;
by cheez 9y ago
I created a class compatible with Redux time travel and debugging tools that works like this (in typescript):
interface FooState {
myNumber: number;
myArray: number[]
}
class Foo extends DataModelImpl<FooState> implements
IDataModel<FooState> {...}
Then you use it like so:
const foo:IDataModel<FooState> = new Foo()
foo.updateState(state => state.myNumber = 10)
Anything that wants to be notified when myNumber changes:
const callback = (num:number) => {...}
const unsubscribe = foo.watch(state => state.myNumber,callback);
...
unsubscribe()
I've bound this to React, Vue and other view libraries. It even works properly with nested objects.
Drawback is that it uses Observers. I believe the next "killer state library" will use a pattern like what I've described above.
- darepublic 9y ago.watch? Reminds me of angularjs :)
- cheez 9y agoHah, that's right :)