Mostly aligned subscription messaging pattern

This commit is contained in:
itdominator 2025-06-18 22:35:28 -05:00
parent 5c7dff5d2b
commit 53bbfe2bc7
9 changed files with 127 additions and 173 deletions

View File

@ -45,11 +45,11 @@ export const Keybindings: Array<{}> = [
bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"},
readOnly: false
}, {
name: "selectSessionLeft",
name: "selectLeftEditor",
bindKey: {win: "ctrl-pageup", mac: "ctrl-pageup"},
readOnly: false
}, {
name: "selectSessionRight",
name: "selectRightEditor",
bindKey: {win: "ctrl-pagedown", mac: "ctrl-pagedown"},
readOnly: false
}, {

View File

@ -13,13 +13,6 @@ import { EditorSettings } from "../../configs/editor.config";
})
export class EditorsService {
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
private activationSubject: ReplaySubject<string> = new ReplaySubject<string>(1);
private switchSessionSubject: ReplaySubject<string> = new ReplaySubject<string>(1);
private closeTabSubject: ReplaySubject<string> = new ReplaySubject<string>(1);
private selectSessionLeftSubject: ReplaySubject<any> = new ReplaySubject<any>(1);
private selectSessionRightSubject: ReplaySubject<any> = new ReplaySubject<any>(1);
private moveSessionLeftSubject: ReplaySubject<any> = new ReplaySubject<any>(1);
private moveSessionRightSubject: ReplaySubject<any> = new ReplaySubject<any>(1);
editors: Map<string, ComponentRef<NewtonEditorComponent>>;
editorSettings: typeof EditorSettings;
@ -44,68 +37,12 @@ export class EditorsService {
}
setData(data: ServiceMessage): void {
sendMessage(data: ServiceMessage): void {
this.messageSubject.next(data);
}
getData$(): Observable<ServiceMessage> {
getMessage$(): Observable<ServiceMessage> {
return this.messageSubject.asObservable();
}
setActiveEditor(data: string): void {
this.activationSubject.next(data);
}
newActiveEditor$(): Observable<string> {
return this.activationSubject.asObservable();
}
setTabToEditor(data: string): void {
this.switchSessionSubject.next(data);
}
loadTabToEditor$(): Observable<string> {
return this.switchSessionSubject.asObservable();
}
closeTab(data: string): void {
this.closeTabSubject.next(data);
}
closeTabRequested$(): Observable<string> {
return this.closeTabSubject.asObservable();
}
moveSessionLeft(data: string): void {
this.moveSessionLeftSubject.next(data);
}
moveSessionLeftRequested$(): Observable<string> {
return this.moveSessionLeftSubject.asObservable();
}
moveSessionRight(data: string): void {
this.moveSessionRightSubject.next(data);
}
moveSessionRightRequested$(): Observable<string> {
return this.moveSessionRightSubject.asObservable();
}
selectSessionLeft(data: string): void {
this.selectSessionLeftSubject.next(data);
}
selectSessionLeftRequested$(): Observable<string> {
return this.selectSessionLeftSubject.asObservable();
}
selectSessionRight(data: string): void {
this.selectSessionRightSubject.next(data);
}
selectSessionRightRequested$(): Observable<string> {
return this.selectSessionRightSubject.asObservable();
}
}

View File

@ -79,11 +79,11 @@ export class FilesService {
async addTab(file: NewtonFile) {
let message = new ServiceMessage();
message.action = "create-tab";
message.message = file.fname;
message.uuid = file.hash;
message.data = file.path;
message.fileName = file.fname;
message.fileUUID = file.hash;
message.filePath = file.path;
this.tabsService.setData(message);
this.tabsService.sendMessage(message);
}

View File

@ -8,15 +8,16 @@ import { ServiceMessage } from '../../../types/service-message.type';
providedIn: 'root'
})
export class TabsService {
private dataSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
constructor() {}
setData(data: ServiceMessage): void {
this.dataSubject.next(data);
sendMessage(data: ServiceMessage): void {
this.messageSubject.next(data);
}
getData$(): Observable<ServiceMessage> {
return this.dataSubject.asObservable();
getMessage$(): Observable<ServiceMessage> {
return this.messageSubject.asObservable();
}
}

View File

@ -1,6 +1,9 @@
export class ServiceMessage {
action: string = "none";
action: string = "";
message: string = "";
uuid!: string;
data: any;
editorUUID: string;
fileName: string;
fileUUID: string;
filePath: string;
rawData: any;
}

View File

@ -54,28 +54,21 @@ export class EditorsComponent {
loadSubscribers() {
this.editorsService.selectSessionLeftRequested$().pipe(
this.editorsService.getMessage$().pipe(
takeUntil(this.unsubscribe)
).subscribe((uuid: string) => {
let editorComponent = this.editorsService.get(uuid);
).subscribe((message: ServiceMessage) => {
if (message.action == "select-left-editor") {
let editorComponent = this.editorsService.get(message.editorUUID);
if (!editorComponent.leftSiblingUUID) return;
let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID);
siblingComponent.editor.focus()
});
this.editorsService.selectSessionRightRequested$().pipe(
takeUntil(this.unsubscribe)
).subscribe((uuid: string) => {
let editorComponent = this.editorsService.get(uuid);
} else if (message.action == "select-right-editor") {
let editorComponent = this.editorsService.get(message.editorUUID);
if (!editorComponent.rightSiblingUUID) return;
let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID);
siblingComponent.editor.focus()
});
this.editorsService.moveSessionLeftRequested$().pipe(
takeUntil(this.unsubscribe)
).subscribe((uuid: string) => {
let editorComponent = this.editorsService.get(uuid);
} else if (message.action == "move-session-left") {
let editorComponent = this.editorsService.get(message.editorUUID);
if (!editorComponent.leftSiblingUUID) return;
let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID);
let session = editorComponent.editor.getSession();
@ -85,12 +78,8 @@ export class EditorsComponent {
siblingComponent.editor.setSession(session);
editorComponent.newBuffer();
siblingComponent.editor.focus()
});
this.editorsService.moveSessionRightRequested$().pipe(
takeUntil(this.unsubscribe)
).subscribe((uuid: string) => {
let editorComponent = this.editorsService.get(uuid);
} else if (message.action == "move-session-right") {
let editorComponent = this.editorsService.get(message.editorUUID);
if (!editorComponent.rightSiblingUUID) return;
let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID);
let session = editorComponent.editor.getSession();
@ -100,31 +89,19 @@ export class EditorsComponent {
siblingComponent.editor.setSession(session);
editorComponent.newBuffer();
siblingComponent.editor.focus()
});
this.editorsService.newActiveEditor$().pipe(
takeUntil(this.unsubscribe)
).subscribe((uuid: string) => {
} else if (message.action == "set-active-editor") {
this.editorsService.get(this.activeEditor).removeActiveStyling();
this.activeEditor = uuid;
this.activeEditor = message.editorUUID;
this.editorsService.get(this.activeEditor).addActiveStyling();
});
this.editorsService.loadTabToEditor$().pipe(
takeUntil(this.unsubscribe)
).subscribe((path: string) => {
let file = this.filesService.get(path);
} else if (message.action == "set-tab-to-editor") {
let file = this.filesService.get(message.filePath);
let editorComponent = this.getActiveEditorComponent();
let editor = editorComponent.editor;
editorComponent.activeFile = file;
editor.setSession(file.session);
});
this.editorsService.closeTabRequested$().pipe(
takeUntil(this.unsubscribe)
).subscribe((path: string) => {
let file = this.filesService.get(path);
} else if (message.action == "close-tab") {
let file = this.filesService.get(message.filePath);
let editors = this.editorsService.getEditorsAsArray();
for (let i = 0; i < editors.length; i++) {
@ -135,6 +112,8 @@ export class EditorsComponent {
}
this.filesService.delete(file);
}
});
}

View File

@ -43,11 +43,11 @@ export class FilesModalComponent {
}
loadSubscribers() {
this.tabsService.getData$().pipe(
this.tabsService.getMessage$().pipe(
takeUntil(this.unsubscribe)
).subscribe((data: ServiceMessage) => {
if (data.action === "create-tab") {
this.createFileRow(data.message, data.uuid, data.data);
this.createFileRow(data.fileName, data.fileUUID, data.filePath);
}
});
@ -71,7 +71,7 @@ export class FilesModalComponent {
}
private createFileRow(title: string, uuid: string, path: string): void {
this.files.push({title: title, path: path, uuid: uuid})
this.files.push({title: title, uuid: uuid, path: path})
}
createModal() {

View File

@ -16,6 +16,8 @@ import { EditorsService } from '../../common/services/editor/editors.service';
import { NewtonEditorBase } from './newton-editor.base';
import { ServiceMessage } from '../../common/types/service-message.type';
@Component({
@ -115,7 +117,11 @@ export class NewtonEditorComponent extends NewtonEditorBase {
});
this.editor.on("focus", () => {
this.editorsService.setActiveEditor(this.uuid);
let message = new ServiceMessage();
message.action = "set-active-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
});
this.editor.on("changeSession", (session) => {
@ -141,20 +147,36 @@ export class NewtonEditorComponent extends NewtonEditorBase {
this.updateInfoBar();
}
public selectSessionLeft() {
this.editorsService.selectSessionLeft(this.uuid);
public selectLeftEditor() {
let message = new ServiceMessage();
message.action = "select-left-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public selectSessionRight() {
this.editorsService.selectSessionRight(this.uuid);
public selectRightEditor() {
let message = new ServiceMessage();
message.action = "select-right-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public moveSessionLeft() {
this.editorsService.moveSessionLeft(this.uuid);
let message = new ServiceMessage();
message.action = "move-session-left";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
public moveSessionRight() {
this.editorsService.moveSessionRight(this.uuid);
let message = new ServiceMessage();
message.action = "move-session-right";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
}
}

View File

@ -40,11 +40,11 @@ export class TabsComponent {
}
public ngAfterViewInit(): void {
this.tabsService.getData$().pipe(
this.tabsService.getMessage$().pipe(
takeUntil(this.unsubscribe)
).subscribe((data: ServiceMessage) => {
if (data.action === "create-tab") {
this.createTab(data.message, data.uuid, data.data);
this.createTab(data.fileName, data.fileUUID, data.filePath);
}
});
}
@ -55,7 +55,7 @@ export class TabsComponent {
}
private createTab(title: string, uuid: string, path: string): void {
this.tabs.push({title: title, path: path, uuid: uuid});
this.tabs.push({title: title, uuid: uuid, path: path});
this.changeDetectorRef.detectChanges();
}
@ -63,11 +63,14 @@ export class TabsComponent {
let target = event.target;
if ( target.classList.contains("tab") ) {
this.editorsService.setTabToEditor(
this.sendEditorsServiceAMessage(
"set-tab-to-editor",
event.srcElement.getAttribute("title")
);
} else if ( target.classList.contains("title") ) {
this.editorsService.setTabToEditor(
this.sendEditorsServiceAMessage(
"set-tab-to-editor",
event.srcElement.parentElement.getAttribute("title")
);
} else if ( target.classList.contains("close-button") ) {
@ -79,7 +82,7 @@ export class TabsComponent {
}
closeTab(fpath: string): void {
this.editorsService.closeTab(fpath);
this.sendEditorsServiceAMessage("close-tab", fpath);
for (let i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].path == fpath) {
@ -94,9 +97,9 @@ export class TabsComponent {
let target = event.event.target;
let fpath = "";
if ( target.classList.contains("title") ) {
fpath = target.parentElement.getAttribute("title")
} else if ( target.classList.contains("close-button") ) {
if ( target.classList.contains("title") ||
target.classList.contains("close-button")
) {
fpath = target.parentElement.getAttribute("title")
} else (
fpath = target.getAttribute("title")
@ -120,4 +123,13 @@ export class TabsComponent {
// moveItemInArray(this.tabs, event.previousIndex, event.currentIndex);
}
private sendEditorsServiceAMessage(action: string, fpath: string) {
let message = new ServiceMessage();
message.action = action;
message.filePath = fpath;
this.editorsService.sendMessage(message);
}
}