-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(pinia-orm-659): Save hydrated models if not updated (#671)
* perf(pinia-orm-659): Save hydrated models if not updated * refactor(pinia-orm): typo closes #659
- Loading branch information
Showing
8 changed files
with
173 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import { defineStore } from 'pinia' | ||
import type { Model } from '../model/Model' | ||
import { useStoreActions } from './useStoreActions' | ||
|
||
export function useDataStore<M extends Model = Model>( | ||
export function useDataStore( | ||
id: string, | ||
options: Record<string, any> | null = null, | ||
) { | ||
return defineStore(id, { | ||
state: (): DataStoreState<M> => ({ data: {} }), | ||
state: (): DataStoreState => ({ data: {} }), | ||
actions: useStoreActions(), | ||
...options, | ||
}) | ||
} | ||
|
||
export interface DataStoreState<M extends Model = Model> { | ||
data: Record<string, M> | ||
export interface DataStoreState { | ||
data: Record<string, Record<string, any>> | ||
} | ||
|
||
export type DataStore = ReturnType<typeof import('@/composables')['useDataStore']> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
packages/pinia-orm/tests/performance/prevent_rerender_of_child_components.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { mount } from '@vue/test-utils' | ||
import { createTestingPinia } from '@pinia/testing' | ||
import { computed, defineComponent, nextTick, onUpdated } from 'vue-demi' | ||
|
||
import { Model, useRepo } from '../../src' | ||
import { Num, Str } from '../../src/decorators' | ||
|
||
describe('performance/prevent_rerender_of_child_components', () => { | ||
class Post extends Model { | ||
static entity = 'posts' | ||
|
||
@Num(0) id!: number | ||
@Str('') title!: string | ||
} | ||
|
||
const PostComponent = defineComponent({ | ||
props: { | ||
post: { | ||
type: Object, | ||
required: true, | ||
}, | ||
}, | ||
setup() { | ||
onUpdated(() => { | ||
console.log('<PostComponent /> Updated') | ||
}) | ||
}, | ||
template: ` | ||
<div>{{ post.title }}</div> | ||
`, | ||
}) | ||
|
||
const MainComponent = defineComponent({ | ||
components: { PostComponent }, | ||
setup() { | ||
const postRepo = useRepo(Post) | ||
|
||
const posts = computed(() => postRepo.all()) | ||
let counter = 10 | ||
|
||
const addPost = () => { | ||
postRepo.insert({ | ||
id: counter++, | ||
title: `Test ${counter}`, | ||
}) | ||
} | ||
|
||
return { | ||
posts, | ||
addPost, | ||
} | ||
}, | ||
template: ` | ||
<div> | ||
<button @click="addPost" > Click me </button> | ||
<post-component v-for="post in posts" :post="post" :key="post.id" /> | ||
</div>`, | ||
}) | ||
|
||
it('it doesnt rerender child', async () => { | ||
expect(MainComponent).toBeTruthy() | ||
|
||
const wrapper = mount(MainComponent, { | ||
global: { | ||
plugins: [ | ||
createTestingPinia({ | ||
stubActions: false, | ||
initialState: { | ||
posts: { | ||
data: { | ||
1: { id: 1, title: 'Test 1' }, | ||
}, | ||
}, | ||
}, | ||
}), | ||
], | ||
}, | ||
}) | ||
|
||
const logger = vi.spyOn(console, 'log') | ||
|
||
await wrapper.find('button').trigger('click') | ||
await nextTick() | ||
await wrapper.find('button').trigger('click') | ||
await nextTick() | ||
await wrapper.find('button').trigger('click') | ||
await nextTick() | ||
|
||
expect(wrapper.html()).toContain('Test 1') | ||
expect(wrapper.html()).toContain('Test 11') | ||
expect(wrapper.html()).toContain('Test 12') | ||
expect(wrapper.html()).toContain('Test 13') | ||
|
||
expect(logger).not.toBeCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.