Skip to content

Commit

Permalink
feat: add optional callback function that can be used to perform extr…
Browse files Browse the repository at this point in the history
…a cleanup
  • Loading branch information
teclone committed Oct 25, 2023
1 parent bdf5a4e commit a5904fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"main": "build/cjs/index",
"typings": "build/es/index",
"module": "build/es/index",
Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const noop = (...args: Array<any>) => {
// do nothing
};
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CancelTokenSource } from 'axios';
import { noop } from './constants';

export interface CreateEffectCleanerOpts {
/**
Expand All @@ -24,6 +25,12 @@ export interface CreateEffectCleanerOpts {
intervalIds?: {
[p: string]: number;
};

/**
* called when running effects cleanup
* @returns
*/
callback?: () => void;
}

/**
Expand All @@ -35,8 +42,13 @@ export const createEffectCleaner = <T>(
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) {
Expand Down Expand Up @@ -87,6 +99,8 @@ export const createEffectCleaner = <T>(
clearTimeout(timeoutIds[key]);
});
}

callback();
};

return proxies;
Expand Down

0 comments on commit a5904fd

Please sign in to comment.