Skip to content

Commit

Permalink
feat: morfeo instance singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed Sep 23, 2021
1 parent 11c82fb commit 913370e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
19 changes: 17 additions & 2 deletions packages/core/src/morfeo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function createMorfeo() {
theme.reset();
}

return Object.freeze({
const instance = Object.freeze({
resolve,
useTheme,
setTheme,
Expand All @@ -135,6 +135,10 @@ function createMorfeo() {
getCurrent,
__dangerousReset,
});

globalThis.__MORFEO_INSTANCE = instance;

return instance;
}

export type Morfeo = ReturnType<typeof createMorfeo>;
Expand All @@ -158,4 +162,15 @@ export type Morfeo = ReturnType<typeof createMorfeo>;
*
* const currentTheme = morfeo.getTheme();
*/
export const morfeo: Morfeo = createMorfeo();
export const morfeo: Morfeo = globalThis.__MORFEO_INSTANCE || createMorfeo();

declare global {
interface Window {
__MORFEO_INSTANCE: Morfeo;
}
module NodeJS {
interface Global {
__MORFEO_INSTANCE: Morfeo;
}
}
}
11 changes: 0 additions & 11 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,3 @@ export interface Themes {
}

export type ThemeName = keyof Themes;

declare global {
interface Window {
__MORFEO_THEMES: Themes;
}
module NodeJS {
interface Global {
__MORFEO_THEMES: Themes;
}
}
}

0 comments on commit 913370e

Please sign in to comment.