Skip to content

Commit b275a21

Browse files
Use async / await, like civilized people.
1 parent e310534 commit b275a21

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

example/src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Device, configureFileSystem, BFSCallback, Stats, File, FileFlag } from "webabi-kernel";
1+
import { Device, configureFileSystem, BFSCallback, Stats, File, FileFlag, FS, asyncRead, asyncWrite } from "webabi-kernel";
22

33
class JSaddleDevice implements Device {
44
open(flag: FileFlag, cb: BFSCallback<File>): void {
@@ -7,12 +7,14 @@ class JSaddleDevice implements Device {
77
}
88
}
99

10-
configureFileSystem({ "/jsaddle": new JSaddleDevice() }, (err, fs) => {
11-
console.log(err);
10+
async function main() {
11+
let fs = await configureFileSystem({ "/jsaddle": new JSaddleDevice() });
1212
let buf = Buffer.from("foo\n");
13-
fs.write(1, buf, 0, buf.length, null, () => {
14-
fs.read(0, buf, 0, 4, null, (err, n, buf) => {
15-
console.log({ err: err, n: n, buf: buf && buf.toString() });
16-
});
17-
});
13+
await asyncWrite(fs, 1, buf, 0, buf.length, null);
14+
const { byteLength } = await asyncRead(fs, 0, buf, 0, 4, null);
15+
console.log({ byteLength: byteLength, buffer: buf.toString() });
16+
}
17+
18+
main().catch(e => {
19+
console.error("Error: ", e);
1820
});

kernel/src/index.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,41 @@ export class DeviceFileSystem extends BaseFileSystem implements FileSystem {
6767
}
6868
}
6969

70-
export function configureFileSystem(devices: { [name: string]: Device }, cb: BFSCallback<FS>): void {
71-
DeviceFileSystem.Create({ devices: devices }, (e, dfs) => {
72-
if (e) {
73-
cb(e);
74-
return;
75-
}
70+
export async function configureFileSystem(devices: { [name: string]: Device }): Promise<FS> {
71+
const dfs = await new Promise<DeviceFileSystem>((resolve, reject) => {
72+
DeviceFileSystem.Create({ devices: devices }, (e, dfs) => e ? reject(e) : resolve(dfs))
73+
});
74+
const mfs = await new Promise<MountableFileSystem>((resolve, reject) => {
7675
MountableFileSystem.Create({
7776
"/dev": dfs
78-
}, (e, mfs) => {
79-
if (e) {
80-
cb(e);
81-
return
82-
}
83-
84-
const fs = new FS();
85-
fs.initialize(mfs);
86-
87-
const fdMap: {[id: number]: File} = (fs as any).fdMap;
88-
fdMap[0] = handles.stdin;
89-
fdMap[1] = handles.stdout;
90-
fdMap[2] = handles.stderr;
91-
92-
cb(undefined, fs);
93-
});
77+
}, (e, mfs) => e ? reject(e) : resolve(mfs));
78+
});
79+
80+
const fs = new FS();
81+
fs.initialize(mfs);
82+
83+
const fdMap: {[id: number]: File} = (fs as any).fdMap;
84+
fdMap[0] = handles.stdin;
85+
fdMap[1] = handles.stdout;
86+
fdMap[2] = handles.stderr;
87+
88+
return fs;
89+
}
90+
91+
export async function asyncRead(fs: FS, fd: number, buffer: Buffer, offset: number, length: number, position: number | null)
92+
: Promise<{ byteLength: number, buffer: Buffer }> {
93+
return new Promise<{ byteLength: number, buffer: Buffer }>((resolve, reject) => {
94+
fs.read(fd, buffer, offset, length, position,
95+
(err, n, buf) => err ? reject(err) : resolve({ byteLength: n, buffer: buf }));
96+
});
97+
}
98+
99+
export async function asyncWrite(fs: FS, fd: number, buffer: Buffer, offset: number, length: number, position: number | null)
100+
: Promise<void> {
101+
return new Promise<void>((resolve, reject) => {
102+
fs.write(fd, buffer, offset, length, position, e => e ? reject(e) : resolve())
94103
});
95104
}
96105

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

0 commit comments

Comments
 (0)