Skip to content

Commit

Permalink
refactor(event)!: improve event code (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Cui <[email protected]>
  • Loading branch information
BlackHole1 authored Jun 27, 2024
1 parent fe6e88b commit 17a594c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
"typescript": "^5.2.2"
},
"dependencies": {
"remitter": "^0.3.2"
"remitter": "^0.4.1"
}
}
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 15 additions & 20 deletions src/darwin.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import cp from "node:child_process";
import fs from "node:fs/promises";
import type { EventReceiver } from "remitter";
import { Remitter } from "remitter";
import { OVMDarwinStatusName } from "./type";
import type { OVMDarwinOptions, OVMDarwinEventData, OVMDarwinInfo, OVMDarwinState } from "./type";
import type { OVMDarwinEventData, OVMDarwinInfo, OVMDarwinOptions, OVMDarwinState } from "./type";
import { Restful } from "./event_restful";
import { RequestDarwin } from "./request";
import path from "node:path";
import { tmpdir } from "node:os";

export class DarwinOVM {
private readonly remitter = new Remitter<OVMDarwinEventData>();
public readonly events : EventReceiver<OVMDarwinEventData>;
readonly #events: Remitter<OVMDarwinEventData>;
private eventSocketPath: string;
private request: RequestDarwin;

private constructor(private options: OVMDarwinOptions) {}
private constructor(private options: OVMDarwinOptions) {
this.events = this.#events = new Remitter();
}

public static async create(options: OVMDarwinOptions): Promise<DarwinOVM> {
const ovm = new DarwinOVM(options);
Expand All @@ -25,18 +28,13 @@ export class DarwinOVM {
return ovm;
}

public on(event: keyof OVMDarwinEventData, listener: (datum: OVMDarwinEventData["status"]) => void): void {
this.remitter.on(event, listener);
}

private async initEventRestful(): Promise<void> {
const restful = new Restful((name, message) => {
if (name in OVMDarwinStatusName) {
this.remitter.emit("status", {
name: name as OVMDarwinStatusName,
message,
});
}
const restful = new Restful();

this.#events.remitAny((o) => {
return restful.events.onAny((data) => {
o.emit(data.event as keyof OVMDarwinEventData, data.data);
});
});

const dir = await fs.mkdtemp(path.join(tmpdir(), "ovm-"));
Expand Down Expand Up @@ -68,7 +66,7 @@ export class DarwinOVM {
reject();
}, 10 * 1000);

const disposer = this.remitter.once("status", () => {
const disposer = this.#events.onceAny(() => {
clearTimeout(id);
resolve();
});
Expand Down Expand Up @@ -102,10 +100,7 @@ export class DarwinOVM {

launchTimeout
.catch(() => {
this.remitter.emit("status", {
name: OVMDarwinStatusName.Error,
message: "OVM start timeout",
});
this.#events.emit("error", "OVM start timeout");
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/event_restful.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import url from "node:url";
import http from "node:http";
import { Remitter } from "remitter";


export class Restful {
private readonly server: http.Server;
public readonly events = new Remitter();

public constructor(
callback: (name: string, message: string) => void,
) {
public constructor() {
this.server = http.createServer((request, response) => {
if (!request.url) {
return;
Expand All @@ -18,7 +18,7 @@ export class Restful {
response.statusCode = 200;
response.end("ok");

callback(parsedUrl.query.event as string, parsedUrl.query.message as string);
this.events.emit(parsedUrl.query.event as string, parsedUrl.query.message as string);
} else {
response.statusCode = 404;
response.end("Not Found");
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const createDarwinOVM = (options: OVMDarwinOptions): Promise<DarwinOVM> =
};

export {
OVMDarwinStatusName,
OVMDarwinAppEventValue,
OVMDarwinVzState,
} from "./type";

Expand Down
9 changes: 3 additions & 6 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ export interface OVMDarwinOptions {
extendShareDir?: string,
}

export enum OVMDarwinStatusName {
export enum OVMDarwinAppEventValue {
Initializing = "Initializing",
GVProxyReady = "GVProxyReady",
IgnitionProgress = "IgnitionProgress",
IgnitionDone = "IgnitionDone",
VMReady = "VMReady",
Exit = "Exit",
Error = "Error",
}

export interface OVMDarwinEventData {
status: {
name: OVMDarwinStatusName,
message: string,
}
app: OVMDarwinAppEventValue,
error: string,
}

export interface OVMDarwinInfo {
Expand Down

0 comments on commit 17a594c

Please sign in to comment.