Skip to content

Commit

Permalink
add getState API
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherny committed Apr 23, 2018
1 parent e6cb574 commit 330ae2c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Store<Actions: {}> {
onAll<K: $Keys<Actions>>(): Observable<$Values<Undux<Actions>>>;
before<K: $Keys<Actions>>(key: K): Observable<$ElementType<Actions, K>>;
beforeAll<K: $Keys<Actions>>(): Observable<$Values<Undux<Actions>>>;
getState(): $ReadOnly<Actions>;
}

declare export class StoreSnapshot<Actions: {}> implements Store<Actions> {
Expand All @@ -25,6 +26,7 @@ declare export class StoreSnapshot<Actions: {}> implements Store<Actions> {
onAll<K: $Keys<Actions>>(): Observable<$Values<Undux<Actions>>>;
before<K: $Keys<Actions>>(key: K): Observable<$ElementType<Actions, K>>;
beforeAll<K: $Keys<Actions>>(): Observable<$Values<Undux<Actions>>>;
getState(): $ReadOnly<Actions>;
}

declare export class StoreDefinition<Actions: {}> implements Store<Actions> {
Expand All @@ -34,6 +36,7 @@ declare export class StoreDefinition<Actions: {}> implements Store<Actions> {
onAll<K: $Keys<Actions>>(): Observable<$Values<Undux<Actions>>>;
before<K: $Keys<Actions>>(key: K): Observable<$ElementType<Actions, K>>;
beforeAll<K: $Keys<Actions>>(): Observable<$Values<Undux<Actions>>>;
getState(): $ReadOnly<Actions>;
}

declare export function createStore<Actions: Object>(initialState: Actions): StoreDefinition<Actions>
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Store<Actions extends object> {
onAll<K extends keyof Actions>(): RxJS.Observable<Undux<Actions>[keyof Actions]>
before<K extends keyof Actions>(key: K): RxJS.Observable<Undux<Actions>[K]>
beforeAll<K extends keyof Actions>(): RxJS.Observable<Undux<Actions>[keyof Actions]>
getState(): Readonly<Actions>
}

export class StoreSnapshot<Actions extends object> implements Store<Actions> {
Expand All @@ -41,6 +42,9 @@ export class StoreSnapshot<Actions extends object> implements Store<Actions> {
beforeAll<K extends keyof Actions>() {
return this.store.beforeAll()
}
getState() {
return Object.freeze(Object.assign({}, this.state))
}

private assign<Actions extends object, K extends keyof Actions>(
key: K, value: Actions[K]) {
Expand Down Expand Up @@ -80,6 +84,9 @@ export class StoreDefinition<Actions extends object> implements Store<Actions> {
this.alls.emit(key, { key, previousValue, value })
}
}
getState() {
return this.store.getState()
}
}

export function createStore<Actions extends object>(
Expand Down
28 changes: 28 additions & 0 deletions test/stateless.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,31 @@ test('[stateless] it should typecheck with additional props', t => {

t.pass()
})

test('#getState should return up to date state', t => {
let A = connect(store)(({ store }) =>
<div>
{store.get('isTrue') ? 'True' : 'False'}
<button onClick={() => store.set('isTrue')(!store.get('isTrue'))}>Update</button>
</div>
)

withElement(A, _ => {
t.deepEqual(store.getState(), { isTrue: true, users: [] })
Simulate.click(_.querySelector('button')!)
t.deepEqual(store.getState(), { isTrue: false, users: [] })
Simulate.click(_.querySelector('button')!)
t.deepEqual(store.getState(), { isTrue: true, users: [] })
Simulate.click(_.querySelector('button')!)
t.deepEqual(store.getState(), { isTrue: false, users: [] })
})
})

test('#getState should not be writeable', t => {
let A = connect(store)(({ store }) =>
<div />
)
withElement(A, _ =>
t.throws(() => (store.getState() as any).isTrue = false)
)
})

0 comments on commit 330ae2c

Please sign in to comment.