npm install unsub resub
## TodosStore.coffeeimport StoreBaseAutoSubscribeStore from "unsub"export default = AutoSubscribeStore@Key_SomeFilter = "Key_SomeFilter"@autoSubscribeWithKey @Key_SomeFilter: ->@_todosFiltered@autoSubscribe @key :@_todosByUserusername## TodoList.coffeeimport ComponentBase from "unsub"import TodosStore from "./TodosStore"export:todosFiltered: TodosStoreInstancegetTodosuserTodos: TodosStoreInstancegetTodosForUser @propsusername: -><div>@stateuserTodosmap<Todo key=todoid>todotext</Todo></div>
Above code is equivalent to this TypeScript code:
/// TodosStore.ts;;/// TodoList.tsx;;
Breaking Change in v2.0.0
v2.0.0-rc.2 introduced a new argument to stores'
# old:::# new:::
Reasoning for the new argument:
Changes in ReSub/React made it unreliable and bugprone to use
_buildState. It was not always updated and would reflect old state. As a workaround they added
state as a third argument.
Reasoning for breaking the old signature:
There's 2 parts to that answer.
Firstly it feels far more natural to have
props. Most (if not all these days) of React's functions taking both
state have them as the first 2 arguments and in that same order; especially if you use preact (which you should).
Secondly, if you made use of
buildState there's a high chance it's buggy now (breaking change in React/ReSub). To fix that you are required to go thru your code and switch to using the
state argument. While doing that you may as well switch to the new (and better) signature.
Yes, if you aren't affected by #2
bInit still moved and I broke your components. The above 2 reasons are big enough to warrant you do a search and replace.
UnSub principially is used the same way as ReSub. One key difference is that decorators which take an argument do not return a decorator when called but instead are called with the arguments, followed by whatever they are decorating.
# correct:@autoSubscribeWithKey @Key_Filtered: -> @filteredData# incorrect:@autoSubscribeWithKey@Key_Filtered : -> @filteredData
Furthermore it allows you to omit the underscore on your overrides. E.g.
buildState instead of
_buildState. This more closely follows React's style of writing components.
Also see v2's breaking changes in the previous section.
MyStore = CustomEqualityShouldComponentUpdate shouldComponentUpdateCb: -> @data
Member Function Decorators
Native to ReSub, undocumented.
Helper which binds the function in the constructor.
This is necessary because fat arrows would bind to the constructor/class.
Used outside parameter list. Index represents
arguments[index], is optional and defaults to 0.
MyStore = AutoSubscribeStore@autoSubscribe @key 1@bound :# `b` is the key ^@databa
Note that member function and parameter decorators (prefixed with
@) are actually static class members of
StoreBase. This means that you must not have static members on your store that use the same identifier as a decorator you make use of inside the class body. It also means that you only need to import classes and class decorators.
For more information please refer to the ReSub documentation.