UNPKG

vuestic-ui

Version:
1 lines 2.23 kB
{"version":3,"file":"current-app.mjs","sources":["../../../../src/services/current-app.ts"],"sourcesContent":["import type { App } from 'vue'\nimport { getCurrentInstance, inject as vueInject } from 'vue'\nimport { throwError } from '../utils/console'\n\n/**\n * Similar to `getCurrentInstance` but for plugins, so we can use inject in plugins.\n */\nlet currentApp: App | null = null\nlet prevRegisteredApp: App | null = null\n\nexport const setCurrentApp = (newApp: App | null) => {\n // If prev app unregistered, set it to null\n if (prevRegisteredApp?._instance === null) {\n prevRegisteredApp = null\n }\n\n // Disallow setting current app to null if prev app is not unregistered\n if (newApp === null && prevRegisteredApp === null) { return }\n\n // In case prevApp registered we allow currentApp to be null\n // This is used to understand if we're in single app mode\n // If user have registered app once we allow him to use composables outside of setup function\n // Otherwise we throw error because we don't know which app context to use\n prevRegisteredApp = currentApp\n currentApp = newApp\n}\n\nexport const getCurrentApp = () => currentApp\n\n/** Wrapper around vue inject, so it can be used in plugins */\nexport const inject = ((key: string, value: any = undefined) => {\n const injectedFromApp = getCurrentApp()?._context.provides[key]\n const vm = getCurrentInstance()\n\n if (vm) {\n return vueInject(key, value)\n }\n\n // In case user in single app mode, we allow him to use composables outside of setup function\n return injectedFromApp ?? throwError('You\\'re using Vuestic composable outside Vue app. Since you registered Vuestic in multiple apps, composables can not be used outside setup function anymore.')\n}) as unknown as typeof vueInject\n"],"names":["vueInject"],"mappings":";;AAOA,IAAI,aAAyB;AAC7B,IAAI,oBAAgC;AAEvB,MAAA,gBAAgB,CAAC,WAAuB;AAE/C,OAAA,uDAAmB,eAAc,MAAM;AACrB,wBAAA;AAAA,EACtB;AAGI,MAAA,WAAW,QAAQ,sBAAsB,MAAM;AAAE;AAAA,EAAO;AAMxC,sBAAA;AACP,eAAA;AACf;AAEO,MAAM,gBAAgB,MAAM;AAG5B,MAAM,SAAU,CAAC,KAAa,QAAa,WAAc;;AAC9D,QAAM,mBAAkB,mBAAiB,MAAjB,mBAAiB,SAAS,SAAS;AAC3D,QAAM,KAAK;AAEX,MAAI,IAAI;AACC,WAAAA,SAAU,KAAK,KAAK;AAAA,EAC7B;AAGO,SAAA,mBAAmB,WAAW,6JAA8J;AACrM;"}