Skip to content

Commit 117b7c5

Browse files
committed
feat: 🎸 add scaffold for CoreFileSystemObserver implementation and restructure fsa() helper
1 parent d581d8c commit 117b7c5

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

src/fsa/CoreFileSystemObserver.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { IFileSystemChangeRecord, IFileSystemDirectoryHandle, IFileSystemFileHandle, IFileSystemObserver, IFileSystemObserverObserveOptions, IFileSystemSyncAccessHandle } from './types';
2+
import type { Superblock } from '../core';
3+
4+
export class CoreFileSystemObserver implements IFileSystemObserver {
5+
constructor (
6+
protected readonly _core: Superblock,
7+
protected readonly callback: (records: IFileSystemChangeRecord[], observer: IFileSystemObserver) => void
8+
) {}
9+
10+
public async observe(handle: IFileSystemFileHandle | IFileSystemDirectoryHandle | IFileSystemSyncAccessHandle, options?: IFileSystemObserverObserveOptions): Promise<void> {
11+
throw new Error('Method not implemented.');
12+
}
13+
14+
/** Disconnect and stop all observations. */
15+
public disconnect(): void {
16+
throw new Error('Method not implemented.');
17+
}
18+
}

src/fsa/__tests__/scenarios.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { Superblock } from '../../core/Superblock';
2-
import { coreToFsa, fsa } from '../index';
2+
import { CoreFsaContext, fsa } from '../index';
33
import { CoreFileSystemDirectoryHandle } from '../CoreFileSystemDirectoryHandle';
44
import { onlyOnNode20 } from '../../__tests__/util';
5-
import { fileURLToPath } from 'url';
5+
6+
const coreToFsa = (
7+
core: Superblock,
8+
dirPath: string = '/',
9+
ctx?: Partial<CoreFsaContext>,
10+
): CoreFileSystemDirectoryHandle => {
11+
const { dir } = fsa(ctx, core, dirPath);
12+
return dir;
13+
};
614

715
onlyOnNode20('coreToFsa scenarios', () => {
816
test('can create FSA from empty Superblock', () => {

src/fsa/index.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
import { CoreFileSystemDirectoryHandle } from './CoreFileSystemDirectoryHandle';
2-
import { CoreFsaContext } from './types';
2+
import { CoreFsaContext, IFileSystemChangeRecord, IFileSystemObserver, IFileSystemObserverConstructable } from './types';
33
import { Superblock } from '../core/Superblock';
4+
import { CoreFileSystemObserver } from './CoreFileSystemObserver';
45

56
export * from './types';
67
export * from './CoreFileSystemHandle';
78
export * from './CoreFileSystemDirectoryHandle';
89
export * from './CoreFileSystemFileHandle';
910
export * from './CoreFileSystemSyncAccessHandle';
1011
export * from './CoreFileSystemWritableFileStream';
12+
export * from './CoreFileSystemObserver';
1113
export * from './CorePermissionStatus';
1214

13-
/**
14-
* Creates a File System Access API implementation on top of a Superblock.
15-
*/
16-
export const coreToFsa = (
17-
core: Superblock,
18-
dirPath: string = '/',
19-
ctx?: Partial<CoreFsaContext>,
20-
): CoreFileSystemDirectoryHandle => {
21-
return new CoreFileSystemDirectoryHandle(core, dirPath, ctx);
22-
};
23-
2415
/**
2516
* Create a new instance of an in-memory File System Access API
2617
* implementation rooted at the root directory of the filesystem.
2718
*
2819
* @param ctx Optional context for the File System Access API.
20+
* @param core Optional low-level file system implementation to
21+
* back the File System Access API. If not provided, a new empty
22+
* Superblock instance will be created.
23+
* @param dirPath Optional path within the filesystem to use as the root
24+
* directory of the File System Access API. Defaults to `/`.
2925
* @returns A File System Access API implementation `dir` rooted at
30-
* the root directory of the filesystem, as well as the `core`
31-
* file system itself.
26+
* the root directory of the filesystem, as well as the `core`,
27+
* a low-level file system implementation itself. Also, returns
28+
* `FileSystemObserver`, a class that can be used to create
29+
* observers that watch for changes to files and directories.
3230
*/
33-
export const fsa = (ctx?: Partial<CoreFsaContext>) => {
34-
const core = new Superblock();
35-
const dir = new CoreFileSystemDirectoryHandle(core, '/', ctx);
36-
return { dir, core };
31+
export const fsa = (ctx?: Partial<CoreFsaContext>, core = new Superblock(), dirPath: string = '/') => {
32+
const dir = new CoreFileSystemDirectoryHandle(core, dirPath, ctx);
33+
const FileSystemObserver: IFileSystemObserverConstructable = class FileSystemObserver extends CoreFileSystemObserver {
34+
constructor (callback: (records: IFileSystemChangeRecord[], observer: IFileSystemObserver) => void) {
35+
super(core, callback);
36+
}
37+
}
38+
return { core, dir, FileSystemObserver };
3739
};

src/fsa/types.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Superblock } from '../core/Superblock';
2-
31
export interface IPermissionStatus {
42
name: string;
53
state: 'granted' | 'denied' | 'prompt';
@@ -213,14 +211,6 @@ export interface IFileSystemObserverObserveOptions {
213211
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemObserver
214212
*/
215213
export interface IFileSystemObserver {
216-
/**
217-
* Constructor for creating a FileSystemObserver.
218-
*
219-
* @param callback Function called with file system change records and the
220-
* observer instance
221-
*/
222-
new (callback: (records: IFileSystemChangeRecord[], observer: IFileSystemObserver) => void): IFileSystemObserver;
223-
224214
/**
225215
* Start observing changes to a given file or directory.
226216
*
@@ -232,3 +222,13 @@ export interface IFileSystemObserver {
232222
/** Disconnect and stop all observations. */
233223
disconnect(): void;
234224
}
225+
226+
export interface IFileSystemObserverConstructable {
227+
/**
228+
* Constructor for creating a FileSystemObserver.
229+
*
230+
* @param callback Function called with file system change records and the
231+
* observer instance
232+
*/
233+
new (callback: (records: IFileSystemChangeRecord[], observer: IFileSystemObserver) => void): IFileSystemObserver;
234+
}

0 commit comments

Comments
 (0)