Skip to content

Commit

Permalink
Replaced function arg deconstruction with options pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Jan 23, 2024
1 parent f0d7eff commit 8b8d9ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
42 changes: 18 additions & 24 deletions frontend/src/framework/Module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export enum ImportState {
Failed = "Failed",
}

export interface ModuleOptions {
name: string;
defaultTitle: string;
syncableSettingKeys?: SyncSettingKey[];
drawPreviewFunc?: DrawPreviewFunc;
description?: string;
channelDefinitions?: ChannelDefinition[];
channelReceiverDefinitions?: ChannelReceiverDefinition[];
}

export class Module<StateType extends StateBaseType> {
private _name: string;
private _defaultTitle: string;
Expand All @@ -47,36 +57,20 @@ export class Module<StateType extends StateBaseType> {
private _channelDefinitions: ChannelDefinition[] | null;
private _channelReceiverDefinitions: ChannelReceiverDefinition[] | null;

constructor({
name,
defaultTitle,
syncableSettingKeys,
drawPreviewFunc,
description,
channelDefinitions,
channelReceiverDefinitions,
}: {
name: string;
defaultTitle: string;
syncableSettingKeys?: SyncSettingKey[];
drawPreviewFunc?: DrawPreviewFunc;
description?: string;
channelDefinitions?: ChannelDefinition[];
channelReceiverDefinitions?: ChannelReceiverDefinition[];
}) {
this._name = name;
this._defaultTitle = defaultTitle;
constructor(options: ModuleOptions) {
this._name = options.name;
this._defaultTitle = options.defaultTitle;
this.viewFC = () => <div>Not defined</div>;
this.settingsFC = () => <div>Not defined</div>;
this._importState = ImportState.NotImported;
this._moduleInstances = [];
this._defaultState = null;
this._workbench = null;
this._syncableSettingKeys = syncableSettingKeys ?? [];
this._drawPreviewFunc = drawPreviewFunc ?? null;
this._description = description ?? null;
this._channelDefinitions = channelDefinitions ?? null;
this._channelReceiverDefinitions = channelReceiverDefinitions ?? null;
this._syncableSettingKeys = options.syncableSettingKeys ?? [];
this._drawPreviewFunc = options.drawPreviewFunc ?? null;
this._description = options.description ?? null;
this._channelDefinitions = options.channelDefinitions ?? null;
this._channelReceiverDefinitions = options.channelReceiverDefinitions ?? null;
}

getDrawPreviewFunc(): DrawPreviewFunc | null {
Expand Down
33 changes: 15 additions & 18 deletions frontend/src/framework/ModuleInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export enum ModuleInstanceState {
RESETTING,
}

export interface ModuleInstanceOptions<StateType extends StateBaseType> {
module: Module<StateType>;
instanceNumber: number;
channelDefinitions: ChannelDefinition[] | null;
channelReceiverDefinitions: ChannelReceiverDefinition[] | null;
}

export class ModuleInstance<StateType extends StateBaseType> {
private _id: string;
private _title: string;
Expand All @@ -38,21 +45,11 @@ export class ModuleInstance<StateType extends StateBaseType> {
private _statusController: ModuleInstanceStatusControllerInternal;
private _channelManager: ChannelManager;

constructor({
module,
instanceNumber,
channelDefinitions,
channelReceiverDefinitions,
}: {
module: Module<StateType>;
instanceNumber: number;
channelDefinitions: ChannelDefinition[] | null;
channelReceiverDefinitions: ChannelReceiverDefinition[] | null;
}) {
this._id = `${module.getName()}-${instanceNumber}`;
this._title = module.getDefaultTitle();
constructor(options: ModuleInstanceOptions<StateType>) {
this._id = `${options.module.getName()}-${options.instanceNumber}`;
this._title = options.module.getDefaultTitle();
this._stateStore = null;
this._module = module;
this._module = options.module;
this._importStateSubscribers = new Set();
this._context = null;
this._initialised = false;
Expand All @@ -68,17 +65,17 @@ export class ModuleInstance<StateType extends StateBaseType> {

this._channelManager = new ChannelManager(this._id);

if (channelReceiverDefinitions) {
if (options.channelReceiverDefinitions) {
this._channelManager.registerReceivers(
channelReceiverDefinitions.map((el) => ({
options.channelReceiverDefinitions.map((el) => ({
...el,
supportsMultiContents: el.supportsMultiContents ?? false,
}))
);
}

if (channelDefinitions) {
this._channelManager.registerChannels(channelDefinitions);
if (options.channelDefinitions) {
this._channelManager.registerChannels(options.channelDefinitions);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { KeyKind } from "@framework/DataChannelTypes";

import { Channel, ChannelNotificationTopic } from "./Channel";
import { ChannelManager } from "./ChannelManager";

import { KeyKind } from "../../DataChannelTypes";

export interface ChannelReceiverDefinition {
readonly idString: string;
readonly displayName: string;
Expand Down

0 comments on commit 8b8d9ad

Please sign in to comment.