Skip to content

Commit

Permalink
Fix #41, #46
Browse files Browse the repository at this point in the history
  • Loading branch information
martin31821 committed Oct 24, 2019
1 parent a18466f commit 789bef6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
2 changes: 1 addition & 1 deletion projects/ngx-golden-layout/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/ngx-golden-layout/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-golden-layout",
"version": "0.0.30",
"version": "0.0.31",
"license": "BSD-3-Clause",
"author": {
"name": "Martin Koppehel",
Expand Down
42 changes: 26 additions & 16 deletions projects/ngx-golden-layout/src/lib/multiwindow-service.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
}
}
}
Expand All @@ -20,20 +22,28 @@ export type Constructor<T> = {
new (...args: any[]): T;
}

export function MultiWindowService<T>() {
export function MultiWindowService<T>(uniqueName: string) {
MultiWindowInit();
return function (constructor: Constructor<T>): Constructor<T> {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginURLProvider>()
@MultiWindowService<PluginURLProvider>('_gl__PluginURLProvider')
@Injectable()
export class PluginURLProvider {
private loadedURLs = new Map<string, string>();
Expand Down
28 changes: 15 additions & 13 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ const CONFIG: IExtendedGoldenLayoutConfig = {
}
};

@MultiWindowService<FooService>()
@MultiWindowService<FooService>('FooService')
@Injectable()
export class FooService {
constructor() {
console.log(`Create FooService`);
}
}

@MultiWindowService<TestService>()
@MultiWindowService<TestService>('TestService')
@Injectable()
export class TestService {
public id: string;
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { environment } from './environments/environment';
import { MultiWindowInit } from 'ngx-golden-layout';
import * as $ from 'jquery';


if (environment.production) {
enableProdMode();
}
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"src/**/*.spec.ts"
],
"angularCompilerOptions": {
"enableIvy": true,
},
"enableIvy": false
}
}

0 comments on commit 789bef6

Please sign in to comment.