@stateflows/http-client
Version:
HTTP-based client for Stateflows framework
2 lines • 3.79 kB
JavaScript
var N=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var C=s=>{throw TypeError(s)};var b=(s,t)=>{for(var i in t)N(s,i,{get:t[i],enumerable:!0})},A=(s,t,i,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of k(t))!P.call(s,a)&&a!==i&&N(s,a,{get:()=>t[a],enumerable:!(e=W(t,a))||e.enumerable});return s};var B=s=>A(N({},"__esModule",{value:!0}),s);var I=(s,t,i)=>t.has(s)||C("Cannot "+i);var n=(s,t,i)=>(I(s,t,"read from private field"),i?i.call(s):t.get(s)),h=(s,t,i)=>t.has(s)?C("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(s):t.set(s,i),p=(s,t,i,e)=>(I(s,t,"write to private field"),e?e.call(s,i):t.set(s,i),i);var E={};b(E,{UseHttp:()=>T});module.exports=B(E);var r=require("@stateflows/common");var c,d=class{constructor(t){h(this,c);this.watches=[];p(this,c,t)}get behaviorId(){return n(this,c).id}handleNotifications(t){let i=this.watches.map(e=>e.notificationName);t.forEach(e=>{i.indexOf(e.name)!==-1&&n(this,c).notify(e)})}};c=new WeakMap;var u=class{constructor(t,i){this.notificationName=t;this.milisecondsSinceLastNotificationCheck=i;this.handlers=[];this.notifications=[]}};var S=require("@stateflows/common");var o,m,g=class{constructor(t){this.url=t;h(this,o,new Map);h(this,m,[]);t.slice(-1)!="/"&&(t=t+"/"),setInterval(async()=>{n(this,o).size!==0&&n(this,o).forEach(async i=>{await this.send(i.behaviorId,new S.NotificationsRequest)})},10*1e3)}updateTimestamp(t){n(this,o).forEach(i=>{i.watches.forEach(e=>{e.lastNotificationCheck=t,delete e.milisecondsSinceLastNotificationCheck})})}handleNotifications(t,i=null){i!==null&&this.updateTimestamp(i),t.forEach(e=>{if(n(this,m).includes(e.id))return;delete e.senderId.behaviorClass.environment;let a=n(this,o).get(r.JsonUtils.stringify(e.senderId));typeof a!="undefined"&&a.watches.forEach(l=>{l.notificationName===e.name&&a.handleNotifications([e])})}),p(this,m,t.map(e=>e.id))}getWatches(t){return n(this,o).has(r.JsonUtils.stringify(t))?n(this,o).get(r.JsonUtils.stringify(t)).watches.map(e=>({notificationName:e.notificationName,lastNotificationCheck:e.lastNotificationCheck,milisecondsSinceLastNotificationCheck:e.milisecondsSinceLastNotificationCheck!==null?Date.now()-e.milisecondsSinceLastNotificationCheck:null})):[]}async getAvailableClasses(){return await(await fetch(`${this.url}stateflows/availableClasses`)).json()}async send(t,i){let e=i.$type.split(",")[0].split("."),a=e[e.length-1];a==="CompoundRequest"&&(a=i.events.map(R=>{let v=R.$type.split(",")[0].split(".");return v[v.length-1]}).join(","));let f=await(await fetch(`${this.url}stateflows/send?${a}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:r.JsonUtils.stringify({$type:"Stateflows.Common.Transport.Classes.StateflowsRequest, Stateflows.Common.Transport",behaviorId:t,event:i,watches:this.getWatches(t)})})).json(),w=f.response,x=f.validation;return w&&(i.response=w),this.handleNotifications(f.notifications,f.responseTime),new r.SendResult(i,f.eventStatus,x)}async watch(t,i){let e=n(this,o).has(r.JsonUtils.stringify(t.id))?n(this,o).get(r.JsonUtils.stringify(t.id)):new d(t);n(this,o).set(r.JsonUtils.stringify(t.id),e),e.watches.findIndex(l=>l.notificationName===i)===-1&&e.watches.push(new u(i,Date.now()))}async unwatch(t,i){if(n(this,o).has(r.JsonUtils.stringify(t.id))){let e=n(this,o).get(r.JsonUtils.stringify(t.id)),a=e.watches.findIndex(l=>l.notificationName===i);a!==-1&&delete e.watches[a],n(this,o).delete(r.JsonUtils.stringify(t.id))}}};o=new WeakMap,m=new WeakMap;var y=class{constructor(t){this.url=t}getTransport(){return Promise.resolve(new g(this.url))}};function T(s){return new y(s)}0&&(module.exports={UseHttp});
//# sourceMappingURL=index.js.map