Skip to content

Commit

Permalink
update mechanism to support multiple cases
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-phan committed Sep 26, 2019
1 parent f8275f1 commit c39817d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "global-state-hook",
"version": "1.1.3",
"version": "1.1.4",
"description": "~200kb for exactly what you need to manage global state",
"source": "src/index.ts",
"main": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ISubscription {
}

export function createSubscription(initialState: any = {}): ISubscription {
const state = initialState
const state = initialState || {}
let listener: Listener[] = []
const subscribe = (fn: Listener) => listener.push(fn)
const unsubscribe = (fn: Listener) => (listener = listener.filter(f => f !== fn))
Expand All @@ -28,7 +28,7 @@ export function useSubscription(subscriber: ISubscription, pick: string[] = []):
const [, setUpdate] = React.useState()

const setState = React.useCallback((newState: State) => {
typeof newState === "object" ? Object.assign(state, newState) : (state = newState)
typeof newState === "object" ? Object.assign(state || {}, newState) : (subscriber.state = newState)
subscriber.listener.forEach(fn => fn(newState))
}, [])

Expand Down

0 comments on commit c39817d

Please sign in to comment.