Skip to content

Commit

Permalink
fix: prevent multiple plugins get installed (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 5, 2020
1 parent 4ebeda4 commit 94d4d87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/install.ts
Expand Up @@ -2,7 +2,11 @@ import type { VueConstructor } from 'vue'
import { AnyObject } from './types/basic'
import { hasSymbol, hasOwn, isPlainObject, assert } from './utils'
import { isRef } from './reactivity'
import { setVueConstructor, isVueRegistered } from './runtimeContext'
import {
setVueConstructor,
isVueRegistered,
isPluginInstalled,
} from './runtimeContext'
import { mixin } from './mixin'

/**
Expand Down Expand Up @@ -40,7 +44,7 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
}

export function install(Vue: VueConstructor) {
if (isVueRegistered()) {
if (isPluginInstalled() || isVueRegistered(Vue)) {
if (__DEV__) {
assert(
false,
Expand All @@ -52,10 +56,7 @@ export function install(Vue: VueConstructor) {

if (__DEV__) {
if (!Vue.version.startsWith('2.')) {
assert(
false,
`@vue/composition-api only works with Vue 2, v${Vue.version} found.`
)
assert(false, `only works with Vue 2, v${Vue.version} found.`)
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/runtimeContext.ts
@@ -1,14 +1,20 @@
import { VueConstructor } from 'vue'
import type { VueConstructor } from 'vue'
import { ComponentInstance } from './component'
import { assert } from './utils'
import { assert, hasOwn } from './utils'

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

export function isVueRegistered() {
const PluginInstalledFlag = '__composition_api_installed__'

export function isPluginInstalled() {
return !!vueConstructor
}

export function isVueRegistered(Vue: VueConstructor) {
return hasOwn(Vue, PluginInstalledFlag)
}

export function getVueConstructor(): VueConstructor {
if (__DEV__) {
assert(
Expand All @@ -22,6 +28,11 @@ export function getVueConstructor(): VueConstructor {

export function setVueConstructor(Vue: VueConstructor) {
vueConstructor = Vue
Object.defineProperty(Vue, PluginInstalledFlag, {
configurable: true,
writable: true,
value: true,
})
}

export function getCurrentInstance(): ComponentInstance | null {
Expand Down

0 comments on commit 94d4d87

Please sign in to comment.