Skip to content

Commit

Permalink
refactor: freeze config
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Jul 31, 2024
1 parent 7b045cd commit 3df8be3
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/client/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import {
} from "../shared";
import { BaseCommand } from "../shared/core/command";
import { BaseRegistry } from "../shared/core/registry";
import { ReadonlyDeep } from "../shared/util/data";
import { ClientConfig, ClientRemotes } from "./types";

export class ServerCommand extends BaseCommand {
private readonly remote: ClientRemotes.Execute;

constructor(
config: ClientConfig,
config: ReadonlyDeep<ClientConfig>,
registry: BaseRegistry,
path: ImmutableRegistryPath,
options: CommandOptions,
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/client/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RunService } from "@rbxts/services";
import { DEFAULT_CONFIG } from "../shared/config";
import { getRemotes } from "../shared/network";
import { ObjectUtil, ReadonlyDeep } from "../shared/util/data";
import { ClientDispatcher } from "./dispatcher";
import { ClientRegistry } from "./registry";
import { ClientConfig } from "./types";
Expand All @@ -9,7 +10,7 @@ export class CenturionClient {
private started = false;
readonly registry: ClientRegistry;
readonly dispatcher: ClientDispatcher;
readonly config: ClientConfig;
readonly config: ReadonlyDeep<ClientConfig>;

constructor(config: Partial<ClientConfig> = {}) {
assert(
Expand All @@ -35,15 +36,15 @@ export class CenturionClient {
};
}

this.config = {
this.config = ObjectUtil.freezeDeep({
...DEFAULT_CONFIG,
historyLength: 1000,
registerBuiltInCommands: true,
shortcutsEnabled: true,
syncTimeout: 10,
network: networkConfig,
...config,
};
});
this.registry = new ClientRegistry(this.config);
this.dispatcher = new ClientDispatcher(this.config, this.registry);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/client/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { Players, UserInputService } from "@rbxts/services";
import { CommandShortcut, RegistryPath } from "../shared";
import { BaseCommand } from "../shared/core/command";
import { BaseDispatcher } from "../shared/core/dispatcher";
import { ReadonlyDeep } from "../shared/util/data";
import { getInputText } from "../shared/util/string";
import { ClientConfig, HistoryEntry } from "./types";
import { getShortcutKeycodes, isShortcutContext } from "./util";

export class ClientDispatcher extends BaseDispatcher<ClientConfig> {
export class ClientDispatcher extends BaseDispatcher<
ReadonlyDeep<ClientConfig>
> {
private readonly history: HistoryEntry[] = [];
readonly historyUpdated = new Signal<[history: HistoryEntry[]]>();

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/client/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { RunService } from "@rbxts/services";
import { CommandOptions, GroupOptions, ImmutableRegistryPath } from "../shared";
import { CommandGroup } from "../shared/core/command";
import { BaseRegistry } from "../shared/core/registry";
import { ReadonlyDeep } from "../shared/util/data";
import { CenturionLogLevel } from "../shared/util/log";
import { ServerCommand } from "./command";
import { ClientConfig } from "./types";

export class ClientRegistry extends BaseRegistry<ClientConfig> {
export class ClientRegistry extends BaseRegistry<ReadonlyDeep<ClientConfig>> {
private initialSyncReceived = false;
private syncedPaths = new Set<string>();

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandContextData, SharedConfig } from "../shared";
import { SyncData } from "../shared/network";
import { ReadonlyDeep } from "../shared/util/data";
import { ClientDispatcher } from "./dispatcher";
import { ClientRegistry } from "./registry";

Expand All @@ -15,7 +16,7 @@ export interface ClientConfig extends SharedConfig {
export interface ClientAPI {
registry: ClientRegistry;
dispatcher: ClientDispatcher;
config: ClientConfig;
config: ReadonlyDeep<ClientConfig>;
}

export interface ClientNetworkConfig {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/server/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RunService } from "@rbxts/services";
import { DEFAULT_CONFIG } from "../shared/config";
import { getRemotes } from "../shared/network";
import { ObjectUtil, ReadonlyDeep } from "../shared/util/data";
import { ServerDispatcher } from "./dispatcher";
import { ServerRegistry } from "./registry";
import { ServerConfig } from "./types";
Expand All @@ -9,7 +10,7 @@ export class CenturionServer {
private started = false;
readonly registry: ServerRegistry;
readonly dispatcher: ServerDispatcher;
readonly config: Readonly<ServerConfig>;
readonly config: ReadonlyDeep<ServerConfig>;

constructor(config: Partial<ServerConfig> = {}) {
assert(
Expand Down Expand Up @@ -37,7 +38,7 @@ export class CenturionServer {
};
}

this.config = table.freeze({
this.config = ObjectUtil.freezeDeep({
...DEFAULT_CONFIG,
network: networkConfig,
commandFilter: () => true,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/server/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { t } from "@rbxts/t";
import { CommandContext, CommandContextData, RegistryPath } from "../shared";
import { BaseDispatcher } from "../shared/core/dispatcher";
import { ReadonlyDeep } from "../shared/util/data";
import { getInputText } from "../shared/util/string";
import { ServerConfig } from "./types";

const isStringArray = t.array(t.string);

export class ServerDispatcher extends BaseDispatcher<ServerConfig> {
export class ServerDispatcher extends BaseDispatcher<
ReadonlyDeep<ServerConfig>
> {
/**
* Initializes the server dispatcher.
*
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/server/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { CommandOptions, GroupOptions } from "../shared";
import { BaseCommand, CommandGroup } from "../shared/core/command";
import { BaseRegistry } from "../shared/core/registry";
import { SyncData } from "../shared/network";
import { ReadonlyDeep } from "../shared/util/data";
import { ServerConfig } from "./types";

export class ServerRegistry extends BaseRegistry<ServerConfig> {
export class ServerRegistry extends BaseRegistry<ReadonlyDeep<ServerConfig>> {
/**
* Initializes the server registry.
*
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/shared/core/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GroupOptions,
SharedConfig,
} from "../types";
import { ObjectUtil, ReadonlyDeepObject } from "../util/data";
import { ObjectUtil, ReadonlyDeep, ReadonlyDeepObject } from "../util/data";
import { CenturionLogger } from "../util/log";
import { TransformResult } from "../util/type";
import { CommandContext } from "./context";
Expand All @@ -21,7 +21,7 @@ export abstract class BaseCommand {
readonly options: ReadonlyDeepObject<CommandOptions>;

constructor(
config: SharedConfig,
config: ReadonlyDeep<SharedConfig>,
registry: BaseRegistry,
path: ImmutableRegistryPath,
options: CommandOptions,
Expand Down Expand Up @@ -79,7 +79,7 @@ export class ExecutableCommand extends BaseCommand {
private readonly guards: ReadonlyArray<CommandGuard>;

constructor(
config: SharedConfig,
config: ReadonlyDeep<SharedConfig>,
registry: BaseRegistry,
path: ImmutableRegistryPath,
options: CommandOptions,
Expand Down Expand Up @@ -147,10 +147,10 @@ export class CommandGroup {
private readonly commands = new Map<string, BaseCommand>();
private readonly groups = new Map<string, CommandGroup>();
private readonly logger: CenturionLogger;
readonly options: ReadonlyDeepObject<GroupOptions>;
readonly options: ReadonlyDeep<GroupOptions>;

constructor(
config: SharedConfig,
config: ReadonlyDeep<SharedConfig>,
readonly path: ImmutableRegistryPath,
options: GroupOptions,
) {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/shared/core/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SharedConfig } from "../types";
import { ReadonlyDeep } from "../util/data";
import { CenturionLogLevel, CenturionLogger } from "../util/log";
import { getInputText } from "../util/string";
import { CommandContext } from "./context";
Expand All @@ -7,7 +8,9 @@ import { BaseRegistry } from "./registry";

const DEFAULT_REPLY_TEXT = "Command executed.";

export abstract class BaseDispatcher<C extends SharedConfig> {
export abstract class BaseDispatcher<
C extends ReadonlyDeep<SharedConfig> = ReadonlyDeep<SharedConfig>,
> {
protected logger: CenturionLogger;

constructor(
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/shared/core/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
RegisterOptions,
SharedConfig,
} from "../types";
import { ReadonlyDeep } from "../util/data";
import { CenturionLogger } from "../util/log";
import { MetadataReflect } from "../util/reflect";
import { BaseCommand, CommandGroup, ExecutableCommand } from "./command";
Expand All @@ -29,7 +30,9 @@ function isArgumentType(value: unknown): value is ArgumentType<unknown> {
return argTypeSchema(value);
}

export abstract class BaseRegistry<C extends SharedConfig = SharedConfig> {
export abstract class BaseRegistry<
C extends ReadonlyDeep<SharedConfig> = ReadonlyDeep<SharedConfig>,
> {
private static readonly ROOT_KEY = "__root__";
private readonly loadModule: (module: ModuleScript) => unknown;

Expand All @@ -46,7 +49,7 @@ export abstract class BaseRegistry<C extends SharedConfig = SharedConfig> {

constructor(protected readonly config: C) {
this.logger = new CenturionLogger(config.logLevel, "Registry");
this.globalGuards = config.guards ?? [];
this.globalGuards = [...config.guards] ?? [];

const tsImpl = (_G as Map<unknown, unknown>).get(script);
this.loadModule = t.interface({
Expand Down

0 comments on commit 3df8be3

Please sign in to comment.