Skip to content

Commit

Permalink
feat: support unwatchFile (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleung committed Jun 18, 2022
1 parent e9a5d96 commit 721f49d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
45 changes: 34 additions & 11 deletions src/__tests__/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Union } from '..';
import { Volume, createFsFromVolume } from 'memfs';
import * as fs from 'fs';

function sleep(millisec: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
setTimeout(resolve, millisec);
});
}

describe('union', () => {
describe('Union', () => {
describe('sync methods', () => {
Expand Down Expand Up @@ -62,6 +68,23 @@ describe('union', () => {

watcher.close();
});

it('can watchFile and unwatchFile', async () => {
const memfs = Volume.fromJSON({ '/foo': '1' });
const ufs = new Union().use(memfs as any);
const mockCallback = jest.fn();
ufs.watchFile('/foo', { interval: 10 }, mockCallback);

memfs.writeFileSync('/foo', '2');
await sleep(100);
expect(mockCallback).toBeCalled();
mockCallback.mockClear();

ufs.unwatchFile('/foo');
memfs.writeFileSync('/foo', '3');
await sleep(100);
expect(mockCallback).not.toBeCalled();
});
});

describe('existsSync()', () => {
Expand Down Expand Up @@ -145,7 +168,7 @@ describe('union', () => {
ufs.use(vol as any);
const files = ufs.readdirSync('/foo', { withFileTypes: true });
expect(files[0]).toBeInstanceOf(createFsFromVolume(vol).Dirent);
expect(files.map(f => f.name)).toEqual(['bar', 'baz', 'zzz']);
expect(files.map((f) => f.name)).toEqual(['bar', 'baz', 'zzz']);
});

it('throws error when all fss fail', () => {
Expand All @@ -160,7 +183,7 @@ describe('union', () => {
});
});
describe('async methods', () => {
it('Basic one file system', done => {
it('Basic one file system', (done) => {
const vol = Volume.fromJSON({ '/foo': 'bar' });
const ufs = new Union();
ufs.use(vol as any);
Expand All @@ -180,7 +203,7 @@ describe('union', () => {
expect(content).toBe('baz');
});
});
it('File not found', done => {
it('File not found', (done) => {
const vol = Volume.fromJSON({ '/foo': 'bar' });
const ufs = new Union();
ufs.use(vol as any);
Expand All @@ -201,15 +224,15 @@ describe('union', () => {
}
});

it('No file systems attached', done => {
it('No file systems attached', (done) => {
const ufs = new Union();
ufs.stat('/foo2', (err, data) => {
expect(err?.message).toBe('No file systems attached.');
done();
});
});

it('callbacks are only called once', done => {
it('callbacks are only called once', (done) => {
const vol = Volume.fromJSON({
'/foo/bar': 'bar',
});
Expand Down Expand Up @@ -242,7 +265,7 @@ describe('union', () => {
});
});

it('reads multiple memfs correctly', done => {
it('reads multiple memfs correctly', (done) => {
const vol = Volume.fromJSON({
'/foo/bar': 'bar',
'/foo/baz': 'baz',
Expand All @@ -261,7 +284,7 @@ describe('union', () => {
});
});

it('reads other fss when one fails', done => {
it('reads other fss when one fails', (done) => {
const vol = Volume.fromJSON({
'/foo/bar': 'bar',
'/foo/baz': 'baz',
Expand All @@ -281,7 +304,7 @@ describe('union', () => {
});
});

it('honors the withFileTypes: true option', done => {
it('honors the withFileTypes: true option', (done) => {
const vol = Volume.fromJSON({
'/foo/bar': 'bar',
'/foo/zzz': 'zzz',
Expand All @@ -292,12 +315,12 @@ describe('union', () => {
ufs.use(vol as any);
ufs.readdir('/foo', { withFileTypes: true }, (err, files) => {
expect(files[0]).toBeInstanceOf(createFsFromVolume(vol).Dirent);
expect(files.map(f => f.name)).toEqual(['bar', 'baz', 'zzz']);
expect(files.map((f) => f.name)).toEqual(['bar', 'baz', 'zzz']);
done();
});
});

it('throws error when all fss fail', done => {
it('throws error when all fss fail', (done) => {
const vol = Volume.fromJSON({});
const vol2 = Volume.fromJSON({});

Expand Down Expand Up @@ -413,7 +436,7 @@ describe('union', () => {
ufs.use(vol as any);
const files = await ufs.promises.readdir('/foo', { withFileTypes: true });
expect(files[0]).toBeInstanceOf(createFsFromVolume(vol).Dirent);
expect(files.map(f => f.name)).toEqual(['bar', 'baz', 'zzz']);
expect(files.map((f) => f.name)).toEqual(['bar', 'baz', 'zzz']);
});

it('throws error when all fss fail', async () => {
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ export const Union = (_Union as any) as new () => IUnionFs;
export const ufs = (new _Union() as any) as IUnionFs;
export default ufs;

export {
IFS
}
export { IFS };
10 changes: 8 additions & 2 deletions src/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ export class Union {
}

public unwatchFile = (...args) => {
throw new Error('unwatchFile is not supported, please use watchFile');
for (const fs of this.fss) {
try {
fs.unwatchFile.apply(fs, args);
} catch (e) {
// dunno what to do here...
}
}
};

public watch = (...args) => {
Expand Down Expand Up @@ -386,7 +392,7 @@ export class Union {
}

// Replace `callback` with our intermediate function.
args[lastarg] = function(err) {
args[lastarg] = function (err) {
if (err) return iterate(i + 1, err);
if (cb) cb.apply(cb, arguments);
};
Expand Down

0 comments on commit 721f49d

Please sign in to comment.