Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "interceptors"

Index

Functions

Const dispatch

Const dispatchNow

Const ensureParamRange

  • Specialization of ensurePred to ensure an event's payload value is within given min / max closed interval. By default, assumes event format like: [event-id, value]. However if value is given, the provided function can be used to extract the value to be validated from any event. If the value is outside the given interval, triggers FX_CANCEL side effect and if err is given, the error interceptor can return any number of other side effects and so be used to dispatch alternative events instead.

    Parameters

    • min: number

      -

    • max: number

      -

    • Optional value: Fn<Event, number>

      event value extractor

    • Optional err: InterceptorFn

      error interceptor

    Returns InterceptorFn

Const ensurePred

  • Higher-order interceptor for validation purposes. Takes a predicate function and an optional interceptor function, which will only be called if the predicate fails for a given event. By default the FX_CANCEL side effect is triggered if the predicate failed, thus ensuring the actual event handler for the failed event will not be executed anymore. However, this can be overridden using the error interceptor's result, which is merged into the result of this interceptor.

    The error interceptor can return any number of other side effects and so be used to dispatch alternative events instead, for example:

    // this interceptor will cause cancellation of current event
    // and trigger an "error" event instead
    ensurePred(
      // a dummy predicate which always fails
      () => false
      // error interceptor fn
      () => ({[FX_DISPATCH_NOW]: ["error", "reason"]})
    )

    Note: For this interceptor to work as expected, it needs to be provided BEFORE the main handler in the interceptor list for a given event, i.e.

    [
       ensurePred((state, e) => false),
       // actual event handler
       (state, e) => console.log("no one never calls me")
    ]

    Parameters

    Returns InterceptorFn

Const ensureStateGreaterThan

Const ensureStateLessThan

  • Specialization of ensurePred to ensure a state value is less than given max at the time when the event is being processed. The optional path fn is used to extract or produce the path for the state value to be validated. If omitted, the event's payload item is interpreted as the value path.

    For example, without a provided path function and for an event of this form: ["event-id", "foo.bar"], the term "foo.bar" would be interpreted as path.

    If the event has this shape: ["event-id", ["foo.bar", 23]], we must provide (e) => e[1][0] as path function to extract "foo.bar" from the event.

    Parameters

    • max: number

      -

    • Optional path: Fn<Event, Path>

      path extractor

    • Optional err: InterceptorFn

      error interceptor

    Returns InterceptorFn

Const ensureStateRange

Const eventPathState

  • eventPathState(state: any, path: Fn<Event, Path> | undefined, e: Event): any

Const forwardSideFx

  • Higher-order interceptor. Returns interceptor which unpacks payload from event and assigns it as is to given side effect ID. Assigns true to side effect if event has no payload.

    Parameters

    • fxID: string

      side effect ID

    Returns InterceptorFn

Const snapshot

  • Higher-order interceptor. Returns interceptor which calls ctx[id].record(), where ctx is the currently active InterceptorContext passed to all event handlers and ctx[id] is assumed to be a {@link @thi.ng/atom#History} instance, passed to EventBus.processQueue. The default ID for the history instance is "history".

    Example usage:

    example
    state = new Atom({});
    history = new History(state);
    bus = new EventBus(state);
    // register event handler
    // each time the `foo` event is triggered, a snapshot of
    // current app state is recorded first
    bus.addHandlers({
     foo: [snapshot(), valueSetter("foo")]
    });
    ...
    // trigger event
    bus.dispatch(["foo", 23]);
    
    // pass history instance via interceptor context to handlers
    bus.processQueue({ history });

    Parameters

    • Default value id: string = "history"

      -

    Returns InterceptorFn

Const trace

  • trace(_: any, e: Event): void

Const valueSetter

  • Higher-order interceptor. Returns new interceptor to set state value at provided path. This allows for dedicated events to set state values more concisely, e.g. given this event definition:

    example
    setFoo: valueSetter("foo.bar")

    ...the setFoo event then can be triggered like so to update the state value at foo.bar:

    example
    bus.dispatch(["setFoo", 23])

    Type parameters

    • T

    Parameters

    • path: Path

      -

    • Optional tx: Fn<T, T>

      -

    Returns InterceptorFn

Const valueUpdater

  • Higher-order interceptor. Returns new interceptor to update state value at provided path with given function. This allows for dedicated events to update state values more concisely, e.g. given this event definition:

    example
    incFoo: valueUpdater("foo.bar", (x, y) => x + y)

    ...the incFoo event then can be triggered like so to update the state value at foo.bar (where 1 is the extra arg provided to the update fn:

    example
    bus.dispatch(["incFoo", 1]) // results in value = value + 1

    Type parameters

    • T

    Parameters

    • path: Path

      -

    • fn: FnO<T, T>

      -

    Returns InterceptorFn

Generated using TypeDoc