UNPKG

@stateflows/http-client

Version:

HTTP-based client for Stateflows framework

1 lines 12.4 kB
{"version":3,"sources":["../src/classes/http-transport.ts","../src/classes/notification-target.ts","../src/classes/watch.ts","../src/classes/http-transport-factory.ts"],"sourcesContent":["import { Event, BehaviorClass, BehaviorId, IStateflowsClientTransport, SendResult, JsonUtils, IWatcher, Notification, CompoundRequest } from \"@stateflows/common\";\r\nimport { NotificationTarget } from \"./notification-target\";\r\nimport { Watch } from \"./watch\";\r\nimport { NotificationsRequest } from \"@stateflows/common\";\r\n\r\nexport class HttpTransport implements IStateflowsClientTransport {\r\n #targets: Map<string, NotificationTarget> = new Map<string, NotificationTarget>();\r\n #notificationIds: Array<string> = [];\r\n\r\n constructor(private url: string) {\r\n if (url.slice(-1) != '/') {\r\n url = url + '/';\r\n }\r\n\r\n setInterval(async () => {\r\n if (this.#targets.size === 0) {\r\n return;\r\n }\r\n\r\n this.#targets.forEach(async target => {\r\n await this.send(target.behaviorId, new NotificationsRequest());\r\n });\r\n }, 10 * 1000);\r\n }\r\n\r\n private updateTimestamp(responseTime: string) {\r\n this.#targets.forEach(target => {\r\n target.watches.forEach(watch => {\r\n watch.lastNotificationCheck = responseTime;\r\n delete watch.milisecondsSinceLastNotificationCheck;\r\n });\r\n });\r\n }\r\n\r\n private handleNotifications(notifications: Array<Notification>, responseTime: string | null = null) {\r\n if (responseTime !== null) {\r\n this.updateTimestamp(responseTime);\r\n }\r\n \r\n notifications.forEach((notification: Notification) => {\r\n if (this.#notificationIds.includes(notification.id)) {\r\n return;\r\n }\r\n delete (notification.senderId.behaviorClass as any).environment;\r\n\r\n let target = this.#targets.get(JsonUtils.stringify(notification.senderId));\r\n if (typeof target !== 'undefined') {\r\n target.watches.forEach(watch => {\r\n if (watch.notificationName === notification.name) {\r\n target.handleNotifications([notification]);\r\n }\r\n });\r\n }\r\n });\r\n\r\n this.#notificationIds = notifications.map(notification => notification.id);\r\n }\r\n\r\n private getWatches(behaviorId: BehaviorId) {\r\n if (this.#targets.has(JsonUtils.stringify(behaviorId))) {\r\n let target = this.#targets.get(JsonUtils.stringify(behaviorId));\r\n return target.watches.map(watch => {\r\n return {\r\n notificationName: watch.notificationName,\r\n lastNotificationCheck: watch.lastNotificationCheck,\r\n milisecondsSinceLastNotificationCheck: watch.milisecondsSinceLastNotificationCheck !== null\r\n ? Date.now() - watch.milisecondsSinceLastNotificationCheck\r\n : null,\r\n };\r\n });\r\n } else {\r\n return [];\r\n }\r\n }\r\n\r\n async getAvailableClasses(): Promise<BehaviorClass[]> {\r\n let result = await fetch(`${this.url}stateflows/availableClasses`);\r\n return await result.json() as BehaviorClass[];\r\n }\r\n \r\n async send(behaviorId: BehaviorId, event: Event): Promise<SendResult> {\r\n const eventNameParts = (event as any).$type.split(',')[0].split('.');\r\n let eventName = eventNameParts[eventNameParts.length - 1];\r\n if (eventName === 'CompoundRequest') {\r\n const eventNames = (event as CompoundRequest).events.map(event => {\r\n const eventNameParts = (event as any).$type.split(',')[0].split('.');\r\n return eventNameParts[eventNameParts.length - 1];\r\n });\r\n eventName = eventNames.join(',');\r\n }\r\n let result = await fetch(\r\n `${this.url}stateflows/send?${eventName}`,\r\n {\r\n method: \"POST\",\r\n headers: {\r\n 'Accept': 'application/json',\r\n 'Content-Type': 'application/json'\r\n },\r\n body: JsonUtils.stringify({\r\n \"$type\": \"Stateflows.Common.Transport.Classes.StateflowsRequest, Stateflows.Common.Transport\",\r\n behaviorId: behaviorId,\r\n event: event,\r\n watches: this.getWatches(behaviorId)\r\n })\r\n }\r\n );\r\n \r\n let stateflowsResponse = await result.json();\r\n let response = stateflowsResponse.response;\r\n let validation = stateflowsResponse.validation;\r\n if (response) {\r\n (event as any).response = response;\r\n }\r\n\r\n this.handleNotifications(stateflowsResponse.notifications, stateflowsResponse.responseTime);\r\n\r\n let sendResult = new SendResult(event, stateflowsResponse.eventStatus, validation);\r\n\r\n return sendResult;\r\n }\r\n\r\n async watch(watcher: IWatcher, notificationName: string): Promise<void> {\r\n let target = this.#targets.has(JsonUtils.stringify(watcher.id))\r\n ? this.#targets.get(JsonUtils.stringify(watcher.id)) as NotificationTarget\r\n : new NotificationTarget(watcher);\r\n\r\n this.#targets.set(JsonUtils.stringify(watcher.id), target);\r\n\r\n let watchIndex = target.watches.findIndex(watch => watch.notificationName === notificationName);\r\n if (watchIndex === -1) {\r\n target.watches.push(new Watch(notificationName, Date.now()));\r\n }\r\n }\r\n\r\n async unwatch(watcher: IWatcher, notificationName: string): Promise<void> {\r\n if (this.#targets.has(JsonUtils.stringify(watcher.id))) {\r\n let target = this.#targets.get(JsonUtils.stringify(watcher.id)) as NotificationTarget;\r\n let index = target.watches.findIndex(watch => watch.notificationName === notificationName);\r\n if (index !== -1) {\r\n delete target.watches[index];\r\n }\r\n this.#targets.delete(JsonUtils.stringify(watcher.id));\r\n }\r\n }\r\n}","import { BehaviorId } from \"@stateflows/common\";\r\nimport { Notification } from \"@stateflows/common\";\r\nimport { IWatcher } from \"@stateflows/common\";\r\nimport { Watch } from \"./watch\";\r\n\r\nexport class NotificationTarget {\r\n #watcher: IWatcher;\r\n\r\n constructor(watcher: IWatcher) {\r\n this.#watcher = watcher;\r\n }\r\n\r\n watches: Array<Watch> = [];\r\n\r\n get behaviorId(): BehaviorId {\r\n return this.#watcher.id;\r\n }\r\n\r\n handleNotifications(notifications: Array<Notification>) {\r\n let notificationNames = this.watches.map(watch => watch.notificationName);\r\n notifications.forEach(notification => {\r\n if (notificationNames.indexOf(notification.name) !== -1) {\r\n this.#watcher.notify(notification);\r\n }\r\n });\r\n }\r\n}","import { Notification, NotificationHandler } from \"@stateflows/common\";\r\n\r\nexport class Watch {\r\n constructor(\r\n public notificationName: string,\r\n public milisecondsSinceLastNotificationCheck: number \r\n ) {}\r\n\r\n handlers: Array<NotificationHandler<Notification>> = [];\r\n notifications: Array<Notification> = [];\r\n lastNotificationCheck: string;\r\n}","import { IStateflowsClientTransport, IStateflowsClientTransportFactory } from \"@stateflows/common\";\r\nimport { HttpTransport } from \"./http-transport\";\r\n\r\nexport class HttpTransportFactory implements IStateflowsClientTransportFactory {\r\n constructor(private url: string) {}\r\n\r\n getTransport(): Promise<IStateflowsClientTransport> {\r\n return Promise.resolve(new HttpTransport(this.url));\r\n }\r\n}\r\n\r\nexport function UseHttp(url: string): IStateflowsClientTransportFactory {\r\n return new HttpTransportFactory(url);\r\n}"],"mappings":"2UAAA,OAAuE,cAAAA,EAAY,aAAAC,MAA0D,qBCA7I,IAAAC,EAKaC,EAAN,KAAyB,CAG5B,YAAYC,EAAmB,CAF/BC,EAAA,KAAAH,GAMA,aAAwB,CAAC,EAHrBI,EAAA,KAAKJ,EAAWE,EACpB,CAIA,IAAI,YAAyB,CACzB,OAAOG,EAAA,KAAKL,GAAS,EACzB,CAEA,oBAAoBM,EAAoC,CACpD,IAAIC,EAAoB,KAAK,QAAQ,IAAIC,GAASA,EAAM,gBAAgB,EACxEF,EAAc,QAAQG,GAAgB,CAC9BF,EAAkB,QAAQE,EAAa,IAAI,IAAM,IACjDJ,EAAA,KAAKL,GAAS,OAAOS,CAAY,CAEzC,CAAC,CACL,CACJ,EApBIT,EAAA,YCJG,IAAMU,EAAN,KAAY,CACf,YACWC,EACAC,EACT,CAFS,sBAAAD,EACA,2CAAAC,EAGX,cAAqD,CAAC,EACtD,mBAAqC,CAAC,CAHnC,CAKP,EFRA,OAAS,wBAAAC,MAA4B,qBAHrC,IAAAC,EAAAC,EAKaC,EAAN,KAA0D,CAI7D,YAAoBC,EAAa,CAAb,SAAAA,EAHpBC,EAAA,KAAAJ,EAA4C,IAAI,KAChDI,EAAA,KAAAH,EAAkC,CAAC,GAG3BE,EAAI,MAAM,EAAE,GAAK,MACjBA,EAAMA,EAAM,KAGhB,YAAY,SAAY,CAChBE,EAAA,KAAKL,GAAS,OAAS,GAI3BK,EAAA,KAAKL,GAAS,QAAQ,MAAMM,GAAU,CAClC,MAAM,KAAK,KAAKA,EAAO,WAAY,IAAIC,CAAsB,CACjE,CAAC,CACL,EAAG,GAAK,GAAI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AKL,GAAS,IAAIY,EAAU,UAAUU,EAAQ,EAAE,CAAC,EAC1DI,EAAQpB,EAAO,QAAQ,UAAUG,GAASA,EAAM,mBAAqBc,CAAgB,EACrFG,IAAU,IACV,OAAOpB,EAAO,QAAQoB,CAAK,EAE/BrB,EAAA,KAAKL,GAAS,OAAOY,EAAU,UAAUU,EAAQ,EAAE,CAAC,CACxD,CACJ,CACJ,EA1IItB,EAAA,YACAC,EAAA,YGJG,IAAM0B,EAAN,KAAwE,CAC3E,YAAoBC,EAAa,CAAb,SAAAA,CAAc,CAElC,cAAoD,CAChD,OAAO,QAAQ,QAAQ,IAAIC,EAAc,KAAK,GAAG,CAAC,CACtD,CACJ,EAEO,SAASC,EAAQF,EAAgD,CACpE,OAAO,IAAID,EAAqBC,CAAG,CACvC","names":["SendResult","JsonUtils","_watcher","NotificationTarget","watcher","__privateAdd","__privateSet","__privateGet","notifications","notificationNames","watch","notification","Watch","notificationName","milisecondsSinceLastNotificationCheck","NotificationsRequest","_targets","_notificationIds","HttpTransport","url","__privateAdd","__privateGet","target","NotificationsRequest","responseTime","watch","notifications","notification","JsonUtils","__privateSet","behaviorId","event","eventNameParts","eventName","stateflowsResponse","response","validation","SendResult","watcher","notificationName","NotificationTarget","Watch","index","HttpTransportFactory","url","HttpTransport","UseHttp"]}