Skip to content

Commit

Permalink
feat: change filter to params
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Aug 21, 2020
1 parent 75745db commit 4a1bc4c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
12 changes: 2 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export interface IOptions {
/* 过滤器,调用 reload 时过滤需要 reload 的模块 */
filter?: (file: string) => boolean;

/* 过滤器,调用 reloadAll 时过滤需要 reload 的模块 */
filterAll?: (file: string) => boolean;

commonRootPath?: string;
}

Expand All @@ -43,7 +40,6 @@ export default class Reloader {
fileMap: IFileMap = {};
filter: (file: string) => boolean;
commonRootPath: string;
filterAll: (file: string) => boolean;

files: string[] = [];

Expand All @@ -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<string>();
for (const moduleId of Object.keys(require.cache)) {
if (this.filterAll(moduleId)) {
if (filter(moduleId)) {
reloadModules.add(moduleId);
}
}
Expand Down
7 changes: 4 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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);
Expand Down

0 comments on commit 4a1bc4c

Please sign in to comment.