UNPKG

@stateflows/http-client

Version:

HTTP-based client for Stateflows framework

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