UNPKG

@empathyco/x-components

Version:
128 lines (125 loc) 5.01 kB
import { filter as filter$1, debounce as debounce$1, throttle as throttle$1, map } from 'rxjs/operators'; import { createTimer, normalizeTime } from './wires-operators.utils.js'; /** * Creates a {@link Wire} that is only executed whenever the condition in the filterFn is true. * * @param wire - The wire to filter. * @param filterFn - A function which must return a boolean and that will be executed every time * the wire is called. * @returns The Wire function filter. * * @public */ function filter(wire, filterFn) { return (observable, store, on) => wire(observable.pipe(filter$1(wirePayload => filterFn({ ...wirePayload, store }))), store, on); } /** * Creates a {@link Wire} that is only executed when the payload is truthy. A truthy value is * whatever is not a {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy | falsy value}. * * @param wire - The wire to avoid executing when the payload is falsy. * @returns The Wire function falsy filter. * * @public */ function filterFalsyPayload(wire) { return filter(wire, ({ eventPayload }) => !!eventPayload); } /** * Creates a {@link Wire} that is only executed when the payload is a * {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy | falsy value}. * * @param wire - The wire to avoid executing when the payload is truthy. * @returns The Wire function truthy filter. * * @public */ function filterTruthyPayload(wire) { return filter(wire, ({ eventPayload }) => !eventPayload); } /** * Creates a {@link Wire} that is only executed if the event is emitted from a {@link XModule} * that is included * in the `whitelist` array passed as parameter. * * @param wire - The wire to filter using the whitelist. * @param whitelist - An array of {@link XModuleName} or null. * @returns The Wire function with whitelisted modules filter. * * @public */ function filterWhitelistedModules(wire, whitelist) { const whitelistSet = new Set(whitelist); return filter(wire, ({ metadata }) => whitelistSet.has(metadata.moduleName)); } /** * Creates a {@link Wire} that is only executed if the event is emitted from a {@link XModule} * that is NOT included * in the `blacklist` array passed as parameter. * * @param wire - The wire to filter using the whitelist. * @param blacklist - An array of {@link XModuleName} or null. * @returns The Wire function with blacklisted modules filter. * * @public */ function filterBlacklistedModules(wire, blacklist) { const blacklistSet = new Set(blacklist); return filter(wire, ({ metadata }) => !blacklistSet.has(metadata.moduleName)); } /** * Creates a debounced {@link Wire}. Being debounced means that it will only be executed after * the time given by `timeInMs` has passed without invoking it. * * @param wire - The wire to debounce. * @param timeInMs - The time in milliseconds to debounce the wire execution or a function to * retrieve it from the store. * @param options - Options to configure this wire with, like an event to force it or cancel it. * @returns The Wire function with a debounced timing. * * @public */ function debounce(wire, timeInMs, options = {}) { return (observable, store, on) => { return wire(observable.pipe(debounce$1(() => createTimer(normalizeTime(timeInMs, store), options, on))), store, on); }; } /** * Creates a throttled {@link Wire}. Being throttled means that it will only be executed once * every couple of milliseconds given by the `timeInMs` parameter. * * @param wire - The wire to throttle. * @param timeInMs - The time in milliseconds to throttle the wire execution or a function to * retrieve it from the store. * @param options - Options to configure this wire with, like an event to force it or cancel it. * @returns The Wire function with a throttle timing. * * @public */ function throttle(wire, timeInMs, options = {}) { return (observable, store, on) => { return wire(observable.pipe(throttle$1(() => createTimer(normalizeTime(timeInMs, store), options, on), { leading: true, trailing: true, })), store, on); }; } /** * Creates a {@link Wire} from other `toWire` wire. It uses `mapFn` to transform the * `FromPayload` received to `ToPayload` which `toWire` requires. This is * useful to reuse wires in different Events where the payload doesn't fit exactly. * * @param toWire - The wire which the new Wire is created from. * @param mapFn - Function to map the payload from `FromPayload` to `ToPayload`. * @returns A new {@link Wire}. * * @public */ function mapWire(toWire, mapFn) { return (observable, ...restWireParams) => toWire(observable.pipe(map(({ eventPayload, ...restWirePayload }) => ({ eventPayload: mapFn(eventPayload), ...restWirePayload, }))), ...restWireParams); } export { debounce, filter, filterBlacklistedModules, filterFalsyPayload, filterTruthyPayload, filterWhitelistedModules, mapWire, throttle }; //# sourceMappingURL=wires.operators.js.map