|
1 | 1 | import type { ModuleOptions, AuthStore, AuthState, StoreMethod, StoreIncludeOptions } from '../../types'; |
2 | 2 | import type { NuxtApp } from '#app'; |
| 3 | +import type { Pinia, StoreDefinition } from 'pinia'; |
3 | 4 | import { isUnset, isSet, decodeValue, encodeValue, setH3Cookie } from '../../utils'; |
4 | | -import { defineStore, type Pinia, type StoreDefinition } from 'pinia'; |
5 | 5 | import { parse, serialize, type CookieSerializeOptions } from 'cookie-es'; |
6 | 6 | import { useState } from '#imports'; |
7 | 7 | import { watch, type Ref } from 'vue'; |
@@ -101,12 +101,13 @@ export class Storage { |
101 | 101 | // Local state (reactive) |
102 | 102 | // ------------------------------------ |
103 | 103 |
|
104 | | - #initState() { |
| 104 | + async #initState() { |
105 | 105 | // Use pinia for local state's if possible |
106 | 106 | const pinia = this.ctx.$pinia as Pinia |
107 | 107 | this.#piniaEnabled = this.options.stores.pinia!.enabled && !!pinia; |
108 | 108 |
|
109 | 109 | if (this.#piniaEnabled) { |
| 110 | + const { defineStore } = await import('pinia') |
110 | 111 | this.#PiniaStore = defineStore(this.options.stores.pinia?.namespace!, { |
111 | 112 | state: (): AuthState => ({ ...this.options.initialState }) |
112 | 113 | }); |
@@ -154,11 +155,11 @@ export class Storage { |
154 | 155 |
|
155 | 156 | watchState(watchKey: string, fn: (value: any) => void) { |
156 | 157 | if (this.#piniaEnabled) { |
157 | | - watch(() => this.#initPiniaStore[watchKey as keyof AuthStore], (modified, old) => { |
| 158 | + watch(() => this.#initPiniaStore?.[watchKey as keyof AuthStore], (modified, old) => { |
158 | 159 | fn(modified) |
159 | 160 | }, { deep: true }) |
160 | 161 | } else { |
161 | | - watch(() => this.#initStore.value[watchKey], (modified, old) => { |
| 162 | + watch(() => this.#initStore?.value?.[watchKey], (modified, old) => { |
162 | 163 | fn(modified) |
163 | 164 | }, { deep: true }) |
164 | 165 | } |
|
0 commit comments