Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenthoms committed Nov 11, 2024
1 parent 35b18aa commit 264524a
Show file tree
Hide file tree
Showing 23 changed files with 209 additions and 120 deletions.
2 changes: 1 addition & 1 deletion frontend/src/framework/WorkbenchSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class WorkbenchSession {
}
}

function createEnsembleRealizationFilterFuncForWorkbenchSession(workbenchSession: WorkbenchSession) {
export function createEnsembleRealizationFilterFuncForWorkbenchSession(workbenchSession: WorkbenchSession) {
return function ensembleRealizationFilterFunc(ensembleIdent: EnsembleIdent): readonly number[] {
const realizationFilterSet = workbenchSession.getRealizationFilterSet();
const realizationFilter = realizationFilterSet.getRealizationFilterForEnsembleIdent(ensembleIdent);
Expand Down
69 changes: 57 additions & 12 deletions frontend/src/modules/2DViewer/layers/LayerManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { WorkbenchSession, WorkbenchSessionEvent } from "@framework/WorkbenchSession";
import { Ensemble } from "@framework/Ensemble";
import {
EnsembleRealizationFilterFunction,
WorkbenchSession,
WorkbenchSessionEvent,
createEnsembleRealizationFilterFuncForWorkbenchSession,
} from "@framework/WorkbenchSession";
import { WorkbenchSettings } from "@framework/WorkbenchSettings";
import { QueryClient } from "@tanstack/react-query";

Expand All @@ -11,11 +17,11 @@ import { UnsubscribeHandlerDelegate } from "./delegates/UnsubscribeHandlerDelega
import { Group, Item, SerializedLayerManager } from "./interfaces";

export enum LayerManagerTopic {
ITEMS_CHANGED = "items-changed",
SETTINGS_CHANGED = "settings-changed",
AVAILABLE_SETTINGS_CHANGED = "available-settings-changed",
LAYER_DATA_REVISION = "layer-data-changed",
GLOBAL_SETTINGS_CHANGED = "global-settings-changed",
ITEMS_CHANGED = "ITEMS_CHANGED",
SETTINGS_CHANGED = "SETTINGS_CHANGED",
AVAILABLE_SETTINGS_CHANGED = "AVAILABLE_SETTINGS_CHANGED",
LAYER_DATA_REVISION = "LAYER_DATA_REVISION",
GLOBAL_SETTINGS_CHANGED = "GLOBAL_SETTINGS_CHANGED",
}

export type LayerManagerTopicPayload = {
Expand All @@ -28,6 +34,8 @@ export type LayerManagerTopicPayload = {

export type GlobalSettings = {
fieldId: string | null;
ensembles: readonly Ensemble[];
realizationFilterFunction: EnsembleRealizationFilterFunction;
};

export class LayerManager implements Group, PublishSubscribe<LayerManagerTopic, LayerManagerTopicPayload> {
Expand All @@ -38,24 +46,33 @@ export class LayerManager implements Group, PublishSubscribe<LayerManagerTopic,
private _publishSubscribeDelegate = new PublishSubscribeDelegate<LayerManagerTopic>();
private _itemDelegate: ItemDelegate;
private _layerDataRevision: number = 0;
private _globalSettings: GlobalSettings = {
fieldId: null,
};
private _globalSettings: GlobalSettings;
private _subscriptionsHandler = new UnsubscribeHandlerDelegate();
private _deserializing = false;

constructor(workbenchSession: WorkbenchSession, workbenchSettings: WorkbenchSettings, queryClient: QueryClient) {
this._workbenchSession = workbenchSession;
this._workbenchSettings = workbenchSettings;
this._queryClient = queryClient;
this._itemDelegate = new ItemDelegate("LayerManager", this);
this._groupDelegate = new GroupDelegate(this);

this._globalSettings = this.initializeGlobalSettings();

this._subscriptionsHandler.registerUnsubscribeFunction(
"workbenchSession",
this._workbenchSession.subscribe(
WorkbenchSessionEvent.EnsembleSetChanged,
this.handleEnsembleSetChanged.bind(this)
)
);
this._subscriptionsHandler.registerUnsubscribeFunction(
"workbenchSession",
this._workbenchSession.subscribe(
WorkbenchSessionEvent.RealizationFilterSetChanged,
this.handleRealizationFilterSetChanged.bind(this)
)
);
this._subscriptionsHandler.registerUnsubscribeFunction(
"groupDelegate",
this._groupDelegate
Expand All @@ -67,7 +84,27 @@ export class LayerManager implements Group, PublishSubscribe<LayerManagerTopic,
);
}

private initializeGlobalSettings(): GlobalSettings {
const ensembles = this._workbenchSession.getEnsembleSet().getEnsembleArr();
return {
fieldId: null,
ensembles,
realizationFilterFunction: createEnsembleRealizationFilterFuncForWorkbenchSession(this._workbenchSession),
};
}

private handleRealizationFilterSetChanged() {
this._globalSettings.realizationFilterFunction = createEnsembleRealizationFilterFuncForWorkbenchSession(
this._workbenchSession
);

this.publishTopic(LayerManagerTopic.GLOBAL_SETTINGS_CHANGED);
}

private handleEnsembleSetChanged() {
const ensembles = this._workbenchSession.getEnsembleSet().getEnsembleArr();
this._globalSettings.ensembles = ensembles;

this.publishTopic(LayerManagerTopic.GLOBAL_SETTINGS_CHANGED);
}

Expand All @@ -80,17 +117,23 @@ export class LayerManager implements Group, PublishSubscribe<LayerManagerTopic,
}

updateGlobalSetting<T extends keyof GlobalSettings>(key: T, value: GlobalSettings[T]): void {
if (!isEqual(this._globalSettings[key], value)) {
this._globalSettings[key] = value;
this._publishSubscribeDelegate.notifySubscribers(LayerManagerTopic.GLOBAL_SETTINGS_CHANGED);
if (isEqual(this._globalSettings[key], value)) {
return;
}

this._globalSettings[key] = value;
this.publishTopic(LayerManagerTopic.GLOBAL_SETTINGS_CHANGED);
}

getGlobalSetting<T extends keyof GlobalSettings>(key: T): GlobalSettings[T] {
return this._globalSettings[key];
}

publishTopic(topic: LayerManagerTopic): void {
if (this._deserializing) {
return;
}

if (topic === LayerManagerTopic.LAYER_DATA_REVISION) {
this._layerDataRevision++;
}
Expand Down Expand Up @@ -149,9 +192,11 @@ export class LayerManager implements Group, PublishSubscribe<LayerManagerTopic,
}

deserializeState(serializedState: SerializedLayerManager): void {
this._deserializing = true;
this._itemDelegate.setId(serializedState.id);
this._itemDelegate.setName(serializedState.name);
this._groupDelegate.deserializeChildren(serializedState.children);
this._deserializing = false;

this.publishTopic(LayerManagerTopic.ITEMS_CHANGED);
this.publishTopic(LayerManagerTopic.GLOBAL_SETTINGS_CHANGED);
Expand Down
36 changes: 16 additions & 20 deletions frontend/src/modules/2DViewer/layers/delegates/GroupDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group
private _publishSubscribeDelegate = new PublishSubscribeDelegate<GroupDelegateTopic>();
private _subscriptions: Map<string, Set<() => void>> = new Map();
private _treeRevisionNumber: number = 0;
private _deserializing = false;

constructor(owner: Item | null) {
this._owner = owner;
Expand All @@ -40,7 +41,7 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group

private incrementTreeRevisionNumber() {
this._treeRevisionNumber++;
this._publishSubscribeDelegate.notifySubscribers(GroupDelegateTopic.TREE_REVISION_NUMBER);
this.publishTopic(GroupDelegateTopic.TREE_REVISION_NUMBER);
}

protected takeOwnershipOfChild(child: Item) {
Expand All @@ -54,26 +55,12 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group
.getItemDelegate()
.getPublishSubscribeDelegate()
.makeSubscriberFunction(ItemDelegateTopic.EXPANDED)(() => {
this._publishSubscribeDelegate.notifySubscribers(GroupDelegateTopic.CHILDREN_EXPANSION_STATES);
this.publishTopic(GroupDelegateTopic.CHILDREN_EXPANSION_STATES);
})
);
}

if (instanceofGroup(child)) {
/*
for (const grandchild of child.getGroupDelegate().getChildren()) {
child.getGroupDelegate().takeOwnershipOfChild(grandchild);
}
*/

subscriptionSet.add(
child
.getGroupDelegate()
.getPublishSubscribeDelegate()
.makeSubscriberFunction(GroupDelegateTopic.CHILDREN)(() => {
this.incrementTreeRevisionNumber();
})
);
subscriptionSet.add(
child
.getGroupDelegate()
Expand All @@ -86,10 +73,17 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group

this._subscriptions.set(child.getItemDelegate().getId(), subscriptionSet);

this._publishSubscribeDelegate.notifySubscribers(GroupDelegateTopic.CHILDREN);
this.publishTopic(GroupDelegateTopic.CHILDREN);
this.incrementTreeRevisionNumber();
}

private publishTopic(topic: GroupDelegateTopic) {
if (this._deserializing) {
return;
}
this._publishSubscribeDelegate.notifySubscribers(topic);
}

private disposeOwnershipOfChild(child: Item) {
if (instanceofGroup(child)) {
const unsubscribeFuncs = this._subscriptions.get(child.getItemDelegate().getId());
Expand All @@ -106,7 +100,7 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group
this._owner?.getItemDelegate().getLayerManager().publishTopic(LayerManagerTopic.SETTINGS_CHANGED);
}

this._publishSubscribeDelegate.notifySubscribers(GroupDelegateTopic.CHILDREN);
this.publishTopic(GroupDelegateTopic.CHILDREN);
}

prependChild(child: Item) {
Expand Down Expand Up @@ -135,7 +129,7 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group
this.disposeOwnershipOfChild(child);
}
this._children = [];
this._publishSubscribeDelegate.notifySubscribers(GroupDelegateTopic.CHILDREN);
this.publishTopic(GroupDelegateTopic.CHILDREN);
this.incrementTreeRevisionNumber();
}

Expand All @@ -148,7 +142,7 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group
this._children = [...this._children.slice(0, currentIndex), ...this._children.slice(currentIndex + 1)];

this._children = [...this._children.slice(0, index), child, ...this._children.slice(index)];
this._publishSubscribeDelegate.notifySubscribers(GroupDelegateTopic.CHILDREN);
this.publishTopic(GroupDelegateTopic.CHILDREN);
this.incrementTreeRevisionNumber();
}

Expand Down Expand Up @@ -242,10 +236,12 @@ export class GroupDelegate implements PublishSubscribe<GroupDelegateTopic, Group
throw new Error("Owner not set");
}

this._deserializing = true;
const factory = new DeserializationFactory(this._owner.getItemDelegate().getLayerManager());
for (const child of children) {
const item = factory.makeItem(child);
this.appendChild(item);
}
this._deserializing = false;
}
}
13 changes: 13 additions & 0 deletions frontend/src/modules/2DViewer/layers/delegates/ItemDelegate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEqual } from "lodash";
import { v4 } from "uuid";

import { GroupDelegate } from "./GroupDelegate";
Expand Down Expand Up @@ -45,6 +46,10 @@ export class ItemDelegate implements PublishSubscribe<ItemDelegateTopic, ItemDel
}

setName(name: string): void {
if (isEqual(this._name, name)) {
return;
}

this._name = name;
this._publishSubscribeDelegate.notifySubscribers(ItemDelegateTopic.NAME);
if (this._layerManager) {
Expand All @@ -69,6 +74,10 @@ export class ItemDelegate implements PublishSubscribe<ItemDelegateTopic, ItemDel
}

setIsVisible(visible: boolean): void {
if (isEqual(this._visible, visible)) {
return;
}

this._visible = visible;
this._publishSubscribeDelegate.notifySubscribers(ItemDelegateTopic.VISIBILITY);
if (this._layerManager) {
Expand All @@ -81,6 +90,10 @@ export class ItemDelegate implements PublishSubscribe<ItemDelegateTopic, ItemDel
}

setIsExpanded(expanded: boolean): void {
if (isEqual(this._expanded, expanded)) {
return;
}

this._expanded = expanded;
this._publishSubscribeDelegate.notifySubscribers(ItemDelegateTopic.EXPANDED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export class LayerDelegate<TSettings extends Settings, TData>
}

setIsSubordinated(isSubordinated: boolean): void {
if (this._isSubordinated === isSubordinated) {
return;
}
this._isSubordinated = isSubordinated;
this._publishSubscribeDelegate.notifySubscribers(LayerDelegateTopic.SUBORDINATED);
}
Expand Down Expand Up @@ -197,6 +200,10 @@ export class LayerDelegate<TSettings extends Settings, TData>
}

private setStatus(status: LayerStatus): void {
if (this._status === status) {
return;
}

this._status = status;
this._layerManager?.publishTopic(LayerManagerTopic.LAYER_DATA_REVISION);
this._publishSubscribeDelegate.notifySubscribers(LayerDelegateTopic.STATUS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class PublishSubscribeDelegate<TTopic extends string> {

notifySubscribers(topic: TTopic): void {
const subscribers = this._subscribers.get(topic);
console.debug(topic);
if (subscribers) {
subscribers.forEach((subscriber) => subscriber());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class SettingDelegate<TValue> implements PublishSubscribe<SettingTopic, S
private _value: TValue;
private _isValueValid: boolean = false;
private _publishSubscribeDelegate = new PublishSubscribeDelegate<SettingTopic>();
private _availableValues: AvailableValuesType<TValue> = [] as AvailableValuesType<TValue>;
private _availableValues: AvailableValuesType<TValue> = [] as unknown as AvailableValuesType<TValue>;
private _overriddenValue: TValue | undefined = undefined;
private _loading: boolean = false;
private _initialized: boolean = false;
Expand Down Expand Up @@ -176,6 +176,10 @@ export class SettingDelegate<TValue> implements PublishSubscribe<SettingTopic, S
this._overriddenValue = overriddenValue;
this._publishSubscribeDelegate.notifySubscribers(SettingTopic.OVERRIDDEN_CHANGED);

if (this._overriddenValue === undefined) {
this.maybeFixupValue();
}

this.checkIfValueIsValid();

if (prevValue === undefined && overriddenValue !== undefined && isEqual(this._value, overriddenValue)) {
Expand Down
Loading

0 comments on commit 264524a

Please sign in to comment.