From: Eduardo San Martin Morote Date: Wed, 4 Aug 2021 13:34:15 +0000 (+0200) Subject: chore: notes about patch X-Git-Tag: v2.0.0-rc.2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=48362eea85ef57bf0858028dc62b5be77d05c5e8;p=thirdparty%2Fvuejs%2Fpinia.git chore: notes about patch --- diff --git a/__tests__/store.patch.spec.ts b/__tests__/store.patch.spec.ts index e4492e24..707d28ba 100644 --- a/__tests__/store.patch.spec.ts +++ b/__tests__/store.patch.spec.ts @@ -54,9 +54,13 @@ describe('store.$patch', () => { it('can patch an item that has been copied to an array', () => { const store = useArrayStore() - store.$patch({ currentItem: { id: 2 } }) + store.$state.currentItem = { id: 2 } + // NOTE: a patch of an object is always recursive, writing in the object, in + // place. + //store.$patch({ currentItem: { id: 2 } }) store.items.push(store.currentItem) - store.$patch({ currentItem: { id: 3 } }) + // store.$patch({ currentItem: { id: 3 } }) + store.$state.currentItem = { id: 3 } expect(store.$state.items).toEqual([{ id: 0 }, { id: 2 }]) expect(store.items).toEqual([{ id: 0 }, { id: 2 }]) diff --git a/playground/src/stores/jokes.ts b/playground/src/stores/jokes.ts index f249a5f7..340d7f50 100644 --- a/playground/src/stores/jokes.ts +++ b/playground/src/stores/jokes.ts @@ -18,8 +18,9 @@ export const useJokes = defineStore('jokes', { this.jokes.push(this.current) } - this.$patch({ current: await getRandomJoke() }) - // this.current = await getRandomJoke() + // NOTE: Avoid patching an object because it's recursive + // this.$patch({ current: await getRandomJoke() }) + this.current = await getRandomJoke() }, }, })