From: Eduardo San Martin Morote Date: Sat, 23 Nov 2019 12:23:26 +0000 (+0100) Subject: feat: use function to build state X-Git-Tag: v0.0.1~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=150e4e19371782dfda7e92a8bbf000c07341dc11;p=thirdparty%2Fvuejs%2Fpinia.git feat: use function to build state --- diff --git a/__tests__/createStore.spec.ts b/__tests__/createStore.spec.ts index cc416bc3..8a6c0434 100644 --- a/__tests__/createStore.spec.ts +++ b/__tests__/createStore.spec.ts @@ -2,12 +2,13 @@ import { createStore } from '../src' describe('createStore', () => { it('sets the initial state', () => { - const state = { + const state = () => ({ a: true, nested: { a: { b: 'string' }, }, - } + }) + const store = createStore('main', state) expect(store.state).toEqual({ a: true, diff --git a/__tests__/store.patch.spec.ts b/__tests__/store.patch.spec.ts new file mode 100644 index 00000000..bbdde42b --- /dev/null +++ b/__tests__/store.patch.spec.ts @@ -0,0 +1,59 @@ +import { createStore } from '../src' + +describe('store.patch', () => { + function buildStore() { + return createStore('main', () => ({ + // TODO: the boolean cas shouldn't be necessary + // https://www.typescriptlang.org/play/#code/MYewdgzgLgBCMF4YG8CwAoGWYEMBcMUATgK4CmGAvhhiAHQ6IwBmOANhBehqJLMETI4oZJgAoAlIgB8MMclwFi5GJQk10vaDGBMBQkZI3AGTVhzJA + a: true as boolean, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + })) + } + + it('patches a property without touching the rest', () => { + const store = buildStore() + store.patch({ a: false }) + expect(store.state).toEqual({ + a: false, + nested: { + foo: 'foo', + a: { b: 'string' }, + }, + }) + }) + + it('patches a nested property without touching the rest', () => { + const store = buildStore() + store.patch({ nested: { foo: 'bar' } }) + expect(store.state).toEqual({ + a: true, + nested: { + foo: 'bar', + a: { b: 'string' }, + }, + }) + store.patch({ nested: { a: { b: 'hello' } } }) + expect(store.state).toEqual({ + a: true, + nested: { + foo: 'bar', + a: { b: 'hello' }, + }, + }) + }) + + it('patches multiple properties at the same time', () => { + const store = buildStore() + store.patch({ a: false, nested: { foo: 'hello' } }) + expect(store.state).toEqual({ + a: false, + nested: { + foo: 'hello', + a: { b: 'string' }, + }, + }) + }) +}) diff --git a/src/index.ts b/src/index.ts index bca3d75b..27babaf1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,21 +9,6 @@ import { } from './types' import { devtoolPlugin } from './devtools' -function createState(initialState: S) { - const state: Ref = ref(initialState) - - // type State = UnwrapRef - - function replaceState(newState: S) { - state.value = newState - } - - return { - state, - replaceState, - } -} - function innerPatch( target: T, patchToApply: DeepPartial @@ -56,10 +41,13 @@ function innerPatch( export function createStore( id: Id, - initialState: S + buildState: () => S // methods: Record ): Store { - const { state, replaceState } = createState(initialState) + const state: Ref = ref(buildState()) + function replaceState(newState: S) { + state.value = newState + } let isListening = true const subscriptions: SubscriptionCallback[] = [] @@ -123,12 +111,12 @@ export function createStore( function makeStore( id: Id, - initialState: S + buildState: () => S ) { let store: Store | undefined function useStore(): Store { - if (!store) store = createStore(id, initialState) + if (!store) store = createStore(id, buildState) return store }