Skip to content

Commit

Permalink
feat: add events structure
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Dec 19, 2023
1 parent f588b7c commit d111dbe
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
24 changes: 18 additions & 6 deletions src/client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { mergeDeep } from "@rbxts/sift/out/Dictionary";
import { ClientDispatcher } from "./dispatcher";
import { DEFAULT_OPTIONS } from "./options";
import { ClientRegistry } from "./registry";
import { CommanderEvents } from "./types";

export namespace CommanderClient {
let started = false;
const registryInstance = new ClientRegistry();
const dispatcherInstance = new ClientDispatcher(registryInstance);
const events: CommanderEvents = {
historyUpdated: new Instance("BindableEvent"),
commandAdded: new Instance("BindableEvent"),
groupAdded: new Instance("BindableEvent"),
};
const registryInstance = new ClientRegistry(events);
const dispatcherInstance = new ClientDispatcher(registryInstance, events);
let optionsObject = DEFAULT_OPTIONS;

const IS_CLIENT = RunService.IsClient();
Expand Down Expand Up @@ -42,10 +48,16 @@ export namespace CommanderClient {
options.app({
options: optionsObject,
execute: (path, text) => dispatcherInstance.run(path, text),
commands: registryInstance.getCommandOptions(),
groups: registryInstance.getGroupOptions(),
history: dispatcherInstance.getHistory(),
onHistoryUpdated: dispatcherInstance.getHistorySignal(),
initialData: {
commands: registryInstance.getCommandOptions(),
groups: registryInstance.getGroupOptions(),
history: dispatcherInstance.getHistory(),
},
events: {
historyUpdated: events.historyUpdated.Event,
commandAdded: events.commandAdded.Event,
groupAdded: events.groupAdded.Event,
},
});
}
}
Expand Down
24 changes: 10 additions & 14 deletions src/client/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Players } from "@rbxts/services";
import { CommandPath } from "../shared";
import { BaseRegistry, CommandPath } from "../shared";
import { BaseDispatcher } from "../shared/core/dispatcher";
import { DEFAULT_HISTORY_LENGTH } from "./options";
import { ClientOptions, HistoryEntry } from "./types";
import { ClientOptions, CommanderEvents, HistoryEntry } from "./types";

export class ClientDispatcher extends BaseDispatcher {
private readonly history: HistoryEntry[] = [];
private readonly historyEvent = new Instance("BindableEvent");
private maxHistoryLength = DEFAULT_HISTORY_LENGTH;

constructor(
registry: BaseRegistry,
private readonly events: CommanderEvents,
) {
super(registry);
}

/**
* Initialises the client dispatcher.
*
Expand Down Expand Up @@ -60,22 +66,12 @@ export class ClientDispatcher extends BaseDispatcher {
return this.history;
}

/**
* Gets the history signal, which will be fired each time a new
* {@link HistoryEntry} is added.
*
* @returns The history signal
*/
getHistorySignal() {
return this.historyEvent.Event;
}

private addHistoryEntry(entry: HistoryEntry) {
if (this.history.size() >= this.maxHistoryLength) {
this.history.remove(0);
}

this.history.push(entry);
this.historyEvent.Fire(entry);
this.events.historyUpdated.Fire(entry);
}
}
21 changes: 11 additions & 10 deletions src/client/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import { CommandGroup } from "../shared/core/command";
import { BaseRegistry } from "../shared/core/registry";
import { remotes } from "../shared/network";
import { ServerCommand } from "./command";
import { CommanderEvents } from "./types";

export class ClientRegistry extends BaseRegistry {
private initialSyncReceived = false;

constructor(private readonly events: CommanderEvents) {
super();
}

init() {
this.registerBuiltInTypes();
}

async sync() {
let firstDispatch = false;
remotes.sync.dispatch.connect((data) => {
if (!firstDispatch) {
firstDispatch = true;
}

if (!this.initialSyncReceived) this.initialSyncReceived = true;
this.registerServerGroups(data.groups);
this.registerServerCommands(data.commands);
});
remotes.sync.start.fire();

return new Promise((resolve) => {
// Wait until dispatch has been received
while (!firstDispatch) {
RunService.Heartbeat.Wait();
}
while (!this.initialSyncReceived) RunService.Heartbeat.Wait();
resolve(undefined);
})
.timeout(5)
Expand All @@ -46,8 +47,8 @@ export class ClientRegistry extends BaseRegistry {

getGroupOptions() {
const groupMap = new Map<string, GroupOptions>();
for (const [k, v] of this.commands) {
groupMap.set(k, copyDeep(v.options as CommandOptions));
for (const [k, v] of this.groups) {
groupMap.set(k, copyDeep(v.options as GroupOptions));
}
return groupMap;
}
Expand Down
30 changes: 23 additions & 7 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@ import { CommandOptions, CommandPath, GroupOptions } from "../shared";
export interface ClientOptions {
historyLength?: number;
activationKeys?: Enum.KeyCode[];
app?: (data: AppData) => void;
app?: (data: AppContext) => void;
}

export interface AppData {
export interface CommanderEvents {
historyUpdated: BindableEvent<(entry: HistoryEntry) => void>;
commandAdded: BindableEvent<(command: CommandOptions) => void>;
groupAdded: BindableEvent<(group: GroupOptions) => void>;
}

export type CommanderEventCallbacks = {
[K in keyof CommanderEvents]: CommanderEvents[K] extends BindableEvent<
infer R
>
? RBXScriptSignal<R>
: never;
};

export type AppContext = {
options: ClientOptions;
execute: (path: CommandPath, text: string) => Promise<HistoryEntry>;
commands: Map<string, CommandOptions>;
groups: Map<string, GroupOptions>;
history: HistoryEntry[];
onHistoryUpdated: RBXScriptSignal<(entry: HistoryEntry) => void>;
}
initialData: {
commands: Map<string, CommandOptions>;
groups: Map<string, GroupOptions>;
history: HistoryEntry[];
};
events: CommanderEventCallbacks;
};

export interface HistoryEntry {
text: string;
Expand Down

0 comments on commit d111dbe

Please sign in to comment.