From 789bef6e31c9dbe6c4b93ca2730c659aeb1328b6 Mon Sep 17 00:00:00 2001 From: Martin Koppehel Date: Thu, 24 Oct 2019 17:48:32 +0200 Subject: [PATCH] Fix #41, #46 --- projects/ngx-golden-layout/package-lock.json | 2 +- projects/ngx-golden-layout/package.json | 2 +- .../src/lib/multiwindow-service.ts | 42 ++++++++++++------- .../src/lib/plugin-registry.service.ts | 2 +- src/app/app.module.ts | 28 +++++++------ src/main.ts | 1 - tsconfig.app.json | 4 +- 7 files changed, 46 insertions(+), 35 deletions(-) diff --git a/projects/ngx-golden-layout/package-lock.json b/projects/ngx-golden-layout/package-lock.json index aba25b7..ec884aa 100644 --- a/projects/ngx-golden-layout/package-lock.json +++ b/projects/ngx-golden-layout/package-lock.json @@ -1,5 +1,5 @@ { "name": "ngx-golden-layout", - "version": "0.0.30", + "version": "0.0.31", "lockfileVersion": 1 } diff --git a/projects/ngx-golden-layout/package.json b/projects/ngx-golden-layout/package.json index 30e9efa..24193ae 100644 --- a/projects/ngx-golden-layout/package.json +++ b/projects/ngx-golden-layout/package.json @@ -1,6 +1,6 @@ { "name": "ngx-golden-layout", - "version": "0.0.30", + "version": "0.0.31", "license": "BSD-3-Clause", "author": { "name": "Martin Koppehel", diff --git a/projects/ngx-golden-layout/src/lib/multiwindow-service.ts b/projects/ngx-golden-layout/src/lib/multiwindow-service.ts index 84c06fd..df2185f 100644 --- a/projects/ngx-golden-layout/src/lib/multiwindow-service.ts +++ b/projects/ngx-golden-layout/src/lib/multiwindow-service.ts @@ -1,17 +1,19 @@ export function MultiWindowInit(): void { console.log('MultiWindowInit'); if (!window.opener) { - (window as any).__services = new (window as any).Map(); - (window as any).__serviceConstructors = new (window as any).Map(); + if (!(window as any).__services && !(window as any).__serviceConstructors) { + (window as any).__services = new (window as any).Map(); + (window as any).__serviceConstructors = new (window as any).Map(); - // Electron compatibility, when we have a global 'require' in our window, we throw it into the new window context - if ((window as any).require) { - const originalWindowOpen = window.open.bind(window); - window.open = (url?: string, target?: string, features?: string, replace?: boolean): Window => { - const newWindow = originalWindowOpen(url, target, features, replace); - newWindow.require = (window as any).require; - return newWindow; - }; + // Electron compatibility, when we have a global 'require' in our window, we throw it into the new window context + if ((window as any).require) { + const originalWindowOpen = window.open.bind(window); + window.open = (url?: string, target?: string, features?: string, replace?: boolean): Window => { + const newWindow = originalWindowOpen(url, target, features, replace); + newWindow.require = (window as any).require; + return newWindow; + }; + } } } } @@ -20,20 +22,28 @@ export type Constructor = { new (...args: any[]): T; } -export function MultiWindowService() { +export function MultiWindowService(uniqueName: string) { + MultiWindowInit(); return function (constructor: Constructor): Constructor { const constr = constructor as any; const rootWindow = (window.opener || window) as any; + const rootWindowIsMyWindow = rootWindow === window; + if (rootWindowIsMyWindow) { + if (rootWindow.__serviceConstructors.has(uniqueName)) { + throw new Error(`MultiWindowService(): uniqueName ${uniqueName} already taken.`); + } + rootWindow.__serviceConstructors.set(uniqueName, constr); + } const newConstructor = (function(...args: any[]): T { - const hasInstance = rootWindow.__services.has(constr.name); + const hasInstance = rootWindow.__services.has(uniqueName); if (!hasInstance) { - const storedConstr = rootWindow.__serviceConstructors.get(constr.name) || constr; - rootWindow.__services.set(constr.name, new storedConstr(...args)); + const storedConstr = rootWindow.__serviceConstructors.get(uniqueName) || constr; + rootWindow.__services.set(uniqueName, new storedConstr(...args)); } - return rootWindow.__services.get(constr.name); + return rootWindow.__services.get(uniqueName); }) as any; try { - if (window === rootWindow) { + if (rootWindowIsMyWindow) { const metadata = (Reflect as any).getMetadata('design:paramtypes', constr); (Reflect as any).metadata('design:paramtypes', metadata)(newConstructor); } diff --git a/projects/ngx-golden-layout/src/lib/plugin-registry.service.ts b/projects/ngx-golden-layout/src/lib/plugin-registry.service.ts index 97f3d9c..b214ba5 100644 --- a/projects/ngx-golden-layout/src/lib/plugin-registry.service.ts +++ b/projects/ngx-golden-layout/src/lib/plugin-registry.service.ts @@ -22,7 +22,7 @@ interface IPluginURL { * Because we can't have progress reporting about all windows, we also don't * return any progress/success indicator here. */ -@MultiWindowService() +@MultiWindowService('_gl__PluginURLProvider') @Injectable() export class PluginURLProvider { private loadedURLs = new Map(); diff --git a/src/app/app.module.ts b/src/app/app.module.ts index a516a5b..70512d2 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -59,7 +59,7 @@ const CONFIG: IExtendedGoldenLayoutConfig = { } }; -@MultiWindowService() +@MultiWindowService('FooService') @Injectable() export class FooService { constructor() { @@ -67,7 +67,7 @@ export class FooService { } } -@MultiWindowService() +@MultiWindowService('TestService') @Injectable() export class TestService { public id: string; @@ -217,18 +217,20 @@ export class FailComponent { constructor(@Inject(FailedComponent) public componentName: string) { } } -const DEPS: PluginDependencyType[] = [{ - name: '@angular/core', - loader: import('@angular/core'), -}, { - name: '@angular/common', - loader: import('@angular/common'), -}, { - name: 'ngx-golden-layout', - loader: import('ngx-golden-layout'), -}]; +export const DEPS: PluginDependencyType[] = [ + //{ + // name: '@angular/core', + // loader: import('@angular/core'), + //}, { + // name: '@angular/common', + // loader: import('@angular/common'), + //}, { + // name: 'ngx-golden-layout', + // loader: import('ngx-golden-layout'), + //} +]; -const COMPONENTS: ComponentType[] = [ +export const COMPONENTS: ComponentType[] = [ { name: 'app-test', type: TestComponent, diff --git a/src/main.ts b/src/main.ts index 74b96e7..fd618e5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,7 +6,6 @@ import { environment } from './environments/environment'; import { MultiWindowInit } from 'ngx-golden-layout'; import * as $ from 'jquery'; - if (environment.production) { enableProdMode(); } diff --git a/tsconfig.app.json b/tsconfig.app.json index cd8ed94..b49f956 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -12,6 +12,6 @@ "src/**/*.spec.ts" ], "angularCompilerOptions": { - "enableIvy": true, - }, + "enableIvy": false + } }