jvilk / BrowserFS

BrowserFS is an in-browser filesystem that emulates the Node JS filesystem API and supports storing and retrieving files from various backends.
Other
3.06k stars 216 forks source link

Internally don't use a Node-style API #341

Closed james-pre closed 1 year ago

james-pre commented 1 year ago

Internally, BrowserFS follows Node's FS API. For example (in a backend):

operation(path: string, cred: Cred, callback: BFSOneArgCallback<ResultType>);
operationSync(path: string, cred: Cred): ResultType;

While emulating Node's API for the end user is the goal, we can be simpler internally. In conjunction with #342 we can simply do something like follows:

operation(path: string, cred: Cred, callback: BFSOneArgCallback<ResultType>): ResultType;

A sync backend could do something like this:

operation(path: string, cred: Cred, callback: BFSOneArgCallback<ResultType>): ResultType {
    const result = this._doSomething(path, cred);
    callback(result);
    return result;
}

While an async backend could implement something like this (See #342):

operation(path: string, cred: Cred, callback: BFSOneArgCallback<ResultType>): ResultType {
    const result = this._sync._doSomething(path, cred);
    this._async.doSomething(path, cred, callback);
    return result;
}

Then, the consumer (FS module) can use the sync or async part of the function.

james-pre commented 1 year ago

This is unnecessary since the current system works fine.