System

System

Class that implements a Pub/Sub Actor Message System Bus that works in all modern browsers and Node.js. It is a lightweight interface on top of EventTarget or EventEmitter for browsers and Node.js respectively. The API follows the far more succinct Node.js method names using on to bind Actor callbacks to schema types and emit to send models to any Actors waiting for those events to occur.

Constructor

new System()

Example

An example primary Actor using an Actor System to send logs to a supporting Actor for post processing.

import { parse, Actor, Model, Schema, System } from 'hive-io'

import ExampleSchema from '../schemas/ExampleSchema.json'

import LogActor from '../actors'
import LogSchema from '../schemas/LogSchema.json'
const logSystem = new System()

const LOG_SCHEMA = Symbol('JSON Schema for generating logs')

class ExampleActor extends Actor {
  constructor(url, schema, logSchema, repository) {
    super(url, schema, repository)
    this[LOG_SCHEMA] = logSchema
  }
  async perform (payload) {
    // Do something interesting with the payloads this Actor receives here

    // fire and forget log
    const log = await new Model({ ... }, this[LOG_SCHEMA])
    logSystem.emit(log)

    // return to process the next request
  }
}
// Proxy<ExampleActor>
export default new Proxy(ExampleActor, {
  construct: async function (ExampleActor, argsList) {
    const example = await new Schema(ExampleSchema)
    const log = await new Schema(LogSchema)

    const logActor = await new LogActor()
    logSystem.on(log.title, logActor)

    return new ExampleActor(parse`/example/${'exampleId'}`, example, log, argsList[0])
  }
})

Methods

emit(model)

Method that emits a model instance to all Actors listening to the model's type.
Parameters:
Name Type Description
model Model An instance of the model to be sent to all Actors listening.

on(schema, actor, optionsopt) → {this}

Method that binds an Actor's perform method to the callback of an event associated with the provided JSON schema title.
Parameters:
Name Type Attributes Description
schema Schema An instance of the JSON Schema (or it's raw JSON definition) associated with the the provided Actor.
actor Actor An instance of an Actor assocated with the provided JSON Schema.
options Object <optional>
An optional Object literal containing the browser's EventTarget options associated with the event listener.
Returns:
Type:
this
A reference to the System instance so that additional event listeners can be chained.