refactoring and fixes

Summary: Slightly refactored JS api to make it work with new js app launcher

Reviewed By: jknoxville

Differential Revision: D18761757

fbshipit-source-id: edb8e5907765a9354e4c636be97d3cf6df63ee98
This commit is contained in:
Timur Valiev
2019-12-02 04:57:19 -08:00
committed by Facebook Github Bot
parent beff2c4cac
commit fe4af064cf
4 changed files with 44 additions and 48 deletions

View File

@@ -14,7 +14,7 @@ export type FlipperMethodID = string;
export class FlipperBridge { export class FlipperBridge {
registerPlugins: (plugins: Array<FlipperPluginID>) => void; registerPlugins: (plugins: Array<FlipperPluginID>) => void;
start: () => void; start: (appName: string) => void;
stop: () => void; stop: () => void;
@@ -104,9 +104,9 @@ export class FlipperClient {
return this.plugins.get(id); return this.plugins.get(id);
} }
start() { start(appName: string) {
this._bridge.registerPlugins([...this.plugins.keys()]); this._bridge.registerPlugins([...this.plugins.keys()]);
this._bridge.start(); this._bridge.start(appName);
} }
stop() { stop() {

View File

@@ -23,7 +23,7 @@ class FlipperManager {
this.furyPlugin = new FuryPlugin(); this.furyPlugin = new FuryPlugin();
this.flipperClient.addPlugin(this.analyticsPlugin); this.flipperClient.addPlugin(this.analyticsPlugin);
this.flipperClient.addPlugin(this.furyPlugin); this.flipperClient.addPlugin(this.furyPlugin);
this.flipperClient.start(); this.flipperClient.start('Example JS App');
} }
} }

View File

@@ -19,8 +19,8 @@ class FlipperWebviewBridgeImpl extends FlipperBridge {
window.FlipperWebviewBridge.registerPlugins(plugins); window.FlipperWebviewBridge.registerPlugins(plugins);
}; };
start = () => { start = (appName: string) => {
window.FlipperWebviewBridge && window.FlipperWebviewBridge.start(); window.FlipperWebviewBridge && window.FlipperWebviewBridge.start(appName);
}; };
stop = () => { stop = () => {
@@ -42,22 +42,22 @@ class FlipperWebviewBridgeImpl extends FlipperBridge {
handler: any => void, handler: any => void,
) => { ) => {
this._subscriptions.set(plugin + method, handler); this._subscriptions.set(plugin + method, handler);
window.FlipperWebviewBridge &&
window.FlipperWebviewBridge.subscribe(plugin, method);
}; };
isAvailable = () => { isAvailable = () => {
return window.FlipperWebviewBridge != null; return window.FlipperWebviewBridge != null;
}; };
handleMessage(plugin: FlipperPluginID, method: FlipperMethodID, data: any) { receive(plugin: FlipperPluginID, method: FlipperMethodID, data: string) {
const handler: ?(any) => void = this._subscriptions.get(plugin + method); const handler = this._subscriptions.get(plugin + method);
handler && handler(data); handler && handler(JSON.parse(data));
} }
} }
export function newWebviewClient(): FlipperClient { export function newWebviewClient(): FlipperClient {
const bridge = new FlipperWebviewBridgeImpl(); const bridge = new FlipperWebviewBridgeImpl();
window.FlipperBridgeClientSide = bridge; window.flipper = {
FlipperWebviewMessageReceiver: bridge,
};
return new FlipperClient(bridge); return new FlipperClient(bridge);
} }

View File

@@ -12,60 +12,56 @@
// ============== // ==============
const {remote, ipcRenderer} = require('electron'); const {remote, ipcRenderer} = require('electron');
let FlipperMainWindowId = 0; const flipperState = {
mainWindowId: 0,
isClientInit: false,
plugins: null,
appName: 'JS App',
};
ipcRenderer.on('parent-window-id', (event, message) => { ipcRenderer.on('parent-window-id', (event, message) => {
FlipperMainWindowId = message; flipperState.mainWindowId = message;
}); });
let FlipperIsClientInit = false; function initClient(plugins, appName) {
let FlipperMemoizedPlugins; if (flipperState.isClientInit) {
function initClient(plugins) {
if (FlipperIsClientInit) {
return; return;
} }
if (plugins) { if (plugins) {
FlipperMemoizedPlugins = plugins; flipperState.plugins = plugins;
} }
if (FlipperMainWindowId != 0) { if (appName) {
ipcRenderer.sendTo(FlipperMainWindowId, 'from-js-emulator-init-client', { flipperState.appName = appName;
}
if (flipperState.mainWindowId != 0) {
ipcRenderer.sendTo(
flipperState.mainWindowId,
'from-js-emulator-init-client',
{
command: 'initClient', command: 'initClient',
windowId: remote.getCurrentWebContents().id, windowId: remote.getCurrentWebContents().id,
payload: { payload: {
plugins: plugins ? plugins : FlipperMemoizedPlugins, plugins: flipperState.plugins,
appName: 'kite/weblite', appName: flipperState.appName,
}, },
}); },
FlipperIsClientInit = true; );
flipperState.isClientInit = true;
} }
} }
window.FlipperWebviewBridge = { window.FlipperWebviewBridge = {
registerPlugins: function(plugins) { registerPlugins: function(plugins) {
console.log(plugins); flipperState.plugins = plugins;
if (FlipperMainWindowId != 0) {
ipcRenderer.sendTo(FlipperMainWindowId, 'from-js-emulator', {
command: 'registerPlugins',
payload: plugins,
});
}
}, },
start: function() { start: function(appName) {
console.log('start'); flipperState.appName = appName;
initClient();
if (FlipperMainWindowId != 0) {
ipcRenderer.sendTo(FlipperMainWindowId, 'from-js-emulator', {
command: 'start',
payload: null,
});
}
}, },
sendFlipperObject: function(plugin, method, data) { sendFlipperObject: function(plugin, method, data) {
console.log(plugin, method, data);
initClient(); initClient();
if (FlipperMainWindowId != 0) { if (flipperState.mainWindowId != 0) {
ipcRenderer.sendTo(FlipperMainWindowId, 'from-js-emulator', { ipcRenderer.sendTo(flipperState.mainWindowId, 'from-js-emulator', {
command: 'sendFlipperObject', command: 'sendFlipperObject',
payload: { payload: {
api: plugin, api: plugin,