blac

    Plugins

    Plugins

    A plugin observes all bloc instances from the outside — the right tool for cross-cutting concerns like logging, inspection, and persistence.

    A plugin is a single object that taps into the registry’s lifecycle — every instance created, every state change, every disposal flows through it. Where system events let one bloc react to its own lifecycle from the inside, a plugin observes all instances from the outside. That makes plugins the right tool for cross-cutting concerns: logging, inspection, persistence — anything you want to apply uniformly without editing each bloc.

    BlaC has an official set of plugins for these common needs. All plugins implement the BlacPlugin interface and are installed once, at startup, via getPluginManager().install().

    Official plugins

    PackagePurposeTypical environment
    @blac/logging-pluginConsole logging, instance monitoring, rapid lifecycle detectionDevelopment
    @blac/devtools-connectAgent that records instances and serves them to DevTools clientsDevelopment
    @blac/devtools-uiIn-app overlay and the shared DevTools panel UIDevelopment
    @blac/plugin-persistPersist state to IndexedDB with automatic hydrationAll

    Installing a plugin

    import { getPluginManager } from '@blac/core/plugins';
    getPluginManager().install(myPlugin, {
    enabled: true,
    environment: 'development', // 'development' | 'production' | 'test' | 'all'
    });

    The environment option controls when the plugin is active. The check is a runtime comparison against process.env.NODE_ENV (which falls back to 'development') performed inside install(): if the environments don’t match, the plugin is skipped with a log message and never wired up. So environment: 'development' means the plugin does nothing in a production build — but the import still ships unless your bundler tree-shakes it. To keep dev-only plugin code out of the bundle entirely, guard the install() call behind your own import.meta.env.DEV (or equivalent) check.

    Multiple plugins

    Install as many plugins as you need. They all receive the same lifecycle callbacks independently.

    import { getPluginManager } from '@blac/core/plugins';
    import { LoggingPlugin } from '@blac/logging-plugin';
    import { createDevToolsAgent } from '@blac/devtools-connect';
    import { createIndexedDbPersistPlugin } from '@blac/plugin-persist';
    const pm = getPluginManager();
    pm.install(new LoggingPlugin({ level: 'info' }), {
    environment: 'development',
    });
    pm.install(createDevToolsAgent(), { environment: 'development' });
    pm.install(createIndexedDbPersistPlugin());

    Building your own

    See Plugin Authoring for the full BlacPlugin interface, the hook firing model, and the PluginContext API.

    See also