Interface Service<TRequest, TNext, TError, TState>

A service is a listener over a bus, which triggers responses in some combination to the requests it recieves. On each request it runs a handler (subject to its concurrency strategy) then triggers events based on that handler's lifecycle. For a service defined with the prefix "time", its event schema would be:

  • time/request - client: requests the time
  • time/cancel - client: cancel the current request for the time
  • time/started - server: time resolution has begun
  • time/next - server: contains the time as a payload
  • time/complete - server: no more times will be sent
  • time/error - server: an error occurred (the listener remains alive due to internal rescueing)
  • time/canceled - server: has canceled the current request for the time
interface Service<TRequest, TNext, TError, TState> {
    CANCEL: ActionCreator<void>;
    CANCELED: ActionCreator<void>;
    COMPLETE: ActionCreator<void>;
    ERROR: ActionCreator<TError>;
    NEXT: ActionCreator<TNext>;
    REQUEST: ActionCreator<TRequest>;
    STARTED: ActionCreator<TRequest>;
    acks: Observable<Action<void | TRequest>>;
    actions: ProcessLifecycleActions<TRequest, TNext, TError>;
    bus: Bus<any>;
    cancelations: Observable<Action<void>>;
    commands: Observable<Action<void | TRequest>>;
    currentError: BehaviorSubject<null | TError>;
    endings: Observable<Action<void | TError>>;
    errors: Observable<TError>;
    events: Observable<Action<void | TRequest | TNext | TError>>;
    finalizers: Observable<Action<void | TError>>;
    isActive: BehaviorSubject<boolean>;
    isHandling: BehaviorSubject<boolean>;
    namespace: string;
    observe: ((cbs) => Subscription);
    onceInactive: (() => Promise<false>);
    onceSettled: (() => Promise<false>);
    responses: Observable<TNext>;
    starts: Observable<Action<TRequest>>;
    state: BehaviorSubject<TState>;
    updates: Observable<Action<void | TRequest | TNext | TError>>;
    addTeardown(teardownFn): void;
    cancelCurrent(): void;
    cancelCurrentAndQueued(): void;
    completeCurrent(final?): void;
    request(req): void;
    reset(): void;
    send(arg, matcher?): Promise<TNext>;
    stop(): Subscription;
    (req): void;
}

Type Parameters

  • TRequest
  • TNext
  • TError
  • TState

Hierarchy (view full)

Properties

CANCEL: ActionCreator<void>

Creates an event which cancels the current invocation.

CANCELED: ActionCreator<void>

Creates an event which indicates an invocation was canceled by a subscriber.

COMPLETE: ActionCreator<void>

Creates an event which indicates an invocation has terminated successfully.

Creates an event which indicates an invocation has terminated with an error.

Creates an event which indicates an invocation has produced data.

Creates an event which invokes the service.

Creates an event which signals an invocation has begun.

acks: Observable<Action<void | TRequest>>

An Observable of just start and canceled events of this service.

The ActionCreator factories this service listens for, and responds with.

bus: Bus<any>

An untyped reference to the bus this service listens and triggers on

cancelations: Observable<Action<void>>

An Observable of just canceled events of this service.

commands: Observable<Action<void | TRequest>>

An Observable of just request, cancel events.

currentError: BehaviorSubject<null | TError>

Contains the last error object, but becomes null at the start of the next handling.

endings: Observable<Action<void | TError>>

An Observable of just the complete and error events of this service.

An Observable of just the error events of this service.

events: Observable<Action<void | TRequest | TNext | TError>>

An Observable of all events of this service.

finalizers: Observable<Action<void | TError>>

An Observable of just the complete, error, and canceled events of this service.

isActive: BehaviorSubject<boolean>

Becomes false the Promise after isHandling becomes false, when no more requests are scheduled to start.

isHandling: BehaviorSubject<boolean>

Indicates whether a handling is in progress. Use .value, or subscribe() for updates.

namespace: string

The namespace given at construction time

observe: ((cbs) => Subscription)

Creates an independent subscription, invoking callbacks on process lifecycle events

Type declaration

onceInactive: (() => Promise<false>)

The next time (including the present) this service is inactive. Represents the completion of an already-running service.

Type declaration

    • (): Promise<false>
    • Returns Promise<false>

onceSettled: (() => Promise<false>)

The next time isActive turns from true to false. Represents the completion of a not-yet-running service.

Type declaration

    • (): Promise<false>
    • Returns Promise<false>

responses: Observable<TNext>

An Observable of just the next events of this service.

An Observable of just started events.

Uses the reducer to aggregate the events that are produced from its handlers, emitting a new state for each action (de-duping is not done). Use .value, or subscribe() for updates.

updates: Observable<Action<void | TRequest | TNext | TError>>

An Observable of just started, next, complete, error , and canceled events.

Methods

  • Adds a function to be called only once when stop() is invoked

    Parameters

    • teardownFn: ((teardown) => void)
        • (teardown): void
        • Adds a finalizer to this subscription, so that finalization will be unsubscribed/called when this subscription is unsubscribed. If this subscription is already #closed, because it has already been unsubscribed, then whatever finalizer is passed to it will automatically be executed (unless the finalizer itself is also a closed subscription).

          Closed Subscriptions cannot be added as finalizers to any subscription. Adding a closed subscription to a any subscription will result in no operation. (A noop).

          Adding a subscription to itself, or adding null or undefined will not perform any operation at all. (A noop).

          Subscription instances that are added to this instance will automatically remove themselves if they are unsubscribed. Functions and Unsubscribable objects that you wish to remove will need to be removed manually with #remove

          Parameters

          • teardown: TeardownLogic

            The finalization logic to add to this subscription.

          Returns void

    Returns void

  • Cancels the current handling by triggering service.actions.cancel. The effect is truly canceled if it was started from an Observable. The canceled event will appear on the bus, and no more next events.

    Returns void

  • Cancels the current handling by triggering service.actions.cancel. In addition, any operations enqueued will not be begun. (Safe to call even if not a queueing service.)

    Returns void

  • Completes the current handling, triggering actions.complete. If a value for final is provided, that is emitted via actions.next first. Useful to end the handling but signal a successful completion to any observers/listeners.

    Parameters

    Returns void

  • Get a promise for the next response or error. Note: works best for a queueing service, otherwise may not be the response/error that was triggered by the request.

    Parameters

    • arg: TRequest
    • Optional matcher: ((req, res) => boolean)
        • (req, res): boolean
        • Parameters

          Returns boolean

    Returns Promise<TNext>

    Argument

    matcher If an immediate mode (mergeMap) service, and you need a promise for a specific result, not just the first one, provide a function that takes a request and a response and returns true if the response belongs to that request.