From 8c48d41eedc011a4b2c40006f9e7688f66fbd7bb Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Mon, 3 Jul 2023 11:41:05 +0530 Subject: [PATCH] refactor: expose function to get a service instance from dioc through module --- .../hoppscotch-common/src/modules/dioc.ts | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-common/src/modules/dioc.ts b/packages/hoppscotch-common/src/modules/dioc.ts index bdbb89619..61fa0b312 100644 --- a/packages/hoppscotch-common/src/modules/dioc.ts +++ b/packages/hoppscotch-common/src/modules/dioc.ts @@ -1,13 +1,33 @@ import { HoppModule } from "." -import { Container } from "dioc" +import { Container, Service } from "dioc" import { diocPlugin } from "dioc/vue" +const serviceContainer = new Container() + +/** + * Gets a service from the app service container. You can use this function + * to get a service if you have no access to the container or if you are not + * in a component (if you are, you can use `useService`) or if you are not in a + * service. + * @param service The class of the service to get + * @returns The service instance + * + * @deprecated This is a temporary escape hatch for legacy code to access + * services. Please use `useService` if within components or try to convert your + * legacy subsystem into a service if possible. + */ +export function getService & { ID: string }>( + service: T +): InstanceType { + return serviceContainer.bind(service) +} + export default { onVueAppInit(app) { // TODO: look into this // @ts-expect-error Something weird with Vue versions app.use(diocPlugin, { - container: new Container(), + container: serviceContainer, }) }, }