diff --git a/build/npm/transport.json b/build/npm/transport.json index b57d172..0d659ab 100644 --- a/build/npm/transport.json +++ b/build/npm/transport.json @@ -29,6 +29,16 @@ "license": "BSD-2-Clause", "changelogHistory": [ { + "date": "8/24/21", + "version": "1.3.1", + "notes": [ + { + "description": "Remove unnecessary macro task scheduling", + "review_uri": "https://github.com/vmware/transport-typescript/pull/54" + } + ] + } + ,{ "date": "8/20/21", "version": "1.3.0", "notes": [ @@ -1097,4 +1107,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 5b5eab3..271ff43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@vmw/transport", - "version": "1.2.9", + "version": "1.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7d2e3b6..090eaee 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vmw/transport", - "version": "1.3.0", + "version": "1.3.1", "private": false, "license": "BSD-2-Clause", "repository": "git@github.com:vmware/transport-typescript.git", @@ -66,4 +66,4 @@ "resolutions": { "glob-parent": "^5.1.2" } -} \ No newline at end of file +} diff --git a/src/bridge/broker-connector.spec.ts b/src/bridge/broker-connector.spec.ts index 3f88c77..40aa9e3 100644 --- a/src/bridge/broker-connector.spec.ts +++ b/src/bridge/broker-connector.spec.ts @@ -85,7 +85,7 @@ describe('BrokerConnector [broker-connector.ts]', () => { session: session.id, isQueue: false, brokerPrefix: 'fake' - }; + }; let headers = session.connect.calls.mostRecent().args[0]; let headerKeys = Object.keys(headers); expect(globalHeaderKeys.every(key => headerKeys.includes(key))).toBeTruthy(); @@ -108,13 +108,13 @@ describe('BrokerConnector [broker-connector.ts]', () => { bc.disconnectClient(session.id); headers = session.disconnect.calls.mostRecent().args[0]; headerKeys = Object.keys(headers); - expect(globalHeaderKeys.every(key => headerKeys.includes(key))).toBeTruthy(); + expect(globalHeaderKeys.every(key => headerKeys.includes(key))).toBeTruthy(); done(); }, 10 // found by trial and error. ); - }); + }); }); describe('Service configuration and basic connect/disconnect', () => { @@ -1210,9 +1210,9 @@ describe('BrokerConnector [broker-connector.ts]', () => { (done) => { let count = 0; /* - + This tests that galactic channels operate over low level API's - + */ const chan = bus.api.getGalacticChannel( diff --git a/src/bus.api.ts b/src/bus.api.ts index 183564f..4b58d00 100644 --- a/src/bus.api.ts +++ b/src/bus.api.ts @@ -17,8 +17,11 @@ import { GeneralUtil } from './util/util'; import { FabricApi } from './fabric.api'; import { BrokerConnector } from './bridge'; +// used to accept NgZone instance from Angular to schedule tasks outside of Zone, enabling efficient change detection +export declare type NgZoneRef = any; + // current version -const version = '1.3.0'; +const version = '1.3.1'; export type ChannelName = string; export type SentFrom = string; @@ -160,6 +163,11 @@ export abstract class EventBus { */ readonly brokerConnector: BrokerConnector; + /** + * Reference to ngZone. + */ + public zoneRef: NgZoneRef; + /** * Simple API Methods */ @@ -549,6 +557,9 @@ export abstract class EventBus { /** Enable Fake Socket for broker connector */ abstract enableDevMode(): void; + + /** Set NgZone reference for efficient macro task scheduling in Angular apps */ + abstract setNgZoneRef(ngZoneRef: NgZoneRef): void; } /** diff --git a/src/bus/bus.lowlevel.ts b/src/bus/bus.lowlevel.ts index eb2f97b..3f7086d 100644 --- a/src/bus/bus.lowlevel.ts +++ b/src/bus/bus.lowlevel.ts @@ -173,17 +173,13 @@ export class EventBusLowLevelApiImpl implements EventBusLowApi { } sendRequest(cname: string, payload: any, name?: string): void { - let mh: MessageHandlerConfig = new MessageHandlerConfig(cname, payload, true, cname); + const mh: MessageHandlerConfig = new MessageHandlerConfig(cname, payload, true, cname); this.send(mh.sendChannel, new Message().request(mh), name); } sendResponse(cname: string, payload: any, name?: string): void { - let mh: MessageHandlerConfig = new MessageHandlerConfig(cname, payload, true, cname); - this.tickEventLoop( - () => { - this.send(mh.sendChannel, new Message().response(mh), name); - } - ); + const mh: MessageHandlerConfig = new MessageHandlerConfig(cname, payload, true, cname); + this.send(mh.sendChannel, new Message().response(mh), name); } complete(cname: ChannelName, from?: SentFrom): boolean { @@ -289,17 +285,30 @@ export class EventBusLowLevelApiImpl implements EventBusLowApi { send(cname: ChannelName, message: Message, from?: SentFrom): boolean { message.sender = from; // make sure we know where this message came from. - let mo = new MonitorObject; - if (!this.internalChannelMap.has(cname)) { - mo = new MonitorObject().build(MonitorType.MonitorDropped, cname, from, message); + const channelFound = this.internalChannelMap.has(cname); + let mo = new MonitorObject().build( + channelFound ? MonitorType.MonitorData : MonitorType.MonitorDropped, + cname, + from, + message + ); + + if (!channelFound) { this.monitorStream.send(new Message().request(mo)); return false; } - mo = new MonitorObject().build(MonitorType.MonitorData, cname, from, message); - this.monitorStream.send(new Message().request(mo)); - this.internalChannelMap.get(cname) - .send(message); + + if (this.eventBusRef.zoneRef) { + this.eventBusRef.zoneRef.runOutsideAngular(() => { + this.internalChannelMap.get(cname).send(message); + this.monitorStream.send(new Message().request(mo)); + }); + } else { + this.monitorStream.send(new Message().request(mo)); + this.internalChannelMap.get(cname).send(message); + } + return true; } @@ -325,6 +334,7 @@ export class EventBusLowLevelApiImpl implements EventBusLowApi { const handler: MessageHandler = this.createMessageHandler(handlerConfig, false, name, id); this.send(handlerConfig.sendChannel, new Message(id).request(handlerConfig), name); return handler; + } respond(handlerConfig: MessageHandlerConfig, name?: SentFrom): MessageResponder { diff --git a/src/bus/bus.spec.ts b/src/bus/bus.spec.ts index 6058c43..2abd096 100644 --- a/src/bus/bus.spec.ts +++ b/src/bus/bus.spec.ts @@ -342,7 +342,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { () => bus.closeChannel('puppers') , 5); - // should have settled + // should have settled bus.api.tickEventLoop( () => { @@ -367,7 +367,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { bus.sendErrorMessage('puppers', 'why are my shoes ruined?'); - // should have settled + // should have settled bus.api.tickEventLoop( () => { expect(bus.api.countListeners()).toEqual(3); @@ -389,7 +389,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { bus.sendErrorMessage('puppers', 'why are my shoes ruined?'); - // should have settled + // should have settled bus.api.tickEventLoop( () => { expect(bus.api.countListeners()).toEqual(3); @@ -437,7 +437,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { bus.sendRequestMessage('puppers', 'how many bones has fox hidden?'); - // should have settled + // should have settled bus.api.tickEventLoop( () => { responder.tick('ignore me'); @@ -449,7 +449,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { , 10); - // should have settled + // should have settled bus.api.tickEventLoop( () => { expect(count).toEqual(1); // only a single event should have made it through @@ -494,7 +494,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { bus.sendRequestMessage('puppers', 'how many bones has fox hidden?'); - // should have settled + // should have settled bus.api.tickEventLoop( () => { handler.tick('ignore me'); @@ -506,7 +506,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { , 10); - // should have settled + // should have settled bus.api.tickEventLoop( () => { expect(count).toEqual(1); // only a single event should have made it through @@ -834,7 +834,6 @@ describe('TransportEventBus [bus/bus.ts]', () => { } } ); - } ); @@ -1478,7 +1477,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { expect(req).toEqual('where is my dinner?'); counter++; if (counter === 3) { - responder.close(); // stop responding, but keep handling. + responder.close(); // stop responding, but keep handling. } return 'coming soon, calm down pup!'; } @@ -1990,18 +1989,18 @@ describe('TransportEventBus [bus/bus.ts]', () => { bus.listenGalacticStream('space-dogs', null, {brokerIdentity: 'connString', isPrivate: false}); bus.sendGalacticMessage('space-dogs', 'off to the moon goes fox!'); }); - + describe('markChannelAsGalactic()', () => { const channelName = 'space-cats'; - beforeEach(() => { + beforeEach(() => { bus.markChannelAsGalactic(channelName, 'connString'); }); it('sets the channel to be galactic', () => { const channel: Channel = bus.api.getChannelObject(channelName); - expect(channel.galactic).toEqual(true); + expect(channel.galactic).toEqual(true); }); it('sends MonitorNewGalacticChannel message', (done) => { @@ -2020,7 +2019,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { } } ); - }); + }); }); it('markChannelsAsGalactic triggers markChannelAsGalactic for each channel names', () => { @@ -2042,14 +2041,14 @@ describe('TransportEventBus [bus/bus.ts]', () => { describe('markChannelAsLocal()', () => { const channelName = 'space-cats'; - beforeEach(() => { + beforeEach(() => { bus.markChannelAsLocal(channelName); }); it('sets the channel to be private', () => { bus.markChannelAsLocal(channelName); const channel: Channel = bus.api.getChannelObject(channelName); - + expect(channel.galactic).toEqual(false); }); @@ -2069,7 +2068,7 @@ describe('TransportEventBus [bus/bus.ts]', () => { } } ); - + bus.markChannelAsLocal(channelName); }); }); diff --git a/src/bus/bus.ts b/src/bus/bus.ts index 6b951e5..22f79bd 100644 --- a/src/bus/bus.ts +++ b/src/bus/bus.ts @@ -35,6 +35,7 @@ import { MessageProxyConfig, ProxyControl } from '../proxy/message.proxy.api'; import { MessageProxy } from '../proxy/message.proxy'; import { FabricApi } from '../fabric.api'; import { FabricApiImpl } from '../fabric/fabric'; +import { NgZoneRef } from '.'; export class TransportEventBus extends EventBus implements EventBusEnabled { @@ -78,7 +79,7 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { * @returns {EventBus} the newly rebooted bus */ public static rebootWithOptions(logLevel: LogLevel, disableBootMessage: boolean): EventBus { - this.instance = new this(logLevel, disableBootMessage); + this.instance = new this(logLevel, disableBootMessage, false); return this.instance; } @@ -152,6 +153,8 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { // this.easterEgg(); } + public zoneRef: NgZoneRef; + public get logger(): Logger { return this.log; } @@ -160,6 +163,10 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { return 'EventBus'; } + public setNgZoneRef(ngZoneRef: NgZoneRef): void { + this.zoneRef = ngZoneRef; + } + public enableDevMode(): void { this.devModeEnabled = true; this.log.warn('Dev Mode Enabled on Event Bus, This should never be enabled in production'); @@ -310,14 +317,10 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { payload: any, name = this.getName()): boolean { - this.api.tickEventLoop( - () => { - this.api.send( - cname, - new Message().response(payload), - name - ); - } + this.api.send( + cname, + new Message().response(payload), + name ); return true; } @@ -328,14 +331,10 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { id: UUID, from?: string, proxyBroadcast: boolean = false): void { - this.api.tickEventLoop( - () => { - this.api.send( - cname, - new Message(id, 1, proxyBroadcast).response(payload), - from - ); - } + this.api.send( + cname, + new Message(id, 1, proxyBroadcast).response(payload), + from ); } @@ -346,14 +345,10 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { version: number, from?: string, proxyBroadcast: boolean = false): void { - this.api.tickEventLoop( - () => { - this.api.send( - cname, - new Message(id, version, proxyBroadcast).response(payload), - from - ); - } + this.api.send( + cname, + new Message(id, version, proxyBroadcast).response(payload), + from ); } @@ -468,7 +463,7 @@ export class TransportEventBus extends EventBus implements EventBusEnabled { return this.api.request( new MessageHandlerConfig(sendChannel, requestPayload, true, returnChannel), - name, + from, uuid ); } diff --git a/src/bus/model/channel.model.ts b/src/bus/model/channel.model.ts index 93ef0e3..12d9330 100644 --- a/src/bus/model/channel.model.ts +++ b/src/bus/model/channel.model.ts @@ -114,10 +114,9 @@ export class Channel { * @param message Message */ send(message: Message) { - setTimeout( - () => { - this._streamObject.next(message); - }); + setTimeout(() => { + this._streamObject.next(message); + }); } /**