Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
feat: ensure that only one room is open per browser
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossantos74 committed Jan 16, 2024
1 parent a048eb9 commit 8385977
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/base/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('BaseComponent', () => {
let DummyComponentInstance: DummyComponent;

beforeEach(() => {
console.error = jest.fn();

jest.clearAllMocks();
DummyComponentInstance = new DummyComponent();
});
Expand Down
21 changes: 19 additions & 2 deletions src/core/launcher/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ describe('Launcher', () => {
let LauncherInstance: Launcher;

beforeEach(() => {
console.warn = jest.fn();
console.error = jest.fn();
console.log = jest.fn();

jest.clearAllMocks();

LauncherInstance = new Launcher(DEFAULT_INITIALIZATION_MOCK);
console.error = jest.fn();
console.log = jest.fn();
});

test('should be defined', () => {
Expand Down Expand Up @@ -449,4 +451,19 @@ describe('Launcher Facade', () => {
expect(LauncherFacadeInstance).toHaveProperty('addComponent');
expect(LauncherFacadeInstance).toHaveProperty('removeComponent');
});

test('should return the same instance if already initialized', () => {
const instance = Facade(DEFAULT_INITIALIZATION_MOCK);
const instance2 = Facade(DEFAULT_INITIALIZATION_MOCK);

expect(instance).toStrictEqual(instance2);
});

test('should return different instances if it`s destroyed', () => {
const instance = Facade(DEFAULT_INITIALIZATION_MOCK);
instance.destroy();
const instance2 = Facade(DEFAULT_INITIALIZATION_MOCK);

expect(instance).not.toStrictEqual(instance2);
});
});
17 changes: 17 additions & 0 deletions src/core/launcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export class Launcher extends Observable implements DefaultLauncher {
this.realtime.leave();
this.realtime = undefined;
this.isDestroyed = true;

// clean window object
window.SUPERVIZ = undefined;
};

/**
Expand Down Expand Up @@ -353,8 +356,22 @@ export class Launcher extends Observable implements DefaultLauncher {
* @returns {LauncherFacade}
*/
export default (options: LauncherOptions): LauncherFacade => {
if (window.SUPERVIZ) {
console.warn('[SUPERVIZ] Room already initialized');

return {
destroy: window.SUPERVIZ.destroy,
subscribe: window.SUPERVIZ.subscribe,
unsubscribe: window.SUPERVIZ.unsubscribe,
addComponent: window.SUPERVIZ.addComponent,
removeComponent: window.SUPERVIZ.removeComponent,
};
}

const launcher = new Launcher(options);

window.SUPERVIZ = launcher;

return {
destroy: launcher.destroy,
subscribe: launcher.subscribe,
Expand Down
2 changes: 2 additions & 0 deletions src/shims.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { SuperVizCdn } from './common/types/cdn.types';
import { Launcher } from './core/launcher';

declare global {
interface Window {
SuperVizRoom: SuperVizCdn;
SUPERVIZ: Launcher;
}
}

0 comments on commit 8385977

Please sign in to comment.