From 4a1bc4ce7dce16034cfedd58cff783d426b2df30 Mon Sep 17 00:00:00 2001 From: cxtom Date: Fri, 21 Aug 2020 10:40:40 +0800 Subject: [PATCH] feat: change filter to params --- src/index.ts | 12 ++---------- test/index.test.ts | 7 ++++--- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 71c14e8..e053668 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,9 +26,6 @@ export interface IOptions { /* 过滤器,调用 reload 时过滤需要 reload 的模块 */ filter?: (file: string) => boolean; - /* 过滤器,调用 reloadAll 时过滤需要 reload 的模块 */ - filterAll?: (file: string) => boolean; - commonRootPath?: string; } @@ -43,7 +40,6 @@ export default class Reloader { fileMap: IFileMap = {}; filter: (file: string) => boolean; commonRootPath: string; - filterAll: (file: string) => boolean; files: string[] = []; @@ -56,18 +52,14 @@ export default class Reloader { if (options.filter) { this.filter = options.filter; } - this.filterAll = (() => false); - if (options.filterAll) { - this.filterAll = options.filterAll; - } this.commonRootPath = options.commonRootPath || ''; this.updateFiles(); } - reloadAll() { + reloadAll(filter: (file: string) => boolean) { const reloadModules = new Set(); for (const moduleId of Object.keys(require.cache)) { - if (this.filterAll(moduleId)) { + if (filter(moduleId)) { reloadModules.add(moduleId); } } diff --git a/test/index.test.ts b/test/index.test.ts index ffb8894..c8d5324 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -67,8 +67,7 @@ describe('Reloader test', () => { const reloader = new Reloader({ context: resolve(__dirname, './fixtures'), - commonRootPath: resolve(__dirname, './fixtures/mainModule.js'), - filterAll: (id) => id.endsWith('fixtures/mod1.js') + commonRootPath: resolve(__dirname, './fixtures/mainModule.js') }); require('./fixtures/mod1').num++; @@ -77,7 +76,9 @@ describe('Reloader test', () => { expect(require('./fixtures/mod1').num).to.be.equal(2); expect(require('./fixtures/mod2').num).to.be.equal(3); - let {errors, reloadModules} = reloader.reloadAll(); + let {errors, reloadModules} = reloader.reloadAll( + id => id.endsWith('fixtures/mod1.js') + ); expect(errors.length).to.be.equal(0); expect(reloadModules.length).to.be.equal(1);