this repo has no description

Merge pull request #203 from moonlight-mod/notnite/node-events

Changed files
+40 -16
packages
browser
src
core
src
extension
node-preload
src
types
src
web-preload
src
+4
packages/browser/src/index.ts
···
import { IndexedDB } from "@zenfs/dom";
import { configureSingle } from "@zenfs/core";
import * as fs from "@zenfs/core/promises";
+
import { NodeEventPayloads, NodeEventType } from "@moonlight-mod/types/core/event";
+
import { createEventEmitter } from "@moonlight-mod/core/util/event";
function getParts(path: string) {
if (path.startsWith("/")) path = path.substring(1);
···
processedExtensions,
nativesCache: {},
isBrowser: true,
+
events: createEventEmitter<NodeEventType, NodeEventPayloads>(),
version: MOONLIGHT_VERSION,
branch: MOONLIGHT_BRANCH as MoonlightBranch,
···
async writeConfig(newConfig) {
await writeConfig(newConfig);
config = newConfig;
+
this.events.dispatchEvent(NodeEventType.ConfigSaved, newConfig);
}
};
+5 -5
packages/core/src/extension/loader.ts
···
import calculateDependencies from "../util/dependency";
import { createEventEmitter } from "../util/event";
import { registerStyles } from "../styles";
-
import { EventPayloads, EventType } from "@moonlight-mod/types/core/event";
+
import { WebEventPayloads, WebEventType } from "@moonlight-mod/types/core/event";
const logger = new Logger("core/extension/loader");
···
}
export async function loadProcessedExtensions({ extensions, dependencyGraph }: ProcessedExtensions) {
-
const eventEmitter = createEventEmitter<EventType, EventPayloads>();
+
const eventEmitter = createEventEmitter<WebEventType, WebEventPayloads>();
const finished: Set<string> = new Set();
logger.trace(
···
}
function done() {
-
eventEmitter.removeEventListener(EventType.ExtensionLoad, cb);
+
eventEmitter.removeEventListener(WebEventType.ExtensionLoad, cb);
r();
}
-
eventEmitter.addEventListener(EventType.ExtensionLoad, cb);
+
eventEmitter.addEventListener(WebEventType.ExtensionLoad, cb);
if (finished.has(dep)) done();
})
);
···
await loadExt(ext);
finished.add(ext.id);
-
eventEmitter.dispatchEvent(EventType.ExtensionLoad, ext.id);
+
eventEmitter.dispatchEvent(WebEventType.ExtensionLoad, ext.id);
logger.debug(`Loaded "${ext.id}"`);
}
+3 -3
packages/core/src/patch.ts
···
} from "@moonlight-mod/types";
import Logger from "./util/logger";
import calculateDependencies, { Dependency } from "./util/dependency";
-
import { EventType } from "@moonlight-mod/types/core/event";
+
import { WebEventType } from "@moonlight-mod/types/core/event";
import { processFind, processReplace, testFind } from "./util/patch";
const logger = new Logger("core/patch");
···
const realPush = jsonp.push;
if (jsonp.push.__moonlight !== true) {
jsonp.push = (items) => {
-
moonlight.events.dispatchEvent(EventType.ChunkLoad, {
+
moonlight.events.dispatchEvent(WebEventType.ChunkLoad, {
chunkId: items[0],
modules: items[1],
require: items[2]
···
set(modules: any) {
const { stack } = new Error();
if (stack!.includes("/assets/") && !Array.isArray(modules)) {
-
moonlight.events.dispatchEvent(EventType.ChunkLoad, {
+
moonlight.events.dispatchEvent(WebEventType.ChunkLoad, {
modules: modules
});
patchModules(modules);
+4
packages/node-preload/src/index.ts
···
import createFS from "@moonlight-mod/core/fs";
import { registerCors, registerBlocked, getDynamicCors } from "@moonlight-mod/core/cors";
import { getConfig, getConfigOption, getManifest, setConfigOption } from "@moonlight-mod/core/util/config";
+
import { NodeEventPayloads, NodeEventType } from "@moonlight-mod/types/core/event";
+
import { createEventEmitter } from "@moonlight-mod/core/util/event";
let initialized = false;
let logger: Logger;
···
processedExtensions,
nativesCache: {},
isBrowser: false,
+
events: createEventEmitter<NodeEventType, NodeEventPayloads>(),
version: MOONLIGHT_VERSION,
branch: MOONLIGHT_BRANCH as MoonlightBranch,
···
async writeConfig(newConfig) {
await writeConfig(newConfig);
config = newConfig;
+
this.events.dispatchEvent(NodeEventType.ConfigSaved, newConfig);
},
getNatives: (ext: string) => global.moonlightNode.nativesCache[ext],
+13 -4
packages/types/src/core/event.ts
···
+
import { Config } from "../config";
import { WebpackModuleFunc, WebpackRequireType } from "../discord";
export interface MoonlightEventEmitter<EventId extends string = string, EventData = Record<EventId, any>> {
···
removeEventListener: <Id extends keyof EventData>(id: Id, cb: (data: EventData[Id]) => void) => void;
}
-
export enum EventType {
+
export enum WebEventType {
ChunkLoad = "chunkLoad",
ExtensionLoad = "extensionLoad"
}
-
export type EventPayloads = {
-
[EventType.ChunkLoad]: {
+
export type WebEventPayloads = {
+
[WebEventType.ChunkLoad]: {
chunkId?: number[];
modules: { [id: string]: WebpackModuleFunc };
require?: (require: WebpackRequireType) => any;
};
-
[EventType.ExtensionLoad]: string;
+
[WebEventType.ExtensionLoad]: string;
+
};
+
+
export enum NodeEventType {
+
ConfigSaved = "configSaved"
+
}
+
+
export type NodeEventPayloads = {
+
[NodeEventType.ConfigSaved]: Config;
};
+9 -2
packages/types/src/globals.ts
···
import type EventEmitter from "events";
import type LunAST from "@moonlight-mod/lunast";
import type Moonmap from "@moonlight-mod/moonmap";
-
import type { EventPayloads, EventType, MoonlightEventEmitter } from "./core/event";
+
import type {
+
WebEventPayloads,
+
WebEventType,
+
MoonlightEventEmitter,
+
NodeEventType,
+
NodeEventPayloads
+
} from "./core/event";
import { MoonlightFS } from "./fs";
export type MoonlightHost = {
···
processedExtensions: ProcessedExtensions;
nativesCache: Record<string, any>;
isBrowser: boolean;
+
events: MoonlightEventEmitter<NodeEventType, NodeEventPayloads>;
version: string;
branch: MoonlightBranch;
···
unpatched: Set<IdentifiedPatch>;
pendingModules: Set<IdentifiedWebpackModule>;
enabledExtensions: Set<string>;
-
events: MoonlightEventEmitter<EventType, EventPayloads>;
+
events: MoonlightEventEmitter<WebEventType, WebEventPayloads>;
patchingInternals: {
onModuleLoad: (moduleId: string | string[], callback: (moduleId: string) => void) => void;
registerPatch: (patch: IdentifiedPatch) => void;
+2 -2
packages/web-preload/src/index.ts
···
import Moonmap from "@moonlight-mod/moonmap";
import loadMappings from "@moonlight-mod/mappings";
import { createEventEmitter } from "@moonlight-mod/core/util/event";
-
import { EventPayloads, EventType } from "@moonlight-mod/types/core/event";
+
import { WebEventPayloads, WebEventType } from "@moonlight-mod/types/core/event";
async function load() {
delete window._moonlightWebLoad;
···
pendingModules: new Set(),
enabledExtensions: new Set(),
-
events: createEventEmitter<EventType, EventPayloads>(),
+
events: createEventEmitter<WebEventType, WebEventPayloads>(),
patchingInternals: {
onModuleLoad,
registerPatch,