Skip to content

Commit

Permalink
refactor: wip remove vue-demi
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jan 30, 2025
1 parent 7279955 commit a046bde
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 205 deletions.
4 changes: 2 additions & 2 deletions packages/docs/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig, Plugin } from 'vite'
import { defineConfig, type Plugin } from 'vite'
import _fs from 'fs'
import path from 'path'
// import TypeDocPlugin from './vite-typedoc-plugin'
Expand All @@ -24,7 +24,7 @@ export default defineConfig({
__BROWSER__: 'true',
},
optimizeDeps: {
exclude: ['vue-demi', '@vueuse/shared', '@vueuse/core', 'pinia'],
exclude: ['@vueuse/shared', '@vueuse/core', 'pinia'],
},
})

Expand Down
42 changes: 0 additions & 42 deletions packages/nuxt/src/runtime/plugin.vue2.ts

This file was deleted.

8 changes: 3 additions & 5 deletions packages/pinia/__tests__/storeToRefs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, beforeEach, it, expect, vi } from 'vitest'
import { computed, reactive, ref, ToRefs } from 'vue'
import { createPinia, defineStore, setActivePinia, storeToRefs } from '../src'
import { set } from 'vue-demi'

describe('storeToRefs', () => {
beforeEach(() => {
Expand Down Expand Up @@ -181,11 +180,10 @@ describe('storeToRefs', () => {
const { double } = storeToRefs(store)

// Assuming HMR operation
set(
store,
'double',
// @ts-expect-error: hmr
store.double =
//
computed(() => 1)
)

expect(double.value).toEqual(1)
})
Expand Down
3 changes: 1 addition & 2 deletions packages/pinia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
"@vue/test-utils": "^2.4.6"
},
"dependencies": {
"@vue/devtools-api": "^6.6.3",
"vue-demi": "^0.14.10"
"@vue/devtools-api": "^6.6.3"
},
"peerDependencies": {
"typescript": ">=4.4.4",
Expand Down
22 changes: 10 additions & 12 deletions packages/pinia/src/createPinia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Pinia, PiniaPlugin, setActivePinia, piniaSymbol } from './rootStore'
import { ref, App, markRaw, effectScope, isVue2, Ref } from 'vue-demi'
import { ref, App, markRaw, effectScope, Ref } from 'vue'
import { registerPiniaDevtools, devtoolsPlugin } from './devtools'
import { IS_CLIENT } from './env'
import { StateTree, StoreGeneric } from './types'
Expand All @@ -24,21 +24,19 @@ export function createPinia(): Pinia {
// this allows calling useStore() outside of a component setup after
// installing pinia's plugin
setActivePinia(pinia)
if (!isVue2) {
pinia._a = app
app.provide(piniaSymbol, pinia)
app.config.globalProperties.$pinia = pinia
/* istanbul ignore else */
if (__USE_DEVTOOLS__ && IS_CLIENT) {
registerPiniaDevtools(app, pinia)
}
toBeInstalled.forEach((plugin) => _p.push(plugin))
toBeInstalled = []
pinia._a = app
app.provide(piniaSymbol, pinia)
app.config.globalProperties.$pinia = pinia
/* istanbul ignore else */
if (__USE_DEVTOOLS__ && IS_CLIENT) {
registerPiniaDevtools(app, pinia)
}
toBeInstalled.forEach((plugin) => _p.push(plugin))
toBeInstalled = []
},

use(plugin) {
if (!this._a && !isVue2) {
if (!this._a) {
toBeInstalled.push(plugin)
} else {
_p.push(plugin)
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia/src/devtools/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CustomInspectorState,
} from '@vue/devtools-api'
import { MutationType, StoreGeneric } from '../types'
import { DebuggerEvent } from 'vue-demi'
import { DebuggerEvent } from 'vue'
import { Pinia } from '../rootStore'
import { isPinia } from './utils'

Expand Down
2 changes: 1 addition & 1 deletion packages/pinia/src/devtools/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
TimelineEvent,
App as DevtoolsApp,
} from '@vue/devtools-api'
import { ComponentPublicInstance, markRaw, toRaw, unref, watch } from 'vue-demi'
import { ComponentPublicInstance, markRaw, toRaw, unref, watch } from 'vue'
import { Pinia, PiniaPluginContext } from '../rootStore'
import {
_GettersTree,
Expand Down
8 changes: 2 additions & 6 deletions packages/pinia/src/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRef, isReactive, isVue2, set } from 'vue-demi'
import { isRef, isReactive } from 'vue'
import { Pinia } from './rootStore'
import {
isPlainObject,
Expand Down Expand Up @@ -53,11 +53,7 @@ export function patchObject(
} else {
// objects are either a bit more complex (e.g. refs) or primitives, so we
// just set the whole thing
if (isVue2) {
set(newState, key, subPatch)
} else {
newState[key] = subPatch
}
newState[key] = subPatch
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pinia/src/mapHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentPublicInstance, ComputedRef, UnwrapRef } from 'vue-demi'
import type { ComponentPublicInstance, ComputedRef, UnwrapRef } from 'vue'
import type {
_GettersTree,
_StoreWithGetters_Writable,
Expand Down
2 changes: 1 addition & 1 deletion packages/pinia/src/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
hasInjectionContext,
InjectionKey,
Ref,
} from 'vue-demi'
} from 'vue'
import {
StateTree,
PiniaCustomProperties,
Expand Down
87 changes: 28 additions & 59 deletions packages/pinia/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ import {
toRefs,
Ref,
ref,
set,
del,
nextTick,
isVue2,
} from 'vue-demi'
} from 'vue'
import {
StateTree,
SubscriptionCallback,
Expand Down Expand Up @@ -166,11 +163,7 @@ function createOptionsStore<
function setup() {
if (!initialState && (!__DEV__ || !hot)) {
/* istanbul ignore if */
if (isVue2) {
set(pinia.state.value, id, state ? state() : {})
} else {
pinia.state.value[id] = state ? state() : {}
}
pinia.state.value[id] = state ? state() : {}
}

// avoid creating a state in pinia.state.value
Expand Down Expand Up @@ -198,8 +191,6 @@ function createOptionsStore<
const store = pinia._s.get(id)!

// allow cross using stores
/* istanbul ignore if */
if (isVue2 && !store._r) return

// @ts-expect-error
// return getters![name].call(context, context)
Expand Down Expand Up @@ -250,7 +241,7 @@ function createSetupStore<
// watcher options for $subscribe
const $subscribeOptions: WatchOptions = { deep: true }
/* istanbul ignore else */
if (__DEV__ && !isVue2) {
if (__DEV__) {
$subscribeOptions.onTrigger = (event) => {
/* istanbul ignore else */
if (isListening) {
Expand Down Expand Up @@ -282,11 +273,7 @@ function createSetupStore<
// by the setup
if (!isOptionsStore && !initialState && (!__DEV__ || !hot)) {
/* istanbul ignore if */
if (isVue2) {
set(pinia.state.value, $id, {})
} else {
pinia.state.value[$id] = {}
}
pinia.state.value[$id] = {}
}

const hotState = ref({} as S)
Expand Down Expand Up @@ -478,12 +465,6 @@ function createSetupStore<
$dispose,
} as _StoreWithState<Id, S, G, A>

/* istanbul ignore if */
if (isVue2) {
// start as non ready
partialStore._r = false
}

const store: Store<Id, S, G, A> = reactive(
__DEV__ || (__USE_DEVTOOLS__ && IS_CLIENT)
? assign(
Expand Down Expand Up @@ -517,7 +498,7 @@ function createSetupStore<
if ((isRef(prop) && !isComputed(prop)) || isReactive(prop)) {
// mark it as a piece of state to be serialized
if (__DEV__ && hot) {
set(hotState.value, key, toRef(setupStore, key))
hotState.value[key] = toRef(setupStore, key)
// createOptionStore directly sets the state in pinia.state.value so we
// can just skip that
} else if (!isOptionsStore) {
Expand All @@ -532,12 +513,7 @@ function createSetupStore<
}
}
// transfer the ref to the pinia state to keep everything in sync
/* istanbul ignore if */
if (isVue2) {
set(pinia.state.value[$id], key, prop)
} else {
pinia.state.value[$id][key] = prop
}
pinia.state.value[$id][key] = prop
}

/* istanbul ignore else */
Expand All @@ -549,13 +525,8 @@ function createSetupStore<
const actionValue = __DEV__ && hot ? prop : action(prop as _Method, key)
// this a hot module replacement store because the hotUpdate method needs
// to do it with the right context
/* istanbul ignore if */
if (isVue2) {
set(setupStore, key, actionValue)
} else {
// @ts-expect-error
setupStore[key] = actionValue
}
// @ts-expect-error
setupStore[key] = actionValue

/* istanbul ignore else */
if (__DEV__) {
Expand Down Expand Up @@ -585,16 +556,10 @@ function createSetupStore<

// add the state, getters, and action properties
/* istanbul ignore if */
if (isVue2) {
Object.keys(setupStore).forEach((key) => {
set(store, key, setupStore[key])
})
} else {
assign(store, setupStore)
// allows retrieving reactive objects with `storeToRefs()`. Must be called after assigning to the reactive object.
// Make `storeToRefs()` work with `reactive()` #799
assign(toRaw(store), setupStore)
}
assign(store, setupStore)
// allows retrieving reactive objects with `storeToRefs()`. Must be called after assigning to the reactive object.
// Make `storeToRefs()` work with `reactive()` #799
assign(toRaw(store), setupStore)

// use this instead of a computed with setter to be able to create it anywhere
// without linking the computed lifespan to wherever the store is first
Expand Down Expand Up @@ -635,13 +600,15 @@ function createSetupStore<
}
// patch direct access properties to allow store.stateProperty to work as
// store.$state.stateProperty
set(store, stateKey, toRef(newStore.$state, stateKey))
// @ts-expect-error: any type
store[stateKey] = toRef(newStore.$state, stateKey)
})

// remove deleted state properties
Object.keys(store.$state).forEach((stateKey) => {
if (!(stateKey in newStore.$state)) {
del(store, stateKey)
// @ts-expect-error: noop if doesn't exist
delete store[stateKey]
}
})

Expand All @@ -657,7 +624,10 @@ function createSetupStore<
for (const actionName in newStore._hmrPayload.actions) {
const actionFn: _Method = newStore[actionName]

set(store, actionName, action(actionFn, actionName))
// @ts-expect-error: actionName is a string
store[actionName] =
//
action(actionFn, actionName)
}

// TODO: does this work in both setup and option store?
Expand All @@ -671,20 +641,25 @@ function createSetupStore<
})
: getter

set(store, getterName, getterValue)
// @ts-expect-error: getterName is a string
store[getterName] =
//
getterValue
}

// remove deleted getters
Object.keys(store._hmrPayload.getters).forEach((key) => {
if (!(key in newStore._hmrPayload.getters)) {
del(store, key)
// @ts-expect-error: noop if doesn't exist
delete store[key]
}
})

// remove old actions
Object.keys(store._hmrPayload.actions).forEach((key) => {
if (!(key in newStore._hmrPayload.actions)) {
del(store, key)
// @ts-expect-error: noop if doesn't exist
delete store[key]
}
})

Expand Down Expand Up @@ -715,12 +690,6 @@ function createSetupStore<
)
}

/* istanbul ignore if */
if (isVue2) {
// mark the store as ready before plugins
store._r = true
}

// apply all plugins
pinia._p.forEach((extender) => {
/* istanbul ignore else */
Expand Down
Loading

0 comments on commit a046bde

Please sign in to comment.