Skip to content

Commit

Permalink
Async patches in constructor
Browse files Browse the repository at this point in the history
Simplified `AsyncMixin` interface
  • Loading branch information
james-pre committed Dec 20, 2024
1 parent c0afa6e commit 9ba868e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
31 changes: 11 additions & 20 deletions src/mixins/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { StoreFS } from '../backends/store/fs.js';
import type { Store } from '../backends/store/store.js';
import { join } from '../emulation/path.js';
import { Errno, ErrnoError } from '../error.js';
import { parseFlag, PreloadFile, type File } from '../file.js';
import { parseFlag, PreloadFile } from '../file.js';
import type { FileSystem } from '../filesystem.js';
import type { Stats } from '../stats.js';
import type { AsyncFSMethods, Mixin } from './shared.js';
import type { _SyncFSKeys, AsyncFSMethods, Mixin } from './shared.js';

/** @internal */
export type AsyncOperation = {
Expand All @@ -15,27 +15,13 @@ export type AsyncOperation = {
/**
* @internal
*/
export interface AsyncMixin {
export interface AsyncMixin extends Pick<FileSystem, Exclude<_SyncFSKeys, 'existsSync'>> {
/**
* @internal @protected
*/
_sync?: FileSystem;
/**
* @internal @protected
*/
_patchAsync(): void;
queueDone(): Promise<void>;
ready(): Promise<void>;
renameSync(oldPath: string, newPath: string): void;
statSync(path: string): Stats;
createFileSync(path: string, flag: string, mode: number): File;
openFileSync(path: string, flag: string): File;
unlinkSync(path: string): void;
rmdirSync(path: string): void;
mkdirSync(path: string, mode: number): void;
readdirSync(path: string): string[];
linkSync(srcpath: string, dstpath: string): void;
syncSync(path: string, data: Uint8Array, stats: Readonly<Stats>): void;
}

/**
Expand Down Expand Up @@ -67,7 +53,12 @@ export function Async<const T extends typeof FileSystem>(FS: T): Mixin<T, AsyncM

private _isInitialized: boolean = false;

_sync?: FileSystem;
abstract _sync?: FileSystem;

public constructor(...args: any[]) {
super(...args);
this._patchAsync();
}

public async ready(): Promise<void> {
await super.ready();
Expand Down Expand Up @@ -229,10 +220,10 @@ export function Async<const T extends typeof FileSystem>(FS: T): Mixin<T, AsyncM
}

/**
* @internal @protected
* @internal
* Patch all async methods to also call their synchronous counterparts unless called from the queue
*/
_patchAsync(): void {
private _patchAsync(): void {
const asyncFSMethodKeys = ['rename', 'stat', 'createFile', 'openFile', 'unlink', 'rmdir', 'mkdir', 'readdir', 'link', 'sync', 'exists'] as const;
for (const key of asyncFSMethodKeys) {
if (typeof this[key] !== 'function') continue;
Expand Down
8 changes: 5 additions & 3 deletions src/mixins/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ export type Mixin<TBase extends typeof FileSystem, TMixin> = (abstract new (...a

/**
* @internal @hidden
* Note this include `existsSync`, even though it is a concrete method.
*/
type _SyncFSKeys = Extract<keyof FileSystem, `${string}Sync`>;
export type _SyncFSKeys = Exclude<Extract<keyof FileSystem, `${string}Sync`>, '_disableSync'>;

/**
* @internal @hidden
* Note this include `exists`, even though it is a concrete method.
*/
type _AsyncFSKeys = {
[K in _SyncFSKeys]: K extends `${infer T}Sync` ? (T extends '_disable' ? never : T) : never;
export type _AsyncFSKeys = {
[K in _SyncFSKeys]: K extends `${infer T}Sync` ? T : never;
}[_SyncFSKeys];

/**
Expand Down
9 changes: 1 addition & 8 deletions tests/common/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ class ExampleAsyncFS extends Async(StoreFS) {

public constructor() {
super(new InMemoryStore('test'));
this._patchAsync();
}
}

const asyncFS = new ExampleAsyncFS();

asyncFS.existsSync('/');

await configure({
mounts: {
'/': asyncFS,
},
});
await configure({ mounts: { '/': asyncFS } });

suite('Async Mixin', () => {
test('async -> cache syncing', async () => {
Expand Down

0 comments on commit 9ba868e

Please sign in to comment.