flipper-plugin
Version:
Flipper Desktop plugin SDK and components
159 lines • 5.87 kB
JavaScript
"use strict";
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SandyPluginInstance = void 0;
const PluginBase_1 = require("./PluginBase");
const batch_1 = require("../state/batch");
const atom_1 = require("../state/atom");
class SandyPluginInstance extends PluginBase_1.BasePluginInstance {
static is(thing) {
return thing instanceof SandyPluginInstance;
}
constructor(serverAddOnControls, flipperLib, definition, realClient, pluginKey, initialStates) {
super(serverAddOnControls, flipperLib, definition, realClient.device, pluginKey, initialStates);
/** connection alive? */
this.connected = (0, atom_1.createState)(false);
this.realClient = realClient;
this.definition = definition;
const self = this;
this.client = {
...this.createBasePluginClient(),
isActivated() {
return self.activated;
},
get appId() {
return realClient.id;
},
get bundleId() {
return realClient.query.app_id ?? 'unknown';
},
get appName() {
return realClient.query.app;
},
connected: self.connected,
get isConnected() {
return self.connected.get();
},
onConnect: (cb) => {
const cbWrapped = (0, batch_1.batched)(cb);
this.events.on('connect', cbWrapped);
return () => {
this.events.off('connect', cbWrapped);
};
},
onDisconnect: (cb) => {
const cbWrapped = (0, batch_1.batched)(cb);
this.events.on('disconnect', cbWrapped);
return () => {
this.events.off('disconnect', cbWrapped);
};
},
send: async (method, params) => {
this.assertConnected();
return await realClient.call(this.definition.id, method, true, params);
},
onMessage: (event, cb) => {
const cbWrapped = (0, batch_1.batched)(cb);
const eventName = `event-${event.toString()}`;
this.events.on(eventName, cbWrapped);
return () => {
this.events.off(eventName, cbWrapped);
};
},
onUnhandledMessage: (cb) => {
const cbWrapped = (0, batch_1.batched)(cb);
this.events.on('unhandled-event', cbWrapped);
return () => {
this.events.off('unhandled-event', cbWrapped);
};
},
supportsMethod: async (method) => {
return await realClient.supportsMethod(this.definition.id, method);
},
selectPlugin(pluginId, deeplink) {
flipperLib.selectPlugin(realClient.device, realClient, pluginId, deeplink);
},
};
this.initializePlugin(() => definition.asPluginModule().plugin(this.client));
}
// the plugin is selected in the UI
activate() {
super.activate();
const pluginId = this.definition.id;
if (!this.connected.get() &&
!this.realClient.isBackgroundPlugin(pluginId)) {
this.realClient.initPlugin(pluginId); // will call connect() if needed
}
}
// the plugin is deselected in the UI
deactivate() {
super.deactivate();
const pluginId = this.definition.id;
if (this.connected.get() && !this.realClient.isBackgroundPlugin(pluginId)) {
this.realClient.deinitPlugin(pluginId);
}
}
connect() {
this.assertNotDestroyed();
if (!this.connected.get()) {
this.startServerAddOn();
this.connected.set(true);
this.events.emit('connect');
}
}
disconnect() {
this.assertNotDestroyed();
if (this.connected.get()) {
this.stopServerAddOn();
this.connected.set(false);
this.events.emit('disconnect');
}
}
destroy() {
if (this.connected.get()) {
this.realClient.deinitPlugin(this.definition.id);
}
super.destroy();
}
receiveMessages(messages) {
messages.forEach((message) => {
if (this.events.listenerCount(`event-${message.method}`) > 0) {
this.events.emit(`event-${message.method}`, message.params);
}
else {
this.events.emit('unhandled-event', message.method, message.params);
}
});
}
toJSON() {
return '[SandyPluginInstance]';
}
get serverAddOnOwner() {
return this.realClient.id;
}
assertConnected() {
this.assertNotDestroyed();
// This is a better-safe-than-sorry; just the first condition should suffice
if (!this.connected.get()) {
throw new Error('SandyPluginInstance.assertConnected -> plugin is not connected');
}
if (!this.realClient.connected.get()) {
throw new Error('SandyPluginInstance.assertConnected -> realClient is not connected');
}
if (!this.device.isConnected) {
throw new Error('SandyPluginInstance.assertConnected -> device is not connected');
}
if (this.device.isArchived) {
throw new Error('SandyPluginInstance.assertConnected -> device is archived');
}
}
}
exports.SandyPluginInstance = SandyPluginInstance;
//# sourceMappingURL=Plugin.js.map