From 48362eea85ef57bf0858028dc62b5be77d05c5e8 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 4 Aug 2021 15:34:15 +0200 Subject: [PATCH] chore: notes about patch --- __tests__/store.patch.spec.ts | 8 ++++++-- playground/src/stores/jokes.ts | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) 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() }, }, }) -- 2.47.3