A Behavior with specialization for a specific cluster.

To implement cluster functionality you should use a subclass provided by ClusterBehavior.for with the ClusterType you are implementing. Most commonly you would use one of the predefined implementations matter.js includes.

Subclasses can be modified using the static builder methods or extended like a normal class.

Behaviors should store all mutable state in their Behavior.state property. Other fields will be transient and may not be retained across invocations.

ClusterBehaviors may be instantiated with unsupported mandatory commands and attributes. This is currently results in a runtime error but it will not cause a type error during development.

Hierarchy (view full)

Constructors

Properties

events: EventEmitter

Access the behavior's events.

state: {}

Access the behavior's state.

cluster: Of<{
    attributes: {};
    commands: {};
    events: {};
    id: 0;
    name: "Unknown";
    revision: 0;
}>

Every cluster behavior has an associated ClusterType defined statically.

dependencies: typeof NetworkBehavior[]

Keep networking alive until I'm destroyed.

early = true

All ClusterBehavior initialization currently runs as part of Endpoint initialization.

id: Uncapitalize<string>

The ID of ClusterBehavior implementations is the uncapitalized cluster name.

Interface: {
    components: never[];
}

Method definitions.

Internal: (new () => {})

Implementation of internal state. Subclasses may override to extend.

Type declaration

    • new (): {}
    • Implementation of internal state. Subclasses may override to extend.

      Returns {}

    schema?: Schema

    A behavior's schema controls access to data, commands and events.

    Schema is inferred from the methods and properties of the behavior but you can specify explicitly for additional control.

    State: (new () => {})

    Implementation of endpoint-scoped state. Subclasses may override to extend.

    Type declaration

      • new (): {}
      • Implementation of endpoint-scoped state. Subclasses may override to extend.

        Returns {}

      Accessors

      • get [reference](): Datasource<StateType>
      • Returns Datasource<StateType>

      • get agent(): Agent
      • The agent that owns the behavior.

        Returns Agent

      • get cluster(): Of<{
            attributes: {};
            commands: {};
            events: {};
            id: 0;
            name: "Unknown";
            revision: 0;
        }>
      • The cluster implemented by this behavior.

        Returns Of<{
            attributes: {};
            commands: {};
            events: {};
            id: 0;
            name: "Unknown";
            revision: 0;
        }>

      • get context(): ActionContext
      • The context in which the behavior operates.

        Returns ActionContext

      • get endpoint(): Endpoint<Empty>
      • The endpoint that owns behavior's agent.

        Returns Endpoint<Empty>

      • get env(): Environment
      • The endpoint's environment.

        Returns Environment

      • get features(): {}
      • Supported features as a flag object.

        Returns {}

        • get session(): SecureSession
        • The session in which the behavior has been invoked.

          Returns SecureSession

        • get defaults(): Record<string, any>
        • Default state values.

          Returns Record<string, any>

        • get supervisor(): RootSupervisor
        • The data supervisor for the behavior. The supervisor controls validation and access to behavior data.

          Returns RootSupervisor

        Methods

        • Release resources. This is the public API for releasing application resources held by behaviors in internal state.

          Returns MaybePromise

        • Execute logic with elevated privileges.

          The provided function executes with privileges escalated to offline mode. This is not commonly necessary.

          Elevated logic effectively ignores ACLs so should be used with care.

          Note that interactions with the behavior will remain elevated until the synchronous completion of this call. You should only elevate privileges for synchronous logic.

          Parameters

          • fn: (() => void)

            the elevated logic

              • (): void
              • Returns void

          Returns void

        • Type Parameters

          • This extends Behavior
          • K extends string | number | symbol

          Parameters

          • this: This
          • attributeName: K

          Returns void

        • Create a generic callback function that has the same properties as a Reactor.

          Like a reactor, the callback's "this" will be bound to an active Behavior instance. Because of this: The reactor MUST be a real JS function - arrow functions will not work!

          Type Parameters

          • A extends any[]
          • R

          Parameters

          Returns ((...args: A) => undefined | R)

            • (...args): undefined | R
            • Parameters

              • Rest...args: A

              Returns undefined | R

        • Behaviors are ephemeral and should not perform initialization in their constructor. They can override this method instead.

          This method may be synchronous or asyncronous. If asynchronous, the behavior will not be available for external use until initialization completes.

          Parameters

          • Optional_options: {}

            Returns MaybePromise

          • Install a Reactor.

            Important: The reactor MUST be a real JS function - arrow functions will not work!

            Type Parameters

            Parameters

            Returns void

          • Type Parameters

            • This extends Behavior
            • K extends string | number | symbol

            Parameters

            • this: This
            • attributeName: K

            Returns Exclude<This["state"][K], undefined>

          • Description used in diagnostic messages.

            Returns string

          • Create a new behavior with different default state values.

            Type Parameters

            Parameters

            Returns This

          • Does this behavior support functionality of a specific implementation?

            Parameters

            Returns boolean

          • Create a ClusterBehavior like this one with different interface methods.

            The Interface "property" is type-only. We define a method however to keep the API consistent. At runtime the method is a no-op.

            Type Parameters

            Returns Type<Of<{
                attributes: {};
                commands: {};
                events: {};
                id: 0;
                name: "Unknown";
                revision: 0;
            }>, typeof ClusterBehavior, I>