Skip to content
This repository was archived by the owner on Sep 7, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions packages/api-browser/src/message-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ import { distributeMessage } from './accessible-windows';

const listenerMap = new Map();

const currentWindowId = (): string => {
return ssf.Window.getCurrentWindow().getId();
const currentWindowId = (): Promise<string> => {
return ssf.Window.getCurrentWindow()
.then(win => win.getId());
};

export class MessageService implements ssf.MessageService {
static send(windowId: string, topic: string, message: any) {
const senderId = currentWindowId();

distributeMessage({
senderId,
windowId,
topic,
message
currentWindowId().then(senderId => {
distributeMessage({
senderId,
windowId,
topic,
message
});
});
}

static subscribe(windowId: string, topic: string, listener: (message: string|object, sender: string) => void) {
const receiveMessage = (event) => {
const thisId = currentWindowId();
if ((windowId === '*' || windowId === event.data.senderId)
&& (event.data.windowId === '*' || thisId === event.data.windowId)
&& event.data.senderId !== thisId
&& topic === event.data.topic) {
// Message intended for this window
listener(event.data.message, event.data.senderId);
}
currentWindowId().then(thisId => {
if ((windowId === '*' || windowId === event.data.senderId)
&& (event.data.windowId === '*' || thisId === event.data.windowId)
&& event.data.senderId !== thisId
&& topic === event.data.topic) {
// Message intended for this window
listener(event.data.message, event.data.senderId);
}
});
};

window.addEventListener('message', receiveMessage, false);
Expand Down
51 changes: 27 additions & 24 deletions packages/api-browser/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DEFAULT_OPTIONS = {
height: 600
};

let currentWindow = null;
let currentWindowPromise: Promise<Window> = null;

const getWindowOffsets = (win) => {
const xOffset = (win.outerWidth / 2);
Expand All @@ -21,7 +21,7 @@ const getWindowOffsets = (win) => {
};

export class Window extends Emitter implements ssf.WindowCore {
children: ssf.Window[];
children: Window[];
innerWindow: any;
id: string;

Expand All @@ -30,35 +30,38 @@ export class Window extends Emitter implements ssf.WindowCore {
this.children = [];

if (!options) {
// Wrap existing window
this.innerWindow = window;
this.id = window.name;
if (callback) {
callback(this);
}
} else {
// Open a new window
const winOptions = Object.assign({}, DEFAULT_OPTIONS, options);
this.innerWindow = window.open(options.url, options.name, objectToFeaturesString(winOptions));
this.id = this.innerWindow.name;
const [xOffset, yOffset] = getWindowOffsets(this.innerWindow);
this.setPosition(options.x || (screen.width / 2) - xOffset, options.y || (screen.height / 2) - yOffset);

const currentWindow = Window.getCurrentWindow();
const childClose = () => this.innerWindow.close();

this.innerWindow.addEventListener('beforeunload', () => {
const index = currentWindow.children.indexOf(this);
if (index !== -1) {
currentWindow.children.splice(index, 1);
currentWindow.innerWindow.removeEventListener('beforeunload', childClose);
Window.getCurrentWindow().then(currentWindow => {
const childClose = () => this.innerWindow.close();

this.innerWindow.addEventListener('beforeunload', () => {
const index = currentWindow.children.indexOf(this);
if (index !== -1) {
currentWindow.children.splice(index, 1);
currentWindow.innerWindow.removeEventListener('beforeunload', childClose);
}
removeAccessibleWindow(this.innerWindow.name);
});

if (options.child) {
currentWindow.children.push(this);
currentWindow.innerWindow.addEventListener('beforeunload', childClose);
}
removeAccessibleWindow(this.innerWindow.name);
addAccessibleWindow(options.name, this.innerWindow);
});

if (options.child) {
currentWindow.children.push(this);
currentWindow.innerWindow.addEventListener('beforeunload', childClose);
}
addAccessibleWindow(options.name, this.innerWindow);
}

if (callback) {
Expand Down Expand Up @@ -165,7 +168,7 @@ export class Window extends Emitter implements ssf.WindowCore {
}

getChildWindows() {
return new Promise<ssf.Window[]>(resolve => resolve(this.children));
return new Promise<ReadonlyArray<ssf.WindowCore>>(resolve => resolve(this.children));
}

asPromise<T>(fn: (...args: any[]) => any): Promise<T> {
Expand All @@ -178,13 +181,13 @@ export class Window extends Emitter implements ssf.WindowCore {
});
}

static getCurrentWindow(callback?: (win: Window) => void, errorCallback?: (err?: any) => void) {
if (currentWindow) {
return currentWindow;
static getCurrentWindow(): Promise<Window> {
if (!currentWindowPromise) {
currentWindowPromise = new Promise<Window>((resolve, reject) => {
new Window(null, resolve, reject);
});
}

currentWindow = new Window(null, callback, errorCallback);
return currentWindow;
return currentWindowPromise;
}

static wrap(win: BrowserWindow) {
Expand Down
4 changes: 3 additions & 1 deletion packages/api-demo/src/messaging/messaging-api-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const appReady = ssf.app.ready();

appReady.then(() => {
const windowDetailsId = document.getElementById('window-uuid');
windowDetailsId.innerText = ssf.Window.getCurrentWindow().getId();
ssf.Window.getCurrentWindow().then(win => {
windowDetailsId.innerText = win.getId();
});

newWindowButton.onclick = () => {
// Create a random hex string as the window name
Expand Down
16 changes: 9 additions & 7 deletions packages/api-electron/src/preload/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Emitter, Display } from 'containerjs-api-utility';
import { MessageService } from './message-service';
import { IpcMessages } from '../common/constants';

let currentWindow = null;
let currentWindowPromise: Promise<Window> = null;
const isUrlPattern = /^https?:\/\//i;

export class Window extends Emitter implements ssf.Window {
Expand All @@ -23,6 +23,7 @@ export class Window extends Emitter implements ssf.Window {
});

if (!options) {
// Wrap existing window
this.innerWindow = remote.getCurrentWindow();
this.id = String(this.innerWindow.id);
if (callback) {
Expand All @@ -31,6 +32,7 @@ export class Window extends Emitter implements ssf.Window {
return this;
}

// Open a new window
const electronOptions = Object.assign({}, options);

Display.getDisplayAlteredPosition(options.display, { x: options.x || 0, y: options.y || 0 }).then(({ x, y }) => {
Expand Down Expand Up @@ -277,13 +279,13 @@ export class Window extends Emitter implements ssf.Window {
});
}

static getCurrentWindow(callback: (win: Window) => void, errorCallback: (err?: any) => void) {
if (currentWindow) {
return currentWindow;
static getCurrentWindow(): Promise<Window> {
if (!currentWindowPromise) {
currentWindowPromise = new Promise<Window>((resolve, reject) => {
new Window(null, resolve, reject);
});
}

currentWindow = new Window(null, callback, errorCallback);
return currentWindow;
return currentWindowPromise;
}

static wrap(win: Electron.BrowserWindow) {
Expand Down
22 changes: 13 additions & 9 deletions packages/api-openfin/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MessageService } from './message-service';
import { createMainProcess } from './main-process';
import { Screen } from './screen';

let currentWindow: Window = null;
let currentWindowPromise: Promise<Window> = null;

const eventMap = {
'auth-requested': 'auth-requested',
Expand Down Expand Up @@ -136,6 +136,7 @@ export class Window extends Emitter implements ssf.Window {
});

if (!options) {
// Wrap existing window
this.innerWindow = fin.desktop.Window.getCurrent();
this.id = `${this.innerWindow.uuid}:${this.innerWindow.name}`;
if (callback) {
Expand All @@ -144,6 +145,7 @@ export class Window extends Emitter implements ssf.Window {
return this;
}

// Open a new window
const optionsCopy = Object.assign({}, options);
const currentPosition: ssf.Position = { x: optionsCopy.x || 0, y: optionsCopy.y || 0 };
Display.getDisplayAlteredPosition(optionsCopy.display, currentPosition).then(({x, y}) => {
Expand All @@ -170,7 +172,9 @@ export class Window extends Emitter implements ssf.Window {
}
}

fin.desktop.Window.getCurrent().getOptions((windowOptions) => {
const currentFinWindow = fin.desktop.Window.getCurrent();

currentFinWindow.getOptions((windowOptions) => {
openFinOptions.preload = windowOptions.preload;
const appOptions = {
name: openFinOptions.name,
Expand All @@ -186,7 +190,7 @@ export class Window extends Emitter implements ssf.Window {

fin.desktop.InterApplicationBus.publish('ssf-new-window', {
windowName: this.innerWindow.uuid,
parentName: options.child ? Window.getCurrentWindow().innerWindow.uuid : null
parentName: options.child ? currentFinWindow.uuid : null
});

if (callback) {
Expand Down Expand Up @@ -442,13 +446,13 @@ export class Window extends Emitter implements ssf.Window {
MessageService.send(`${this.innerWindow.uuid}:${this.innerWindow.name}`, 'ssf-window-message', message);
}

static getCurrentWindow(callback?: (win: Window) => void, errorCallback?: (err?: any) => void) {
if (currentWindow) {
return currentWindow;
static getCurrentWindow(): Promise<Window> {
if (!currentWindowPromise) {
currentWindowPromise = new Promise<Window>((resolve, reject) => {
new Window(null, resolve, reject);
});
}

currentWindow = new Window(null, callback, errorCallback);
return currentWindow;
return currentWindowPromise;
}

static wrap(win: fin.OpenFinWindow) {
Expand Down
8 changes: 5 additions & 3 deletions packages/api-specification/interface/event-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ declare namespace ssf {
* such as <a href="#Window">Window</a>.</i>
*
* <pre>
* ssf.Window.getCurrentWindow().on('focus', () => {
* console.log('Window received focus');
* });
* ssf.Window.getCurrentWindow().then(win => {
* win.on('focus', () => {
* console.log('Window received focus');
* });
* })
* </pre>
*/
abstract class EventEmitter {
Expand Down
Loading