Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace functions

Contains utilities to wrap one or more functions.

// esm
import { functions } from '@paychex/core';

// cjs
const { functions } = require('@paychex/core');

// iife
const { functions } = window['@paychex/core'];

// amd
require(['@paychex/core'], function({ functions }) { ... });
define(['@paychex/core'], function({ functions }) { ... });

Index

Type aliases

InvocationData: [any, any[]]

Array containing the invocation context (the this value) and any parameters passed to the invocation.

Functions

  • Queues invocations of a function until the specified signals are ready. Optionally, allows filtering the queued invocations or modifying their arguments or this contexts.

    example
    // pause or resume tracking without losing events

    const ready = signals.manualReset(false);
    const collectors = functions.parallel();
    const buffered = functions.buffer(collectors, [ready]);

    export const tracker = trackers.create(buffered);

    export function add(collector) {
    collectors.add(collector);
    };

    export function pause() {
    ready.reset();
    }

    export function start() {
    ready.set();
    }
    example
    // only run most recent invocation of a function
    // if invoked multiple times while queued

    const signal = signals.manualReset(true);

    function onlyMostRecent(invocations) {
    return invocations.slice(-1);
    }

    async function loadData(arg) {
    try {
    await signal.ready();
    // make data call here
    } finally {
    signal.set();
    }
    }

    export const load = functions.buffer(loadData, [signal], onlyMostRecent);

    // consumer:
    load(...); // runs, queues future calls until finished
    load(...); // will be queued, then dropped in favor of most recent call
    load(...); // will be queued, then run after first load completes

    Parameters

    • fn: Function

      The function whose invocations should be buffered.

    • signals: Signal[]

      The signals to wait on before ending buffering.

    • filter: BufferFilter = identity

      Provides optional manipulation of the buffered invocations. Passed an array of invocations and should return an array of invocations. See the example for details.

    Returns BufferFunction

    A function that will queue invocations while any of the given signals are in a blocked state.

  • invokeIf<T, R>(fn: Invocable<T, R>, predicate: string | number | symbol | object | Function): Invocable<T, R | never>
  • Conditionally invokes the supplied function if the given predicate returns true.

    example
    const collectors = functions.parallel();

    collectors.add(console.log);
    collectors.add(gaCollector(window.ga));

    function isEvent(item) {
    return item.type === 'event';
    }

    const tracker = trackers.create(functions.invokeIf(collectors, isEvent));

    // we could also use lodash iteratee syntax:
    const tracker = trackers.create(functions.invokeIf(collectors, { type: 'event' }));
    const tracker = trackers.create(functions.invokeIf(collectors, ['type', 'event']));

    Type parameters

    • T

      the type of arguments the wrapped function accepts

    • R

      the return type of the wrapped function

    Parameters

    • fn: Invocable<T, R>

      The function to invoke conditionally.

    • predicate: string | number | symbol | object | Function

      A lodash iteratee to act as a predicate function. Iteratee will return true or false depending on whether the passed function should be invoked and will be called with the original set of arguments and context.

    Returns Invocable<T, R | never>

    A function that will invoke fn only if predicate returns true.

  • Invokes the specified functions in parallel, handling any returned Promises correctly.

    example
    const concurrent = functions.parallel(fn1, fn2, fn3);
    const results = await concurrent('abc', 123);
    results.length; // 3
    example
    // combining parallel() and sequence()

    const workflow = functions.parallel(
    step1,
    functions.sequence(step2a, step2b, step2c),
    functions.sequence(step3a, step3b),
    step4,
    );

    workflow.add(functions.sequence(step5a, step5b));

    await workflow('some args');

    Parameters

    • Rest ...functions: Function[]

    Returns GroupedFunction<ParallelFunction>

    A function that will invoke the given functions in parallel, waiting for any returned Promises to settle, and either resolving with the array of settled return values or else rejecting with the first rejection reason or thrown error.

  • Invokes the specified functions in sequence, handling any returned Promises correctly.

    example
    const bus = events.bus();
    const checkDirty = functions.parallel();

    bus.on('navigate', functions.sequence(checkDirty, loadNewRoute));

    export function addDirtyChecker(fn) {
    checkDirty.add(fn);
    return function remove() {
    checkDirty.remove(fn);
    };
    }

    export async function navigate(container, path) {
    await bus.fire('navigate', container, path);
    }
    example
    // combining parallel() and sequence()

    const workflow = functions.parallel(
    step1,
    functions.sequence(step2a, step2b, step2c),
    functions.sequence(step3a, step3b),
    step4,
    );

    workflow.add(functions.sequence(step5a, step5b));

    await workflow('some args');

    Parameters

    • Rest ...functions: Function[]

    Returns GroupedFunction<SequentialFunction>

    A function that will invoke the given functions in sequence, waiting for any returned Promises to settle, and either resolving with the last settled return value or else rejecting with the first rejection reason or thrown error.