import { debounce as debounceRx , filter as filterRx , map , throttle as throttleRx } from 'rxjs/operators' ; import { XModuleName } from '../x-modules/x-modules.types' ; import { createTimer , normalizeTime } from './wires-operators.utils' ; import { TimedWireOperatorOptions , TimeSelector , Wire , WireParams } from './wiring.types' ; /**
 * Creates a  Wire that is only executed whenever the condition in the filterFn is true.
 *
 *  param wire param filterFn returns public export function filter < Payload > ( wire , filterFn ) { return ( observable , store , on ) => wire ( observable . pipe ( filterRx ( wirePayload => filterFn ( { ... wirePayload , store } ) ) ) , store , on ) ; } /**
 * Creates a  Wire that is only executed when the payload is truthy. A truthy value is
 * whatever is not a  https .
 *
 *  param wire returns public export function filterFalsyPayload < Payload > ( wire ) { return filter ( wire , ( { eventPayload } ) => ! ! eventPayload ) ; } /**
 * Creates a  Wire that is only executed when the payload is a
 *  https .
 *
 *  param wire returns public export function filterTruthyPayload < Payload > ( wire ) { return filter ( wire , ( { eventPayload } ) => ! eventPayload ) ; } /**
 * Creates a  Wire that is only executed if the event is emitted from a  XModule * that is included
 * in the `whitelist` array passed as parameter.
 *
 *  param wire param whitelist - An array of  XModuleName or null.
 *  returns public export function filterWhitelistedModules < Payload > ( wire , whitelist ) { const whitelistSet = new Set ( whitelist ) ; return filter ( wire , ( { metadata } ) => whitelistSet . has ( metadata . moduleName ) ) ; } /**
 * Creates a  Wire that is only executed if the event is emitted from a  XModule * that is NOT included
 * in the `blacklist` array passed as parameter.
 *
 *  param wire param blacklist - An array of  XModuleName or null.
 *  returns public export function filterBlacklistedModules < Payload > ( wire , blacklist ) { const blacklistSet = new Set ( blacklist ) ; return filter ( wire , ( { metadata } ) => ! blacklistSet . has ( metadata . moduleName ) ) ; } /**
 * Creates a debounced  Wire . Being debounced means that it will only be executed after
 * the time given by `timeInMs` has passed without invoking it.
 *
 *  param wire param timeInMs param options returns public export function debounce < Payload > ( wire , timeInMs , options = { } ) { return ( observable , store , on ) => { return wire ( observable . pipe ( debounceRx ( ( ) => createTimer ( normalizeTime ( timeInMs , store ) , options , on ) ) ) , store , on ) ; } ; } /**
 * Creates a throttled  Wire . Being throttled means that it will only be executed once
 * every couple of milliseconds given by the `timeInMs` parameter.
 *
 *  param wire param timeInMs param options returns public export function throttle < Payload > ( wire , timeInMs , options = { } ) { return ( observable , store , on ) => { return wire ( observable . pipe ( throttleRx ( ( ) => createTimer ( normalizeTime ( timeInMs , store ) , options , on ) , { leading : true , trailing : true } ) ) , store , on ) ; } ; } /**
 * Creates a  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 param mapFn returns A new  Wire .
 *
 *  public export function mapWire < FromPayload , ToPayload > ( toWire , mapFn ) { return ( observable , ... restWireParams ) => toWire ( observable . pipe ( map ( ( { eventPayload , ... restWirePayload } ) => ( { eventPayload : mapFn ( eventPayload ) , ... restWirePayload } ) ) ) , ... restWireParams ) ; }