Actor

Actor

Class that implements a basic Actor instance in the Actor Model pattern. It is essentially a combination of a Controller and Model class from the MVC pattern. It takes 2 optional parameters: an instance of the associated Model's JSON Schema definition and a reference to a downstream repository.

Primary use case(s) are:

  • local, atomic domain logic in the perform method
  • pass messages to other actors via internal (System) or external (Kafka) message buses
  • straight forward routing via switch/if conditions for microservices

NOTE: If no parameters are defined, be careful not to call inherited methods as they expect the Schema to be defined.

Constructor

new Actor(modelSchemaopt, repositoryopt)

Properties:
Name Type Description
repository any A reference to a storage layer client of your choosing or undefined.
Parameters:
Name Type Attributes Description
modelSchema Schema <optional>
The instance of the associated Model's JSON Schema definition.
repository any <optional>
An optional reference to a storage layer client of your choosing.
Example

An example Actor class. It is meant to be wrapped with one of the microservice types (Base, Producer, Consumer, Stream Processor) or to include a connection to the DB of your choice.

import { Actor, Schema } from 'hive-io'
import ExampleSchema from '../schemas/ExampleSchema.json'

class ExampleActor extends Actor {
  async perform (model, action) {
    // Do something interesting with the data
  }
}
// Proxy<ExampleActor>
export default new Proxy(ExampleActor, {
  construct: async function (ExampleActor, argsList) {
    const example = await new Schema(ExampleSchema)
    return new ExampleActor(example, argsList[0])
  }
})

Methods

assign(model, payloadopt) → {Object}

Method used to generate the materialized view of a specified model from specified payload. Arrays on the model are completely replaced by the provided payload currently.
Parameters:
Name Type Attributes Default Description
model Object The instance of a model to receive new data.
payload Object <optional>
{} The payload to apply to the model.
Returns:
Type:
Object
The updated instance of the model.

(async) perform(model, actionopt) → {Object}

Method that carries out the actions specified in the action to the specified model.
Parameters:
Name Type Attributes Description
model Object An instance of the model associated with the received data generated from replay.
action Object <optional>
An optional FSA Object literal containing the Model's type and optional meta and payload.
Returns:
Type:
Object
An Object literal containing the latest materialized view of the model.

(async) replay(aggregate) → {Object}

Method that carries out the historical events or model snapshot specified in the aggregate.
Parameters:
Name Type Description
aggregate Object | Array.<Object> The historical events or model snapshot specified.
Returns:
Type:
Object
An Object literal containing the latest materialized view of the model.