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(reactive): allow usage of reactive before Vue.use #515

Merged
Merged
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
4 changes: 2 additions & 2 deletions src/reactivity/reactive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyObject } from '../types/basic'
import { getVueConstructor } from '../runtimeContext'
import { getRegisteredVueOrDefault } from '../runtimeContext'
import { isPlainObject, def, warn } from '../utils'
import { isComponentInstance, defineComponentInstance } from '../utils/helper'
import { RefKey } from '../utils/symbols'
Expand Down Expand Up @@ -94,7 +94,7 @@ export function defineAccessControl(target: AnyObject, key: any, val?: any) {
}

function observe<T>(obj: T): T {
const Vue = getVueConstructor()
const Vue = getRegisteredVueOrDefault()
let observed: T
if (Vue.observable) {
observed = Vue.observable(obj)
Expand Down
19 changes: 19 additions & 0 deletions src/runtimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import type { VueConstructor } from 'vue'
import { ComponentInstance } from './component'
import { assert, hasOwn, warn } from './utils'

let vueDependency: VueConstructor | undefined = undefined

try {
vueDependency = require('vue')
} catch {
// not available
}

let vueConstructor: VueConstructor | null = null
let currentInstance: ComponentInstance | null = null

Expand All @@ -26,6 +34,17 @@ export function getVueConstructor(): VueConstructor {
return vueConstructor!
}

// returns registered vue or `vue` dependency
export function getRegisteredVueOrDefault(): VueConstructor {
let constructor = vueConstructor || vueDependency

if (__DEV__) {
assert(vueConstructor, `No vue dependency found.`)
}

return constructor!
}

export function setVueConstructor(Vue: VueConstructor) {
if (__DEV__ && vueConstructor) {
warn('Another instance of vue installed')
Expand Down