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"}, bindKey: {win: "ctrl-shift-s", mac: "ctrl-shift-s"},
readOnly: false readOnly: false
}, { }, {
name: "selectSessionLeft", name: "selectLeftEditor",
bindKey: {win: "ctrl-pageup", mac: "ctrl-pageup"}, bindKey: {win: "ctrl-pageup", mac: "ctrl-pageup"},
readOnly: false readOnly: false
}, { }, {
name: "selectSessionRight", name: "selectRightEditor",
bindKey: {win: "ctrl-pagedown", mac: "ctrl-pagedown"}, bindKey: {win: "ctrl-pagedown", mac: "ctrl-pagedown"},
readOnly: false readOnly: false
}, { }, {

View File

@ -13,13 +13,6 @@ import { EditorSettings } from "../../configs/editor.config";
}) })
export class EditorsService { export class EditorsService {
private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1); 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>>; editors: Map<string, ComponentRef<NewtonEditorComponent>>;
editorSettings: typeof EditorSettings; editorSettings: typeof EditorSettings;
@ -44,68 +37,12 @@ export class EditorsService {
} }
setData(data: ServiceMessage): void { sendMessage(data: ServiceMessage): void {
this.messageSubject.next(data); this.messageSubject.next(data);
} }
getData$(): Observable<ServiceMessage> { getMessage$(): Observable<ServiceMessage> {
return this.messageSubject.asObservable(); 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

@ -77,13 +77,13 @@ export class FilesService {
} }
async addTab(file: NewtonFile) { async addTab(file: NewtonFile) {
let message = new ServiceMessage(); let message = new ServiceMessage();
message.action = "create-tab"; message.action = "create-tab";
message.message = file.fname; message.fileName = file.fname;
message.uuid = file.hash; message.fileUUID = file.hash;
message.data = file.path; 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' providedIn: 'root'
}) })
export class TabsService { export class TabsService {
private dataSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1); private messageSubject: ReplaySubject<ServiceMessage> = new ReplaySubject<ServiceMessage>(1);
constructor() {} constructor() {}
setData(data: ServiceMessage): void {
this.dataSubject.next(data); sendMessage(data: ServiceMessage): void {
this.messageSubject.next(data);
} }
getData$(): Observable<ServiceMessage> { getMessage$(): Observable<ServiceMessage> {
return this.dataSubject.asObservable(); return this.messageSubject.asObservable();
} }
} }

View File

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

View File

@ -54,87 +54,66 @@ export class EditorsComponent {
loadSubscribers() { loadSubscribers() {
this.editorsService.selectSessionLeftRequested$().pipe( this.editorsService.getMessage$().pipe(
takeUntil(this.unsubscribe) takeUntil(this.unsubscribe)
).subscribe((uuid: string) => { ).subscribe((message: ServiceMessage) => {
let editorComponent = this.editorsService.get(uuid); if (message.action == "select-left-editor") {
if (!editorComponent.leftSiblingUUID) return; let editorComponent = this.editorsService.get(message.editorUUID);
let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID); if (!editorComponent.leftSiblingUUID) return;
siblingComponent.editor.focus() let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID);
}); siblingComponent.editor.focus()
} 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()
} 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();
let siblingSession = siblingComponent.editor.getSession();
this.editorsService.selectSessionRightRequested$().pipe( if (session == siblingSession) return;
takeUntil(this.unsubscribe) siblingComponent.editor.setSession(session);
).subscribe((uuid: string) => { editorComponent.newBuffer();
let editorComponent = this.editorsService.get(uuid); siblingComponent.editor.focus()
if (!editorComponent.rightSiblingUUID) return; } else if (message.action == "move-session-right") {
let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID); let editorComponent = this.editorsService.get(message.editorUUID);
siblingComponent.editor.focus() if (!editorComponent.rightSiblingUUID) return;
}); let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID);
let session = editorComponent.editor.getSession();
let siblingSession = siblingComponent.editor.getSession();
this.editorsService.moveSessionLeftRequested$().pipe( if (session == siblingSession) return;
takeUntil(this.unsubscribe) siblingComponent.editor.setSession(session);
).subscribe((uuid: string) => { editorComponent.newBuffer();
let editorComponent = this.editorsService.get(uuid); siblingComponent.editor.focus()
if (!editorComponent.leftSiblingUUID) return; } else if (message.action == "set-active-editor") {
let siblingComponent = this.editorsService.get(editorComponent.leftSiblingUUID); this.editorsService.get(this.activeEditor).removeActiveStyling();
let session = editorComponent.editor.getSession(); this.activeEditor = message.editorUUID;
let siblingSession = siblingComponent.editor.getSession(); this.editorsService.get(this.activeEditor).addActiveStyling();
} else if (message.action == "set-tab-to-editor") {
let file = this.filesService.get(message.filePath);
let editorComponent = this.getActiveEditorComponent();
let editor = editorComponent.editor;
if (session == siblingSession) return; editorComponent.activeFile = file;
siblingComponent.editor.setSession(session); editor.setSession(file.session);
editorComponent.newBuffer(); } else if (message.action == "close-tab") {
siblingComponent.editor.focus() let file = this.filesService.get(message.filePath);
}); let editors = this.editorsService.getEditorsAsArray();
this.editorsService.moveSessionRightRequested$().pipe( for (let i = 0; i < editors.length; i++) {
takeUntil(this.unsubscribe) let editorComponent = editors[i].instance;
).subscribe((uuid: string) => { if (editorComponent.editor.session == file.session) {
let editorComponent = this.editorsService.get(uuid); editorComponent.newBuffer();
if (!editorComponent.rightSiblingUUID) return; }
let siblingComponent = this.editorsService.get(editorComponent.rightSiblingUUID);
let session = editorComponent.editor.getSession();
let siblingSession = siblingComponent.editor.getSession();
if (session == siblingSession) return;
siblingComponent.editor.setSession(session);
editorComponent.newBuffer();
siblingComponent.editor.focus()
});
this.editorsService.newActiveEditor$().pipe(
takeUntil(this.unsubscribe)
).subscribe((uuid: string) => {
this.editorsService.get(this.activeEditor).removeActiveStyling();
this.activeEditor = uuid;
this.editorsService.get(this.activeEditor).addActiveStyling();
});
this.editorsService.loadTabToEditor$().pipe(
takeUntil(this.unsubscribe)
).subscribe((path: string) => {
let file = this.filesService.get(path);
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);
let editors = this.editorsService.getEditorsAsArray();
for (let i = 0; i < editors.length; i++) {
let editorComponent = editors[i].instance;
if (editorComponent.editor.session == file.session) {
editorComponent.newBuffer();
} }
this.filesService.delete(file);
} }
this.filesService.delete(file);
}); });
} }

