From b3d55d9194e7a98ef0abcc2b4e26436654be8646 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Thu, 2 Aug 2018 01:14:23 -0400 Subject: [PATCH] Use async / await, like civilized people. --- example/src/index.ts | 18 ++++++++------- kernel/src/index.ts | 55 ++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/example/src/index.ts b/example/src/index.ts index d67fc5c..71d38dc 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -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): void { @@ -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, buf.length, null); + console.log({ byteLength: byteLength, buffer: buf.toString() }); +} + +main().catch(e => { + console.error("Error: ", e); }); diff --git a/kernel/src/index.ts b/kernel/src/index.ts index 0500894..ecc5092 100644 --- a/kernel/src/index.ts +++ b/kernel/src/index.ts @@ -67,32 +67,41 @@ export class DeviceFileSystem extends BaseFileSystem implements FileSystem { } } -export function configureFileSystem(devices: { [name: string]: Device }, cb: BFSCallback): void { - DeviceFileSystem.Create({ devices: devices }, (e, dfs) => { - if (e) { - cb(e); - return; - } +export async function configureFileSystem(devices: { [name: string]: Device }): Promise { + const dfs = await new Promise((resolve, reject) => { + DeviceFileSystem.Create({ devices: devices }, (e, dfs) => e ? reject(e) : resolve(dfs)) + }); + const mfs = await new Promise((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 { + return new Promise((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 };