Skip to content

Commit

Permalink
refactor(event)!: improve event (#154)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Cui <[email protected]>
  • Loading branch information
BlackHole1 authored Jan 7, 2025
1 parent cce0a23 commit 5faaf55
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 75 deletions.
12 changes: 6 additions & 6 deletions scripts/resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"darwin-x64": [
{
"name": "ovm",
"version": "v1.0.0-RC8",
"version": "v1.0.0-RC10",
"download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/ovm-amd64",
"sha256": "09d3ba46ec931658090b9a351482a50982bffaf01a98ab39b05eb48b31ab7855",
"sha256": "86d7adfca2310ea112c919fbb453d940d3474cb2d03ffe9612730d5509c61336",
"out": "bin/ovm"
},
{
Expand All @@ -25,9 +25,9 @@
"darwin-arm64": [
{
"name": "ovm",
"version": "v1.0.0-RC8",
"version": "v1.0.0-RC10",
"download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/ovm-arm64",
"sha256": "fb660439b2f9240821302baaf397ef7431a65a5184d142311b582e820d723b02",
"sha256": "4db337614bc079005bda2545d381963c777da1dce5b19c29f1d5834ca2df5886",
"out": "bin/ovm"
},
{
Expand All @@ -48,9 +48,9 @@
"win32-x64": [
{
"name": "ovm",
"version": "v1.0.0-RC15",
"version": "v1.0.0-RC16",
"download": "https://static.oomol.com/ovm-resources/ovm-win/{version}/ovm-amd64.exe",
"sha256": "c6e1ff99d63cd3ebf5ffa9ce175db66b65d27270896964d162eb2091c3c03470",
"sha256": "4a4a01ea8dcbf7c5bd9ec65cb91a90d5aa0a0fb2c2f918d278a09afb893973ae",
"out": "ovm.exe"
},
{
Expand Down
46 changes: 29 additions & 17 deletions src/darwin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { RequestDarwin } from "./request";
import cp from "node:child_process";
import type { EventReceiver } from "remitter";
import { Remitter } from "remitter";
import type { OVMDarwinInitEvent, OVMDarwinOptions, OVMDarwinStartEvent } from "./type";
import {
OVMDarwinInitEventValue,
OVMDarwinRunEventValue,
type OVMDarwinInitEvent,
type OVMDarwinInitEventValueType,
type OVMDarwinOptions,
type OVMDarwinRunEvent,
type OVMDarwinRunEventValueType,
} from "./type";
import { Restful } from "./event_restful";
import fs from "node:fs/promises";
import path from "node:path";
Expand All @@ -12,22 +20,22 @@ import { enableDebug, resource } from "./utils";
export class DarwinOVM extends RequestDarwin {
public readonly events: {
init: EventReceiver<OVMDarwinInitEvent>;
start: EventReceiver<OVMDarwinStartEvent>;
run: EventReceiver<OVMDarwinRunEvent>;
};
readonly #events: {
init: Remitter<OVMDarwinInitEvent>;
start: Remitter<OVMDarwinStartEvent>;
run: Remitter<OVMDarwinRunEvent>;
};

private restful: Record<"init" | "start", Restful>;
private restful: Record<"init" | "run", Restful>;
private restfulWithInit: string;
private restfulWithStart: string;
private restfulWithRun: string;

private constructor(private options: OVMDarwinOptions) {
super(options.workspace);
this.events = this.#events = {
init: new Remitter(),
start: new Remitter(),
run: new Remitter(),
};
}

Expand All @@ -45,27 +53,27 @@ export class DarwinOVM extends RequestDarwin {
private async initEventRestful(): Promise<void> {
this.restful = {
init: new Restful(),
start: new Restful(),
run: new Restful(),
};

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

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

const dir = await fs.mkdtemp(path.join(tmpdir(), "ovm-"));
this.restfulWithInit = path.join(dir, "event-restful-init.sock");
this.restfulWithStart = path.join(dir, "event-restful-start.sock");
this.restfulWithRun = path.join(dir, "event-restful-run.sock");

this.restful.init.start(this.restfulWithInit);
this.restful.start.start(this.restfulWithStart);
this.restful.run.start(this.restfulWithRun);
}

public init(): void {
Expand Down Expand Up @@ -118,7 +126,9 @@ export class DarwinOVM extends RequestDarwin {

initTimeout
.catch(() => {
this.#events.init.emit("error", "OVM init timeout");
this.#events.init.emit(OVMDarwinInitEventValue.Error, {
value: "OVM init timeout",
});
});

}
Expand All @@ -131,7 +141,7 @@ export class DarwinOVM extends RequestDarwin {
reject();
}, 10 * 1000);

const disposer = this.#events.start.onceAny(() => {
const disposer = this.#events.run.onceAny(() => {
clearTimeout(id);
resolve();
});
Expand All @@ -141,7 +151,7 @@ export class DarwinOVM extends RequestDarwin {
const ovmArgs = [
"machine",
"start",
"--report-url", `unix://${this.restfulWithStart}`,
"--report-url", `unix://${this.restfulWithRun}`,
"--workspace", this.options.workspace,
"--ppid", String(this.options.bindPID),
"default",
Expand All @@ -163,7 +173,9 @@ export class DarwinOVM extends RequestDarwin {

startTimeout
.catch(() => {
this.#events.start.emit("error", "OVM start timeout");
this.#events.run.emit(OVMDarwinRunEventValue.Error, {
value: "OVM run timeout",
});
});
}
}
4 changes: 3 additions & 1 deletion src/event_restful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export class Restful {
response.statusCode = 200;
response.end("ok");

this.events.emit(parsedUrl.query.event as string, parsedUrl.query.message as string);
this.events.emit(parsedUrl.query.name as string, {
value: parsedUrl.query.value as string,
});
} else {
response.statusCode = 404;
response.end("Not Found");
Expand Down
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ export const createWindowsOVM = (options: OVMWindowsOptions): WindowsOVM => {
return WindowsOVM.create(options);
};

export { OVMWindowsRunEventValue, OVMWindowsPrepareEventValue } from "./type";
export {
OVMDarwinInitEventValue,
OVMDarwinRunEventValue,
OVMWindowsInitEventValue,
OVMWindowsRunEventValue,
} from "./type";

export type {
OVMDarwinOptions,
OVMDarwinInitEventValueType,
OVMDarwinRunEventValueType,
OVMDarwinInitEvent,
OVMDarwinStartEvent,
OVMDarwinRunEvent,
OVMDarwinInfo,
OVMDarwinState,
OVMWindowsOptions,
OVMWindowsEventData,
OVMWindowsInitEventValueType,
OVMWindowsRunEventValueType,
OVMWindowsInitEvent,
OVMWindowsRunEvent,
OVMWindowsInfo,
} from "./type";
export type { DarwinOVM, WindowsOVM };
65 changes: 39 additions & 26 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ export interface OVMDarwinOptions {
appendVolume?: string[];
}

export interface OVMDarwinInitEvent {
decompress: "running" | "success";
writeConfig: "running" | "success";
exit: void;
error: string;
export type OVMDarwinInitEventValueType = keyof typeof OVMDarwinInitEventValue;

export enum OVMDarwinInitEventValue {
Error = "Error",
Exit = "Exit",
}

export type OVMDarwinRunEventValueType = keyof typeof OVMDarwinRunEventValue;

export enum OVMDarwinRunEventValue {
Ready = "Ready",
Error = "Error",
Exit = "Exit",
}

export interface OVMDarwinStartEvent {
start: string;
ready: void;
exit: void;
error: string;
export type OVMDarwinInitEvent = {
[k in OVMDarwinInitEventValueType]: {
value?: string;
}
}

export type OVMDarwinRunEvent = {
[k in OVMDarwinRunEventValueType]: {
value?: string;
}
}

export interface OVMDarwinInfo {
Expand Down Expand Up @@ -69,9 +82,9 @@ export interface OVMWindowsOptions {
cwd: string;
}

export type OVMWindowsPrepareEventValueType = keyof typeof OVMWindowsPrepareEventValue;
export type OVMWindowsInitEventValueType = keyof typeof OVMWindowsInitEventValue;

export enum OVMWindowsPrepareEventValue {
export enum OVMWindowsInitEventValue {
SystemNotSupport = "SystemNotSupport",

NotSupportVirtualization = "NotSupportVirtualization",
Expand All @@ -85,27 +98,27 @@ export enum OVMWindowsPrepareEventValue {
UpdatingWSL = "UpdatingWSL",
UpdateWSLFailed = "UpdateWSLFailed",
UpdateWSLSuccess = "UpdateWSLSuccess",
Exit = "Exit",
Error = "Error",
}

export type OVMWindowsRunEventValueType = keyof typeof OVMWindowsRunEventValue;

export enum OVMWindowsRunEventValue {
UpdatingRootFS = "UpdatingRootFS",
UpdateRootFSFailed = "UpdateRootFSFailed",
UpdateRootFSSuccess = "UpdateRootFSSuccess",

UpdatingData = "UpdatingData",
UpdateDataFailed = "UpdateDataFailed",
UpdateDataSuccess = "UpdateDataSuccess",

Starting = "Starting",
Ready = "Ready",
Exit = "Exit",
Error = "Error",
}

export type OVMWindowsInitEvent = {
[k in OVMWindowsInitEventValueType]: {
value?: string;
}
}

export interface OVMWindowsEventData {
run: OVMWindowsRunEventValue,
prepare: OVMWindowsPrepareEventValue,
error: string,
exit: void,
export type OVMWindowsRunEvent = {
[k in OVMWindowsRunEventValueType]: {
value?: string;
}
}

Loading

0 comments on commit 5faaf55

Please sign in to comment.