Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn when create store with exsist id #1564

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/pinia/__tests__/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,12 @@ describe('Store', () => {
`[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "anyName" in store "main".`
).toHaveBeenWarnedTimes(1)
})
})
it('warns when creating store with existing id', async () => {
const id = 'test id';
const useFirstStore = defineStore(id, {});
const useSecondStore = defineStore(id, {});
useFirstStore();
useSecondStore();
expect(`Store with id: ${id} already exists. Stores should have unique id.`).toHaveBeenWarned();
});
});
62 changes: 33 additions & 29 deletions packages/pinia/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function createOptionsStore<
const localState =
__DEV__ && hot
? // use ref() to unwrap refs inside state TODO: check if this is still necessary
toRefs(ref(state ? state() : {}).value)
toRefs(ref(state ? state() : {}).value)
: toRefs(pinia.state.value[id])

return assign(
Expand Down Expand Up @@ -316,10 +316,10 @@ function createSetupStore<
/* istanbul ignore next */
const $reset = __DEV__
? () => {
throw new Error(
`🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().`
)
}
throw new Error(
`🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().`
)
}
: noop

function $dispose() {
Expand Down Expand Up @@ -441,10 +441,10 @@ function createSetupStore<
assign(
__DEV__ && IS_CLIENT
? // devtools custom properties
{
_customProperties: markRaw(new Set<string>()),
_hmrPayload,
}
{
_customProperties: markRaw(new Set<string>()),
_hmrPayload,
}
: {},
partialStore
// must be added later
Expand Down Expand Up @@ -522,7 +522,7 @@ function createSetupStore<
if (isComputed(prop)) {
_hmrPayload.getters[key] = isOptionsStore
? // @ts-expect-error
options.getters[key]
options.getters[key]
: prop
if (IS_CLIENT) {
const getters: string[] =
Expand Down Expand Up @@ -620,10 +620,10 @@ function createSetupStore<
const getter: _Method = newStore._hmrPayload.getters[getterName]
const getterValue = isOptionsStore
? // special handling of options api
computed(() => {
setActivePinia(pinia)
return getter.call(store, store)
})
computed(() => {
setActivePinia(pinia)
return getter.call(store, store)
})
: getter

set(store, getterName, getterValue)
Expand Down Expand Up @@ -715,8 +715,8 @@ function createSetupStore<
) {
console.warn(
`[🍍]: The "state" must be a plain object. It cannot be\n` +
`\tstate: () => new MyClass()\n` +
`Found in store "${store.$id}".`
`\tstate: () => new MyClass()\n` +
`Found in store "${store.$id}".`
)
}

Expand Down Expand Up @@ -845,17 +845,17 @@ export function defineStore(
let id: string
let options:
| DefineStoreOptions<
string,
StateTree,
_GettersTree<StateTree>,
_ActionsTree
>
string,
StateTree,
_GettersTree<StateTree>,
_ActionsTree
>
| DefineSetupStoreOptions<
string,
StateTree,
_GettersTree<StateTree>,
_ActionsTree
>
string,
StateTree,
_GettersTree<StateTree>,
_ActionsTree
>

const isSetupStore = typeof setup === 'function'
if (typeof idOrOptions === 'string') {
Expand All @@ -879,9 +879,9 @@ export function defineStore(
if (__DEV__ && !activePinia) {
throw new Error(
`[🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?\n` +
`\tconst pinia = createPinia()\n` +
`\tapp.use(pinia)\n` +
`This will fail in production.`
`\tconst pinia = createPinia()\n` +
`\tapp.use(pinia)\n` +
`This will fail in production.`
)
}

Expand All @@ -900,6 +900,10 @@ export function defineStore(
// @ts-expect-error: not the right inferred type
useStore._pinia = pinia
}
} else {
if (__DEV__) console.warn(
`Store with id: ${id} already exists. Stores should have unique id.`
)
}

const store: StoreGeneric = pinia._s.get(id)!
Expand Down