Skip to content

Commit

Permalink
feat(reactive): allow usage of reactive before Vue.use (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Sep 15, 2020
1 parent 2275bf9 commit 89fd11c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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

0 comments on commit 89fd11c

Please sign in to comment.