meshcore.js
Plugins

Configuring and runtime

configure(), registration, load, unload and reload, and how failures are reported.

Configuring

PluginBuilder<Options> is exported unconfigured. configure(options) returns a copy carrying the options, so the original export stays reusable. Nothing runs when configure() is called. The factory sees the options at load time.

weather-bot shares one WeatherService between the plugin and the rest of the bot by passing it through the options.

weather.ts
import { config } from './config.js';
import { WeatherService } from './weather-plugin/index.js';

export const weather = new WeatherService({ location: config.location, timezone: config.timezone });
plugins/weather.ts
import { weather } from '../weather.js';
import weatherPlugin from '../weather-plugin/index.js';

export default weatherPlugin.configure({ weather });

Runtime

main.ts
import { Client, SerialTransport } from '@meshcorejs/client';

const client = new Client({ transport: new SerialTransport({ path: '/dev/ttyACM0' }) });

client.on('pluginLoad', (plugin) => console.log(`${plugin.name} loaded (${plugin.bricks.length} bricks)`));
client.on('pluginUnload', (plugin) => console.log(`${plugin.name} unloaded`));
client.on('error', (error, source) => {
  if (source.type === 'plugin') console.error(`plugin "${source.name}" failed:`, error);
});

await client.login();

const weather = client.plugins.get('weather');
console.log(weather?.state); // 'pending' | 'loaded' | 'unloaded'
await weather?.unload();
await weather?.load();
await weather?.reload();

Plugins found under plugins/ are pending until login() loads them, before connecting and emitting ready. After ready, a file added to a watched folder or a client.register() call loads a new plugin at once. A failure is reported through error with source.type set to plugin and the plugin stays unloaded.

reload() rebuilds the bricks. On failure it puts the previous ones back, so the plugin keeps running with its old bricks. From a DM, /plugins load|unload|reload <name> does the same.

Reference

On this page