UNPKG

@jagodin/turbo-stream

Version:

A streaming data transport format that aims to support built-in features such as Promises, Dates, RegExps, Maps, Sets and more.

176 lines (175 loc) 5.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.globalRegistry = exports.getGlobalDecodePluginCount = exports.getGlobalEncodePluginCount = exports.unregisterDecodePlugin = exports.unregisterEncodePlugin = exports.clearGlobalPlugins = exports.getGlobalDecodePlugins = exports.getGlobalEncodePlugins = exports.registerPlugin = exports.registerDecodePlugin = exports.registerEncodePlugin = void 0; /** * Global plugin registry for turbo-stream * Allows registering plugins that will be automatically used by all encode/decode calls */ class PluginRegistry { encodePlugins = []; decodePlugins = []; /** * Register an encode plugin that will be used by all encode calls * @param plugin The encode plugin to register */ registerEncodePlugin(plugin) { this.encodePlugins.push(plugin); } /** * Register a decode plugin that will be used by all decode calls * @param plugin The decode plugin to register */ registerDecodePlugin(plugin) { this.decodePlugins.push(plugin); } /** * Register both encode and decode plugins for a complete serialization solution * @param encodePlugin The encode plugin to register * @param decodePlugin The decode plugin to register */ registerPlugin(encodePlugin, decodePlugin) { this.registerEncodePlugin(encodePlugin); this.registerDecodePlugin(decodePlugin); } /** * Get all registered encode plugins * @returns Array of registered encode plugins */ getEncodePlugins() { return this.encodePlugins; } /** * Get all registered decode plugins * @returns Array of registered decode plugins */ getDecodePlugins() { return this.decodePlugins; } /** * Clear all registered plugins */ clear() { this.encodePlugins = []; this.decodePlugins = []; } /** * Remove a specific encode plugin * @param plugin The plugin to remove */ unregisterEncodePlugin(plugin) { const index = this.encodePlugins.indexOf(plugin); if (index !== -1) { this.encodePlugins.splice(index, 1); } } /** * Remove a specific decode plugin * @param plugin The plugin to remove */ unregisterDecodePlugin(plugin) { const index = this.decodePlugins.indexOf(plugin); if (index !== -1) { this.decodePlugins.splice(index, 1); } } /** * Get the number of registered encode plugins */ get encodePluginCount() { return this.encodePlugins.length; } /** * Get the number of registered decode plugins */ get decodePluginCount() { return this.decodePlugins.length; } } // Create a singleton instance const globalRegistry = new PluginRegistry(); exports.globalRegistry = globalRegistry; /** * Register an encode plugin globally * @param plugin The encode plugin to register */ function registerEncodePlugin(plugin) { globalRegistry.registerEncodePlugin(plugin); } exports.registerEncodePlugin = registerEncodePlugin; /** * Register a decode plugin globally * @param plugin The decode plugin to register */ function registerDecodePlugin(plugin) { globalRegistry.registerDecodePlugin(plugin); } exports.registerDecodePlugin = registerDecodePlugin; /** * Register both encode and decode plugins globally * @param encodePlugin The encode plugin to register * @param decodePlugin The decode plugin to register */ function registerPlugin(encodePlugin, decodePlugin) { globalRegistry.registerPlugin(encodePlugin, decodePlugin); } exports.registerPlugin = registerPlugin; /** * Get all globally registered encode plugins * @returns Array of registered encode plugins */ function getGlobalEncodePlugins() { return globalRegistry.getEncodePlugins(); } exports.getGlobalEncodePlugins = getGlobalEncodePlugins; /** * Get all globally registered decode plugins * @returns Array of registered decode plugins */ function getGlobalDecodePlugins() { return globalRegistry.getDecodePlugins(); } exports.getGlobalDecodePlugins = getGlobalDecodePlugins; /** * Clear all globally registered plugins */ function clearGlobalPlugins() { globalRegistry.clear(); } exports.clearGlobalPlugins = clearGlobalPlugins; /** * Remove a specific encode plugin from global registry * @param plugin The plugin to remove */ function unregisterEncodePlugin(plugin) { globalRegistry.unregisterEncodePlugin(plugin); } exports.unregisterEncodePlugin = unregisterEncodePlugin; /** * Remove a specific decode plugin from global registry * @param plugin The plugin to remove */ function unregisterDecodePlugin(plugin) { globalRegistry.unregisterDecodePlugin(plugin); } exports.unregisterDecodePlugin = unregisterDecodePlugin; /** * Get the number of globally registered encode plugins */ function getGlobalEncodePluginCount() { return globalRegistry.encodePluginCount; } exports.getGlobalEncodePluginCount = getGlobalEncodePluginCount; /** * Get the number of globally registered decode plugins */ function getGlobalDecodePluginCount() { return globalRegistry.decodePluginCount; } exports.getGlobalDecodePluginCount = getGlobalDecodePluginCount; // Expose the registry globally for access by encode/decode functions if (typeof globalThis !== "undefined") { globalThis.__turboStreamPluginRegistry = { getGlobalEncodePlugins, getGlobalDecodePlugins, }; }