diff --git a/package.json b/package.json index f84ce01..47d5cdd 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "publishConfig": { "access": "public" }, + "sideEffects": false, "main": "build/cjs/index", "typings": "build/es/index", "module": "build/es/index", @@ -15,7 +16,7 @@ "url": "https://github.com/teclone/react-use-effect-cleaner.git" }, "files": [ - "build" + "./build" ], "scripts": { "build": "rollup-all --formats es,cjs,iife", diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..5444758 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,3 @@ +export const noop = (...args: Array) => { + // do nothing +}; diff --git a/src/index.ts b/src/index.ts index 7f01865..dbed120 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import type { CancelTokenSource } from 'axios'; +import { noop } from './constants'; export interface CreateEffectCleanerOpts { /** @@ -24,6 +25,12 @@ export interface CreateEffectCleanerOpts { intervalIds?: { [p: string]: number; }; + + /** + * called when running effects cleanup + * @returns + */ + callback?: () => void; } /** @@ -35,8 +42,13 @@ export const createEffectCleaner = ( opts?: CreateEffectCleanerOpts ) => { let _stalled = false; - const { abortController, cancelTokenSource, intervalIds, timeoutIds } = - opts || {}; + const { + abortController, + cancelTokenSource, + intervalIds, + timeoutIds, + callback = noop, + } = opts || {}; const handler = { apply(stateModifier, thisArg, argArray) { @@ -87,6 +99,8 @@ export const createEffectCleaner = ( clearTimeout(timeoutIds[key]); }); } + + callback(); }; return proxies;