Skip to content

Commit

Permalink
feat: allow empty state option to make it easy to group stores
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jan 20, 2020
1 parent bc37197 commit 810e0f0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 114 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ import { useCartStore } from './cart'

export const useSharedStore = createStore({
id: 'shared',
state: () => ({}),
getters: {
summary() {
const user = useUserStore()
Expand Down
58 changes: 58 additions & 0 deletions __tests__/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ describe('Store', () => {
})
})

it('can be reset', () => {
const store = useStore()
store.state.a = false
const spy = jest.fn()
store.subscribe(spy)
store.reset()
store.state.nested.foo = 'bar'
expect(spy).not.toHaveBeenCalled()
expect(store.state).toEqual({
a: true,
nested: {
foo: 'bar',
a: { b: 'string' },
},
})
})

it('can create an empty state if no state option is provided', () => {
const store = createStore({ id: 'some' })()

expect(store.state).toEqual({})
})

it('can hydrate the state', () => {
setActiveReq({})
const useStore = createStore({
Expand Down Expand Up @@ -93,4 +116,39 @@ describe('Store', () => {
store.state.nested.a.b = 'hey'
expect(store2.state.nested.a.b).toBe('string')
})

it('subscribe to changes', () => {
const store = useStore()
const spy = jest.fn()
store.subscribe(spy)

store.state.a = false

expect(spy).toHaveBeenCalledWith(
{
payload: {},
storeName: 'main',
type: expect.stringContaining('in place'),
},
store.state
)
})

it('subscribe to changes done via patch', () => {
const store = useStore()
const spy = jest.fn()
store.subscribe(spy)

const patch = { a: false }
store.patch(patch)

expect(spy).toHaveBeenCalledWith(
{
payload: patch,
storeName: 'main',
type: expect.stringContaining('patch'),
},
store.state
)
})
})
91 changes: 0 additions & 91 deletions src/pinia.ts

This file was deleted.

24 changes: 2 additions & 22 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
StoreWithGetters,
StoreGetter,
NonNullObject,
StoreReactiveGetters,
} from './types'
import { useStoreDevtools } from './devtools'

Expand Down Expand Up @@ -61,25 +60,6 @@ export type Store<
A extends Record<string, StoreAction>
> = StoreWithState<Id, S> & StoreWithGetters<S, G> & StoreWithActions<A>

export type WrapStoreWithId<
S extends Store<string, StateTree, any, any>
> = S extends Store<infer Id, infer S, infer G, infer A>
? {
[k in Id]: Store<Id, S, G, A>
}
: never

export type ExtractGettersFromStore<S> = S extends Store<
any,
infer S,
infer G,
any
>
? {
[k in keyof G]: ReturnType<G[k]>
}
: never

export type PiniaStore<
P extends Record<string, Store<any, any, any, any>>
> = P extends Record<infer name, any>
Expand Down Expand Up @@ -107,7 +87,7 @@ export function buildStore<
A extends Record<string, StoreAction>
>(
id: Id,
buildState: () => S,
buildState = () => ({} as S),
getters: G = {} as G,
actions: A = {} as A,
initialState?: S | undefined
Expand Down Expand Up @@ -281,7 +261,7 @@ export function createStore<
A extends Record<string, StoreAction>
>(options: {
id: Id
state: () => S
state?: () => S
getters?: G
// allow actions use other actions
actions?: A & ThisType<A & StoreWithState<Id, S> & StoreWithGetters<S, G>>
Expand Down

0 comments on commit 810e0f0

Please sign in to comment.