Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Cdm2883 committed Jun 23, 2024
1 parent 95b2e13 commit 4131fc0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/bridgeo/plugin/register/plugin-register-logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PluginRegister from "@/bridgeo/plugin/register/plugin-register";
import { binding } from "@/bridgeo/utils/js/functions";
import { Logger } from "@/bridgeo/utils/js/logger";

export interface IPluginRegisterLogger {
Expand All @@ -8,10 +9,10 @@ export interface IPluginRegisterLogger {
}
export class PluginRegisterLogger extends PluginRegister<IPluginRegisterLogger> {
register(handler: IPluginRegisterLogger) {
const onLoggerRaw = handler.onLoggerRaw?.bind(handler);
const onLoggerRaw = binding(handler).onLoggerRaw;
if (onLoggerRaw) handler.logger.on('log.raw', onLoggerRaw);

const onLoggerLogging = handler.onLoggerLogging?.bind(handler);
const onLoggerLogging = binding(handler).onLoggerLogging;
if (onLoggerLogging) handler.logger.on('log', onLoggerLogging);

this.plugin.lifecycle.on('self.unload', () => {
Expand Down
15 changes: 9 additions & 6 deletions src/bridgeo/plugin/register/plugin-register-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PluginRegister from "@/bridgeo/plugin/register/plugin-register";
import { PacketOptionsStub, PacketsReceiver, PacketStub } from "@/bridgeo/relay/packet-bus";
import CommonRelay from "@/bridgeo/relay/relay";
import { RelayContext, RelayCreatingMaterial, relays } from "@/bridgeo/relay/starter";
import { binding } from "@/bridgeo/utils/js/functions";

export interface IPluginRegisterRelay extends PacketsReceiver {
onRelayCreating?(material: RelayCreatingMaterial): void;
Expand All @@ -13,10 +14,10 @@ export interface IPluginRegisterRelay extends PacketsReceiver {
}
export class PluginRegisterRelay extends PluginRegister<IPluginRegisterRelay> {
register(handler: IPluginRegisterRelay) {
const onRelayCreating = handler.onRelayCreating?.bind(handler);
const onRelayCreating = binding(handler).onRelayCreating;
if (onRelayCreating) this.plugin.lifecycle.on('relay.creating', onRelayCreating);

const onRelayCreated = handler.onRelayCreated?.bind(handler);
const onRelayCreated = binding(handler).onRelayCreated;
if (onRelayCreated) {
relays.forEach(relay => onRelayCreated(relay, {
class: relay.constructor as typeof CommonRelay,
Expand All @@ -40,13 +41,15 @@ export class PluginRegisterRelay extends PluginRegister<IPluginRegisterRelay> {

const onUpstream = handler.onUpstream?.bind(context);
if (onUpstream) {
context.client.on('upstream', onUpstream);
this.plugin.lifecycle.on('self.unload', () => context.client.off('upstream', onUpstream));
const args = [ 'upstream', onUpstream ] as const;
context.client.on(...args);
this.plugin.lifecycle.on('self.unload', () => context.client.off(...args));
}
const onDownstream = handler.onDownstream?.bind(context);
if (onDownstream) {
context.client.on('downstream', onDownstream);
this.plugin.lifecycle.on('self.unload', () => context.client.off('downstream', onDownstream));
const args = [ 'downstream', onDownstream ] as const;
context.client.on(...args);
this.plugin.lifecycle.on('self.unload', () => context.client.off(...args));
}
};
relays.flatMap(relay => Object.values(relay.clients))
Expand Down
17 changes: 9 additions & 8 deletions src/bridgeo/relay/flow/menu-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import form, {
DialogFormRequest
} from "@/bridgeo/relay/packets/form";
import { RelayContext } from "@/bridgeo/relay/starter";
import { binding } from "@/bridgeo/utils/js/functions";
import { ArrayIndexes } from "@/bridgeo/utils/js/type-utils";
import globalId from "@/bridgeo/utils/mc/global-id";

Expand All @@ -31,11 +32,11 @@ abstract class MenuForm<R> {
constructor(arg0: RequireQueue | RelayContext | Player, arg1: RequireOnce | undefined = undefined) {
if ((arg0 as RelayContext).relay) {
const context = arg0 as RelayContext;
this.#queue = context.client.queue.bind(context.client);
this.#queue = binding(context.client).queue;
this.#once = listener => context.packets.once('client.modal_form_response', listener);
} else if ((arg0 as Player).version) {
const player = arg0 as Player;
this.#queue = player.queue.bind(player);
this.#queue = binding(player).queue;
this.#once = listener => player.once('modal_form_response', listener);
} else {
this.#queue = arg0 as RequireQueue;
Expand Down Expand Up @@ -246,17 +247,17 @@ export class MenuCustomForm extends MenuForm<CustomFormResponse> {
// noinspection TypeScriptValidateTypes
block({
// @ts-expect-error
label: this.addLabel.bind(this),
label: binding(this).addLabel,
// @ts-expect-error
input: this.addInput.bind(this),
input: binding(this).addInput,
// @ts-expect-error
toggle: this.addToggle.bind(this),
toggle: binding(this).addToggle,
// @ts-expect-error
dropdown: this.addDropdown.bind(this),
dropdown: binding(this).addDropdown,
// @ts-expect-error
slider: this.addSlider.bind(this),
slider: binding(this).addSlider,
// @ts-expect-error
stepSlider: this.addStepSlider.bind(this),
stepSlider: binding(this).addStepSlider,
response: () => Promise.reject()
}).then().catch(_.noop);
/* eslint-enable @typescript-eslint/ban-ts-comment */
Expand Down
7 changes: 4 additions & 3 deletions src/bridgeo/relay/starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import notification from "@/bridgeo/relay/packets/notification";
import CommonRelay from "@/bridgeo/relay/relay";
import CommonRelayPlayer from "@/bridgeo/relay/relay-player";
import { BridgeoConfig, BridgeoConfigGenerated, generatedBridgeoConfig } from "@/bridgeo/utils/js/bridgeo-config";
import { binding } from "@/bridgeo/utils/js/functions";
import { Logger } from "@/bridgeo/utils/js/logger";
import { findFreeUdpPort } from "@/bridgeo/utils/js/port-utils";
import { mchalk } from "@/bridgeo/utils/mc/mc-formatter";
Expand Down Expand Up @@ -150,15 +151,15 @@ export async function createRelay(options: BridgeoRelayOptions) {
playing = false;
relay.options.disposable && relay.running && relay.clientCount === 0 && relay.close();
});
client.on('clientbound', context.packets.onClientBound.bind(context.packets));
client.on('serverbound', context.packets.onServerBound.bind(context.packets));
client.on('clientbound', binding(context.packets).onClientBound);
client.on('serverbound', binding(context.packets).onServerBound);
context.packets.register(context.local);
context.client.context = context;
setupRelayPlayer(context);
lifecycle.emit('relay.joined', context);
});

relay.on('error', logger.error.bind(logger));
relay.on('error', binding(logger).error);

relays.push(relay);
await relay.listen();
Expand Down
11 changes: 6 additions & 5 deletions src/bridgeo/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chalk from "chalk";

import { reloadPlugins, unloadPlugins } from "@/bridgeo/plugin/loader";
import { relays } from "@/bridgeo/relay/starter";
import { binding } from "@/bridgeo/utils/js/functions";
import { Logger, loggerPool } from "@/bridgeo/utils/js/logger";
import { typeCenter } from "@/bridgeo/utils/js/terminal-typography";
import { toAnsiColorFormat } from "@/bridgeo/utils/mc/mc-formatter";
Expand All @@ -23,11 +24,11 @@ const consoleReal = {
debug: console.debug,
};
const consoleLogger = new Logger('Console').inPool();
console.log = consoleLogger.info.bind(consoleLogger);
console.info = consoleLogger.info.bind(consoleLogger);
console.warn = consoleLogger.warn.bind(consoleLogger);
console.error = consoleLogger.error.bind(consoleLogger);
console.debug = consoleLogger.debug.bind(consoleLogger);
console.info = binding(consoleLogger).info;
console.warn = binding(consoleLogger).warn;
console.error = binding(consoleLogger).error;
console.debug = binding(consoleLogger).debug;
console.log = console.info;

export function initTerminal() {
loggerPool.on('log', (_, content) => {
Expand Down
10 changes: 10 additions & 0 deletions src/bridgeo/utils/js/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ export function mixinsClassInstance<T>(clazz: T, objects: any[], exclude: (strin
}
return clazz;
}

export function binding<T extends object | null | undefined>(receiver: T): T {
if (receiver === null || receiver === undefined) return receiver;
return new Proxy(receiver, {
get(...args) {
const value = Reflect.get(...args);
return typeof value === 'function' ? value.bind(receiver) : value;
}
});
}
22 changes: 11 additions & 11 deletions src/bridgeo/utils/js/json-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ export default function createJsonBinding<T extends object>(path: string, defaul
read();

const handler: ProxyHandler<T> = {
get(target, key, receiver) {
get(...args) {
if (dynamic) read();
const value = Reflect.get(target, key, receiver);
const value = Reflect.get(...args);
if (value === null) return value;
return typeof value === 'object' ? new Proxy(value as object, handler) : value;
},
set(target, key, value) {
const result = Reflect.set(target, key, value);
set(...args) {
const value = Reflect.set(...args);
write();
return result;
return value;
},
defineProperty(target: T, property: string | symbol, attributes: PropertyDescriptor) {
const result = Reflect.defineProperty(target, property, attributes);
defineProperty(...args) {
const value = Reflect.defineProperty(...args);
write();
return result;
return value;
},
deleteProperty(target: T, p: string | symbol) {
const result = Reflect.deleteProperty(target, p);
deleteProperty(...args) {
const value = Reflect.deleteProperty(...args);
write();
return result;
return value;
}
};
return new Proxy(json, handler);
Expand Down

0 comments on commit 4131fc0

Please sign in to comment.