UNPKG

@empathyco/x-components

Version:
113 lines (111 loc) 4.96 kB
/** * Creates a wire that executes the function passed. This function will receive a * {@link WireParams} object. * * @param fn - The function to execute whenever a new value is emitted to the observable. * @returns The Wire function. * @public */ function createWireFromFunction(fn) { return (observable, store) => observable.subscribe(({ metadata, eventPayload }) => { fn({ eventPayload, store, metadata }); }); } function wireCommit(mutation, payload) { return (observable, store) => observable.subscribe(createSubscriptionCallback(store, 'commit', mutation, payload)); } /** * Creates a wire that commits a mutation to the store, but without any payload. This wire can * be used in every event, as it does not have a payload type associated. * * @param mutation - The full mutation path to commit. I.e. `x/searchBox/setQuery`. * @returns {@link AnyWire} A wire that commits the mutation without any payload. * @public */ function wireCommitWithoutPayload(mutation) { return (observable, store) => observable.subscribe(() => store.commit(mutation)); } function wireDispatch(action, payload) { return (observable, store) => observable.subscribe(createSubscriptionCallback(store, 'dispatch', action, payload)); } /** * Creates a wire that dispatches an action to the store, but without any payload. This wire can * be used in every event, as it does not have a payload type associated. * * @param action - The full action path to dispatch. I.e. `x/querySuggestions/fetchSuggestions`. * @returns {@link AnyWire} A wire that dispatches the action without any payload. * @public */ function wireDispatchWithoutPayload(action) { // eslint-disable-next-line ts/no-unsafe-return,ts/no-misused-promises return (observable, store) => observable.subscribe(async () => store.dispatch(action)); } /** * Creates a wires factory that can create wires that will invoke the service methods. * * @param service - The service to invoke its methods. * @returns A factory to create wires that invoke the service methods. * @public */ function wireService(service) { return (method, payload) => { return observable => observable.subscribe(payload !== undefined ? // eslint-disable-next-line ts/no-unsafe-return () => service[method](payload) : // eslint-disable-next-line ts/no-unsafe-return // eslint-disable-next-line ts/no-unsafe-return observablePayload => service[method](observablePayload.eventPayload)); }; } /** * Creates a wires factory that can create wires that will invoke the service methods but * without payload. * * @param service - The service to invoke its methods. * @returns A factory to create wires that invoke the service methods without payload. * @public */ function wireServiceWithoutPayload(service) { return method => { // eslint-disable-next-line ts/no-unsafe-return return observable => observable.subscribe(() => service[method]()); }; } /** * Creates the callback function for the {@link wireCommit} and {@link wireDispatch} * subscriptions. It can be based on the payload as function which retrieves the observable * payload from the store, a static payload or the event value from the observable. * * @param store - The {@link RootXStoreState} store. * @param commitOrDispatch - The executor over store. It can be `commit` or `dispatch`. * @param mutationOrAction - The mutation or action to commit or dispatch respectively. * @param payload - The payload for the store executor. It can be a function which retrieves the * payload from the store, a static payload or the event value from the observable. * @typeParam Payload - The type of the payload to get the observable event value type. * @returns A function to commit or dispatch a payload value over store. * @internal */ function createSubscriptionCallback(store, commitOrDispatch, mutationOrAction, payload) { const storeExecutor = store[commitOrDispatch]; return typeof payload === 'function' ? async (wirePayload) => { // eslint-disable-next-line ts/no-unsafe-return return storeExecutor(mutationOrAction, // eslint-disable-next-line ts/no-unsafe-call payload({ state: store.state, // eslint-disable-next-line ts/no-unsafe-assignment getters: store.getters, ...wirePayload, })); } : payload !== undefined ? () => { void storeExecutor(mutationOrAction, payload); } : observableValue => { void storeExecutor(mutationOrAction, observableValue.eventPayload); }; } export { createWireFromFunction, wireCommit, wireCommitWithoutPayload, wireDispatch, wireDispatchWithoutPayload, wireService, wireServiceWithoutPayload }; //# sourceMappingURL=wires.factory.js.map