Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @crikey/stores-base-queue

@crikey/stores-base-queue

Internal peer package used to ensure signals emitted from stores are executed in a reliable predictable manner even as stores and subscriptions are changed.

API

Queue functions

Action running:

Default action runners:

Installation

Note: It is important that this package be installed as a peer dependency to ensure the queue is shared between store implementations

# pnpm
$ pnpm add --save-peer @crikey/stores-base-queue

# npm
$ npm add --save-peer @crikey/stores-base-queue

# yarn
$ yarn add --peer @crikey/stores-base-queue

Usage

Enqueue the provided actions using a FIFO queue. If the queue is empty, the actions will begin being called immediately until the queue is exhausted. Further actions may be added during execution which will be executed once the preceding actions are exhausted.

Example:

From ./packages/stores-base-queue/examples/store-queue.test.ts#8~21


const action_a = () => { console.log('action a') };
const action_b = () => { console.log('action b') };

enqueue_store_signals([
action_a,
action_b
]);

// > action a
// > action b

Example with nesting:

From ./packages/stores-base-queue/examples/store-queue.test.ts#32~54


enqueue_store_signals([
() => { console.log("action 1") },
() => {
console.log("action 2");
enqueue_store_signals([
() => { console.log("action 5") },
() => { console.log("action 6") }
]);
console.log("action 3");
},
() => { console.log("action 4") },
]);

// > action 1
// > action 2
// > action 3
// > action 4
// > action 5
// > action 6

Example Exception Handling:

From ./packages/stores-base-queue/examples/store-queue.test.ts#70~94

    const original_runner = set_store_runner(create_store_runner_log_errors(console.error));
try {
enqueue_store_signals([
() => {
throw new Error('error 1');
},
() => {
throw new Error('error 2');
},
() => {
throw new Error('error 3');
}
]);

console.log('done.');

// > Error: error 1
// > Error: error 2
// > Error: error 3
// > done.
} finally {
set_store_runner(original_runner);
}

Index

Type aliases

Action: () => void

Type declaration

    • (): void
    • Generic action.

      Returns void

Logger: (error: any) => void

Type declaration

    • (error: any): void
    • Callback used to log errors

      Parameters

      • error: any

      Returns void

StoreRunner: (action: Action) => void

Type declaration

    • Wrapper used to call actions and handler any possible errors

      Parameters

      Returns void

Core Functions

  • enqueue_store_signals(actions: Action[]): void
  • Enqueue the provided actions using a FIFO queue. If the queue is empty, the actions will begin being called immediately until the queue is exhausted. Further actions may be added during execution which will be executed once the preceding actions are exhausted.

    Example:

    From ./packages/stores-base-queue/examples/store-queue.test.ts#8~21


    const action_a = () => { console.log('action a') };
    const action_b = () => { console.log('action b') };

    enqueue_store_signals([
    action_a,
    action_b
    ]);

    // > action a
    // > action b

    Example with nesting:

    From ./packages/stores-base-queue/examples/store-queue.test.ts#32~54


    enqueue_store_signals([
    () => { console.log("action 1") },
    () => {
    console.log("action 2");
    enqueue_store_signals([
    () => { console.log("action 5") },
    () => { console.log("action 6") }
    ]);
    console.log("action 3");
    },
    () => { console.log("action 4") },
    ]);

    // > action 1
    // > action 2
    // > action 3
    // > action 4
    // > action 5
    // > action 6

    Parameters

    • actions: Action[]

      array of actions to enqueue

    Returns void

Exception Handling Functions

  • store_runner(action: Action): void
  • The active store runner used while executing actions queued with enqueue_store_signals

    The store runner is primarily used for handling unhandled exceptions thrown by actions.

    Example:

    From ./packages/stores-base-queue/examples/store-queue.test.ts#70~94

        const original_runner = set_store_runner(create_store_runner_log_errors(console.error));
    try {
    enqueue_store_signals([
    () => {
    throw new Error('error 1');
    },
    () => {
    throw new Error('error 2');
    },
    () => {
    throw new Error('error 3');
    }
    ]);

    console.log('done.');

    // > Error: error 1
    // > Error: error 2
    // > Error: error 3
    // > done.
    } finally {
    set_store_runner(original_runner);
    }

    Parameters

    Returns void

Predefined Store Runners Functions

  • create_store_runner_log_errors(logger?: Logger): (action: Action) => void
  • store_runner_hide_errors(action: Action): void
  • store_runner_throw_errors(action: Action): void

Generated using TypeDoc