From 430107c065d48568cc1b6391d9166221d92ca13b Mon Sep 17 00:00:00 2001 From: Piotr Pekala Date: Fri, 26 Jul 2019 06:13:42 -0700 Subject: [PATCH 1/2] Update project-web-service-handler --- .../project-map/project-map.component.ts | 20 ++++-- .../handlers/project-web-service-handler.ts | 62 +++++++++---------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/app/components/project-map/project-map.component.ts b/src/app/components/project-map/project-map.component.ts index 2e9fe5b2..874a3c2c 100644 --- a/src/app/components/project-map/project-map.component.ts +++ b/src/app/components/project-map/project-map.component.ts @@ -50,6 +50,7 @@ import { InterfaceLabelWidget } from '../../cartography/widgets/interface-label' import { LabelWidget } from '../../cartography/widgets/label'; import { MapLinkNodeToLinkNodeConverter } from '../../cartography/converters/map/map-link-node-to-link-node-converter'; import { ProjectMapMenuComponent } from './project-map-menu/project-map-menu.component'; +import { ToasterService } from '../../services/toaster.service'; @Component({ @@ -65,7 +66,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy { public symbols: Symbol[] = []; public project: Project; public server: Server; - private ws: Subject; + private ws: WebSocket; public isProjectMapMenuVisible: boolean = false; tools = { @@ -113,7 +114,8 @@ export class ProjectMapComponent implements OnInit, OnDestroy { private recentlyOpenedProjectService: RecentlyOpenedProjectService, private movingEventSource: MovingEventSource, private mapScaleService: MapScaleService, - private nodeCreatedLabelStylesFixer: NodeCreatedLabelStylesFixer + private nodeCreatedLabelStylesFixer: NodeCreatedLabelStylesFixer, + private toasterService: ToasterService ) {} ngOnInit() { @@ -228,9 +230,15 @@ export class ProjectMapComponent implements OnInit, OnDestroy { } setUpWS(project: Project) { - this.ws = webSocket(this.projectService.notificationsPath(this.server, project.project_id)); + this.ws = new WebSocket(this.projectService.notificationsPath(this.server, project.project_id)); - this.subscriptions.push(this.projectWebServiceHandler.connect(this.ws)); + this.ws.onmessage = (ev: MessageEvent) => { + this.projectWebServiceHandler.handleMessage(ev); + }; + + this.ws.onerror = (ev: MessageEvent) => { + this.toasterService.error('Connection to host lost.'); + }; } setUpMapCallbacks() { @@ -405,8 +413,8 @@ export class ProjectMapComponent implements OnInit, OnDestroy { this.nodesDataSource.clear(); this.linksDataSource.clear(); - if (this.ws) { - this.ws.unsubscribe(); + if (this.ws.OPEN) { + this.ws.close(); } this.subscriptions.forEach((subscription: Subscription) => subscription.unsubscribe()); } diff --git a/src/app/handlers/project-web-service-handler.ts b/src/app/handlers/project-web-service-handler.ts index 0a9a7e2f..38d9f18f 100644 --- a/src/app/handlers/project-web-service-handler.ts +++ b/src/app/handlers/project-web-service-handler.ts @@ -21,36 +21,36 @@ export class ProjectWebServiceHandler { private drawingsDataSource: DrawingsDataSource ) {} - public connect(ws: Subject) { - const subscription = ws.subscribe((message: WebServiceMessage) => { - if (message.action === 'node.updated') { - this.nodesDataSource.update(message.event as Node); - } - if (message.action === 'node.created') { - this.nodesDataSource.add(message.event as Node); - } - if (message.action === 'node.deleted') { - this.nodesDataSource.remove(message.event as Node); - } - if (message.action === 'link.created') { - this.linksDataSource.add(message.event as Link); - } - if (message.action === 'link.updated') { - this.linksDataSource.update(message.event as Link); - } - if (message.action === 'link.deleted') { - this.linksDataSource.remove(message.event as Link); - } - if (message.action === 'drawing.created') { - this.drawingsDataSource.add(message.event as Drawing); - } - if (message.action === 'drawing.updated') { - this.drawingsDataSource.update(message.event as Drawing); - } - if (message.action === 'drawing.deleted') { - this.drawingsDataSource.remove(message.event as Drawing); - } - }); - return subscription; + public handleMessage(event: MessageEvent) { + console.log(event); + let message = event.data; + if (message.action === 'node.updated') { + console.log('should work'); + this.nodesDataSource.update(message.event as Node); + } + if (message.action === 'node.created') { + this.nodesDataSource.add(message.event as Node); + } + if (message.action === 'node.deleted') { + this.nodesDataSource.remove(message.event as Node); + } + if (message.action === 'link.created') { + this.linksDataSource.add(message.event as Link); + } + if (message.action === 'link.updated') { + this.linksDataSource.update(message.event as Link); + } + if (message.action === 'link.deleted') { + this.linksDataSource.remove(message.event as Link); + } + if (message.action === 'drawing.created') { + this.drawingsDataSource.add(message.event as Drawing); + } + if (message.action === 'drawing.updated') { + this.drawingsDataSource.update(message.event as Drawing); + } + if (message.action === 'drawing.deleted') { + this.drawingsDataSource.remove(message.event as Drawing); + } } } From dfaea6297be2cb26f6f924847eb106cde499952e Mon Sep 17 00:00:00 2001 From: Piotr Pekala Date: Fri, 26 Jul 2019 07:56:51 -0700 Subject: [PATCH 2/2] Bug fixed --- .../duplicate-action.component.spec.ts | 2 +- .../project-map/project-map.component.spec.ts | 10 +++++- .../project-map/project-map.component.ts | 8 ++--- .../project-web-service-handler.spec.ts | 36 +++++-------------- .../handlers/project-web-service-handler.ts | 5 +-- 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/app/components/project-map/context-menu/actions/duplicate-action/duplicate-action.component.spec.ts b/src/app/components/project-map/context-menu/actions/duplicate-action/duplicate-action.component.spec.ts index c7e42a0c..74772dec 100644 --- a/src/app/components/project-map/context-menu/actions/duplicate-action/duplicate-action.component.spec.ts +++ b/src/app/components/project-map/context-menu/actions/duplicate-action/duplicate-action.component.spec.ts @@ -13,7 +13,7 @@ import { DuplicateActionComponent } from './duplicate-action.component'; import { ToasterService } from '../../../../../services/toaster.service'; import { MockedToasterService } from '../../../../../services/toaster.service.spec'; -fdescribe('DuplicateActionComponent', () => { +describe('DuplicateActionComponent', () => { let component: DuplicateActionComponent; let fixture: ComponentFixture; let mockedNodeService: MockedNodeService = new MockedNodeService(); diff --git a/src/app/components/project-map/project-map.component.spec.ts b/src/app/components/project-map/project-map.component.spec.ts index f6048af5..8daeda92 100644 --- a/src/app/components/project-map/project-map.component.spec.ts +++ b/src/app/components/project-map/project-map.component.spec.ts @@ -50,6 +50,8 @@ import { InterfaceLabelWidget } from '../../cartography/widgets/interface-label' import { MapLinkNodeToLinkNodeConverter } from '../../cartography/converters/map/map-link-node-to-link-node-converter'; import { MapSettingService } from '../../services/mapsettings.service'; import { ProjectMapMenuComponent } from './project-map-menu/project-map-menu.component'; +import { MockedToasterService } from '../../services/toaster.service.spec'; +import { ToasterService } from '../../services/toaster.service'; export class MockedProgressService { public activate() {} @@ -198,6 +200,7 @@ describe('ProjectMapComponent', () => { let drawingsDataSource = new MockedDrawingsDataSource(); let nodesDataSource = new MockedNodesDataSource(); let linksDataSource = new MockedLinksDataSource(); + let mockedToasterService = new MockedToasterService(); let nodeCreatedLabelStylesFixer; beforeEach(async(() => { @@ -241,7 +244,8 @@ describe('ProjectMapComponent', () => { }, { provide: NodeCreatedLabelStylesFixer, useValue: nodeCreatedLabelStylesFixer}, { provide: MapScaleService }, - { provide: NodeCreatedLabelStylesFixer, useValue: nodeCreatedLabelStylesFixer} + { provide: NodeCreatedLabelStylesFixer, useValue: nodeCreatedLabelStylesFixer}, + { provide: ToasterService, useValue: mockedToasterService } ], declarations: [ProjectMapComponent, ProjectMapMenuComponent, D3MapComponent, ...ANGULAR_MAP_DECLARATIONS], schemas: [NO_ERRORS_SCHEMA] @@ -254,6 +258,10 @@ describe('ProjectMapComponent', () => { component.projectMapMenuComponent = { resetDrawToolChoice(){} } as ProjectMapMenuComponent; + + component.ws = { + OPEN: 0, + } as WebSocket; }); afterEach(() => { diff --git a/src/app/components/project-map/project-map.component.ts b/src/app/components/project-map/project-map.component.ts index 874a3c2c..002d167b 100644 --- a/src/app/components/project-map/project-map.component.ts +++ b/src/app/components/project-map/project-map.component.ts @@ -66,7 +66,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy { public symbols: Symbol[] = []; public project: Project; public server: Server; - private ws: WebSocket; + public ws: WebSocket; public isProjectMapMenuVisible: boolean = false; tools = { @@ -232,11 +232,11 @@ export class ProjectMapComponent implements OnInit, OnDestroy { setUpWS(project: Project) { this.ws = new WebSocket(this.projectService.notificationsPath(this.server, project.project_id)); - this.ws.onmessage = (ev: MessageEvent) => { - this.projectWebServiceHandler.handleMessage(ev); + this.ws.onmessage = (event: MessageEvent) => { + this.projectWebServiceHandler.handleMessage(JSON.parse(event.data)); }; - this.ws.onerror = (ev: MessageEvent) => { + this.ws.onerror = (event: MessageEvent) => { this.toasterService.error('Connection to host lost.'); }; } diff --git a/src/app/handlers/project-web-service-handler.spec.ts b/src/app/handlers/project-web-service-handler.spec.ts index f9a85e3c..e8cae8f2 100644 --- a/src/app/handlers/project-web-service-handler.spec.ts +++ b/src/app/handlers/project-web-service-handler.spec.ts @@ -30,13 +30,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, nodesDataSource: NodesDataSource) => { spyOn(nodesDataSource, 'add'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'node.created'; message.event = new Node(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(nodesDataSource.add).toHaveBeenCalledWith(message.event); @@ -48,13 +46,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, nodesDataSource: NodesDataSource) => { spyOn(nodesDataSource, 'update'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'node.updated'; message.event = new Node(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(nodesDataSource.update).toHaveBeenCalledWith(message.event); @@ -66,13 +62,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, nodesDataSource: NodesDataSource) => { spyOn(nodesDataSource, 'remove'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'node.deleted'; message.event = new Node(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(nodesDataSource.remove).toHaveBeenCalledWith(message.event); @@ -84,13 +78,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, linksDataSource: LinksDataSource) => { spyOn(linksDataSource, 'add'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'link.created'; message.event = new Link(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(linksDataSource.add).toHaveBeenCalledWith(message.event); @@ -102,13 +94,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, linksDataSource: LinksDataSource) => { spyOn(linksDataSource, 'update'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'link.updated'; message.event = new Link(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(linksDataSource.update).toHaveBeenCalledWith(message.event); @@ -120,13 +110,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, linksDataSource: LinksDataSource) => { spyOn(linksDataSource, 'remove'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'link.deleted'; message.event = new Link(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(linksDataSource.remove).toHaveBeenCalledWith(message.event); @@ -138,13 +126,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, drawingsDataSource: DrawingsDataSource) => { spyOn(drawingsDataSource, 'add'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'drawing.created'; message.event = new Drawing(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(drawingsDataSource.add).toHaveBeenCalledWith(message.event); @@ -156,13 +142,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, drawingsDataSource: DrawingsDataSource) => { spyOn(drawingsDataSource, 'update'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'drawing.updated'; message.event = new Drawing(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(drawingsDataSource.update).toHaveBeenCalledWith(message.event); @@ -174,13 +158,11 @@ describe('ProjectWebServiceHandler', () => { (service: ProjectWebServiceHandler, drawingsDataSource: DrawingsDataSource) => { spyOn(drawingsDataSource, 'remove'); - service.connect(ws); - const message = new WebServiceMessage(); message.action = 'drawing.deleted'; message.event = new Drawing(); - ws.next(message); + service.handleMessage(message); expect(service).toBeTruthy(); expect(drawingsDataSource.remove).toHaveBeenCalledWith(message.event); diff --git a/src/app/handlers/project-web-service-handler.ts b/src/app/handlers/project-web-service-handler.ts index 38d9f18f..597553f6 100644 --- a/src/app/handlers/project-web-service-handler.ts +++ b/src/app/handlers/project-web-service-handler.ts @@ -21,11 +21,8 @@ export class ProjectWebServiceHandler { private drawingsDataSource: DrawingsDataSource ) {} - public handleMessage(event: MessageEvent) { - console.log(event); - let message = event.data; + public handleMessage(message: WebServiceMessage) { if (message.action === 'node.updated') { - console.log('should work'); this.nodesDataSource.update(message.event as Node); } if (message.action === 'node.created') {