-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Fix#3336/allow using composables outside setup context (#3355)"
This reverts commit 3b37d87.
- Loading branch information
Showing
6 changed files
with
37 additions
and
55 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
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 |
---|---|---|
@@ -1,52 +1,17 @@ | ||
import type { App, provide as vueProvide } from 'vue' | ||
import { inject as vueInject, getCurrentInstance } from 'vue' | ||
import type { App } from 'vue' | ||
import { inject as vueInject } from 'vue' | ||
|
||
/** | ||
* Similar to `getCurrentInstance` but for plugins, so we can use inject in plugins. | ||
*/ | ||
let app: App | null | ||
let singleApp: App | null | undefined | ||
|
||
export const setCurrentApp = (instance: App | null) => { | ||
app = instance | ||
export const setCurrentApp = (instance: App | null) => { app = instance } | ||
export const getCurrentApp = () => app | ||
|
||
if (singleApp && instance !== singleApp) { | ||
// This means that user has multiple apps on page. | ||
singleApp = null | ||
} else { | ||
singleApp = instance | ||
} | ||
} | ||
|
||
/** | ||
* Returns current app if Vuestic UI is used in single app mode. | ||
* | ||
* @throws Error if Vuestic UI is used in multiple apps. | ||
* @throws Error if Vuestic UI plugin is not installed. | ||
*/ | ||
export const getCurrentApp = () => { | ||
const app = getCurrentInstance()?.appContext.app | ||
if (app) { return app } | ||
|
||
if (singleApp === undefined) { | ||
throw new Error('Vuestic UI plugin is not installed.') | ||
} | ||
if (singleApp === null) { | ||
throw new Error('Vuestic UI is used in multiple apps. You`re not allowed to use composable outside of setup function context.') | ||
} | ||
return singleApp | ||
} | ||
|
||
/** Wrapper around vue inject, so we can use it in plugins and outside of setup context if only one app is used */ | ||
/** Wrapper around vue inject, so it can be used in plugins */ | ||
export const inject = ((key: string, value?: any) => { | ||
const app = getCurrentApp()?._context.provides[key] | ||
|
||
return app || vueInject(key, value) | ||
}) as typeof vueInject | ||
|
||
/** Wrapper around vue provide, so we can use it in plugins and outside of setup context if only one app is used */ | ||
export const provide = ((key: string, value: any) => { | ||
const provides = getCurrentInstance()?.appContext.provides || getCurrentApp()._context.provides | ||
|
||
provides[key] = value | ||
}) as typeof vueProvide | ||
}) as unknown as typeof vueInject |
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