/* * MIT License * * Copyright (c) 2018 Choko (choko@curioswitch.org) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import React from 'react'; import { ReactReduxContext, ReactReduxContextValue } from 'react-redux'; import getInjectors from '../state/saga/injector'; interface Options { key: string; saga: any; mode?: string; } interface HocProps { reduxCtx: ReactReduxContextValue; } /** * Dynamically injects a saga, passes component's props as saga arguments * * @param {string} key A key of the saga * @param {function} saga A root saga that will be injected * @param {string} [mode] By default (constants.RESTART_ON_REMOUNT) the saga will be started on component mount and * cancelled with `task.cancel()` on component un-mount for improved performance. Another two options: * - constants.DAEMON—starts the saga on component mount and never cancels it or starts again, * - constants.ONCE_TILL_UNMOUNT—behaves like 'RESTART_ON_REMOUNT' but never runs it again. * * Deprecated - this HOC will be deleted when React 17 is released. Migrate to hooks and useSaga before then. * */ export default ({ key, saga, mode }: Options) => ( WrappedComponent: | React.ComponentClass | React.StatelessComponent, ) => { class InjectSaga extends React.Component { // eslint-disable-next-line react/static-property-placement public static displayName = `withSaga(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`; private injectors = getInjectors(this.props.reduxCtx.store as any); // eslint-disable-next-line camelcase public UNSAFE_componentWillMount() { const { injectSaga } = this.injectors; injectSaga(key, { saga, mode }, this.props); } public componentWillUnmount() { const { ejectSaga } = this.injectors; ejectSaga(key); } public render() { const { reduxCtx, ...others } = this.props as any; // eslint-disable-next-line react/jsx-props-no-spreading return ; } } return (props: TOriginalProps) => ( {(value) => ( // eslint-disable-next-line react/jsx-props-no-spreading )} ); };