tarant-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.13 • Public • Published

tarant-utils

Downloads npm npm GitHub Workflow Status All Contributors Maintainability Test Coverage PRs Welcome issues Welcome GitHub issues GitHub pull requests

Motivation

Provide a set of utils that help apply some of the paterns that tarant uses.

Installation

add it to your project using npm install tarant-utils --save or yarn add tarant-utils

Usage

Decorator

We recomend to use the decorator pattern to keaping separation of concerns in between the business logic of an actor and the logic that some of the resolvers and materializers require.

For example having the next actor

class AppActor extends Actor {

  constructor(name: string) {
      super(name)
  }

  public addOne() : void {
      this.counter++
  }

   public counter = 1; 
}

the definition of the serialization information could be done like

class SerializationDecorator extends decorator<AppActor> {
    constructor(actor: AppActor) {
        super(actor)
    }

    toJson() {
        return {
            id: this.actor.id,
            type: "AppActor",
            counter: this.actor.counter
        }
    }

    updateFrom({ counter }: any): void {
        this.actor.counter = counter
    }
}

and the mix the actor and the decorator for it to be resolved in the actor system that is registered

const DecoratedActor = decorate(AppActor, SerializationDecorator)
export default DecoratedActor

Dependents (0)

Package Sidebar

Install

npm i tarant-utils

Weekly Downloads

1

Version

1.0.13

License

MIT

Unpacked Size

9.61 kB

Total Files

7

Last publish

Collaborators

  • kanekotic