Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Sep 9, 2024
1 parent c5e83ec commit 976a0e2
Show file tree
Hide file tree
Showing 24 changed files with 508 additions and 238 deletions.
59 changes: 48 additions & 11 deletions frontend/src/modules/LayerSpike/layers/Broker.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
import { WorkbenchServices } from "@framework/WorkbenchServices";
import { WorkbenchSession } from "@framework/WorkbenchSession";
import { Message, MessageDirection } from "./Message";

export class Broker {
private _workbenchServices: WorkbenchServices;
private _workbenchSession: WorkbenchSession;
private _parentBroker: Broker | null = null;
private _childrenBrokers: Broker[] = [];
private _messageCallback: ((message: Message) => void) | null = null;

constructor(workbenchServices: WorkbenchServices, workbenchSession: WorkbenchSession) {
this._workbenchServices = workbenchServices;
this._workbenchSession = workbenchSession;
constructor(parent: Broker | null) {
this._parentBroker = parent;
}

getWorkbenchServices(): WorkbenchServices {
return this._workbenchServices;
setParent(parent: Broker | null) {
this._parentBroker = parent;
}

getWorkbenchSession(): WorkbenchSession {
return this._workbenchSession;
setChildren(children: Broker[]) {
this._childrenBrokers = children;
}

addChild(child: Broker) {
this._childrenBrokers.push(child);
}

removeChild(child: Broker) {
this._childrenBrokers = this._childrenBrokers.filter((broker) => broker !== child);
}

emit(message: Message) {
this.callCallback(message);

if (message.isPropagationStopped()) {
return;
}

if (message.getDirection() === MessageDirection.UP && this._parentBroker) {
this._parentBroker.emit(message);
return;
}

if (message.getDirection() === MessageDirection.DOWN) {
for (const child of this._childrenBrokers) {
child.emit(message);
}
}
}

onMessage(callback: (message: Message) => void) {
this._messageCallback = callback;
}

callCallback(message: Message): void {
const callback = this._messageCallback;
if (callback) {
callback(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import React from "react";

import { v4 } from "uuid";

import { Broker } from "./Broker";
import { LayerManager } from "./LayerManager";
import { Message, MessageDirection, MessageType } from "./Message";
import { PublishSubscribe, PublishSubscribeHandler } from "./PublishSubscribeHandler";
import { Item, instanceofGroup } from "./interfaces";
import { Item, instanceofGroup, instanceofLayer } from "./interfaces";

export enum GroupBaseTopic {
CHILDREN_CHANGED = "CHILDREN_CHANGED",
Expand All @@ -13,35 +16,76 @@ export type GroupBaseTopicPayloads = {
[GroupBaseTopic.CHILDREN_CHANGED]: Item[];
};

export class GroupHandler implements Item, PublishSubscribe<GroupBaseTopic, GroupBaseTopicPayloads> {
export class GroupDelegate implements Item, PublishSubscribe<GroupBaseTopic, GroupBaseTopicPayloads> {
private _children: Item[] = [];
private _id: string;
private _manager: LayerManager;
private _broker: Broker;
private _publishSubscribeHandler = new PublishSubscribeHandler<GroupBaseTopic>();

constructor() {
constructor(manager: LayerManager) {
this._id = v4();
this._broker = new Broker(null);
this._manager = manager;

this._broker.onMessage(this.handleAvailableSettingsChanged.bind(this));
}

private handleAvailableSettingsChanged(message: Message) {
if (message.getType() === MessageType.AVAILABLE_SETTINGS_CHANGED) {
if (message.getDirection() === MessageDirection.DOWN) {
message.stopPropagation();
return;
}

this._broker.emit(new Message(MessageType.AVAILABLE_SETTINGS_CHANGED, MessageDirection.DOWN));
}
}

getId() {
return this._id;
}

getBroker() {
return this._broker;
}

private setBrokerAndManagerOfChild(child: Item) {
child.getBroker().setParent(this._broker);
this._broker.addChild(child.getBroker());
if (instanceofLayer(child)) {
child.getLayerDelegate().setLayerManager(this._manager);
}
}

private removeBrokerAndManagerOfChild(child: Item) {
child.getBroker().setParent(null);
this._broker.removeChild(child.getBroker());
if (instanceofLayer(child)) {
child.getLayerDelegate().setLayerManager(null);
}
}

prependChild(child: Item) {
this.setBrokerAndManagerOfChild(child);
this._children = [child, ...this._children];
this._publishSubscribeHandler.notifySubscribers(GroupBaseTopic.CHILDREN_CHANGED);
}

appendChild(child: Item) {
this.setBrokerAndManagerOfChild(child);
this._children = [...this._children, child];
this._publishSubscribeHandler.notifySubscribers(GroupBaseTopic.CHILDREN_CHANGED);
}

insertChild(child: Item, index: number) {
this.setBrokerAndManagerOfChild(child);
this._children = [...this._children.slice(0, index), child, ...this._children.slice(index)];
this._publishSubscribeHandler.notifySubscribers(GroupBaseTopic.CHILDREN_CHANGED);
}

removeChild(child: Item) {
this.removeBrokerAndManagerOfChild(child);
this._children = this._children.filter((c) => c !== child);
this._publishSubscribeHandler.notifySubscribers(GroupBaseTopic.CHILDREN_CHANGED);
}
Expand Down Expand Up @@ -69,7 +113,7 @@ export class GroupHandler implements Item, PublishSubscribe<GroupBaseTopic, Grou
}

if (instanceofGroup(child)) {
const descendant = child.getGroupHandler().findDescendantById(id);
const descendant = child.getGroupDelegate().findDescendantById(id);
if (descendant) {
return descendant;
}
Expand All @@ -95,7 +139,7 @@ export class GroupHandler implements Item, PublishSubscribe<GroupBaseTopic, Grou
}

export function useGroupBaseTopicValue<T extends GroupBaseTopic>(
layerGroup: GroupHandler,
layerGroup: GroupDelegate,
topic: T
): GroupBaseTopicPayloads[T] {
const value = React.useSyncExternalStore<GroupBaseTopicPayloads[T]>(
Expand Down
27 changes: 0 additions & 27 deletions frontend/src/modules/LayerSpike/layers/LayerBase.ts

This file was deleted.

53 changes: 53 additions & 0 deletions frontend/src/modules/LayerSpike/layers/LayerDelegate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { v4 } from "uuid";

import { Broker } from "./Broker";
import { LayerManager } from "./LayerManager";
import { Message, MessageDirection, MessageType } from "./Message";
import { SettingsContextDelegateTopic } from "./SettingsContextDelegate";
import { Item, Settings, SettingsContext } from "./interfaces";

export class LayerDelegate<TSettings extends Settings> implements Item {
private _name: string;
private _id: string;
private _broker: Broker = new Broker(null);
private _settingsContext: SettingsContext<TSettings>;
private _layerManager: LayerManager | null = null;

constructor(name: string, settingsContext: SettingsContext<TSettings>) {
this._id = v4();
this._name = name;
this._settingsContext = settingsContext;
this._settingsContext.getDelegate().makeSubscriberFunction(SettingsContextDelegateTopic.SETTINGS_CHANGED)(
() => {
this._broker.emit(new Message(MessageType.SETTINGS_CHANGED, MessageDirection.UP));
}
);
}

getBroker(): Broker {
return this._broker;
}

getId(): string {
return this._id;
}

getName(): string {
return this._name;
}

getSettingsContext(): SettingsContext<TSettings> {
return this._settingsContext;
}

setLayerManager(layerManager: LayerManager | null): void {
this._layerManager = layerManager;
}

getLayerManager(): LayerManager {
if (this._layerManager === null) {
throw new Error("LayerManager not set");
}
return this._layerManager;
}
}
33 changes: 33 additions & 0 deletions frontend/src/modules/LayerSpike/layers/LayerManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { WorkbenchSession } from "@framework/WorkbenchSession";
import { WorkbenchSettings } from "@framework/WorkbenchSettings";

import { GroupDelegate } from "./GroupDelegate";
import { View } from "./View";

export class LayerManager {
private _workbenchSession: WorkbenchSession;
private _workbenchSettings: WorkbenchSettings;
private _groupDelegate: GroupDelegate;

constructor(workbenchSession: WorkbenchSession, workbenchSettings: WorkbenchSettings) {
this._workbenchSession = workbenchSession;
this._workbenchSettings = workbenchSettings;
this._groupDelegate = new GroupDelegate(this);
}

getWorkbenchSession(): WorkbenchSession {
return this._workbenchSession;
}

getWorkbenchSettings(): WorkbenchSettings {
return this._workbenchSettings;
}

getGroupDelegate(): GroupDelegate {
return this._groupDelegate;
}

makeView(name: string): View {
return new View(this, name);
}
}
36 changes: 36 additions & 0 deletions frontend/src/modules/LayerSpike/layers/Message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export enum MessageDirection {
UP = "UP",
DOWN = "DOWN",
}

export enum MessageType {
SETTINGS_CHANGED = "SETTINGS_CHANGED",
AVAILABLE_SETTINGS_CHANGED = "AVAILABLE_SETTINGS_CHANGED",
}

export class Message {
private _direction: MessageDirection;
private _type: MessageType;
private _propagationStopped: boolean = false;

constructor(type: MessageType, direction: MessageDirection) {
this._type = type;
this._direction = direction;
}

getType() {
return this._type;
}

getDirection() {
return this._direction;
}

stopPropagation() {
this._propagationStopped = true;
}

isPropagationStopped() {
return this._propagationStopped;
}
}
53 changes: 0 additions & 53 deletions frontend/src/modules/LayerSpike/layers/SettingsContext.ts

This file was deleted.

Loading

0 comments on commit 976a0e2

Please sign in to comment.