Hacker News new | ask | show | jobs
by cheez 3037 days 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.

1 comments

.watch? Reminds me of angularjs :)
Hah, that's right :)