Skip to content

Commit

Permalink
feat(core): #276 add "bind" method to store
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Jan 15, 2020
1 parent ed6e3ba commit 13e3727
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
26 changes: 26 additions & 0 deletions docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,36 @@ Communicating state**ful** context between actions and atoms.
```js
import { createStore } from '@reatom/core'

/* CREATION */

const store = createStore(
atom, // optional
preloadedState, // optional
)

/* DISPATCHING */

store.dispatch(action)
store.dispatch(actionCreator())
store.dispatch(actionCreator(payload))

/* SUBSCRIBING */

store.subscribe(atom, (atomValue) => 'side effect')
store.subscribe(actionCreator, (actionPayload) => 'side effect')
store.subscribe((dispatchedAction, stateDiff) => 'side effect')

/* STATE */

store.getState() // clone state snapshot
store.getState(atom) // atom state

/* BINDING */

const actionCreatorBinded = store.bind(actionCreator)

actionCreator(0) // `{ type: '...', payload: 0 }`
actionCreatorBinded(0) // dispatching, void
```

---
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree, State, TreeId, createCtx } from './kernel'
import { Tree, State, TreeId, createCtx, BaseAction } from './kernel'
import {
throwError,
getTree,
Expand All @@ -7,7 +7,7 @@ import {
getIsAtom,
getIsAction,
} from './shared'
import { declareAction, Action, PayloadActionCreator } from './declareAction'
import { Action, PayloadActionCreator } from './declareAction'
import { Atom, initAction, getState } from './declareAtom'

type ActionsSubscriber = (action: Action<unknown>, stateDiff: State) => any
Expand All @@ -27,6 +27,9 @@ export type Store = {
dispatch: (action: Action<unknown>) => void
subscribe: SubscribeFunction
getState: GetStateFunction
bind: <A extends (...a: any[]) => BaseAction>(
a: A,
) => (...a: A extends (...a: infer Args) => any ? Args : never) => void
}

export function createStore(initState?: State): Store
Expand Down Expand Up @@ -187,10 +190,14 @@ export function createStore(
callFromList((dispatchListeners = nextDispatchListeners), action, stateNew)
}

const bind: Store['bind'] = actionCreator => (...a) =>
store.dispatch(actionCreator(...a))

const store = {
getState: _getState,
subscribe,
dispatch,
bind,
}

return store
Expand Down
11 changes: 11 additions & 0 deletions packages/core/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,17 @@ describe('@reatom/core', () => {
expect(listener).toHaveBeenLastCalledWith(2)
})

test('createStore().bind', () => {
const a = declareAction<0>()
const store = createStore()
const track = jest.fn()

store.subscribe(a, track)
store.bind(a)(0)

expect(track).toBeCalledWith(0)
})

test('declareAction reactions', async () => {
const delay = () => new Promise(on => setTimeout(on, 10))
const setValue = declareAction<number>()
Expand Down

0 comments on commit 13e3727

Please sign in to comment.