UNPKG

flipper-plugin

Version:

Flipper Desktop plugin SDK and components

41 lines 1.8 kB
"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.usePlugin = exports.usePluginInstanceMaybe = exports.usePluginInstance = exports.SandyPluginContext = void 0; const react_1 = require("react"); exports.SandyPluginContext = (0, react_1.createContext)(undefined); function usePluginInstance() { const pluginInstance = (0, react_1.useContext)(exports.SandyPluginContext); if (!pluginInstance) { throw new Error('Sandy Plugin context not available'); } return pluginInstance; } exports.usePluginInstance = usePluginInstance; function usePluginInstanceMaybe() { return (0, react_1.useContext)(exports.SandyPluginContext); } exports.usePluginInstanceMaybe = usePluginInstanceMaybe; function usePlugin(plugin) { const pluginInstance = usePluginInstance(); // In principle we don't *need* the plugin, but having it passed it makes sure the // return of this function is strongly typed, without the user needing to create it's own // context. // But since we pass it, let's make sure the user did request the proper context const pluginFromDefinition = pluginInstance.definition.isDevicePlugin ? pluginInstance.definition.asDevicePluginModule().devicePlugin : pluginInstance.definition.asPluginModule().plugin; if (pluginFromDefinition !== plugin) { throw new Error(`Plugin in context (${pluginFromDefinition}) didn't match the type of the requested plugin (${plugin})`); } return pluginInstance.instanceApi; } exports.usePlugin = usePlugin; //# sourceMappingURL=PluginContext.js.map