View File

@ -43,11 +43,11 @@ export class FilesModalComponent {
} }
loadSubscribers() { loadSubscribers() {
this.tabsService.getData$().pipe( this.tabsService.getMessage$().pipe(
takeUntil(this.unsubscribe) takeUntil(this.unsubscribe)
).subscribe((data: ServiceMessage) => { ).subscribe((data: ServiceMessage) => {
if (data.action === "create-tab") { 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 { 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() { createModal() {

View File

@ -16,6 +16,8 @@ import { EditorsService } from '../../common/services/editor/editors.service';
import { NewtonEditorBase } from './newton-editor.base'; import { NewtonEditorBase } from './newton-editor.base';
import { ServiceMessage } from '../../common/types/service-message.type';
@Component({ @Component({
@ -115,7 +117,11 @@ export class NewtonEditorComponent extends NewtonEditorBase {
}); });
this.editor.on("focus", () => { 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) => { this.editor.on("changeSession", (session) => {
@ -141,20 +147,36 @@ export class NewtonEditorComponent extends NewtonEditorBase {
this.updateInfoBar(); this.updateInfoBar();
} }
public selectSessionLeft() { public selectLeftEditor() {
this.editorsService.selectSessionLeft(this.uuid); let message = new ServiceMessage();
message.action = "select-left-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
} }
public selectSessionRight() { public selectRightEditor() {
this.editorsService.selectSessionRight(this.uuid); let message = new ServiceMessage();
message.action = "select-right-editor";
message.editorUUID = this.uuid;
this.editorsService.sendMessage(message);
} }
public moveSessionLeft() { 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() { 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 { public ngAfterViewInit(): void {
this.tabsService.getData$().pipe( this.tabsService.getMessage$().pipe(
takeUntil(this.unsubscribe) takeUntil(this.unsubscribe)
).subscribe((data: ServiceMessage) => { ).subscribe((data: ServiceMessage) => {
if (data.action === "create-tab") { 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 { 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(); this.changeDetectorRef.detectChanges();
} }
@ -63,11 +63,14 @@ export class TabsComponent {
let target = event.target; let target = event.target;
if ( target.classList.contains("tab") ) { if ( target.classList.contains("tab") ) {
this.editorsService.setTabToEditor( this.sendEditorsServiceAMessage(
"set-tab-to-editor",
event.srcElement.getAttribute("title") event.srcElement.getAttribute("title")
); );
} else if ( target.classList.contains("title") ) { } else if ( target.classList.contains("title") ) {
this.editorsService.setTabToEditor( this.sendEditorsServiceAMessage(
"set-tab-to-editor",
event.srcElement.parentElement.getAttribute("title") event.srcElement.parentElement.getAttribute("title")
); );
} else if ( target.classList.contains("close-button") ) { } else if ( target.classList.contains("close-button") ) {
@ -79,7 +82,7 @@ export class TabsComponent {
} }
closeTab(fpath: string): void { closeTab(fpath: string): void {
this.editorsService.closeTab(fpath); this.sendEditorsServiceAMessage("close-tab", fpath);
for (let i = 0; i < this.tabs.length; i++) { for (let i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].path == fpath) { if (this.tabs[i].path == fpath) {
@ -94,9 +97,9 @@ export class TabsComponent {
let target = event.event.target; let target = event.event.target;
let fpath = ""; let fpath = "";
if ( target.classList.contains("title") ) { if ( target.classList.contains("title") ||
fpath = target.parentElement.getAttribute("title") target.classList.contains("close-button")
} else if ( target.classList.contains("close-button") ) { ) {
fpath = target.parentElement.getAttribute("title") fpath = target.parentElement.getAttribute("title")
} else ( } else (
fpath = target.getAttribute("title") fpath = target.getAttribute("title")
@ -120,4 +123,13 @@ export class TabsComponent {
// moveItemInArray(this.tabs, event.previousIndex, event.currentIndex); // 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);
}
} }