agljs
Version:
AGL.js is a lightweight framework for integrating web apps with Epic's Active Guidelines platform, using modern JavaScript features. Manage action queuing, event handling, subscriptions, and state within Epic Hyperspace.
9 lines (8 loc) • 7.43 kB
JavaScript
/*!
* agljs
* Author: Joshua Faulkenberry
* License: Kopimi
* Copyright 2025
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).agl=t()}(this,(function(){"use strict";function e(e,t,s,i){return new(s||(s=Promise))((function(n,r){function o(e){try{l(i.next(e))}catch(e){r(e)}}function a(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?n(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(o,a)}l((i=i.apply(e,t||[])).next())}))}function t(e,t,s,i){if("a"===s&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===s?i:"a"===s?i.call(e):i?i.value:t.get(e)}function s(e,t,s,i,n){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?n.call(e,s):n?n.value=s:t.set(e,s),s}var i,n,r,o,a,l,c,h,f,u,d,w,m,p,v,g,b,y,k;"function"==typeof SuppressedError&&SuppressedError;class E{constructor(e={}){i.add(this),o.set(this,null),a.set(this,!1),l.set(this,2e3),c.set(this,null),h.set(this,[]),f.set(this,!1),u.set(this,{availableActions:[],interfaceVersion:null,readOnly:!1,token:null}),d.set(this,{}),w.set(this,{}),m.set(this,"Epic.Clinical.Informatics.Web."),p.set(this,{5:"Action in progress. This means that two messages were posted back-to-back without waiting for a response from the first.",7:"An action was posted which requires a token, but no token was provided.",9:"An action was attempted which does not exist.",15:"An action is not allowed during closing.",16:"An invalid SubscriptionRequest was sent.",18:"A browser launch was attempted for a URL that is not allowlisted."});const{debug:g=t(this,a,"f"),timeout:b=t(this,l,"f"),subscribe:y={}}=e,E=function(e,t){var s={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(s[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(i=Object.getOwnPropertySymbols(e);n<i.length;n++)t.indexOf(i[n])<0&&Object.prototype.propertyIsEnumerable.call(e,i[n])&&(s[i[n]]=e[i[n]])}return s}(e,["debug","timeout","subscribe"]);let A=!0;if(t(n,n,"f",r)?(console.warn("[AGL] Reconfiguring existing instance. Previous settings may be overwritten."),A=!1):s(n,n,this,"f",r),s(t(n,n,"f",r),a,g,"f"),s(t(n,n,"f",r),l,b,"f"),Object.keys(E).forEach((e=>{if(e.startsWith("on")){const s=e.replace(/^on/,"").toLowerCase();t(t(n,n,"f",r),d,"f")[s]=E[e]}})),!A)return t(n,n,"f",r);if(s(this,w,y,"f"),window!==window.parent){const e={};if(Object.keys(t(this,w,"f")).length){e.SubscriptionRequests=[];for(const[s,i]of Object.entries(t(this,w,"f")))e.SubscriptionRequests.push({EventName:s,EventArgs:i})}s(this,c,t(this,i,"m",v).call(this,"InitiateHandshake").then((()=>(s(this,o,!0,"f"),t(this,i,"m",k).call(this,"Handshake complete, AGL is initialized"),console.log("[AGL] Initialized..."),!0))).catch((e=>(s(this,o,!1,"f"),console.error("[AGL] Handshake failed:",e.message),!1))),"f")}else s(this,o,!1,"f"),console.log("[AGL] Not in Epic..."),t(this,i,"m",k).call(this,"AGL is not initialized")}get active(){return null!==t(this,o,"f")?t(this,o,"f"):t(this,c,"f")}get details(){return t(this,u,"f")}set debug(e){s(this,a,e,"f")}on(e,s){return t(this,d,"f")[e]=s,this}do(s){return e(this,arguments,void 0,(function*(e,s=null,n=!1){return!0!==t(this,o,"f")?Promise.resolve(!1):t(this,i,"m",y).call(this,(()=>t(this,i,"m",v).call(this,e,s)),n)}))}}return n=E,o=new WeakMap,a=new WeakMap,l=new WeakMap,c=new WeakMap,h=new WeakMap,f=new WeakMap,u=new WeakMap,d=new WeakMap,w=new WeakMap,m=new WeakMap,p=new WeakMap,i=new WeakSet,v=function(e,s=null){const n=-1===e.indexOf(".")?t(this,m,"f"):"";if("InitiateHandshake"!==e&&!t(this,u,"f").availableActions.includes(n+e)){const s=new Error(`Invalid action: ${e}`);return t(this,d,"f").error?(t(this,d,"f").error({message:s.message,details:[]}),Promise.resolve(!1)):Promise.reject(s)}return new Promise(((r,o)=>{const a={token:t(this,u,"f").token,action:n+e};s&&(a.args=s);const c=e=>{const{success:s,error:n}=t(this,i,"m",g).call(this,e);window.removeEventListener("message",c),s?r(!0):t(this,d,"f").error?(t(this,d,"f").error(n||{message:"Unknown error",details:[]}),r(!1)):o(new Error((null==n?void 0:n.message)||"Unknown error occurred"))};window.addEventListener("message",c),t(this,i,"m",k).call(this,"Sending message:",a),window.parent.postMessage(a,"*"),setTimeout((()=>{window.removeEventListener("message",c);const e=new Error("Timeout waiting for response");t(this,d,"f").error?(t(this,d,"f").error({message:"Timeout waiting for response",details:[`Action: ${a.action}`,`Timeout: ${t(this,l,"f")}ms`]}),r(!1)):o(e)}),t(this,l,"f"))}))},g=function(e){var s,n;if(t(this,i,"m",k).call(this,"Received message:",e.data),!e.data||"object"!=typeof e.data||!("token"in e.data)&&!("actions"in e.data))return t(this,i,"m",k).call(this,"Invalid message received:",e.data),{success:!1,error:{message:"Invalid message format",details:[]}};let r,o=!1;for(const s in e.data){const n=t(this,i,"m",b).call(this,s)(e.data[s],e.data);"actionExecuted"===s?o=n:"error"===s&&(r=n)}return r&&(t(this,i,"m",k).call(this,"Error received:",r),null===(n=(s=t(this,d,"f")).error)||void 0===n||n.call(s,r)),{success:o,error:r}},b=function(e){var s;const n={actionExecuted:(e,t)=>"token"in t||e,actions:(e,s)=>{t(this,u,"f").availableActions.push(...e)},error:(e,s)=>{const i={message:e,details:[]};return s.errorCodes&&s.errorCodes.length&&(i.details=t(this,d,"f").error?s.errorCodes:s.errorCodes.map((e=>{var s;return null!==(s=t(this,p,"f")[e])&&void 0!==s?s:`Unknown error code: ${e}`}))),i},EventName:(e,s)=>{var n,r,o;t(this,i,"m",k).call(this,"AGL event received:",e),null===(r=(n=t(this,d,"f")).aglEvent)||void 0===r||r.call(n,{name:e,args:null!==(o=s.EventArgs)&&void 0!==o?o:null})},history:(e,s)=>{var n,r;t(this,i,"m",k).call(this,"History navigation event:",e),null===(r=(n=t(this,d,"f")).navigate)||void 0===r||r.call(n,{direction:e})},historyPackage:(e,s)=>{var n,r;t(this,i,"m",k).call(this,"Received historyPackage:",e);const{state:o,fromHibernation:a}=e;null===(r=(n=t(this,d,"f")).reload)||void 0===r||r.call(n,{state:o,fromHibernation:a})},isContextReadOnly:(e,s)=>{t(this,u,"f").readOnly=e},subscriptionResults:(e,s)=>{var n,r;t(this,i,"m",k).call(this,"Subscription results:",e),null===(r=(n=t(this,d,"f")).subscribed)||void 0===r||r.call(n,e)},token:(e,s)=>{t(this,u,"f").token=e},version:(e,s)=>{t(this,u,"f").interfaceVersion=e}};return null!==(s=n[e])&&void 0!==s?s:(s,i)=>{t(this,u,"f")[e]=s}},y=function(i,n=!1){const r=()=>{t(this,f,"f")||0===t(this,h,"f").length||(s(this,f,!0,"f"),t(this,h,"f").shift()().finally((()=>{s(this,f,!1,"f"),r()})))};return new Promise(((o,a)=>{t(this,h,"f").push((()=>e(this,void 0,void 0,(function*(){try{const e=yield i();o(e)}catch(e){a(e),n&&(console.warn("[AGL] Queue stopped due to error:",e),s(this,h,[],"f"))}finally{r()}})))),!t(this,f,"f")&&r()}))},k=function(...e){t(this,a,"f")&&console.debug("[AGL]",...e)},r={value:null},E}));
//# sourceMappingURL=agl.min.js.map