Skip to content

Commit

Permalink
Use async / await, like civilized people.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElvishJerricco committed Aug 2, 2018
1 parent e310534 commit b275a21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
18 changes: 10 additions & 8 deletions example/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Device, configureFileSystem, BFSCallback, Stats, File, FileFlag } from "webabi-kernel";
import { Device, configureFileSystem, BFSCallback, Stats, File, FileFlag, FS, asyncRead, asyncWrite } from "webabi-kernel";

class JSaddleDevice implements Device {
open(flag: FileFlag, cb: BFSCallback<File>): void {
Expand All @@ -7,12 +7,14 @@ class JSaddleDevice implements Device {
}
}

configureFileSystem({ "/jsaddle": new JSaddleDevice() }, (err, fs) => {
console.log(err);
async function main() {
let fs = await configureFileSystem({ "/jsaddle": new JSaddleDevice() });
let buf = Buffer.from("foo\n");
fs.write(1, buf, 0, buf.length, null, () => {
fs.read(0, buf, 0, 4, null, (err, n, buf) => {
console.log({ err: err, n: n, buf: buf && buf.toString() });
});
});
await asyncWrite(fs, 1, buf, 0, buf.length, null);
const { byteLength } = await asyncRead(fs, 0, buf, 0, 4, null);
console.log({ byteLength: byteLength, buffer: buf.toString() });
}

main().catch(e => {
console.error("Error: ", e);
});
55 changes: 32 additions & 23 deletions kernel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,41 @@ export class DeviceFileSystem extends BaseFileSystem implements FileSystem {
}
}

export function configureFileSystem(devices: { [name: string]: Device }, cb: BFSCallback<FS>): void {
DeviceFileSystem.Create({ devices: devices }, (e, dfs) => {
if (e) {
cb(e);
return;
}
export async function configureFileSystem(devices: { [name: string]: Device }): Promise<FS> {
const dfs = await new Promise<DeviceFileSystem>((resolve, reject) => {
DeviceFileSystem.Create({ devices: devices }, (e, dfs) => e ? reject(e) : resolve(dfs))
});
const mfs = await new Promise<MountableFileSystem>((resolve, reject) => {
MountableFileSystem.Create({
"/dev": dfs
}, (e, mfs) => {
if (e) {
cb(e);
return
}

const fs = new FS();
fs.initialize(mfs);

const fdMap: {[id: number]: File} = (fs as any).fdMap;
fdMap[0] = handles.stdin;
fdMap[1] = handles.stdout;
fdMap[2] = handles.stderr;

cb(undefined, fs);
});
}, (e, mfs) => e ? reject(e) : resolve(mfs));
});

const fs = new FS();
fs.initialize(mfs);

const fdMap: {[id: number]: File} = (fs as any).fdMap;
fdMap[0] = handles.stdin;
fdMap[1] = handles.stdout;
fdMap[2] = handles.stderr;

return fs;
}

export async function asyncRead(fs: FS, fd: number, buffer: Buffer, offset: number, length: number, position: number | null)
: Promise<{ byteLength: number, buffer: Buffer }> {
return new Promise<{ byteLength: number, buffer: Buffer }>((resolve, reject) => {
fs.read(fd, buffer, offset, length, position,
(err, n, buf) => err ? reject(err) : resolve({ byteLength: n, buffer: buf }));
});
}

export async function asyncWrite(fs: FS, fd: number, buffer: Buffer, offset: number, length: number, position: number | null)
: Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.write(fd, buffer, offset, length, position, e => e ? reject(e) : resolve())
});
}

// Re-export for device implementors
export { BFSCallback, Stats, File, FileFlag };
export { BFSCallback, Stats, File, FileFlag, FS };

0 comments on commit b275a21

Please sign in to comment.