Tarant

website·Indexed 29 days ago

https://tarant.js.org/

A TypeScript/JavaScript library for building software using the actor system model. It provides a framework for implementing the Actor Model, featuring actor systems, supervisors, fibers, mailboxes, and materializers. The ecosystem includes specialized modules such as Tarant Vue for framework rendering, Tarant Local Storage and Tarant DB Persist for state persistence, and Tarant Sync Client and Tarant Sync Router Express for backend synchronization.

Tokens
8.4K
Snippets
46
Records
66
Agent score
47%

What's inside Tarant

  1. Overview of the Tarant Actor System

    Tarant is a TypeScript/JavaScript library for building software using the actor system model. It provides a framework where actors serve as the primary unit of state and logic. Key characteristics include:

    • Transactional State: Actors are transactional, eliminating the need for manual state rollbacks during errors.
    • Non-blocking Performance: Communication is asynchronous by default, ensuring slow actors do not block fast ones.
    • Extensibility: Actor classes support inheritance, composition, and injection. The system allows for custom supervisors and materializers to implement features like implicit persistence.
    • Event Bus: The Actor System includes a built-in event bus for type-safe publishing, subscribing, and requesting messages via topics.
    • Debuggability: Messages include sender information, and state is tracked in a 'time machine' for navigation and debugging.
  2. Overview of the Tarant Actor System

    Tarant is a TypeScript/JavaScript library for building software using the actor system model. In this model, an actor serves as the unit of state and logic. Key benefits include:

    • Transactional State: Actors are transactional, removing the need to manually handle state rollbacks during errors.
    • Non-blocking Performance: Communication is asynchronous by default, ensuring that slow actors do not block fast actors.
  3. Overview of the Tarant Actor Model library

    Tarant is a lightweight (10KB minified) JavaScript library used for building software based on the Actor Model. It is implemented in TypeScript and relies on standard ES6 features like classes and Promises, making it compatible with both frontend and backend environments. Key characteristics include:

    • Asynchronous & Non-blocking: Actors handle messages in parallel without blocking other actors.
    • Extensible: Supports custom materializers to add persistence or rendering capabilities.
    • Type-safe: Provides full TypeScript typings for better editor suggestions and code safety.
  4. Understand the Actor Supervision Hierarchy

    Tarant.js uses a hierarchical supervision model to handle errors:

    1. Parent Actors: Every actor acts as a supervisor for its child actors. If a child fails, the parent decides the recovery strategy.
    2. Delegation: If a parent actor does not define a supervision strategy, the error is delegated up the chain to the grandparent, and so on.
    3. Root Actors: If a root actor (created directly by the Actor System) fails and has no defined strategy, the error is delegated to the Top Level Supervisor.
    4. Top Level Supervisor: An object implementing the IActorSupervisor interface. The Actor System delegates all unhandled errors to this global supervisor.
  5. Understand Tarant.js Mailbox properties

    Mailboxes in Tarant.js function as message queues with guaranteed ordering. They possess three primary characteristics:

    1. Partitioned: Messages are distributed across partitions, allowing subscribers to read from different partitions concurrently.
    2. Dynamic: Subscribers can subscribe and unsubscribe from a mailbox multiple times.
    3. Ordered/Only-Once Delivery: The mailbox guarantees that a message is delivered only once, regardless of whether the actor processes it multiple times due to recovery strategies.
  6. Implement PubSub patterns using Tarant Topics

    Tarant provides a pubsub mechanism called Topics. A Topic is a stream of events where every message sent to the topic is delivered to all current subscribers. Messages are guaranteed to be processed in order and only once by the topic itself. Because a Topic is also an Actor, it inherits all Actor properties.
  7. Understand Fibers in Tarant.js

    Fibers in Tarant.js act as green threads that manage a set of resources. They provide CPU ticks to processors, which are then used to process actor mailboxes. While most applications typically use a single fiber due to JavaScript's single-threaded event loop, multiple fibers can be utilized when working with webworkers.
  8. Understand the Actor System architecture in Tarant.js

    The Actor System is responsible for tracking the location of all actors within an application, regardless of whether they are currently in memory. It is composed of several core architectural components: Materializers, Resolvers, a Top Level Supervisor, Fibers, and Mailboxes.
  9. Schedule dynamic message execution with Tarant actors

    Tarant actors support the dynamic scheduling of message execution. These scheduled operations are handled by actors, are non-blocking, and support asynchronous methods.
  10. Define a custom Actor in Tarant

    Actors in Tarant are thread-safe, asynchronous objects. To create a custom Actor, define a class that extends the Actor base class. Methods defined within this class can be invoked asynchronously via the Actor System.
    const { Actor } = require('tarant');
    
    class Pinger extends Actor {
        ping() {
            console.log('Ping!');
        }
    }
  11. Integrate Tarant Actors with Vue

    The tarant-vue module allows actors to be rendered using the Vue framework. To use it, extend the VueActor class and provide the HTML component ID to the constructor.
    npm install tarant-vue --save
    # or
    yarn add tarant-vue
    import { VueActor } from "tarant-vue";
    
    export default class AppActor extends VueActor {
      constructor() {
        super("#app");
        // actor logic here
      }
    }
  12. Define and instantiate an Actor in tarant.js

    Actors are the primary unit of logic in the Actor Model, providing guarantees that a single actor processes one message at a time, processes messages in order, isolates failures, and does not block other actors. To create an actor, define a class that extends Actor. The methods defined in this class represent the 'protocol' (the set of messages the actor can handle).

    To instantiate an actor, use the actorOf method of a running ActorSystem.

    const { Actor, ActorSystem } = require('tarant')
    
    // Define the Actor protocol
    class Person extends Actor {
      sayHi(whom) {
        console.log('Hi', whom)
      }
    
      sayBye(whom) {
        console.log('Bye', whom)
      }
    }
    
    // Initialize the actor system
    const system = ActorSystem.default()
    
    // Instantiate the actor
    const dante = system.actorOf(Person)
    
    // Call a method on the actor
    dante.sayHi('Martin')