-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
236 additions
and
213 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
Pinia, | ||
PiniaStorePlugin, | ||
setActivePinia, | ||
piniaSymbol, | ||
} from './rootStore' | ||
import { ref, App } from 'vue' | ||
import { devtoolsPlugin } from './devtools' | ||
import { IS_CLIENT } from './env' | ||
|
||
/** | ||
* Creates a Pinia instance to be used by the application | ||
*/ | ||
export function createPinia(): Pinia { | ||
// NOTE: here we could check the window object for a state and directly set it | ||
// if there is anything like it with Vue 3 SSR | ||
const state = ref({}) | ||
|
||
let localApp: App | undefined | ||
let _p: Pinia['_p'] = [] | ||
// plugins added before calling app.use(pinia) | ||
const toBeInstalled: PiniaStorePlugin[] = [] | ||
|
||
const pinia: Pinia = { | ||
install(app: App) { | ||
pinia._a = localApp = app | ||
// pinia._a = app | ||
app.provide(piniaSymbol, pinia) | ||
app.config.globalProperties.$pinia = pinia | ||
// TODO: write test | ||
// only set the app on client for devtools | ||
if (__BROWSER__ && IS_CLIENT) { | ||
// this allows calling useStore() outside of a component setup after | ||
// installing pinia's plugin | ||
setActivePinia(pinia) | ||
} | ||
toBeInstalled.forEach((plugin) => _p.push(plugin)) | ||
}, | ||
|
||
use(plugin) { | ||
if (!localApp) { | ||
toBeInstalled.push(plugin) | ||
} else { | ||
_p.push(plugin) | ||
} | ||
return this | ||
}, | ||
|
||
_p, | ||
// it's actually undefined here | ||
_a: localApp!, | ||
|
||
state, | ||
} | ||
|
||
if (IS_CLIENT && __BROWSER__ && __DEV__) { | ||
pinia.use(devtoolsPlugin) | ||
} | ||
|
||
return pinia | ||
} |
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,88 @@ | ||
import { CustomInspectorNode, CustomInspectorState } from '@vue/devtools-api' | ||
import { GenericStore, GettersTree, MutationType, StateTree } from '../types' | ||
import { DebuggerEvent } from 'vue' | ||
|
||
export function formatDisplay(display: string) { | ||
return { | ||
_custom: { | ||
display, | ||
}, | ||
} | ||
} | ||
|
||
export function formatStoreForInspectorTree( | ||
store: GenericStore | ||
): CustomInspectorNode { | ||
return { | ||
id: store.$id, | ||
label: store.$id, | ||
tags: [], | ||
} | ||
} | ||
|
||
export function formatStoreForInspectorState( | ||
store: GenericStore | ||
): CustomInspectorState[string] { | ||
const fields: CustomInspectorState[string] = [ | ||
{ editable: false, key: 'id', value: formatDisplay(store.$id) }, | ||
{ editable: true, key: 'state', value: store.$state }, | ||
] | ||
|
||
// avoid adding empty getters | ||
if (store._getters?.length) { | ||
fields.push({ | ||
editable: false, | ||
key: 'getters', | ||
value: store._getters.reduce((getters, key) => { | ||
getters[key] = store[key] | ||
return getters | ||
}, {} as GettersTree<StateTree>), | ||
}) | ||
} | ||
|
||
return fields | ||
} | ||
|
||
export function formatEventData( | ||
events: DebuggerEvent[] | DebuggerEvent | undefined | ||
) { | ||
if (!events) return {} | ||
if (Array.isArray(events)) { | ||
// TODO: handle add and delete for arrays and objects | ||
return events.reduce( | ||
(data, event) => { | ||
data.keys.push(event.key) | ||
data.operations.push(event.type) | ||
data.oldValue[event.key] = event.oldValue | ||
data.newValue[event.key] = event.newValue | ||
return data | ||
}, | ||
{ | ||
oldValue: {} as Record<string, any>, | ||
keys: [] as string[], | ||
operations: [] as string[], | ||
newValue: {} as Record<string, any>, | ||
} | ||
) | ||
} else { | ||
return { | ||
operation: formatDisplay(events.type), | ||
key: formatDisplay(events.key), | ||
oldValue: events.oldValue, | ||
newValue: events.newValue, | ||
} | ||
} | ||
} | ||
|
||
export function formatMutationType(type: MutationType): string { | ||
switch (type) { | ||
case MutationType.direct: | ||
return 'mutation' | ||
case MutationType.patchFunction: | ||
return '$patch' | ||
case MutationType.patchObject: | ||
return '$patch' | ||
default: | ||
return 'unknown' | ||
} | ||
} |
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 @@ | ||
export { devtoolsPlugin } from './plugin' |
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
Oops, something went wrong.