0.1.2 • Published 2 years ago

@synonymdev/slashtags-core v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Slashtags-core

Plugin framework for Slashtags nodes.

Slashtags core provides a plugins architecture framework, to build the core protocol features as well as extending clients in app-land in a highly modular manner.

Features

  • Asynchronous loading of plugins similar to avvio/fastify.
  • Decorate the client with values and methods from plugins.
  • Emit events from anywhere and await all async listeners.
  • Life cycle hooks.

Install

npm install @synonymdev/slashtags-loader

Example

import slashtags from '@synonymdev/slashtags-core';

const logs = [];
const logger = { ...console, log: (...args) => logs.push(...args) };

const slash = await slashtags({ logger })
  .use(first, { foo: 1 })
  .use(third, { foo: 3 })
  .ready();

// logs = ['first loaded', 'second loaded', 'third loaded']
// slash.first = 1
// slash.second = 2
// slash.third = 3

async function first(slash, options) {
  slash.logger.log('first loaded');
  slash.use(second, { foo: 2 });
  slash.decorate('first', options.foo);
  slash.onReady(afterReady.bind(slash));
  slash.onClose(afterClose.bind(slash));
}

async function second(slash, options) {
  slash.logger.log('second loaded');
  slash.decorate('second', options.foo);
}

async function third(slash, options) {
  slash.logger.log('third loaded');
  slash.decorate('third', options.foo);
}

async function afterReady(slash) {
  return new Promise((resolve) => {
    setTimeout(() => {
      this.logger.log('after all');
      resolve();
    }, 10);
  });
}

async function afterClose(slash) {
  return new Promise((resolve) => {
    setTimeout(() => {
      this.logger.log('after close');
      resolve();
    }, 10);
  });
}

API

  • slashtags()
  • slash.use()
  • slash.ready()
  • slash.decorate()
  • slash.emit()
  • slash.onReady()
  • slash.onClose()
  • slash.status

slash = slashtags(options)

Options:

slash.use(plugin, options)

Returns a thenable instance of slashtags, so it can be awaited, or chained with .ready() or .use() for other plugins.

  • plugin: The plugin to load.
  • options: Options to pass to the plugin.

slash.ready()

Returns a promise of the instance, after omitting onReady and executing all async listeners for that event.

Once ready() is called, .use() and .decorate() can't be called.

slash.decorate(name, value)

Adds a value to the instance, and returns the instance.

Unlike fastify there is no encapsulation by default, so all decorators are going to be on the root instance, .decorate() it will throw an error if the property already exists.

slash.emit(type, ...args)

Same as EventEmitter.emit(), but returns a promise that resolves after awaiting all listeners for that event.

slash.onReady(cb)

Same as EventEmitter.on('onReady', cb).

slash.onClose(cb)

Same as EventEmitter.on('onClose', cb).

slash.close()

Emits onClose and returns a promise that resolves after awaiting all listeners for that event.

License

Licensed under MIT.

0.1.2

2 years ago

0.1.0-alpha.8

2 years ago

0.2.0-alpha.0

2 years ago

0.1.1

2 years ago

0.1.0-alpha.6

2 years ago

0.1.0-alpha.5

2 years ago

0.1.0-alpha.3

2 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago