codicent-app-sdk 0.3.48 → 0.3.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"appState.d.ts","sourceRoot":"","sources":["../../../src/utils/appState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAIhE,MAAM,WAAW,YAAY;IAC3B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,SAAS,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,
|
1
|
+
{"version":3,"file":"appState.d.ts","sourceRoot":"","sources":["../../../src/utils/appState.ts"],"names":[],"mappings":"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"}
|
@@ -1 +1 @@
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../_virtual/index.js"),e=require("../config/index.js");class i extends t.distExports.StateMachine{constructor(t,e){super(),this.context={isAuthenticated:void 0,hasAccess:!1,error:null,errorType:void 0,apps:[],hasApps:!1,appsLoaded:!1},this.currentStateName="",this.resetContext(),this.service=t,this.onStateChange=e,this.allStates=this.initializeStateMachine()}resetContext(){this.context={isAuthenticated:!1,isRegistered:void 0,hasValidPurchase:void 0,hasAccess:!1,selectedApp:localStorage.getItem("selectedApp")||void 0,error:null,errorType:void 0,accessToken:void 0,nickname:void 0,apps:[],hasApps:!1,appsLoaded:!1},e.getConfigValue("APP_PREFIX")&&this.context.selectedApp&&!this.context.selectedApp.startsWith(e.getConfigValue("APP_PREFIX"))?this.context.selectedApp=void 0:this.context.selectedApp}update(){super.update()}handlePurchase(){window.location.href="./#/purchase"}initializeStateMachine(){const i=new t.distExports.State,n=new t.distExports.State({entering:()=>{this.context.selectedApp&&!this.service.codicent&&(this.service.codicent=this.context.selectedApp)}}),s=new t.distExports.State,o=new t.distExports.State,r=new t.distExports.State({entering:()=>{this.service.findUsername().then((t=>{t?this.service.nicknameExists(t).then((e=>{e?this.updateContext({error:"Användarnamnet finns redan.",errorType:"UsernameExists"}):this.service.registerUser(t,this.context.user.email,this.context.user.sub,this.context.accessToken).then((()=>{this.service.setToken(this.context.accessToken),this.updateContext({nickname:t,isRegistered:!0})})).catch((t=>{console.warn("Failed to register account",t),this.updateContext({error:"Ett fel uppstod vid registrering. Försök igen senare.",errorType:"ErrorRegistering"})}))})).catch((t=>{console.warn("Failed to check username",t),this.updateContext({error:"Ett fel uppstod vid kontroll av användarnamn.",errorType:"ErrorCheckingUsername"})})):this.updateContext({error:"Användarnamn kunde inte hittas.",errorType:"NoUsernameFound"})})).catch((t=>{console.warn("Failed to find username",t),this.updateContext({error:"Ett fel uppstod vid sökning av användarnamn.",errorType:"ErrorFindingUsername"})}))}}),a=new t.distExports.State,c=new t.distExports.State({entering:()=>{this.checkPurchase()}}),d=new t.distExports.State({entering:()=>{this.handlePurchase()}}),h=new t.distExports.State({entering:()=>{this.getApps()}}),p=new t.distExports.State,l=new t.distExports.State,g=new t.distExports.State({}),x=new t.distExports.State,u=new t.distExports.State({entering:()=>{this.context.nickname&&(localStorage.setItem("nickname",this.context.nickname),this.service.getUserInfo(this.context.nickname).then((t=>{const e=t?.name||t?.nickname||"";this.updateContext({name:e})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorLoadingUser"}))))}}),A=new t.distExports.State({entering:()=>{localStorage.removeItem("selectedApp"),localStorage.removeItem("nickname"),localStorage.removeItem("codicent"),sessionStorage.clear(),this.updateContext({isAuthenticated:!1,isRegistered:void 0,apps:[],hasApps:!1,selectedApp:void 0,accessToken:void 0,user:void 0}),setTimeout((()=>{this.current=this.allStates.initial}),1e3)}}),T=new t.distExports.State({entering:()=>{this.service.createApp().then((t=>{t?(this.service.codicent=t,this.updateContext({selectedApp:t,apps:[...this.context.apps||[],{id:t,title:t,logo:""}]})):this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})})).catch((t=>{console.warn("Error creating app",t),this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})}))}}),C=new t.distExports.State({entering:()=>{this.service.loginUser(this.context.user.sub,this.context.accessToken).then((t=>{null===t?this.updateContext({isRegistered:!1}):""===t?this.updateContext({isRegistered:!1,error:"Kunde inte logga in hos Codicent.",errorType:"CodicentLoginFailed"}):(this.service.setToken(this.context.accessToken),this.service.getNickname().then((t=>{t?this.updateContext({isRegistered:!0,nickname:t}):this.updateContext({error:"Kunde inte hitta användarnamn.",errorType:"ErrorFindingUsername"})})).catch((()=>{this.updateContext({error:"Fel vid hämtning av användarnamn.",errorType:"ErrorFindingUsername"})})))})).catch((()=>{this.updateContext({isRegistered:!1,error:"Fel vid inloggning hos Codicent.",errorType:"CodicentLoginError"})}))}}),E=new t.distExports.State({entering:()=>{this.service.setToken(e.getConfigValue("ANONYMOUS_TOKEN")),this.service.codicent=e.getConfigValue("ANONYMOUS_CODICENT")||e.getConfigValue("APP_NAME")||""}});i.addTransition(n,{condition:()=>!0}),n.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),n.addTransition(x,{condition:()=>Boolean(this.context.error)}),n.addTransition(E,{condition:()=>!0===this.context.isAnonymous&&!!e.getConfigValue("ANONYMOUS_TOKEN")}),n.addTransition(s,{condition:()=>!1===this.context.isAuthenticated}),s.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),s.addTransition(x,{condition:()=>!!this.context.error}),o.addTransition(C,{}),C.addTransition(r,{condition:()=>!1===this.context.isRegistered}),C.addTransition(a,{condition:()=>!0===this.context.isRegistered}),C.addTransition(x,{condition:()=>!!this.context.error}),a.addTransition(u,{condition:()=>!!this.context.nickname}),a.addTransition(x,{condition:()=>!this.context.nickname,action:()=>this.updateContext({error:"Missing nickname after registration",errorType:"ErrorLoadingUser"})}),c.addTransition(d,{condition:()=>!1===this.context.hasValidPurchase}),c.addTransition(h,{condition:()=>!0===this.context.hasValidPurchase}),h.addTransition(x,{condition:()=>!!this.context.error,action:()=>console.log("getting apps -> error")}),h.addTransition(p,{condition:()=>!!e.getConfigValue("APP_NAME")&&this.context.hasApps}),h.addTransition(T,{condition:()=>!e.getConfigValue("APP_NAME")&&this.context.appsLoaded&&0===this.context.apps.length,action:()=>this.updateContext({selectedApp:void 0})}),h.addTransition(p,{condition:()=>!e.getConfigValue("APP_NAME")&&this.context.hasApps&&this.context.apps.length>0}),T.addTransition(p,{condition:()=>!!this.context.selectedApp}),T.addTransition(x,{condition:()=>!!this.context.error}),p.addTransition(g,{condition:()=>!e.getConfigValue("APP_NAME")&&this.context.apps.some((t=>t.id===this.context.selectedApp)),action:()=>{this.service.codicent=this.context.selectedApp||"",localStorage.setItem("selectedApp",this.context.selectedApp||"")}}),p.addTransition(g,{condition:()=>!e.getConfigValue("APP_NAME")&&!this.context.selectedApp,action:()=>{const t=this.context.apps[0].id;this.service.codicent=t,localStorage.setItem("selectedApp",t),this.updateContext({selectedApp:t})}}),p.addTransition(g,{condition:()=>this.context.apps.some((t=>t.id===e.getConfigValue("APP_NAME"))),action:()=>{this.service.codicent=e.getConfigValue("APP_NAME"),this.updateContext({selectedApp:e.getConfigValue("APP_NAME")})}}),p.addTransition(l,{condition:()=>!this.context.apps.some((t=>t.id===this.context.selectedApp||t.id===e.getConfigValue("APP_NAME"))),action:()=>console.log("checking access -> no access",this.context.apps,this.context.selectedApp,e.getConfigValue("APP_NAME"))}),g.addTransition(u,{condition:()=>!!localStorage.getItem("selectedApp")&&localStorage.getItem("selectedApp")!==this.context.selectedApp,action:()=>{this.service.codicent=localStorage.getItem("selectedApp")||"",this.updateContext({selectedApp:localStorage.getItem("selectedApp")||void 0})}}),r.addTransition(a,{condition:()=>!0===this.context.isRegistered}),r.addTransition(x,{condition:()=>!!this.context.error}),d.addTransition(c,{condition:()=>void 0===this.context.hasValidPurchase,action:()=>{window.history.replaceState({},document.title,window.location.pathname),this.checkPurchase()}}),[n,h,c,C,r].forEach((t=>{t.addTransition(x,{condition:()=>this.context.error?.includes("network")||this.context.error?.includes("connectivity")||!1}),t.addTransition(x,{condition:()=>!!this.context.error})})),[o,a,g,l].forEach((t=>{t.addTransition(A,{condition:()=>!1===this.context.isAuthenticated})})),a.addTransition(u,{condition:()=>!this.context.user&&!!this.context.nickname}),u.addTransition(c,{condition:()=>!!this.context.name&&e.getConfigValue("SUBSCRIPTION_NEEDED")}),u.addTransition(h,{condition:()=>!!this.context.name&&!e.getConfigValue("SUBSCRIPTION_NEEDED")}),x.addTransition(c,{condition:()=>!this.context.error&&"ErrorCheckingPurchase"!==this.context.errorType}),x.addTransition(h,{condition:()=>!this.context.error&&"ErrorGettingApps"!==this.context.errorType}),x.addTransition(p,{condition:()=>!this.context.error&&"ErrorCheckingAccess"!==this.context.errorType}),x.addTransition(u,{condition:()=>!this.context.error&&"ErrorLoadingUser"!==this.context.errorType}),x.addTransition(T,{condition:()=>!this.context.error&&"ErrorCreatingApp"!==this.context.errorType}),x.addTransition(C,{condition:()=>!this.context.error&&"CodicentLoginFailed"!==this.context.errorType}),A.addTransition(i,{condition:()=>!this.context.isAuthenticated&&!this.context.accessToken});const S={initial:i,authenticating:n,notAuthenticated:s,authenticated:o,checkingCodicentAccess:C,registering:r,registered:a,checkingPurchase:c,needsPurchase:d,gettingApps:h,creatingApp:T,checkingAccess:p,noAccess:l,error:x,loadingUser:u,loggingOut:A,hasAccess:g,anonymous:E};return this.states.push(...Object.values(S)),S}get error(){return this.context.error}checkPurchase(){this.service.checkSubscriptionStatus().then((t=>this.updateContext({hasValidPurchase:"active"===t}))).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorCheckingPurchase"})))}getApps(){this.service.getApps().then((t=>{if(null===t)throw new Error("Failed to fetch apps.");e.getConfigValue("APP_PREFIX")&&(t=t.filter((t=>t.id.startsWith(e.getConfigValue("APP_PREFIX"))))),this.updateContext({apps:t,hasApps:!0,appsLoaded:!0}),this.context.selectedApp&&!t.some((t=>t.id===this.context.selectedApp))&&this.updateContext({selectedApp:void 0})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorGettingApps"})))}setUser(t){this.context.user=t}getContext(){return{...this.context}}updateContext(t){this.context={...this.context,...t}}getCurrentStateName(){const t=Object.entries(this.allStates).find((([t,e])=>e===this.current));return t?t[0]:""}logout(){localStorage.clear(),sessionStorage.clear(),this.resetContext()}setSelectedApp(t){localStorage.setItem("selectedApp",t),this.update()}isBusy(){return this.current===this.allStates.gettingApps||this.current===this.allStates.checkingPurchase||this.current===this.allStates.checkingAccess||this.current===this.allStates.checkingCodicentAccess||this.current===this.allStates.registering||this.current===this.allStates.loadingUser||this.current===this.allStates.creatingApp||this.current===this.allStates.loggingOut}}exports.AppStateMachine=i,exports.createAppStateMachine=(t,e)=>new i(t,e),exports.default=i;
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("../_virtual/index.js"),e=require("../config/index.js");class i extends t.distExports.StateMachine{constructor(t,e){super(),this.context={isAuthenticated:void 0,hasAccess:!1,error:null,errorType:void 0,apps:[],hasApps:!1,appsLoaded:!1},this.currentStateName="",this.resetContext(),this.service=t,this.onStateChange=e,this.allStates=this.initializeStateMachine()}resetContext(){this.context={isAuthenticated:!1,isRegistered:void 0,hasValidPurchase:void 0,hasAccess:!1,selectedApp:localStorage.getItem("selectedApp")||void 0,error:null,errorType:void 0,accessToken:void 0,nickname:void 0,apps:[],hasApps:!1,appsLoaded:!1},e.getConfigValue("APP_PREFIX")&&this.context.selectedApp&&!this.context.selectedApp.startsWith(e.getConfigValue("APP_PREFIX"))?this.context.selectedApp=void 0:this.context.selectedApp}update(){super.update()}handlePurchase(){window.location.href="./#/purchase"}initializeStateMachine(){const i=new t.distExports.State,n=new t.distExports.State({entering:()=>{this.context.selectedApp&&!this.service.codicent&&(this.service.codicent=this.context.selectedApp)}}),s=new t.distExports.State,o=new t.distExports.State,r=new t.distExports.State({entering:()=>{this.service.findUsername().then((t=>{t?this.service.nicknameExists(t).then((e=>{e?this.updateContext({error:"Användarnamnet finns redan.",errorType:"UsernameExists"}):this.service.registerUser(t,this.context.user.email,this.context.user.sub,this.context.accessToken).then((()=>{this.service.setToken(this.context.accessToken),this.updateContext({nickname:t,isRegistered:!0})})).catch((t=>{console.warn("Failed to register account",t),this.updateContext({error:"Ett fel uppstod vid registrering. Försök igen senare.",errorType:"ErrorRegistering"})}))})).catch((t=>{console.warn("Failed to check username",t),this.updateContext({error:"Ett fel uppstod vid kontroll av användarnamn.",errorType:"ErrorCheckingUsername"})})):this.updateContext({error:"Användarnamn kunde inte hittas.",errorType:"NoUsernameFound"})})).catch((t=>{console.warn("Failed to find username",t),this.updateContext({error:"Ett fel uppstod vid sökning av användarnamn.",errorType:"ErrorFindingUsername"})}))}}),a=new t.distExports.State,c=new t.distExports.State({entering:()=>{this.checkPurchase()}}),d=new t.distExports.State({entering:()=>{this.handlePurchase()}}),h=new t.distExports.State({entering:()=>{this.getApps()}}),p=new t.distExports.State,l=new t.distExports.State,g=new t.distExports.State({}),x=new t.distExports.State,u=new t.distExports.State({entering:()=>{this.context.nickname&&(localStorage.setItem("nickname",this.context.nickname),this.service.getUserInfo(this.context.nickname).then((t=>{const e=t?.name||t?.nickname||"";this.updateContext({name:e})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorLoadingUser"}))))}}),A=new t.distExports.State({entering:()=>{localStorage.removeItem("selectedApp"),localStorage.removeItem("nickname"),localStorage.removeItem("codicent"),sessionStorage.clear(),this.updateContext({isAuthenticated:!1,isRegistered:void 0,apps:[],hasApps:!1,selectedApp:void 0,accessToken:void 0,user:void 0}),setTimeout((()=>{this.current=this.allStates.initial}),1e3)}}),T=new t.distExports.State({entering:()=>{this.service.createApp().then((t=>{t?(this.service.codicent=t,this.updateContext({selectedApp:t,apps:[...this.context.apps||[],{id:t,title:t,logo:""}]})):this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})})).catch((t=>{console.warn("Error creating app",t),this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})}))}}),C=new t.distExports.State({entering:()=>{this.service.loginUser(this.context.user.sub,this.context.accessToken).then((t=>{null===t?this.updateContext({isRegistered:!1}):""===t?this.updateContext({isRegistered:!1,error:"Kunde inte logga in hos Codicent.",errorType:"CodicentLoginFailed"}):(this.service.setToken(this.context.accessToken),this.service.getNickname().then((t=>{t?this.updateContext({isRegistered:!0,nickname:t}):this.updateContext({error:"Kunde inte hitta användarnamn.",errorType:"ErrorFindingUsername"})})).catch((()=>{this.updateContext({error:"Fel vid hämtning av användarnamn.",errorType:"ErrorFindingUsername"})})))})).catch((()=>{this.updateContext({isRegistered:!1,error:"Fel vid inloggning hos Codicent.",errorType:"CodicentLoginError"})}))}}),E=new t.distExports.State({entering:()=>{this.service.setToken(e.getConfigValue("ANONYMOUS_TOKEN")),this.service.codicent=e.getConfigValue("ANONYMOUS_CODICENT")||e.getConfigValue("APP_NAME")||""}});i.addTransition(n,{condition:()=>!0}),n.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),n.addTransition(x,{condition:()=>Boolean(this.context.error)}),n.addTransition(E,{condition:()=>!0===this.context.isAnonymous&&!!e.getConfigValue("ANONYMOUS_TOKEN")}),n.addTransition(s,{condition:()=>!1===this.context.isAuthenticated}),s.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),s.addTransition(x,{condition:()=>!!this.context.error}),o.addTransition(C,{}),C.addTransition(r,{condition:()=>!1===this.context.isRegistered}),C.addTransition(a,{condition:()=>!0===this.context.isRegistered}),C.addTransition(x,{condition:()=>!!this.context.error}),a.addTransition(u,{condition:()=>!!this.context.nickname}),a.addTransition(x,{condition:()=>!this.context.nickname,action:()=>this.updateContext({error:"Missing nickname after registration",errorType:"ErrorLoadingUser"})}),c.addTransition(d,{condition:()=>!1===this.context.hasValidPurchase}),c.addTransition(h,{condition:()=>!0===this.context.hasValidPurchase}),h.addTransition(x,{condition:()=>!!this.context.error,action:()=>console.log("getting apps -> error")}),h.addTransition(p,{condition:()=>!!e.getConfigValue("APP_NAME")&&this.context.hasApps}),h.addTransition(T,{condition:()=>!e.getConfigValue("APP_NAME")&&this.context.appsLoaded&&0===this.context.apps.length,action:()=>this.updateContext({selectedApp:void 0})}),h.addTransition(p,{condition:()=>!e.getConfigValue("APP_NAME")&&this.context.hasApps&&this.context.apps.length>0}),T.addTransition(p,{condition:()=>!!this.context.selectedApp}),T.addTransition(x,{condition:()=>!!this.context.error}),p.addTransition(g,{condition:()=>!e.getConfigValue("APP_NAME")&&this.context.apps.some((t=>t.id===this.context.selectedApp)),action:()=>{this.service.codicent=this.context.selectedApp||"",localStorage.setItem("selectedApp",this.context.selectedApp||"")}}),p.addTransition(g,{condition:()=>!e.getConfigValue("APP_NAME")&&!this.context.selectedApp,action:()=>{const t=this.context.apps[0].id;this.context.selectedApp!==t&&(this.service.codicent=t,localStorage.setItem("selectedApp",t),this.updateContext({selectedApp:t}))}}),p.addTransition(g,{condition:()=>this.context.apps.some((t=>t.id===e.getConfigValue("APP_NAME"))),action:()=>{const t=e.getConfigValue("APP_NAME");this.context.selectedApp!==t&&(this.service.codicent=t,this.updateContext({selectedApp:t}))}}),p.addTransition(l,{condition:()=>!this.context.apps.some((t=>t.id===this.context.selectedApp||t.id===e.getConfigValue("APP_NAME"))),action:()=>console.log("checking access -> no access",this.context.apps,this.context.selectedApp,e.getConfigValue("APP_NAME"))}),g.addTransition(u,{condition:()=>!!localStorage.getItem("selectedApp")&&localStorage.getItem("selectedApp")!==this.context.selectedApp,action:()=>{this.service.codicent=localStorage.getItem("selectedApp")||"",this.updateContext({selectedApp:localStorage.getItem("selectedApp")||void 0})}}),r.addTransition(a,{condition:()=>!0===this.context.isRegistered}),r.addTransition(x,{condition:()=>!!this.context.error}),d.addTransition(c,{condition:()=>void 0===this.context.hasValidPurchase,action:()=>{window.history.replaceState({},document.title,window.location.pathname),this.checkPurchase()}}),[n,h,c,C,r].forEach((t=>{t.addTransition(x,{condition:()=>this.context.error?.includes("network")||this.context.error?.includes("connectivity")||!1}),t.addTransition(x,{condition:()=>!!this.context.error})})),[o,a,g,l].forEach((t=>{t.addTransition(A,{condition:()=>!1===this.context.isAuthenticated})})),a.addTransition(u,{condition:()=>!this.context.user&&!!this.context.nickname}),u.addTransition(c,{condition:()=>!!this.context.name&&e.getConfigValue("SUBSCRIPTION_NEEDED")}),u.addTransition(h,{condition:()=>!!this.context.name&&!e.getConfigValue("SUBSCRIPTION_NEEDED")}),x.addTransition(c,{condition:()=>!this.context.error&&"ErrorCheckingPurchase"!==this.context.errorType}),x.addTransition(h,{condition:()=>!this.context.error&&"ErrorGettingApps"!==this.context.errorType}),x.addTransition(p,{condition:()=>!this.context.error&&"ErrorCheckingAccess"!==this.context.errorType}),x.addTransition(u,{condition:()=>!this.context.error&&"ErrorLoadingUser"!==this.context.errorType}),x.addTransition(T,{condition:()=>!this.context.error&&"ErrorCreatingApp"!==this.context.errorType}),x.addTransition(C,{condition:()=>!this.context.error&&"CodicentLoginFailed"!==this.context.errorType}),A.addTransition(i,{condition:()=>!this.context.isAuthenticated&&!this.context.accessToken});const S={initial:i,authenticating:n,notAuthenticated:s,authenticated:o,checkingCodicentAccess:C,registering:r,registered:a,checkingPurchase:c,needsPurchase:d,gettingApps:h,creatingApp:T,checkingAccess:p,noAccess:l,error:x,loadingUser:u,loggingOut:A,hasAccess:g,anonymous:E};return this.states.push(...Object.values(S)),S}get error(){return this.context.error}checkPurchase(){this.service.checkSubscriptionStatus().then((t=>this.updateContext({hasValidPurchase:"active"===t}))).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorCheckingPurchase"})))}getApps(){this.service.getApps().then((t=>{if(null===t)throw new Error("Failed to fetch apps.");e.getConfigValue("APP_PREFIX")&&(t=t.filter((t=>t.id.startsWith(e.getConfigValue("APP_PREFIX"))))),this.updateContext({apps:t,hasApps:!0,appsLoaded:!0}),this.context.selectedApp&&!t.some((t=>t.id===this.context.selectedApp))&&this.updateContext({selectedApp:void 0})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorGettingApps"})))}setUser(t){this.context.user=t}getContext(){return{...this.context}}updateContext(t){this.context={...this.context,...t}}getCurrentStateName(){const t=Object.entries(this.allStates).find((([t,e])=>e===this.current));return t?t[0]:""}logout(){localStorage.clear(),sessionStorage.clear(),this.resetContext()}setSelectedApp(t){localStorage.setItem("selectedApp",t),this.update()}isBusy(){return this.current===this.allStates.gettingApps||this.current===this.allStates.checkingPurchase||this.current===this.allStates.checkingAccess||this.current===this.allStates.checkingCodicentAccess||this.current===this.allStates.registering||this.current===this.allStates.loadingUser||this.current===this.allStates.creatingApp||this.current===this.allStates.loggingOut}}exports.AppStateMachine=i,exports.createAppStateMachine=(t,e)=>new i(t,e),exports.default=i;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"appState.d.ts","sourceRoot":"","sources":["../../../src/utils/appState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAIhE,MAAM,WAAW,YAAY;IAC3B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,SAAS,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,
|
1
|
+
{"version":3,"file":"appState.d.ts","sourceRoot":"","sources":["../../../src/utils/appState.ts"],"names":[],"mappings":"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"}
|
@@ -1 +1 @@
|
|
1
|
-
import{d as t}from"../_virtual/index.js";import{getConfigValue as e}from"../config/index.js";class i extends t.StateMachine{constructor(t,e){super(),this.context={isAuthenticated:void 0,hasAccess:!1,error:null,errorType:void 0,apps:[],hasApps:!1,appsLoaded:!1},this.currentStateName="",this.resetContext(),this.service=t,this.onStateChange=e,this.allStates=this.initializeStateMachine()}resetContext(){this.context={isAuthenticated:!1,isRegistered:void 0,hasValidPurchase:void 0,hasAccess:!1,selectedApp:localStorage.getItem("selectedApp")||void 0,error:null,errorType:void 0,accessToken:void 0,nickname:void 0,apps:[],hasApps:!1,appsLoaded:!1},e("APP_PREFIX")&&this.context.selectedApp&&!this.context.selectedApp.startsWith(e("APP_PREFIX"))?this.context.selectedApp=void 0:this.context.selectedApp}update(){super.update()}handlePurchase(){window.location.href="./#/purchase"}initializeStateMachine(){const i=new t.State,n=new t.State({entering:()=>{this.context.selectedApp&&!this.service.codicent&&(this.service.codicent=this.context.selectedApp)}}),s=new t.State,o=new t.State,r=new t.State({entering:()=>{this.service.findUsername().then((t=>{t?this.service.nicknameExists(t).then((e=>{e?this.updateContext({error:"Användarnamnet finns redan.",errorType:"UsernameExists"}):this.service.registerUser(t,this.context.user.email,this.context.user.sub,this.context.accessToken).then((()=>{this.service.setToken(this.context.accessToken),this.updateContext({nickname:t,isRegistered:!0})})).catch((t=>{console.warn("Failed to register account",t),this.updateContext({error:"Ett fel uppstod vid registrering. Försök igen senare.",errorType:"ErrorRegistering"})}))})).catch((t=>{console.warn("Failed to check username",t),this.updateContext({error:"Ett fel uppstod vid kontroll av användarnamn.",errorType:"ErrorCheckingUsername"})})):this.updateContext({error:"Användarnamn kunde inte hittas.",errorType:"NoUsernameFound"})})).catch((t=>{console.warn("Failed to find username",t),this.updateContext({error:"Ett fel uppstod vid sökning av användarnamn.",errorType:"ErrorFindingUsername"})}))}}),a=new t.State,c=new t.State({entering:()=>{this.checkPurchase()}}),d=new t.State({entering:()=>{this.handlePurchase()}}),h=new t.State({entering:()=>{this.getApps()}}),p=new t.State,l=new t.State,g=new t.State({}),x=new t.State,u=new t.State({entering:()=>{this.context.nickname&&(localStorage.setItem("nickname",this.context.nickname),this.service.getUserInfo(this.context.nickname).then((t=>{const e=t?.name||t?.nickname||"";this.updateContext({name:e})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorLoadingUser"}))))}}),A=new t.State({entering:()=>{localStorage.removeItem("selectedApp"),localStorage.removeItem("nickname"),localStorage.removeItem("codicent"),sessionStorage.clear(),this.updateContext({isAuthenticated:!1,isRegistered:void 0,apps:[],hasApps:!1,selectedApp:void 0,accessToken:void 0,user:void 0}),setTimeout((()=>{this.current=this.allStates.initial}),1e3)}}),T=new t.State({entering:()=>{this.service.createApp().then((t=>{t?(this.service.codicent=t,this.updateContext({selectedApp:t,apps:[...this.context.apps||[],{id:t,title:t,logo:""}]})):this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})})).catch((t=>{console.warn("Error creating app",t),this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})}))}}),m=new t.State({entering:()=>{this.service.loginUser(this.context.user.sub,this.context.accessToken).then((t=>{null===t?this.updateContext({isRegistered:!1}):""===t?this.updateContext({isRegistered:!1,error:"Kunde inte logga in hos Codicent.",errorType:"CodicentLoginFailed"}):(this.service.setToken(this.context.accessToken),this.service.getNickname().then((t=>{t?this.updateContext({isRegistered:!0,nickname:t}):this.updateContext({error:"Kunde inte hitta användarnamn.",errorType:"ErrorFindingUsername"})})).catch((()=>{this.updateContext({error:"Fel vid hämtning av användarnamn.",errorType:"ErrorFindingUsername"})})))})).catch((()=>{this.updateContext({isRegistered:!1,error:"Fel vid inloggning hos Codicent.",errorType:"CodicentLoginError"})}))}}),S=new t.State({entering:()=>{this.service.setToken(e("ANONYMOUS_TOKEN")),this.service.codicent=e("ANONYMOUS_CODICENT")||e("APP_NAME")||""}});i.addTransition(n,{condition:()=>!0}),n.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),n.addTransition(x,{condition:()=>Boolean(this.context.error)}),n.addTransition(S,{condition:()=>!0===this.context.isAnonymous&&!!e("ANONYMOUS_TOKEN")}),n.addTransition(s,{condition:()=>!1===this.context.isAuthenticated}),s.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),s.addTransition(x,{condition:()=>!!this.context.error}),o.addTransition(m,{}),m.addTransition(r,{condition:()=>!1===this.context.isRegistered}),m.addTransition(a,{condition:()=>!0===this.context.isRegistered}),m.addTransition(x,{condition:()=>!!this.context.error}),a.addTransition(u,{condition:()=>!!this.context.nickname}),a.addTransition(x,{condition:()=>!this.context.nickname,action:()=>this.updateContext({error:"Missing nickname after registration",errorType:"ErrorLoadingUser"})}),c.addTransition(d,{condition:()=>!1===this.context.hasValidPurchase}),c.addTransition(h,{condition:()=>!0===this.context.hasValidPurchase}),h.addTransition(x,{condition:()=>!!this.context.error,action:()=>console.log("getting apps -> error")}),h.addTransition(p,{condition:()=>!!e("APP_NAME")&&this.context.hasApps}),h.addTransition(T,{condition:()=>!e("APP_NAME")&&this.context.appsLoaded&&0===this.context.apps.length,action:()=>this.updateContext({selectedApp:void 0})}),h.addTransition(p,{condition:()=>!e("APP_NAME")&&this.context.hasApps&&this.context.apps.length>0}),T.addTransition(p,{condition:()=>!!this.context.selectedApp}),T.addTransition(x,{condition:()=>!!this.context.error}),p.addTransition(g,{condition:()=>!e("APP_NAME")&&this.context.apps.some((t=>t.id===this.context.selectedApp)),action:()=>{this.service.codicent=this.context.selectedApp||"",localStorage.setItem("selectedApp",this.context.selectedApp||"")}}),p.addTransition(g,{condition:()=>!e("APP_NAME")&&!this.context.selectedApp,action:()=>{const t=this.context.apps[0].id;this.service.codicent=t,localStorage.setItem("selectedApp",t),this.updateContext({selectedApp:t})}}),p.addTransition(g,{condition:()=>this.context.apps.some((t=>t.id===e("APP_NAME"))),action:()=>{this.service.codicent=e("APP_NAME"),this.updateContext({selectedApp:e("APP_NAME")})}}),p.addTransition(l,{condition:()=>!this.context.apps.some((t=>t.id===this.context.selectedApp||t.id===e("APP_NAME"))),action:()=>console.log("checking access -> no access",this.context.apps,this.context.selectedApp,e("APP_NAME"))}),g.addTransition(u,{condition:()=>!!localStorage.getItem("selectedApp")&&localStorage.getItem("selectedApp")!==this.context.selectedApp,action:()=>{this.service.codicent=localStorage.getItem("selectedApp")||"",this.updateContext({selectedApp:localStorage.getItem("selectedApp")||void 0})}}),r.addTransition(a,{condition:()=>!0===this.context.isRegistered}),r.addTransition(x,{condition:()=>!!this.context.error}),d.addTransition(c,{condition:()=>void 0===this.context.hasValidPurchase,action:()=>{window.history.replaceState({},document.title,window.location.pathname),this.checkPurchase()}}),[n,h,c,m,r].forEach((t=>{t.addTransition(x,{condition:()=>this.context.error?.includes("network")||this.context.error?.includes("connectivity")||!1}),t.addTransition(x,{condition:()=>!!this.context.error})})),[o,a,g,l].forEach((t=>{t.addTransition(A,{condition:()=>!1===this.context.isAuthenticated})})),a.addTransition(u,{condition:()=>!this.context.user&&!!this.context.nickname}),u.addTransition(c,{condition:()=>!!this.context.name&&e("SUBSCRIPTION_NEEDED")}),u.addTransition(h,{condition:()=>!!this.context.name&&!e("SUBSCRIPTION_NEEDED")}),x.addTransition(c,{condition:()=>!this.context.error&&"ErrorCheckingPurchase"!==this.context.errorType}),x.addTransition(h,{condition:()=>!this.context.error&&"ErrorGettingApps"!==this.context.errorType}),x.addTransition(p,{condition:()=>!this.context.error&&"ErrorCheckingAccess"!==this.context.errorType}),x.addTransition(u,{condition:()=>!this.context.error&&"ErrorLoadingUser"!==this.context.errorType}),x.addTransition(T,{condition:()=>!this.context.error&&"ErrorCreatingApp"!==this.context.errorType}),x.addTransition(m,{condition:()=>!this.context.error&&"CodicentLoginFailed"!==this.context.errorType}),A.addTransition(i,{condition:()=>!this.context.isAuthenticated&&!this.context.accessToken});const v={initial:i,authenticating:n,notAuthenticated:s,authenticated:o,checkingCodicentAccess:m,registering:r,registered:a,checkingPurchase:c,needsPurchase:d,gettingApps:h,creatingApp:T,checkingAccess:p,noAccess:l,error:x,loadingUser:u,loggingOut:A,hasAccess:g,anonymous:S};return this.states.push(...Object.values(v)),v}get error(){return this.context.error}checkPurchase(){this.service.checkSubscriptionStatus().then((t=>this.updateContext({hasValidPurchase:"active"===t}))).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorCheckingPurchase"})))}getApps(){this.service.getApps().then((t=>{if(null===t)throw new Error("Failed to fetch apps.");e("APP_PREFIX")&&(t=t.filter((t=>t.id.startsWith(e("APP_PREFIX"))))),this.updateContext({apps:t,hasApps:!0,appsLoaded:!0}),this.context.selectedApp&&!t.some((t=>t.id===this.context.selectedApp))&&this.updateContext({selectedApp:void 0})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorGettingApps"})))}setUser(t){this.context.user=t}getContext(){return{...this.context}}updateContext(t){this.context={...this.context,...t}}getCurrentStateName(){const t=Object.entries(this.allStates).find((([t,e])=>e===this.current));return t?t[0]:""}logout(){localStorage.clear(),sessionStorage.clear(),this.resetContext()}setSelectedApp(t){localStorage.setItem("selectedApp",t),this.update()}isBusy(){return this.current===this.allStates.gettingApps||this.current===this.allStates.checkingPurchase||this.current===this.allStates.checkingAccess||this.current===this.allStates.checkingCodicentAccess||this.current===this.allStates.registering||this.current===this.allStates.loadingUser||this.current===this.allStates.creatingApp||this.current===this.allStates.loggingOut}}const n=(t,e)=>new i(t,e);export{i as AppStateMachine,n as createAppStateMachine,i as default};
|
1
|
+
import{d as t}from"../_virtual/index.js";import{getConfigValue as e}from"../config/index.js";class i extends t.StateMachine{constructor(t,e){super(),this.context={isAuthenticated:void 0,hasAccess:!1,error:null,errorType:void 0,apps:[],hasApps:!1,appsLoaded:!1},this.currentStateName="",this.resetContext(),this.service=t,this.onStateChange=e,this.allStates=this.initializeStateMachine()}resetContext(){this.context={isAuthenticated:!1,isRegistered:void 0,hasValidPurchase:void 0,hasAccess:!1,selectedApp:localStorage.getItem("selectedApp")||void 0,error:null,errorType:void 0,accessToken:void 0,nickname:void 0,apps:[],hasApps:!1,appsLoaded:!1},e("APP_PREFIX")&&this.context.selectedApp&&!this.context.selectedApp.startsWith(e("APP_PREFIX"))?this.context.selectedApp=void 0:this.context.selectedApp}update(){super.update()}handlePurchase(){window.location.href="./#/purchase"}initializeStateMachine(){const i=new t.State,n=new t.State({entering:()=>{this.context.selectedApp&&!this.service.codicent&&(this.service.codicent=this.context.selectedApp)}}),s=new t.State,o=new t.State,r=new t.State({entering:()=>{this.service.findUsername().then((t=>{t?this.service.nicknameExists(t).then((e=>{e?this.updateContext({error:"Användarnamnet finns redan.",errorType:"UsernameExists"}):this.service.registerUser(t,this.context.user.email,this.context.user.sub,this.context.accessToken).then((()=>{this.service.setToken(this.context.accessToken),this.updateContext({nickname:t,isRegistered:!0})})).catch((t=>{console.warn("Failed to register account",t),this.updateContext({error:"Ett fel uppstod vid registrering. Försök igen senare.",errorType:"ErrorRegistering"})}))})).catch((t=>{console.warn("Failed to check username",t),this.updateContext({error:"Ett fel uppstod vid kontroll av användarnamn.",errorType:"ErrorCheckingUsername"})})):this.updateContext({error:"Användarnamn kunde inte hittas.",errorType:"NoUsernameFound"})})).catch((t=>{console.warn("Failed to find username",t),this.updateContext({error:"Ett fel uppstod vid sökning av användarnamn.",errorType:"ErrorFindingUsername"})}))}}),a=new t.State,c=new t.State({entering:()=>{this.checkPurchase()}}),d=new t.State({entering:()=>{this.handlePurchase()}}),h=new t.State({entering:()=>{this.getApps()}}),p=new t.State,l=new t.State,g=new t.State({}),x=new t.State,u=new t.State({entering:()=>{this.context.nickname&&(localStorage.setItem("nickname",this.context.nickname),this.service.getUserInfo(this.context.nickname).then((t=>{const e=t?.name||t?.nickname||"";this.updateContext({name:e})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorLoadingUser"}))))}}),A=new t.State({entering:()=>{localStorage.removeItem("selectedApp"),localStorage.removeItem("nickname"),localStorage.removeItem("codicent"),sessionStorage.clear(),this.updateContext({isAuthenticated:!1,isRegistered:void 0,apps:[],hasApps:!1,selectedApp:void 0,accessToken:void 0,user:void 0}),setTimeout((()=>{this.current=this.allStates.initial}),1e3)}}),T=new t.State({entering:()=>{this.service.createApp().then((t=>{t?(this.service.codicent=t,this.updateContext({selectedApp:t,apps:[...this.context.apps||[],{id:t,title:t,logo:""}]})):this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})})).catch((t=>{console.warn("Error creating app",t),this.updateContext({error:"Misslyckades att skapa din app. Försök igen senare.",errorType:"ErrorCreatingApp"})}))}}),m=new t.State({entering:()=>{this.service.loginUser(this.context.user.sub,this.context.accessToken).then((t=>{null===t?this.updateContext({isRegistered:!1}):""===t?this.updateContext({isRegistered:!1,error:"Kunde inte logga in hos Codicent.",errorType:"CodicentLoginFailed"}):(this.service.setToken(this.context.accessToken),this.service.getNickname().then((t=>{t?this.updateContext({isRegistered:!0,nickname:t}):this.updateContext({error:"Kunde inte hitta användarnamn.",errorType:"ErrorFindingUsername"})})).catch((()=>{this.updateContext({error:"Fel vid hämtning av användarnamn.",errorType:"ErrorFindingUsername"})})))})).catch((()=>{this.updateContext({isRegistered:!1,error:"Fel vid inloggning hos Codicent.",errorType:"CodicentLoginError"})}))}}),S=new t.State({entering:()=>{this.service.setToken(e("ANONYMOUS_TOKEN")),this.service.codicent=e("ANONYMOUS_CODICENT")||e("APP_NAME")||""}});i.addTransition(n,{condition:()=>!0}),n.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),n.addTransition(x,{condition:()=>Boolean(this.context.error)}),n.addTransition(S,{condition:()=>!0===this.context.isAnonymous&&!!e("ANONYMOUS_TOKEN")}),n.addTransition(s,{condition:()=>!1===this.context.isAuthenticated}),s.addTransition(o,{condition:()=>!0===this.context.isAuthenticated}),s.addTransition(x,{condition:()=>!!this.context.error}),o.addTransition(m,{}),m.addTransition(r,{condition:()=>!1===this.context.isRegistered}),m.addTransition(a,{condition:()=>!0===this.context.isRegistered}),m.addTransition(x,{condition:()=>!!this.context.error}),a.addTransition(u,{condition:()=>!!this.context.nickname}),a.addTransition(x,{condition:()=>!this.context.nickname,action:()=>this.updateContext({error:"Missing nickname after registration",errorType:"ErrorLoadingUser"})}),c.addTransition(d,{condition:()=>!1===this.context.hasValidPurchase}),c.addTransition(h,{condition:()=>!0===this.context.hasValidPurchase}),h.addTransition(x,{condition:()=>!!this.context.error,action:()=>console.log("getting apps -> error")}),h.addTransition(p,{condition:()=>!!e("APP_NAME")&&this.context.hasApps}),h.addTransition(T,{condition:()=>!e("APP_NAME")&&this.context.appsLoaded&&0===this.context.apps.length,action:()=>this.updateContext({selectedApp:void 0})}),h.addTransition(p,{condition:()=>!e("APP_NAME")&&this.context.hasApps&&this.context.apps.length>0}),T.addTransition(p,{condition:()=>!!this.context.selectedApp}),T.addTransition(x,{condition:()=>!!this.context.error}),p.addTransition(g,{condition:()=>!e("APP_NAME")&&this.context.apps.some((t=>t.id===this.context.selectedApp)),action:()=>{this.service.codicent=this.context.selectedApp||"",localStorage.setItem("selectedApp",this.context.selectedApp||"")}}),p.addTransition(g,{condition:()=>!e("APP_NAME")&&!this.context.selectedApp,action:()=>{const t=this.context.apps[0].id;this.context.selectedApp!==t&&(this.service.codicent=t,localStorage.setItem("selectedApp",t),this.updateContext({selectedApp:t}))}}),p.addTransition(g,{condition:()=>this.context.apps.some((t=>t.id===e("APP_NAME"))),action:()=>{const t=e("APP_NAME");this.context.selectedApp!==t&&(this.service.codicent=t,this.updateContext({selectedApp:t}))}}),p.addTransition(l,{condition:()=>!this.context.apps.some((t=>t.id===this.context.selectedApp||t.id===e("APP_NAME"))),action:()=>console.log("checking access -> no access",this.context.apps,this.context.selectedApp,e("APP_NAME"))}),g.addTransition(u,{condition:()=>!!localStorage.getItem("selectedApp")&&localStorage.getItem("selectedApp")!==this.context.selectedApp,action:()=>{this.service.codicent=localStorage.getItem("selectedApp")||"",this.updateContext({selectedApp:localStorage.getItem("selectedApp")||void 0})}}),r.addTransition(a,{condition:()=>!0===this.context.isRegistered}),r.addTransition(x,{condition:()=>!!this.context.error}),d.addTransition(c,{condition:()=>void 0===this.context.hasValidPurchase,action:()=>{window.history.replaceState({},document.title,window.location.pathname),this.checkPurchase()}}),[n,h,c,m,r].forEach((t=>{t.addTransition(x,{condition:()=>this.context.error?.includes("network")||this.context.error?.includes("connectivity")||!1}),t.addTransition(x,{condition:()=>!!this.context.error})})),[o,a,g,l].forEach((t=>{t.addTransition(A,{condition:()=>!1===this.context.isAuthenticated})})),a.addTransition(u,{condition:()=>!this.context.user&&!!this.context.nickname}),u.addTransition(c,{condition:()=>!!this.context.name&&e("SUBSCRIPTION_NEEDED")}),u.addTransition(h,{condition:()=>!!this.context.name&&!e("SUBSCRIPTION_NEEDED")}),x.addTransition(c,{condition:()=>!this.context.error&&"ErrorCheckingPurchase"!==this.context.errorType}),x.addTransition(h,{condition:()=>!this.context.error&&"ErrorGettingApps"!==this.context.errorType}),x.addTransition(p,{condition:()=>!this.context.error&&"ErrorCheckingAccess"!==this.context.errorType}),x.addTransition(u,{condition:()=>!this.context.error&&"ErrorLoadingUser"!==this.context.errorType}),x.addTransition(T,{condition:()=>!this.context.error&&"ErrorCreatingApp"!==this.context.errorType}),x.addTransition(m,{condition:()=>!this.context.error&&"CodicentLoginFailed"!==this.context.errorType}),A.addTransition(i,{condition:()=>!this.context.isAuthenticated&&!this.context.accessToken});const v={initial:i,authenticating:n,notAuthenticated:s,authenticated:o,checkingCodicentAccess:m,registering:r,registered:a,checkingPurchase:c,needsPurchase:d,gettingApps:h,creatingApp:T,checkingAccess:p,noAccess:l,error:x,loadingUser:u,loggingOut:A,hasAccess:g,anonymous:S};return this.states.push(...Object.values(v)),v}get error(){return this.context.error}checkPurchase(){this.service.checkSubscriptionStatus().then((t=>this.updateContext({hasValidPurchase:"active"===t}))).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorCheckingPurchase"})))}getApps(){this.service.getApps().then((t=>{if(null===t)throw new Error("Failed to fetch apps.");e("APP_PREFIX")&&(t=t.filter((t=>t.id.startsWith(e("APP_PREFIX"))))),this.updateContext({apps:t,hasApps:!0,appsLoaded:!0}),this.context.selectedApp&&!t.some((t=>t.id===this.context.selectedApp))&&this.updateContext({selectedApp:void 0})})).catch((t=>this.updateContext({error:t.toString(),errorType:"ErrorGettingApps"})))}setUser(t){this.context.user=t}getContext(){return{...this.context}}updateContext(t){this.context={...this.context,...t}}getCurrentStateName(){const t=Object.entries(this.allStates).find((([t,e])=>e===this.current));return t?t[0]:""}logout(){localStorage.clear(),sessionStorage.clear(),this.resetContext()}setSelectedApp(t){localStorage.setItem("selectedApp",t),this.update()}isBusy(){return this.current===this.allStates.gettingApps||this.current===this.allStates.checkingPurchase||this.current===this.allStates.checkingAccess||this.current===this.allStates.checkingCodicentAccess||this.current===this.allStates.registering||this.current===this.allStates.loadingUser||this.current===this.allStates.creatingApp||this.current===this.allStates.loggingOut}}const n=(t,e)=>new i(t,e);export{i as AppStateMachine,n as createAppStateMachine,i as default};
|