Skip to content

Commit a5904fd

Browse files
Harrison IfeanyichukwuHarrison Ifeanyichukwu
authored andcommitted
feat: add optional callback function that can be used to perform extra cleanup
1 parent bdf5a4e commit a5904fd

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"publishConfig": {
66
"access": "public"
77
},
8+
"sideEffects": false,
89
"main": "build/cjs/index",
910
"typings": "build/es/index",
1011
"module": "build/es/index",
@@ -15,7 +16,7 @@
1516
"url": "https://github.com/teclone/react-use-effect-cleaner.git"
1617
},
1718
"files": [
18-
"build"
19+
"./build"
1920
],
2021
"scripts": {
2122
"build": "rollup-all --formats es,cjs,iife",

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const noop = (...args: Array<any>) => {
2+
// do nothing
3+
};

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CancelTokenSource } from 'axios';
2+
import { noop } from './constants';
23

34
export interface CreateEffectCleanerOpts {
45
/**
@@ -24,6 +25,12 @@ export interface CreateEffectCleanerOpts {
2425
intervalIds?: {
2526
[p: string]: number;
2627
};
28+
29+
/**
30+
* called when running effects cleanup
31+
* @returns
32+
*/
33+
callback?: () => void;
2734
}
2835

2936
/**
@@ -35,8 +42,13 @@ export const createEffectCleaner = <T>(
3542
opts?: CreateEffectCleanerOpts
3643
) => {
3744
let _stalled = false;
38-
const { abortController, cancelTokenSource, intervalIds, timeoutIds } =
39-
opts || {};
45+
const {
46+
abortController,
47+
cancelTokenSource,
48+
intervalIds,
49+
timeoutIds,
50+
callback = noop,
51+
} = opts || {};
4052

4153
const handler = {
4254
apply(stateModifier, thisArg, argArray) {
@@ -87,6 +99,8 @@ export const createEffectCleaner = <T>(
8799
clearTimeout(timeoutIds[key]);
88100
});
89101
}
102+
103+
callback();
90104
};
91105

92106
return proxies;

0 commit comments

Comments
 (0)