@knocklabs/client 0.16.3 → 0.16.5

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.16.5
4
+
5
+ ### Patch Changes
6
+
7
+ - dc84f25: chore: limit retries for guide client real time subscription
8
+
9
+ ## 0.16.4
10
+
11
+ ### Patch Changes
12
+
13
+ - 49f791b: fix: guard against undefined window object in ssr
14
+
3
15
  ## 0.16.3
4
16
 
5
17
  ### Patch Changes
package/dist/cjs/api.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var a=Object.defineProperty;var n=(t,e,s)=>e in t?a(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var r=(t,e,s)=>n(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("axios"),l=require("axios-retry"),c=require("phoenix"),i=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},d=i(u),o=i(l);class h{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null,this.axiosClient=d.default.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken,"X-Knock-Client":this.getKnockClientHeader()}}),typeof window<"u"&&(this.socket=new c.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o.default(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.default.exponentialDelay})}async makeRequest(e){try{const s=await this.axiosClient(e);return{statusCode:s.status<300?"ok":"error",body:s.data,error:void 0,status:s.status}}catch(s){return console.error(s),{statusCode:"error",status:500,body:void 0,error:s}}}canRetryRequest(e){return o.default.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}getKnockClientHeader(){return"Knock/ClientJS 0.16.3"}}exports.default=h;
1
+ "use strict";var a=Object.defineProperty;var n=(t,e,s)=>e in t?a(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var r=(t,e,s)=>n(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("axios"),l=require("axios-retry"),c=require("phoenix"),i=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},d=i(u),o=i(l);class h{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null,this.axiosClient=d.default.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken,"X-Knock-Client":this.getKnockClientHeader()}}),typeof window<"u"&&(this.socket=new c.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o.default(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.default.exponentialDelay})}async makeRequest(e){try{const s=await this.axiosClient(e);return{statusCode:s.status<300?"ok":"error",body:s.data,error:void 0,status:s.status}}catch(s){return console.error(s),{statusCode:"error",status:500,body:void 0,error:s}}}canRetryRequest(e){return o.default.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}getKnockClientHeader(){return"Knock/ClientJS 0.16.5"}}exports.default=h;
2
2
  //# sourceMappingURL=api.js.map
@@ -1,2 +1,2 @@
1
- "use strict";var m=Object.defineProperty;var f=(u,e,t)=>e in u?m(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var d=(u,e,t)=>f(u,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@tanstack/store"),S=require("urlpattern-polyfill"),c=require("./helpers.js"),v=50,G=30*1e3,p=u=>`/v1/users/${u}/guides`,g=(u,e={})=>{const t=new c.SelectionResult,s=c.findDefaultGroup(u.guideGroups);if(!s)return t;const r=s.display_sequence,i=u.location;for(const[n,o]of r.entries()){const a=u.guides[o];!a||!_(a,{location:i,filters:e})||t.set(n,a)}return t.metadata={guideGroup:s},t},_=(u,{location:e,filters:t={}})=>{if(t.type&&t.type!==u.type||t.key&&t.key!==u.key||u.steps.every(r=>!!r.message.archived_at))return!1;const s=u.activation_location_rules||[];return!(s.length>0&&e&&!s.reduce((i,n)=>{if(i===!1)return!1;switch(n.directive){case"allow":return i===!0||n.pattern.test(e)?!0:void 0;case"block":return n.pattern.test(e)?!1:i}},void 0))};class C{constructor(e,t,s={},r={}){d(this,"store");d(this,"socket");d(this,"socketChannel");d(this,"socketChannelTopic");d(this,"socketEventTypes",["guide.added","guide.updated","guide.removed","guide_group.added","guide_group.updated"]);d(this,"pushStateFn");d(this,"replaceStateFn");d(this,"stage");d(this,"counterIntervalId");d(this,"handleLocationChange",()=>{const e=window.location.href;this.store.state.location!==e&&(this.knock.log(`[Guide] Handle Location change: ${e}`),this.setLocation(e))});this.knock=e,this.channelId=t,this.targetParams=s,this.options=r;const{trackLocationFromWindow:i=!0}=r,n=i?window==null?void 0:window.location.href:void 0;this.store=new y.Store({guideGroups:[],guideGroupDisplayLogs:{},guides:{},queries:{},location:n,counter:0});const{socket:o}=this.knock.client();this.socket=o,this.socketChannelTopic=`guides:${t}`,i&&this.listenForLocationChangesFromWindow(),this.startCounterInterval(),this.knock.log("[Guide] Initialized a guide client")}incrementCounter(){this.knock.log("[Guide] Incrementing the counter"),this.store.setState(e=>({...e,counter:e.counter+1}))}startCounterInterval(){const{throttleCheckInterval:e=G}=this.options;this.counterIntervalId=setInterval(()=>{this.knock.log("[Guide] Counter interval tick"),!(this.stage&&this.stage.status!=="closed")&&this.incrementCounter()},e)}clearCounterInterval(){this.counterIntervalId&&(clearInterval(this.counterIntervalId),this.counterIntervalId=void 0)}cleanup(){this.unsubscribe(),this.removeEventListeners(),this.clearGroupStage(),this.clearCounterInterval()}async fetch(e){this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Loading all eligible guides");const t=this.buildQueryParams(e==null?void 0:e.filters),s=this.formatQueryKey(t),r=this.store.state.queries[s];if(r)return r;this.store.setState(n=>({...n,queries:{...n.queries,[s]:{status:"loading"}}}));let i;try{const n=await this.knock.user.getGuides(this.channelId,t);i={status:"ok"};const{entries:o,guide_groups:a,guide_group_display_logs:l}=n;this.store.setState(h=>({...h,guideGroups:(a==null?void 0:a.length)>0?a:[c.mockDefaultGroup(o)],guideGroupDisplayLogs:l||{},guides:c.byKey(o.map(k=>this.localCopy(k))),queries:{...h.queries,[s]:i}}))}catch(n){i={status:"error",error:n},this.store.setState(o=>({...o,queries:{...o.queries,[s]:i}}))}return i}subscribe(){if(!this.socket)return;this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Subscribing to real time updates"),this.socket.isConnected()||this.socket.connect(),this.socketChannel&&this.unsubscribe();const e={...this.targetParams,user_id:this.knock.userId},t=this.socket.channel(this.socketChannelTopic,e);for(const s of this.socketEventTypes)t.on(s,r=>this.handleSocketEvent(r));["closed","errored"].includes(t.state)&&t.join(),this.socketChannel=t}unsubscribe(){if(this.socketChannel){this.knock.log("[Guide] Unsubscribing from real time updates");for(const e of this.socketEventTypes)this.socketChannel.off(e);this.socketChannel.leave(),this.socketChannel=void 0}}handleSocketEvent(e){const{event:t,data:s}=e;switch(t){case"guide.added":return this.addOrReplaceGuide(e);case"guide.updated":return s.eligible?this.addOrReplaceGuide(e):this.removeGuide(e);case"guide.removed":return this.removeGuide(e);case"guide_group.added":case"guide_group.updated":return this.addOrReplaceGuideGroup(e);default:return}}setLocation(e){this.clearGroupStage(),this.store.setState(t=>({...t,location:e}))}selectGuides(e,t={}){if(Object.keys(e.guides).length===0)return[];this.knock.log(`[Guide] Selecting guides for: ${c.formatFilters(t)}`);const s=g(e,t);return s.size===0?(this.knock.log("[Guide] Selection returned zero result"),[]):[...s.values()]}selectGuide(e,t={}){if(Object.keys(e.guides).length===0)return;this.knock.log(`[Guide] Selecting a guide for: ${c.formatFilters(t)}`);const s=g(e,t);if(s.size===0){this.knock.log("[Guide] Selection returned zero result");return}const[r,i]=[...s][0];if(i.bypass_global_group_limit)return i;const n=c.findDefaultGroup(e.guideGroups),o=e.guideGroupDisplayLogs[c.DEFAULT_GROUP_KEY];if(!(n&&n.display_interval&&o&&c.checkIfThrottled(o,n.display_interval)))switch(this.stage||(this.stage=this.openGroupStage()),this.stage.status){case"open":{this.knock.log(`[Guide] Addng to the group stage: ${i.key}`),this.stage.ordered[r]=i.key;return}case"patch":return this.knock.log(`[Guide] Patching the group stage: ${i.key}`),this.stage.ordered[r]=i.key,this.stage.resolved===i.key?i:void 0;case"closed":return this.stage.resolved===i.key?i:void 0}}openGroupStage(){this.knock.log("[Guide] Opening a new group stage");const{orderResolutionDuration:e=v}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.stage={status:"open",ordered:[],timeoutId:t},this.stage}closePendingGroupStage(){if(!(!this.stage||this.stage.status==="closed"))return this.knock.log("[Guide] Closing the current group stage"),this.ensureClearTimeout(),this.stage={...this.stage,status:"closed",resolved:this.stage.ordered.find(e=>e!==void 0),timeoutId:null},this.stage}patchClosedGroupStage(){var s;if(((s=this.stage)==null?void 0:s.status)!=="closed")return;this.knock.log("[Guide] Patching the current group stage");const{orderResolutionDuration:e=0}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.ensureClearTimeout(),this.stage={...this.stage,status:"patch",ordered:[],timeoutId:t},this.stage}clearGroupStage(){this.stage&&(this.knock.log("[Guide] Clearing the current group stage"),this.ensureClearTimeout(),this.stage=void 0)}ensureClearTimeout(){var e;(e=this.stage)!=null&&e.timeoutId&&clearTimeout(this.stage.timeoutId)}_selectGuide(e,t={}){return this.openGroupStage(),this.selectGuide(e,t),this.closePendingGroupStage(),this.selectGuide(e,t)}async markAsSeen(e,t){if(t.message.seen_at)return;this.knock.log(`[Guide] Marking as seen (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{seen_at:new Date().toISOString()});if(!s)return;const r={...this.buildEngagementEventBaseParams(e,s),content:s.content,data:this.targetParams.data,tenant:this.targetParams.tenant};return this.knock.user.markGuideStepAs("seen",r),s}async markAsInteracted(e,t,s){this.knock.log(`[Guide] Marking as interacted (Guide key: ${e.key}, Step ref:${t.ref})`);const r=new Date().toISOString(),i=this.setStepMessageAttrs(e.key,t.ref,{read_at:r,interacted_at:r});if(!i)return;const n={...this.buildEngagementEventBaseParams(e,i),metadata:s};return this.knock.user.markGuideStepAs("interacted",n),i}async markAsArchived(e,t){if(t.message.archived_at)return;this.knock.log(`[Guide] Marking as archived (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{archived_at:new Date().toISOString()});if(!s)return;const r=this.buildEngagementEventBaseParams(e,s);return this.knock.user.markGuideStepAs("archived",{...r,unthrottled:e.bypass_global_group_limit}),s}localCopy(e){const t=this,s={...e,getStep(){return this.steps.find(r=>!r.message.archived_at)}};return s.getStep=s.getStep.bind(s),s.steps=e.steps.map(({message:r,...i})=>{const n={...i,message:{...r},markAsSeen(){if(!this.message.seen_at)return t.markAsSeen(s,this)},markAsInteracted({metadata:o}={}){return t.markAsInteracted(s,this,o)},markAsArchived(){if(!this.message.archived_at)return t.markAsArchived(s,this)}};return n.markAsSeen=n.markAsSeen.bind(n),n.markAsInteracted=n.markAsInteracted.bind(n),n.markAsArchived=n.markAsArchived.bind(n),n}),s.activation_location_rules=e.activation_location_rules.map(r=>({...r,pattern:new S.URLPattern({pathname:r.pathname})})),s}buildQueryParams(e={}){const t={...this.targetParams,...e};let s=Object.fromEntries(Object.entries(t).filter(([r,i])=>i!=null));return s=s.data?{...s,data:JSON.stringify(s.data)}:s,s}formatQueryKey(e){const s=Object.keys(e).sort().map(i=>`${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`).join("&"),r=p(this.knock.userId);return s?`${r}?${s}`:r}setStepMessageAttrs(e,t,s){let r;return s.archived_at&&this.clearGroupStage(),this.store.setState(i=>{const n=i.guides[e];if(!n)return i;const o=n.steps.map(h=>(h.ref!==t||(h.message={...h.message,...s},r=h),h));n.steps=o;const a={...i.guides,[n.key]:n},l=s.archived_at&&!n.bypass_global_group_limit?{...i.guideGroupDisplayLogs,[c.DEFAULT_GROUP_KEY]:s.archived_at}:i.guideGroupDisplayLogs;return{...i,guides:a,guideGroupDisplayLogs:l}}),r}buildEngagementEventBaseParams(e,t){return{message_id:t.message.id,channel_id:e.channel_id,guide_key:e.key,guide_id:e.id,guide_step_ref:t.ref}}addOrReplaceGuide({data:e}){this.patchClosedGroupStage();const t=this.localCopy(e.guide);this.store.setState(s=>{const r={...s.guides,[t.key]:t};return{...s,guides:r}})}removeGuide({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const{[e.guide.key]:s,...r}=t.guides;return{...t,guides:r}})}addOrReplaceGuideGroup({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const s=[e.guide_group],r=e.guide_group.display_sequence_unthrottled||[],i=e.guide_group.display_sequence_throttled||[];let n=t.guides;return n=r.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!0};return{...o,[a]:l}},n),n=i.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!1};return{...o,[a]:l}},n),{...t,guides:n,guideGroups:s}})}listenForLocationChangesFromWindow(){if(window!=null&&window.history){window.addEventListener("popstate",this.handleLocationChange),window.addEventListener("hashchange",this.handleLocationChange);const e=window.history.pushState,t=window.history.replaceState;window.history.pushState=new Proxy(e,{apply:(s,r,i)=>{Reflect.apply(s,r,i),setTimeout(()=>{this.handleLocationChange()},0)}}),window.history.replaceState=new Proxy(t,{apply:(s,r,i)=>{Reflect.apply(s,r,i),setTimeout(()=>{this.handleLocationChange()},0)}}),this.pushStateFn=e,this.replaceStateFn=t}else this.knock.log("[Guide] Unable to access the `window.history` object to detect location changes")}removeEventListeners(){window.removeEventListener("popstate",this.handleLocationChange),window.removeEventListener("hashchange",this.handleLocationChange),this.pushStateFn&&(window.history.pushState=this.pushStateFn,this.pushStateFn=void 0),this.replaceStateFn&&(window.history.replaceState=this.replaceStateFn,this.replaceStateFn=void 0)}}exports.KnockGuideClient=C;exports.guidesApiRootPath=p;
1
+ "use strict";var f=Object.defineProperty;var y=(u,e,t)=>e in u?f(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var c=(u,e,t)=>y(u,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@tanstack/store"),v=require("urlpattern-polyfill"),d=require("./helpers.js"),G=50,_=30*1e3,b=3,g=()=>{if(typeof window<"u")return window},k=u=>`/v1/users/${u}/guides`,p=(u,e={})=>{const t=new d.SelectionResult,s=d.findDefaultGroup(u.guideGroups);if(!s)return t;const n=s.display_sequence,i=u.location;for(const[r,o]of n.entries()){const a=u.guides[o];!a||!C(a,{location:i,filters:e})||t.set(r,a)}return t.metadata={guideGroup:s},t},C=(u,{location:e,filters:t={}})=>{if(t.type&&t.type!==u.type||t.key&&t.key!==u.key||u.steps.every(n=>!!n.message.archived_at))return!1;const s=u.activation_location_rules||[];return!(s.length>0&&e&&!s.reduce((i,r)=>{if(i===!1)return!1;switch(r.directive){case"allow":return i===!0||r.pattern.test(e)?!0:void 0;case"block":return r.pattern.test(e)?!1:i}},void 0))};class I{constructor(e,t,s={},n={}){c(this,"store");c(this,"socket");c(this,"socketChannel");c(this,"socketChannelTopic");c(this,"socketEventTypes",["guide.added","guide.updated","guide.removed","guide_group.added","guide_group.updated"]);c(this,"subscribeRetryCount",0);c(this,"pushStateFn");c(this,"replaceStateFn");c(this,"stage");c(this,"counterIntervalId");c(this,"handleLocationChange",()=>{const e=g();if(!(e!=null&&e.location))return;const t=e.location.href;this.store.state.location!==t&&(this.knock.log(`[Guide] Handle Location change: ${t}`),this.setLocation(t))});this.knock=e,this.channelId=t,this.targetParams=s,this.options=n;const{trackLocationFromWindow:i=!0}=n,r=g(),o=i?r==null?void 0:r.location.href:void 0;this.store=new S.Store({guideGroups:[],guideGroupDisplayLogs:{},guides:{},queries:{},location:o,counter:0});const{socket:a}=this.knock.client();this.socket=a,this.socketChannelTopic=`guides:${t}`,i&&this.listenForLocationChangesFromWindow(),this.startCounterInterval(),this.knock.log("[Guide] Initialized a guide client")}incrementCounter(){this.knock.log("[Guide] Incrementing the counter"),this.store.setState(e=>({...e,counter:e.counter+1}))}startCounterInterval(){const{throttleCheckInterval:e=_}=this.options;this.counterIntervalId=setInterval(()=>{this.knock.log("[Guide] Counter interval tick"),!(this.stage&&this.stage.status!=="closed")&&this.incrementCounter()},e)}clearCounterInterval(){this.counterIntervalId&&(clearInterval(this.counterIntervalId),this.counterIntervalId=void 0)}cleanup(){this.unsubscribe(),this.removeEventListeners(),this.clearGroupStage(),this.clearCounterInterval()}async fetch(e){this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Loading all eligible guides");const t=this.buildQueryParams(e==null?void 0:e.filters),s=this.formatQueryKey(t),n=this.store.state.queries[s];if(n)return n;this.store.setState(r=>({...r,queries:{...r.queries,[s]:{status:"loading"}}}));let i;try{const r=await this.knock.user.getGuides(this.channelId,t);i={status:"ok"};const{entries:o,guide_groups:a,guide_group_display_logs:l}=r;this.store.setState(h=>({...h,guideGroups:(a==null?void 0:a.length)>0?a:[d.mockDefaultGroup(o)],guideGroupDisplayLogs:l||{},guides:d.byKey(o.map(m=>this.localCopy(m))),queries:{...h.queries,[s]:i}}))}catch(r){i={status:"error",error:r},this.store.setState(o=>({...o,queries:{...o.queries,[s]:i}}))}return i}subscribe(){if(!this.socket)return;this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Subscribing to real time updates"),this.socket.isConnected()||this.socket.connect(),this.socketChannel&&this.unsubscribe();const e={...this.targetParams,user_id:this.knock.userId},t=this.socket.channel(this.socketChannelTopic,e);for(const s of this.socketEventTypes)t.on(s,n=>this.handleSocketEvent(n));["closed","errored"].includes(t.state)&&(this.subscribeRetryCount=0,t.join().receive("ok",()=>{this.knock.log("[Guide] Successfully joined channel")}).receive("error",s=>{this.knock.log(`[Guide] Failed to join channel: ${JSON.stringify(s)}`),this.handleChannelJoinError()}).receive("timeout",()=>{this.knock.log("[Guide] Channel join timed out"),this.handleChannelJoinError()})),this.socketChannel=t}handleChannelJoinError(){if(this.subscribeRetryCount>=b){this.knock.log(`[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`),this.unsubscribe();return}this.subscribeRetryCount++}unsubscribe(){if(this.socketChannel){this.knock.log("[Guide] Unsubscribing from real time updates");for(const e of this.socketEventTypes)this.socketChannel.off(e);this.socketChannel.leave(),this.socketChannel=void 0}}handleSocketEvent(e){const{event:t,data:s}=e;switch(t){case"guide.added":return this.addOrReplaceGuide(e);case"guide.updated":return s.eligible?this.addOrReplaceGuide(e):this.removeGuide(e);case"guide.removed":return this.removeGuide(e);case"guide_group.added":case"guide_group.updated":return this.addOrReplaceGuideGroup(e);default:return}}setLocation(e){this.clearGroupStage(),this.store.setState(t=>({...t,location:e}))}selectGuides(e,t={}){if(Object.keys(e.guides).length===0)return[];this.knock.log(`[Guide] Selecting guides for: ${d.formatFilters(t)}`);const s=p(e,t);return s.size===0?(this.knock.log("[Guide] Selection returned zero result"),[]):[...s.values()]}selectGuide(e,t={}){if(Object.keys(e.guides).length===0)return;this.knock.log(`[Guide] Selecting a guide for: ${d.formatFilters(t)}`);const s=p(e,t);if(s.size===0){this.knock.log("[Guide] Selection returned zero result");return}const[n,i]=[...s][0];if(i.bypass_global_group_limit)return i;const r=d.findDefaultGroup(e.guideGroups),o=e.guideGroupDisplayLogs[d.DEFAULT_GROUP_KEY];if(!(r&&r.display_interval&&o&&d.checkIfThrottled(o,r.display_interval)))switch(this.stage||(this.stage=this.openGroupStage()),this.stage.status){case"open":{this.knock.log(`[Guide] Addng to the group stage: ${i.key}`),this.stage.ordered[n]=i.key;return}case"patch":return this.knock.log(`[Guide] Patching the group stage: ${i.key}`),this.stage.ordered[n]=i.key,this.stage.resolved===i.key?i:void 0;case"closed":return this.stage.resolved===i.key?i:void 0}}openGroupStage(){this.knock.log("[Guide] Opening a new group stage");const{orderResolutionDuration:e=G}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.stage={status:"open",ordered:[],timeoutId:t},this.stage}closePendingGroupStage(){if(!(!this.stage||this.stage.status==="closed"))return this.knock.log("[Guide] Closing the current group stage"),this.ensureClearTimeout(),this.stage={...this.stage,status:"closed",resolved:this.stage.ordered.find(e=>e!==void 0),timeoutId:null},this.stage}patchClosedGroupStage(){var s;if(((s=this.stage)==null?void 0:s.status)!=="closed")return;this.knock.log("[Guide] Patching the current group stage");const{orderResolutionDuration:e=0}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.ensureClearTimeout(),this.stage={...this.stage,status:"patch",ordered:[],timeoutId:t},this.stage}clearGroupStage(){this.stage&&(this.knock.log("[Guide] Clearing the current group stage"),this.ensureClearTimeout(),this.stage=void 0)}ensureClearTimeout(){var e;(e=this.stage)!=null&&e.timeoutId&&clearTimeout(this.stage.timeoutId)}_selectGuide(e,t={}){return this.openGroupStage(),this.selectGuide(e,t),this.closePendingGroupStage(),this.selectGuide(e,t)}async markAsSeen(e,t){if(t.message.seen_at)return;this.knock.log(`[Guide] Marking as seen (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{seen_at:new Date().toISOString()});if(!s)return;const n={...this.buildEngagementEventBaseParams(e,s),content:s.content,data:this.targetParams.data,tenant:this.targetParams.tenant};return this.knock.user.markGuideStepAs("seen",n),s}async markAsInteracted(e,t,s){this.knock.log(`[Guide] Marking as interacted (Guide key: ${e.key}, Step ref:${t.ref})`);const n=new Date().toISOString(),i=this.setStepMessageAttrs(e.key,t.ref,{read_at:n,interacted_at:n});if(!i)return;const r={...this.buildEngagementEventBaseParams(e,i),metadata:s};return this.knock.user.markGuideStepAs("interacted",r),i}async markAsArchived(e,t){if(t.message.archived_at)return;this.knock.log(`[Guide] Marking as archived (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{archived_at:new Date().toISOString()});if(!s)return;const n=this.buildEngagementEventBaseParams(e,s);return this.knock.user.markGuideStepAs("archived",{...n,unthrottled:e.bypass_global_group_limit}),s}localCopy(e){const t=this,s={...e,getStep(){return this.steps.find(n=>!n.message.archived_at)}};return s.getStep=s.getStep.bind(s),s.steps=e.steps.map(({message:n,...i})=>{const r={...i,message:{...n},markAsSeen(){if(!this.message.seen_at)return t.markAsSeen(s,this)},markAsInteracted({metadata:o}={}){return t.markAsInteracted(s,this,o)},markAsArchived(){if(!this.message.archived_at)return t.markAsArchived(s,this)}};return r.markAsSeen=r.markAsSeen.bind(r),r.markAsInteracted=r.markAsInteracted.bind(r),r.markAsArchived=r.markAsArchived.bind(r),r}),s.activation_location_rules=e.activation_location_rules.map(n=>({...n,pattern:new v.URLPattern({pathname:n.pathname})})),s}buildQueryParams(e={}){const t={...this.targetParams,...e};let s=Object.fromEntries(Object.entries(t).filter(([n,i])=>i!=null));return s=s.data?{...s,data:JSON.stringify(s.data)}:s,s}formatQueryKey(e){const s=Object.keys(e).sort().map(i=>`${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`).join("&"),n=k(this.knock.userId);return s?`${n}?${s}`:n}setStepMessageAttrs(e,t,s){let n;return s.archived_at&&this.clearGroupStage(),this.store.setState(i=>{const r=i.guides[e];if(!r)return i;const o=r.steps.map(h=>(h.ref!==t||(h.message={...h.message,...s},n=h),h));r.steps=o;const a={...i.guides,[r.key]:r},l=s.archived_at&&!r.bypass_global_group_limit?{...i.guideGroupDisplayLogs,[d.DEFAULT_GROUP_KEY]:s.archived_at}:i.guideGroupDisplayLogs;return{...i,guides:a,guideGroupDisplayLogs:l}}),n}buildEngagementEventBaseParams(e,t){return{message_id:t.message.id,channel_id:e.channel_id,guide_key:e.key,guide_id:e.id,guide_step_ref:t.ref}}addOrReplaceGuide({data:e}){this.patchClosedGroupStage();const t=this.localCopy(e.guide);this.store.setState(s=>{const n={...s.guides,[t.key]:t};return{...s,guides:n}})}removeGuide({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const{[e.guide.key]:s,...n}=t.guides;return{...t,guides:n}})}addOrReplaceGuideGroup({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const s=[e.guide_group],n=e.guide_group.display_sequence_unthrottled||[],i=e.guide_group.display_sequence_throttled||[];let r=t.guides;return r=n.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!0};return{...o,[a]:l}},r),r=i.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!1};return{...o,[a]:l}},r),{...t,guides:r,guideGroups:s}})}listenForLocationChangesFromWindow(){const e=g();if(e!=null&&e.history){e.addEventListener("popstate",this.handleLocationChange),e.addEventListener("hashchange",this.handleLocationChange);const t=e.history.pushState,s=e.history.replaceState;e.history.pushState=new Proxy(t,{apply:(n,i,r)=>{Reflect.apply(n,i,r),setTimeout(()=>{this.handleLocationChange()},0)}}),e.history.replaceState=new Proxy(s,{apply:(n,i,r)=>{Reflect.apply(n,i,r),setTimeout(()=>{this.handleLocationChange()},0)}}),this.pushStateFn=t,this.replaceStateFn=s}else this.knock.log("[Guide] Unable to access the `window.history` object to detect location changes")}removeEventListeners(){const e=g();e!=null&&e.history&&(e.removeEventListener("popstate",this.handleLocationChange),e.removeEventListener("hashchange",this.handleLocationChange),this.pushStateFn&&(e.history.pushState=this.pushStateFn,this.pushStateFn=void 0),this.replaceStateFn&&(e.history.replaceState=this.replaceStateFn,this.replaceStateFn=void 0))}}exports.KnockGuideClient=I;exports.guidesApiRootPath=k;
2
2
  //# sourceMappingURL=client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n\n const location = trackLocationFromWindow\n ? window?.location.href\n : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n newChannel.join();\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const href = window.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n if (window?.history) {\n // 1. Listen for browser back/forward button clicks.\n window.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n window.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = window.history.pushState;\n const replaceStateFn = window.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n window.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n window.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n window.removeEventListener(\"popstate\", this.handleLocationChange);\n window.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n window.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n window.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","guideGroupDisplayLogs","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":"kVA6CMA,EAAoC,GAIpCC,EAAqC,GAAK,IAEnCC,EAAqBC,GAChC,aAAaA,CAAM,UAEfC,EAAS,CAACC,EAAmBC,EAA8B,KAAO,CAEhE,MAAAC,EAAS,IAAIC,kBAEbC,EAAeC,EAAAA,iBAAiBL,EAAM,WAAW,EACnD,GAAA,CAACI,EAAqB,OAAAF,EAE1B,MAAMI,EAAkBF,EAAa,iBAC/BG,EAAWP,EAAM,SAEvB,SAAW,CAACQ,EAAOC,CAAQ,IAAKH,EAAgB,UAAW,CACnD,MAAAI,EAAQV,EAAM,OAAOS,CAAQ,EAC/B,CAACC,GAGD,CADaC,EAAUD,EAAO,CAAE,SAAAH,EAAU,QAAAN,EAAS,GAGhDC,EAAA,IAAIM,EAAOE,CAAK,CAAA,CAGlB,OAAAR,EAAA,SAAW,CAAE,WAAYE,CAAa,EACtCF,CACT,EAOMS,EAAY,CAChBD,EACA,CAAE,SAAAH,EAAU,QAAAN,EAAU,MACnB,CASC,GARAA,EAAQ,MAAQA,EAAQ,OAASS,EAAM,MAIvCT,EAAQ,KA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A,MAEtB,KAAK,MAAM,IACT,iFACF,CACF,CAGM,sBAAuB,CACtB,OAAA,oBAAoB,WAAY,KAAK,oBAAoB,EACzD,OAAA,oBAAoB,aAAc,KAAK,oBAAoB,EAE9D,KAAK,cACA,OAAA,QAAQ,UAAY,KAAK,YAChC,KAAK,YAAc,QAEjB,KAAK,iBACA,OAAA,QAAQ,aAAe,KAAK,eACnC,KAAK,eAAiB,OACxB,CAEJ"}
1
+ {"version":3,"file":"client.js","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\n// Maximum number of retry attempts for channel subscription\nconst SUBSCRIBE_RETRY_LIMIT = 3;\n\n// Return the global window object if defined, so to safely guard against SSR.\nconst checkForWindow = () => {\n if (typeof window !== \"undefined\") {\n return window;\n }\n};\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n private subscribeRetryCount = 0;\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n const win = checkForWindow();\n\n const location = trackLocationFromWindow ? win?.location.href : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n // Reset retry count for new subscription attempt\n this.subscribeRetryCount = 0;\n\n newChannel\n .join()\n .receive(\"ok\", () => {\n this.knock.log(\"[Guide] Successfully joined channel\");\n })\n .receive(\"error\", (resp) => {\n this.knock.log(\n `[Guide] Failed to join channel: ${JSON.stringify(resp)}`,\n );\n this.handleChannelJoinError();\n })\n .receive(\"timeout\", () => {\n this.knock.log(\"[Guide] Channel join timed out\");\n this.handleChannelJoinError();\n });\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n private handleChannelJoinError() {\n // Prevent phx channel from retrying forever in case of either network or\n // other errors (e.g. auth error, invalid channel etc)\n if (this.subscribeRetryCount >= SUBSCRIBE_RETRY_LIMIT) {\n this.knock.log(\n `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`,\n );\n this.unsubscribe();\n return;\n }\n\n this.subscribeRetryCount++;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const win = checkForWindow();\n if (!win?.location) return;\n\n const href = win.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n const win = checkForWindow();\n if (win?.history) {\n // 1. Listen for browser back/forward button clicks.\n win.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n win.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = win.history.pushState;\n const replaceStateFn = win.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n win.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n win.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n const win = checkForWindow();\n if (!win?.history) return;\n\n win.removeEventListener(\"popstate\", this.handleLocationChange);\n win.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n win.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n win.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","SUBSCRIBE_RETRY_LIMIT","checkForWindow","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","win","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","resp","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","guideGroupDisplayLogs","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":"kVA6CMA,EAAoC,GAIpCC,EAAqC,GAAK,IAG1CC,EAAwB,EAGxBC,EAAiB,IAAM,CACvB,GAAA,OAAO,OAAW,IACb,OAAA,MAEX,EAEaC,EAAqBC,GAChC,aAAaA,CAAM,UAEfC,EAAS,CAACC,EAAmBC,EAA8B,KAAO,CAEhE,MAAAC,EAAS,IAAIC,kBAEbC,EAAeC,EAAAA,iBAAiBL,EAAM,WAAW,EACnD,GAAA,CAACI,EAAqB,OAAAF,EAE1B,MAAMI,EAAkBF,EAAa,iBAC/BG,EAAWP,EAAM,SAEvB,SAAW,CAACQ,EAAOC,CAAQ,IAAKH,EAAgB,UAAW,CACnD,MAAAI,EAAQV,EAAM,OAAOS,CAAQ,EAC/B,CAACC,GAGD,CADaC,EAAUD,EAAO,CAAE,SAAAH,EAAU,QAAAN,EAAS,GAGhDC,EAAA,IAAIM,EAAOE,CAAK,CAAA,CAGlB,OAAAR,EAAA,SAAW,CAAE,WAAYE,CAAa,EACtCF,CACT,EAOMS,EAAY,CAChBD,EACA,CAAE,SAAAH,EAAU,QAAAN,EAAU,MACnB,CASC,GARAA,EAAQ,MAAQA,EAAQ,OAASS,EAAM,MAIvCT,EAAQ,KAAOA,EAAQ,MAAQS,EAAM,KAIrCA,EAAM,MAAM,MAAOE,GAAM,CAAC,CAACA,EAAE,QAAQ,WAAW,EAC3C,MAAA,GAGH,MAAAC,EAAgBH,EAAM,2BA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oBAAoB,EAGtD,MAAA2D,EAAc3D,EAAI,QAAQ,UAC1B4D,EAAiB5D,EAAI,QAAQ,aAGnCA,EAAI,QAAQ,UAAY,IAAI,MAAM2D,EAAa,CAC7C,MAAO,CAACE,EAAQC,EAASC,IAAS,CACxB,QAAA,MAAMF,EAAQC,EAASC,CAAI,EACnC,WAAW,IAAM,CACf,KAAK,qBAAqB,GACzB,CAAC,CAAA,CACN,CACD,EACD/D,EAAI,QAAQ,aAAe,IAAI,MAAM4D,EAAgB,CACnD,MAAO,CAACC,EAAQC,EAASC,IAAS,CACxB,QAAA,MAAMF,EAAQC,EAASC,CAAI,EACnC,WAAW,IAAM,CACf,KAAK,qBAAqB,GACzB,CAAC,CAAA,CACN,CACD,EAGD,KAAK,YAAcJ,EACnB,KAAK,eAAiBC,CAAA,MAEtB,KAAK,MAAM,IACT,iFACF,CACF,CAGM,sBAAuB,CAC7B,MAAM5D,EAAM1B,EAAe,EACtB0B,GAAA,MAAAA,EAAK,UAENA,EAAA,oBAAoB,WAAY,KAAK,oBAAoB,EACzDA,EAAA,oBAAoB,aAAc,KAAK,oBAAoB,EAE3D,KAAK,cACHA,EAAA,QAAQ,UAAY,KAAK,YAC7B,KAAK,YAAc,QAEjB,KAAK,iBACHA,EAAA,QAAQ,aAAe,KAAK,eAChC,KAAK,eAAiB,QACxB,CAEJ"}
package/dist/esm/api.mjs CHANGED
@@ -53,7 +53,7 @@ class k {
53
53
  return o.isNetworkError(e) ? !0 : e.response ? e.response.status >= 500 && e.response.status <= 599 || e.response.status === 429 : !1;
54
54
  }
55
55
  getKnockClientHeader() {
56
- return "Knock/ClientJS 0.16.3";
56
+ return "Knock/ClientJS 0.16.5";
57
57
  }
58
58
  }
59
59
  export {
@@ -1,79 +1,85 @@
1
- var f = Object.defineProperty;
2
- var S = (u, e, t) => e in u ? f(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
- var d = (u, e, t) => S(u, typeof e != "symbol" ? e + "" : e, t);
4
- import { Store as y } from "@tanstack/store";
5
- import { URLPattern as v } from "urlpattern-polyfill";
6
- import { byKey as G, mockDefaultGroup as _, formatFilters as l, findDefaultGroup as k, DEFAULT_GROUP_KEY as g, checkIfThrottled as w, SelectionResult as C } from "./helpers.mjs";
7
- const b = 50, I = 30 * 1e3, A = (u) => `/v1/users/${u}/guides`, p = (u, e = {}) => {
8
- const t = new C(), s = k(u.guideGroups);
1
+ var y = Object.defineProperty;
2
+ var S = (u, e, t) => e in u ? y(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
+ var c = (u, e, t) => S(u, typeof e != "symbol" ? e + "" : e, t);
4
+ import { Store as v } from "@tanstack/store";
5
+ import { URLPattern as G } from "urlpattern-polyfill";
6
+ import { byKey as _, mockDefaultGroup as C, formatFilters as g, findDefaultGroup as m, DEFAULT_GROUP_KEY as p, checkIfThrottled as b, SelectionResult as I } from "./helpers.mjs";
7
+ const A = 50, E = 30 * 1e3, L = 3, l = () => {
8
+ if (typeof window < "u")
9
+ return window;
10
+ }, R = (u) => `/v1/users/${u}/guides`, k = (u, e = {}) => {
11
+ const t = new I(), s = m(u.guideGroups);
9
12
  if (!s) return t;
10
- const r = s.display_sequence, i = u.location;
11
- for (const [n, o] of r.entries()) {
13
+ const n = s.display_sequence, i = u.location;
14
+ for (const [r, o] of n.entries()) {
12
15
  const a = u.guides[o];
13
- !a || !L(a, { location: i, filters: e }) || t.set(n, a);
16
+ !a || !T(a, { location: i, filters: e }) || t.set(r, a);
14
17
  }
15
18
  return t.metadata = { guideGroup: s }, t;
16
- }, L = (u, { location: e, filters: t = {} }) => {
17
- if (t.type && t.type !== u.type || t.key && t.key !== u.key || u.steps.every((r) => !!r.message.archived_at))
19
+ }, T = (u, { location: e, filters: t = {} }) => {
20
+ if (t.type && t.type !== u.type || t.key && t.key !== u.key || u.steps.every((n) => !!n.message.archived_at))
18
21
  return !1;
19
22
  const s = u.activation_location_rules || [];
20
- return !(s.length > 0 && e && !s.reduce((i, n) => {
23
+ return !(s.length > 0 && e && !s.reduce((i, r) => {
21
24
  if (i === !1) return !1;
22
- switch (n.directive) {
25
+ switch (r.directive) {
23
26
  case "allow":
24
- return i === !0 || n.pattern.test(e) ? !0 : void 0;
27
+ return i === !0 || r.pattern.test(e) ? !0 : void 0;
25
28
  case "block":
26
- return n.pattern.test(e) ? !1 : i;
29
+ return r.pattern.test(e) ? !1 : i;
27
30
  }
28
31
  }, void 0));
29
32
  };
30
- class F {
31
- constructor(e, t, s = {}, r = {}) {
32
- d(this, "store");
33
+ class $ {
34
+ constructor(e, t, s = {}, n = {}) {
35
+ c(this, "store");
33
36
  // Phoenix channels for real time guide updates over websocket
34
- d(this, "socket");
35
- d(this, "socketChannel");
36
- d(this, "socketChannelTopic");
37
- d(this, "socketEventTypes", [
37
+ c(this, "socket");
38
+ c(this, "socketChannel");
39
+ c(this, "socketChannelTopic");
40
+ c(this, "socketEventTypes", [
38
41
  "guide.added",
39
42
  "guide.updated",
40
43
  "guide.removed",
41
44
  "guide_group.added",
42
45
  "guide_group.updated"
43
46
  ]);
47
+ c(this, "subscribeRetryCount", 0);
44
48
  // Original history methods to monkey patch, or restore in cleanups.
45
- d(this, "pushStateFn");
46
- d(this, "replaceStateFn");
49
+ c(this, "pushStateFn");
50
+ c(this, "replaceStateFn");
47
51
  // Guides that are competing to render are "staged" first without rendering
48
52
  // and ranked based on its relative order in the group over a duration of time
49
53
  // to resolve and render the prevailing one.
50
- d(this, "stage");
51
- d(this, "counterIntervalId");
54
+ c(this, "stage");
55
+ c(this, "counterIntervalId");
52
56
  // Define as an arrow func property to always bind this to the class instance.
53
- d(this, "handleLocationChange", () => {
54
- const e = window.location.href;
55
- this.store.state.location !== e && (this.knock.log(`[Guide] Handle Location change: ${e}`), this.setLocation(e));
57
+ c(this, "handleLocationChange", () => {
58
+ const e = l();
59
+ if (!(e != null && e.location)) return;
60
+ const t = e.location.href;
61
+ this.store.state.location !== t && (this.knock.log(`[Guide] Handle Location change: ${t}`), this.setLocation(t));
56
62
  });
57
- this.knock = e, this.channelId = t, this.targetParams = s, this.options = r;
58
- const { trackLocationFromWindow: i = !0 } = r, n = i ? window == null ? void 0 : window.location.href : void 0;
59
- this.store = new y({
63
+ this.knock = e, this.channelId = t, this.targetParams = s, this.options = n;
64
+ const { trackLocationFromWindow: i = !0 } = n, r = l(), o = i ? r == null ? void 0 : r.location.href : void 0;
65
+ this.store = new v({
60
66
  guideGroups: [],
61
67
  guideGroupDisplayLogs: {},
62
68
  guides: {},
63
69
  queries: {},
64
- location: n,
70
+ location: o,
65
71
  // Increment to update the state store and trigger re-selection.
66
72
  counter: 0
67
73
  });
68
- const { socket: o } = this.knock.client();
69
- this.socket = o, this.socketChannelTopic = `guides:${t}`, i && this.listenForLocationChangesFromWindow(), this.startCounterInterval(), this.knock.log("[Guide] Initialized a guide client");
74
+ const { socket: a } = this.knock.client();
75
+ this.socket = a, this.socketChannelTopic = `guides:${t}`, i && this.listenForLocationChangesFromWindow(), this.startCounterInterval(), this.knock.log("[Guide] Initialized a guide client");
70
76
  }
71
77
  incrementCounter() {
72
78
  this.knock.log("[Guide] Incrementing the counter"), this.store.setState((e) => ({ ...e, counter: e.counter + 1 }));
73
79
  }
74
80
  startCounterInterval() {
75
81
  const {
76
- throttleCheckInterval: e = I
82
+ throttleCheckInterval: e = E
77
83
  } = this.options;
78
84
  this.counterIntervalId = setInterval(() => {
79
85
  this.knock.log("[Guide] Counter interval tick"), !(this.stage && this.stage.status !== "closed") && this.incrementCounter();
@@ -87,27 +93,27 @@ class F {
87
93
  }
88
94
  async fetch(e) {
89
95
  this.knock.failIfNotAuthenticated(), this.knock.log("[Guide] Loading all eligible guides");
90
- const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), r = this.store.state.queries[s];
91
- if (r)
92
- return r;
93
- this.store.setState((n) => ({
94
- ...n,
95
- queries: { ...n.queries, [s]: { status: "loading" } }
96
+ const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), n = this.store.state.queries[s];
97
+ if (n)
98
+ return n;
99
+ this.store.setState((r) => ({
100
+ ...r,
101
+ queries: { ...r.queries, [s]: { status: "loading" } }
96
102
  }));
97
103
  let i;
98
104
  try {
99
- const n = await this.knock.user.getGuides(this.channelId, t);
105
+ const r = await this.knock.user.getGuides(this.channelId, t);
100
106
  i = { status: "ok" };
101
- const { entries: o, guide_groups: a, guide_group_display_logs: h } = n;
102
- this.store.setState((c) => ({
103
- ...c,
104
- guideGroups: (a == null ? void 0 : a.length) > 0 ? a : [_(o)],
107
+ const { entries: o, guide_groups: a, guide_group_display_logs: h } = r;
108
+ this.store.setState((d) => ({
109
+ ...d,
110
+ guideGroups: (a == null ? void 0 : a.length) > 0 ? a : [C(o)],
105
111
  guideGroupDisplayLogs: h || {},
106
- guides: G(o.map((m) => this.localCopy(m))),
107
- queries: { ...c.queries, [s]: i }
112
+ guides: _(o.map((f) => this.localCopy(f))),
113
+ queries: { ...d.queries, [s]: i }
108
114
  }));
109
- } catch (n) {
110
- i = { status: "error", error: n }, this.store.setState((o) => ({
115
+ } catch (r) {
116
+ i = { status: "error", error: r }, this.store.setState((o) => ({
111
117
  ...o,
112
118
  queries: { ...o.queries, [s]: i }
113
119
  }));
@@ -119,8 +125,25 @@ class F {
119
125
  this.knock.failIfNotAuthenticated(), this.knock.log("[Guide] Subscribing to real time updates"), this.socket.isConnected() || this.socket.connect(), this.socketChannel && this.unsubscribe();
120
126
  const e = { ...this.targetParams, user_id: this.knock.userId }, t = this.socket.channel(this.socketChannelTopic, e);
121
127
  for (const s of this.socketEventTypes)
122
- t.on(s, (r) => this.handleSocketEvent(r));
123
- ["closed", "errored"].includes(t.state) && t.join(), this.socketChannel = t;
128
+ t.on(s, (n) => this.handleSocketEvent(n));
129
+ ["closed", "errored"].includes(t.state) && (this.subscribeRetryCount = 0, t.join().receive("ok", () => {
130
+ this.knock.log("[Guide] Successfully joined channel");
131
+ }).receive("error", (s) => {
132
+ this.knock.log(
133
+ `[Guide] Failed to join channel: ${JSON.stringify(s)}`
134
+ ), this.handleChannelJoinError();
135
+ }).receive("timeout", () => {
136
+ this.knock.log("[Guide] Channel join timed out"), this.handleChannelJoinError();
137
+ })), this.socketChannel = t;
138
+ }
139
+ handleChannelJoinError() {
140
+ if (this.subscribeRetryCount >= L) {
141
+ this.knock.log(
142
+ `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`
143
+ ), this.unsubscribe();
144
+ return;
145
+ }
146
+ this.subscribeRetryCount++;
124
147
  }
125
148
  unsubscribe() {
126
149
  if (this.socketChannel) {
@@ -155,34 +178,34 @@ class F {
155
178
  selectGuides(e, t = {}) {
156
179
  if (Object.keys(e.guides).length === 0)
157
180
  return [];
158
- this.knock.log(`[Guide] Selecting guides for: ${l(t)}`);
159
- const s = p(e, t);
181
+ this.knock.log(`[Guide] Selecting guides for: ${g(t)}`);
182
+ const s = k(e, t);
160
183
  return s.size === 0 ? (this.knock.log("[Guide] Selection returned zero result"), []) : [...s.values()];
161
184
  }
162
185
  selectGuide(e, t = {}) {
163
186
  if (Object.keys(e.guides).length === 0)
164
187
  return;
165
- this.knock.log(`[Guide] Selecting a guide for: ${l(t)}`);
166
- const s = p(e, t);
188
+ this.knock.log(`[Guide] Selecting a guide for: ${g(t)}`);
189
+ const s = k(e, t);
167
190
  if (s.size === 0) {
168
191
  this.knock.log("[Guide] Selection returned zero result");
169
192
  return;
170
193
  }
171
- const [r, i] = [...s][0];
194
+ const [n, i] = [...s][0];
172
195
  if (i.bypass_global_group_limit)
173
196
  return i;
174
- const n = k(e.guideGroups), o = e.guideGroupDisplayLogs[g];
175
- if (!(n && n.display_interval && o && w(
197
+ const r = m(e.guideGroups), o = e.guideGroupDisplayLogs[p];
198
+ if (!(r && r.display_interval && o && b(
176
199
  o,
177
- n.display_interval
200
+ r.display_interval
178
201
  )))
179
202
  switch (this.stage || (this.stage = this.openGroupStage()), this.stage.status) {
180
203
  case "open": {
181
- this.knock.log(`[Guide] Addng to the group stage: ${i.key}`), this.stage.ordered[r] = i.key;
204
+ this.knock.log(`[Guide] Addng to the group stage: ${i.key}`), this.stage.ordered[n] = i.key;
182
205
  return;
183
206
  }
184
207
  case "patch":
185
- return this.knock.log(`[Guide] Patching the group stage: ${i.key}`), this.stage.ordered[r] = i.key, this.stage.resolved === i.key ? i : void 0;
208
+ return this.knock.log(`[Guide] Patching the group stage: ${i.key}`), this.stage.ordered[n] = i.key, this.stage.resolved === i.key ? i : void 0;
186
209
  case "closed":
187
210
  return this.stage.resolved === i.key ? i : void 0;
188
211
  }
@@ -190,7 +213,7 @@ class F {
190
213
  openGroupStage() {
191
214
  this.knock.log("[Guide] Opening a new group stage");
192
215
  const {
193
- orderResolutionDuration: e = b
216
+ orderResolutionDuration: e = A
194
217
  } = this.options, t = setTimeout(() => {
195
218
  this.closePendingGroupStage(), this.incrementCounter();
196
219
  }, e);
@@ -257,7 +280,7 @@ class F {
257
280
  seen_at: (/* @__PURE__ */ new Date()).toISOString()
258
281
  });
259
282
  if (!s) return;
260
- const r = {
283
+ const n = {
261
284
  ...this.buildEngagementEventBaseParams(e, s),
262
285
  content: s.content,
263
286
  data: this.targetParams.data,
@@ -265,23 +288,23 @@ class F {
265
288
  };
266
289
  return this.knock.user.markGuideStepAs(
267
290
  "seen",
268
- r
291
+ n
269
292
  ), s;
270
293
  }
271
294
  async markAsInteracted(e, t, s) {
272
295
  this.knock.log(
273
296
  `[Guide] Marking as interacted (Guide key: ${e.key}, Step ref:${t.ref})`
274
297
  );
275
- const r = (/* @__PURE__ */ new Date()).toISOString(), i = this.setStepMessageAttrs(e.key, t.ref, {
276
- read_at: r,
277
- interacted_at: r
298
+ const n = (/* @__PURE__ */ new Date()).toISOString(), i = this.setStepMessageAttrs(e.key, t.ref, {
299
+ read_at: n,
300
+ interacted_at: n
278
301
  });
279
302
  if (!i) return;
280
- const n = {
303
+ const r = {
281
304
  ...this.buildEngagementEventBaseParams(e, i),
282
305
  metadata: s
283
306
  };
284
- return this.knock.user.markGuideStepAs("interacted", n), i;
307
+ return this.knock.user.markGuideStepAs("interacted", r), i;
285
308
  }
286
309
  async markAsArchived(e, t) {
287
310
  if (t.message.archived_at) return;
@@ -292,11 +315,11 @@ class F {
292
315
  archived_at: (/* @__PURE__ */ new Date()).toISOString()
293
316
  });
294
317
  if (!s) return;
295
- const r = this.buildEngagementEventBaseParams(e, s);
318
+ const n = this.buildEngagementEventBaseParams(e, s);
296
319
  return this.knock.user.markGuideStepAs(
297
320
  "archived",
298
321
  {
299
- ...r,
322
+ ...n,
300
323
  unthrottled: e.bypass_global_group_limit
301
324
  }
302
325
  ), s;
@@ -309,13 +332,13 @@ class F {
309
332
  ...e,
310
333
  // Get the next unarchived step.
311
334
  getStep() {
312
- return this.steps.find((r) => !r.message.archived_at);
335
+ return this.steps.find((n) => !n.message.archived_at);
313
336
  }
314
337
  };
315
- return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: r, ...i }) => {
316
- const n = {
338
+ return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: n, ...i }) => {
339
+ const r = {
317
340
  ...i,
318
- message: { ...r },
341
+ message: { ...n },
319
342
  markAsSeen() {
320
343
  if (!this.message.seen_at)
321
344
  return t.markAsSeen(s, this);
@@ -328,17 +351,17 @@ class F {
328
351
  return t.markAsArchived(s, this);
329
352
  }
330
353
  };
331
- return n.markAsSeen = n.markAsSeen.bind(n), n.markAsInteracted = n.markAsInteracted.bind(n), n.markAsArchived = n.markAsArchived.bind(n), n;
332
- }), s.activation_location_rules = e.activation_location_rules.map((r) => ({
333
- ...r,
334
- pattern: new v({ pathname: r.pathname })
354
+ return r.markAsSeen = r.markAsSeen.bind(r), r.markAsInteracted = r.markAsInteracted.bind(r), r.markAsArchived = r.markAsArchived.bind(r), r;
355
+ }), s.activation_location_rules = e.activation_location_rules.map((n) => ({
356
+ ...n,
357
+ pattern: new G({ pathname: n.pathname })
335
358
  })), s;
336
359
  }
337
360
  buildQueryParams(e = {}) {
338
361
  const t = { ...this.targetParams, ...e };
339
362
  let s = Object.fromEntries(
340
363
  Object.entries(t).filter(
341
- ([r, i]) => i != null
364
+ ([n, i]) => i != null
342
365
  )
343
366
  );
344
367
  return s = s.data ? { ...s, data: JSON.stringify(s.data) } : s, s;
@@ -346,22 +369,22 @@ class F {
346
369
  formatQueryKey(e) {
347
370
  const s = Object.keys(e).sort().map(
348
371
  (i) => `${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`
349
- ).join("&"), r = A(this.knock.userId);
350
- return s ? `${r}?${s}` : r;
372
+ ).join("&"), n = R(this.knock.userId);
373
+ return s ? `${n}?${s}` : n;
351
374
  }
352
375
  setStepMessageAttrs(e, t, s) {
353
- let r;
376
+ let n;
354
377
  return s.archived_at && this.clearGroupStage(), this.store.setState((i) => {
355
- const n = i.guides[e];
356
- if (!n) return i;
357
- const o = n.steps.map((c) => (c.ref !== t || (c.message = { ...c.message, ...s }, r = c), c));
358
- n.steps = o;
359
- const a = { ...i.guides, [n.key]: n }, h = s.archived_at && !n.bypass_global_group_limit ? {
378
+ const r = i.guides[e];
379
+ if (!r) return i;
380
+ const o = r.steps.map((d) => (d.ref !== t || (d.message = { ...d.message, ...s }, n = d), d));
381
+ r.steps = o;
382
+ const a = { ...i.guides, [r.key]: r }, h = s.archived_at && !r.bypass_global_group_limit ? {
360
383
  ...i.guideGroupDisplayLogs,
361
- [g]: s.archived_at
384
+ [p]: s.archived_at
362
385
  } : i.guideGroupDisplayLogs;
363
386
  return { ...i, guides: a, guideGroupDisplayLogs: h };
364
- }), r;
387
+ }), n;
365
388
  }
366
389
  buildEngagementEventBaseParams(e, t) {
367
390
  return {
@@ -376,61 +399,63 @@ class F {
376
399
  this.patchClosedGroupStage();
377
400
  const t = this.localCopy(e.guide);
378
401
  this.store.setState((s) => {
379
- const r = { ...s.guides, [t.key]: t };
380
- return { ...s, guides: r };
402
+ const n = { ...s.guides, [t.key]: t };
403
+ return { ...s, guides: n };
381
404
  });
382
405
  }
383
406
  removeGuide({ data: e }) {
384
407
  this.patchClosedGroupStage(), this.store.setState((t) => {
385
- const { [e.guide.key]: s, ...r } = t.guides;
386
- return { ...t, guides: r };
408
+ const { [e.guide.key]: s, ...n } = t.guides;
409
+ return { ...t, guides: n };
387
410
  });
388
411
  }
389
412
  addOrReplaceGuideGroup({
390
413
  data: e
391
414
  }) {
392
415
  this.patchClosedGroupStage(), this.store.setState((t) => {
393
- const s = [e.guide_group], r = e.guide_group.display_sequence_unthrottled || [], i = e.guide_group.display_sequence_throttled || [];
394
- let n = t.guides;
395
- return n = r.reduce((o, a) => {
416
+ const s = [e.guide_group], n = e.guide_group.display_sequence_unthrottled || [], i = e.guide_group.display_sequence_throttled || [];
417
+ let r = t.guides;
418
+ return r = n.reduce((o, a) => {
396
419
  if (!o[a]) return o;
397
420
  const h = { ...o[a], bypass_global_group_limit: !0 };
398
421
  return { ...o, [a]: h };
399
- }, n), n = i.reduce((o, a) => {
422
+ }, r), r = i.reduce((o, a) => {
400
423
  if (!o[a]) return o;
401
424
  const h = { ...o[a], bypass_global_group_limit: !1 };
402
425
  return { ...o, [a]: h };
403
- }, n), { ...t, guides: n, guideGroups: s };
426
+ }, r), { ...t, guides: r, guideGroups: s };
404
427
  });
405
428
  }
406
429
  listenForLocationChangesFromWindow() {
407
- if (window != null && window.history) {
408
- window.addEventListener("popstate", this.handleLocationChange), window.addEventListener("hashchange", this.handleLocationChange);
409
- const e = window.history.pushState, t = window.history.replaceState;
410
- window.history.pushState = new Proxy(e, {
411
- apply: (s, r, i) => {
412
- Reflect.apply(s, r, i), setTimeout(() => {
430
+ const e = l();
431
+ if (e != null && e.history) {
432
+ e.addEventListener("popstate", this.handleLocationChange), e.addEventListener("hashchange", this.handleLocationChange);
433
+ const t = e.history.pushState, s = e.history.replaceState;
434
+ e.history.pushState = new Proxy(t, {
435
+ apply: (n, i, r) => {
436
+ Reflect.apply(n, i, r), setTimeout(() => {
413
437
  this.handleLocationChange();
414
438
  }, 0);
415
439
  }
416
- }), window.history.replaceState = new Proxy(t, {
417
- apply: (s, r, i) => {
418
- Reflect.apply(s, r, i), setTimeout(() => {
440
+ }), e.history.replaceState = new Proxy(s, {
441
+ apply: (n, i, r) => {
442
+ Reflect.apply(n, i, r), setTimeout(() => {
419
443
  this.handleLocationChange();
420
444
  }, 0);
421
445
  }
422
- }), this.pushStateFn = e, this.replaceStateFn = t;
446
+ }), this.pushStateFn = t, this.replaceStateFn = s;
423
447
  } else
424
448
  this.knock.log(
425
449
  "[Guide] Unable to access the `window.history` object to detect location changes"
426
450
  );
427
451
  }
428
452
  removeEventListeners() {
429
- window.removeEventListener("popstate", this.handleLocationChange), window.removeEventListener("hashchange", this.handleLocationChange), this.pushStateFn && (window.history.pushState = this.pushStateFn, this.pushStateFn = void 0), this.replaceStateFn && (window.history.replaceState = this.replaceStateFn, this.replaceStateFn = void 0);
453
+ const e = l();
454
+ e != null && e.history && (e.removeEventListener("popstate", this.handleLocationChange), e.removeEventListener("hashchange", this.handleLocationChange), this.pushStateFn && (e.history.pushState = this.pushStateFn, this.pushStateFn = void 0), this.replaceStateFn && (e.history.replaceState = this.replaceStateFn, this.replaceStateFn = void 0));
430
455
  }
431
456
  }
432
457
  export {
433
- F as KnockGuideClient,
434
- A as guidesApiRootPath
458
+ $ as KnockGuideClient,
459
+ R as guidesApiRootPath
435
460
  };
436
461
  //# sourceMappingURL=client.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.mjs","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n\n const location = trackLocationFromWindow\n ? window?.location.href\n : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n newChannel.join();\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const href = window.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n if (window?.history) {\n // 1. Listen for browser back/forward button clicks.\n window.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n window.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = window.history.pushState;\n const replaceStateFn = window.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n window.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n window.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n window.removeEventListener(\"popstate\", this.handleLocationChange);\n window.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n window.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n window.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","guideGroupDisplayLogs","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":";;;;;;AA6CA,MAAMA,IAAoC,IAIpCC,IAAqC,KAAK,KAEnCC,IAAoB,CAACC,MAChC,aAAaA,CAAM,WAEfC,IAAS,CAACC,GAAmBC,IAA8B,OAAO;AAEhE,QAAAC,IAAS,IAAIC,EAAgB,GAE7BC,IAAeC,EAAiBL,EAAM,WAAW;AACnD,MAAA,CAACI,EAAqB,QAAAF;AAE1B,QAAMI,IAAkBF,EAAa,kBAC/BG,IAAWP,EAAM;AAEvB,aAAW,CAACQ,GAAOC,CAAQ,KAAKH,EAAgB,WAAW;AACnD,UAAAI,IAAQV,EAAM,OAAOS,CAAQ;AAInC,IAHI,CAACC,KAGD,CADaC,EAAUD,GAAO,EAAE,UAAAH,GAAU,SAAAN,GAAS,KAGhDC,EAAA,IAAIM,GAAOE,CAAK;AAAA,EAAA;AAGlB,SAAAR,EAAA,WAAW,EAAE,YAAYE,EAAa,GACtCF;AACT,GAOMS,IAAY,CAChBD,GACA,EAAE,UAAAH,GAAU,SAAAN,IAAU,SACnB;AASC,MARAA,EAAQ,QAAQA,EAAQ,SAASS,EAAM,QAIvCT,EAAQ,OAAOA,EAAQ,QAAQS,EAAM,OAIrCA,EAAM,MAAM,MAAM,CAACE,MAAM,CAAC,CAACA,EAAE,QAAQ,WAAW;AAC3C,WAAA;AAGH,QAAAC,IAAgBH,EAAM,6BAA6B,CAAC;AAEtD,SAAA,EAAAG,EAAc,SAAS,KAAKN,KA4B1B,CA3BYM,EAAc,OAA4B,CAACC,GAAKC,MAAS;AAGnE,QAAAD,MAAQ,GAAc,QAAA;AAK1B,YAAQC,EAAK,WAAW;AAAA,MACtB,KAAK;AAGC,eAAAD,MAAQ,MAEIC,EAAK,QAAQ,KAAKR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AA,IAAA;AAEtB,WAAK,MAAM;AAAA,QACT;AAAA,MACF;AAAA,EACF;AAAA,EAGM,uBAAuB;AACtB,WAAA,oBAAoB,YAAY,KAAK,oBAAoB,GACzD,OAAA,oBAAoB,cAAc,KAAK,oBAAoB,GAE9D,KAAK,gBACA,OAAA,QAAQ,YAAY,KAAK,aAChC,KAAK,cAAc,SAEjB,KAAK,mBACA,OAAA,QAAQ,eAAe,KAAK,gBACnC,KAAK,iBAAiB;AAAA,EACxB;AAEJ;"}
1
+ {"version":3,"file":"client.mjs","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\n// Maximum number of retry attempts for channel subscription\nconst SUBSCRIBE_RETRY_LIMIT = 3;\n\n// Return the global window object if defined, so to safely guard against SSR.\nconst checkForWindow = () => {\n if (typeof window !== \"undefined\") {\n return window;\n }\n};\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n private subscribeRetryCount = 0;\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n const win = checkForWindow();\n\n const location = trackLocationFromWindow ? win?.location.href : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n // Reset retry count for new subscription attempt\n this.subscribeRetryCount = 0;\n\n newChannel\n .join()\n .receive(\"ok\", () => {\n this.knock.log(\"[Guide] Successfully joined channel\");\n })\n .receive(\"error\", (resp) => {\n this.knock.log(\n `[Guide] Failed to join channel: ${JSON.stringify(resp)}`,\n );\n this.handleChannelJoinError();\n })\n .receive(\"timeout\", () => {\n this.knock.log(\"[Guide] Channel join timed out\");\n this.handleChannelJoinError();\n });\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n private handleChannelJoinError() {\n // Prevent phx channel from retrying forever in case of either network or\n // other errors (e.g. auth error, invalid channel etc)\n if (this.subscribeRetryCount >= SUBSCRIBE_RETRY_LIMIT) {\n this.knock.log(\n `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`,\n );\n this.unsubscribe();\n return;\n }\n\n this.subscribeRetryCount++;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const win = checkForWindow();\n if (!win?.location) return;\n\n const href = win.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n const win = checkForWindow();\n if (win?.history) {\n // 1. Listen for browser back/forward button clicks.\n win.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n win.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = win.history.pushState;\n const replaceStateFn = win.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n win.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n win.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n const win = checkForWindow();\n if (!win?.history) return;\n\n win.removeEventListener(\"popstate\", this.handleLocationChange);\n win.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n win.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n win.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","SUBSCRIBE_RETRY_LIMIT","checkForWindow","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","win","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","resp","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","guideGroupDisplayLogs","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":";;;;;;AA6CA,MAAMA,IAAoC,IAIpCC,IAAqC,KAAK,KAG1CC,IAAwB,GAGxBC,IAAiB,MAAM;AACvB,MAAA,OAAO,SAAW;AACb,WAAA;AAEX,GAEaC,IAAoB,CAACC,MAChC,aAAaA,CAAM,WAEfC,IAAS,CAACC,GAAmBC,IAA8B,OAAO;AAEhE,QAAAC,IAAS,IAAIC,EAAgB,GAE7BC,IAAeC,EAAiBL,EAAM,WAAW;AACnD,MAAA,CAACI,EAAqB,QAAAF;AAE1B,QAAMI,IAAkBF,EAAa,kBAC/BG,IAAWP,EAAM;AAEvB,aAAW,CAACQ,GAAOC,CAAQ,KAAKH,EAAgB,WAAW;AACnD,UAAAI,IAAQV,EAAM,OAAOS,CAAQ;AAInC,IAHI,CAACC,KAGD,CADaC,EAAUD,GAAO,EAAE,UAAAH,GAAU,SAAAN,GAAS,KAGhDC,EAAA,IAAIM,GAAOE,CAAK;AAAA,EAAA;AAGlB,SAAAR,EAAA,WAAW,EAAE,YAAYE,EAAa,GACtCF;AACT,GAOMS,IAAY,CAChBD,GACA,EAAE,UAAAH,GAAU,SAAAN,IAAU,SACnB;AASC,MARAA,EAAQ,QAAQA,EAAQ,SAASS,EAAM,QAIvCT,EAAQ,OAAOA,EAAQ,QAAQS,EAAM,OAIrCA,EAAM,MAAM,MAAM,CAACE,MAAM,CA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c,KAAK,oBAAoB,GAE3D,KAAK,gBACHA,EAAA,QAAQ,YAAY,KAAK,aAC7B,KAAK,cAAc,SAEjB,KAAK,mBACHA,EAAA,QAAQ,eAAe,KAAK,gBAChC,KAAK,iBAAiB;AAAA,EACxB;AAEJ;"}
@@ -13,6 +13,7 @@ export declare class KnockGuideClient {
13
13
  private socketChannel;
14
14
  private socketChannelTopic;
15
15
  private socketEventTypes;
16
+ private subscribeRetryCount;
16
17
  private pushStateFn;
17
18
  private replaceStateFn;
18
19
  private stage;
@@ -26,6 +27,7 @@ export declare class KnockGuideClient {
26
27
  filters?: QueryFilterParams;
27
28
  }): Promise<QueryStatus>;
28
29
  subscribe(): void;
30
+ private handleChannelJoinError;
29
31
  unsubscribe(): void;
30
32
  private handleSocketEvent;
31
33
  setLocation(href: string): void;
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../../src/clients/guide/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAIxC,OAAO,KAAK,MAAM,aAAa,CAAC;AAWhC,OAAO,EACL,eAAe,EAKf,SAAS,EAKT,aAAa,EAEb,UAAU,EACV,cAAc,EAKd,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EAElB,UAAU,EACV,YAAY,EACb,MAAM,SAAS,CAAC;AAUjB,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,GAAG,SAAS,GAAG,IAAI,WACrC,CAAC;AAmF/B,qBAAa,gBAAgB;IA2BzB,QAAQ,CAAC,KAAK,EAAE,KAAK;IACrB,QAAQ,CAAC,SAAS,EAAE,MAAM;IAC1B,QAAQ,CAAC,YAAY,EAAE,YAAY;IACnC,QAAQ,CAAC,OAAO,EAAE,eAAe;IA7B5B,KAAK,EAAE,KAAK,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,UAAU,C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CAAC,oBAAoB;CAgB7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knocklabs/client",
3
- "version": "0.16.3",
3
+ "version": "0.16.5",
4
4
  "description": "The clientside library for interacting with Knock",
5
5
  "homepage": "https://github.com/knocklabs/javascript/tree/main/packages/client",
6
6
  "author": "@knocklabs",
@@ -50,12 +50,12 @@
50
50
  "@babel/plugin-proposal-class-properties": "^7.16.7",
51
51
  "@babel/plugin-proposal-object-rest-spread": "^7.16.7",
52
52
  "@babel/plugin-transform-runtime": "^7.28.0",
53
- "@babel/preset-env": "^7.27.1",
53
+ "@babel/preset-env": "^7.28.3",
54
54
  "@babel/preset-typescript": "^7.27.0",
55
55
  "@codecov/vite-plugin": "^1.9.1",
56
56
  "@types/jsonwebtoken": "^9.0.10",
57
57
  "@typescript-eslint/eslint-plugin": "^8.32.0",
58
- "@typescript-eslint/parser": "^8.32.1",
58
+ "@typescript-eslint/parser": "^8.39.1",
59
59
  "cross-env": "^7.0.3",
60
60
  "crypto": "^1.0.1",
61
61
  "eslint": "^8.56.0",
@@ -70,7 +70,7 @@
70
70
  "dependencies": {
71
71
  "@babel/runtime": "^7.27.1",
72
72
  "@knocklabs/types": "^0.1.5",
73
- "@tanstack/store": "^0.7.1",
73
+ "@tanstack/store": "^0.7.2",
74
74
  "@types/phoenix": "^1.6.6",
75
75
  "axios": "^1.11.0",
76
76
  "axios-retry": "^4.5.0",
@@ -49,6 +49,16 @@ const DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds
49
49
  // trigger subscribed callbacks.
50
50
  const DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds
51
51
 
52
+ // Maximum number of retry attempts for channel subscription
53
+ const SUBSCRIBE_RETRY_LIMIT = 3;
54
+
55
+ // Return the global window object if defined, so to safely guard against SSR.
56
+ const checkForWindow = () => {
57
+ if (typeof window !== "undefined") {
58
+ return window;
59
+ }
60
+ };
61
+
52
62
  export const guidesApiRootPath = (userId: string | undefined | null) =>
53
63
  `/v1/users/${userId}/guides`;
54
64
 
@@ -147,6 +157,7 @@ export class KnockGuideClient {
147
157
  "guide_group.added",
148
158
  "guide_group.updated",
149
159
  ];
160
+ private subscribeRetryCount = 0;
150
161
 
151
162
  // Original history methods to monkey patch, or restore in cleanups.
152
163
  private pushStateFn: History["pushState"] | undefined;
@@ -166,10 +177,9 @@ export class KnockGuideClient {
166
177
  readonly options: ConstructorOpts = {},
167
178
  ) {
168
179
  const { trackLocationFromWindow = true } = options;
180
+ const win = checkForWindow();
169
181
 
170
- const location = trackLocationFromWindow
171
- ? window?.location.href
172
- : undefined;
182
+ const location = trackLocationFromWindow ? win?.location.href : undefined;
173
183
 
174
184
  this.store = new Store<StoreState>({
175
185
  guideGroups: [],
@@ -300,13 +310,44 @@ export class KnockGuideClient {
300
310
  }
301
311
 
302
312
  if (["closed", "errored"].includes(newChannel.state)) {
303
- newChannel.join();
313
+ // Reset retry count for new subscription attempt
314
+ this.subscribeRetryCount = 0;
315
+
316
+ newChannel
317
+ .join()
318
+ .receive("ok", () => {
319
+ this.knock.log("[Guide] Successfully joined channel");
320
+ })
321
+ .receive("error", (resp) => {
322
+ this.knock.log(
323
+ `[Guide] Failed to join channel: ${JSON.stringify(resp)}`,
324
+ );
325
+ this.handleChannelJoinError();
326
+ })
327
+ .receive("timeout", () => {
328
+ this.knock.log("[Guide] Channel join timed out");
329
+ this.handleChannelJoinError();
330
+ });
304
331
  }
305
332
 
306
333
  // Track the joined channel.
307
334
  this.socketChannel = newChannel;
308
335
  }
309
336
 
337
+ private handleChannelJoinError() {
338
+ // Prevent phx channel from retrying forever in case of either network or
339
+ // other errors (e.g. auth error, invalid channel etc)
340
+ if (this.subscribeRetryCount >= SUBSCRIBE_RETRY_LIMIT) {
341
+ this.knock.log(
342
+ `[Guide] Channel join max retry limit reached: ${this.subscribeRetryCount}`,
343
+ );
344
+ this.unsubscribe();
345
+ return;
346
+ }
347
+
348
+ this.subscribeRetryCount++;
349
+ }
350
+
310
351
  unsubscribe() {
311
352
  if (!this.socketChannel) return;
312
353
  this.knock.log("[Guide] Unsubscribing from real time updates");
@@ -860,7 +901,10 @@ export class KnockGuideClient {
860
901
 
861
902
  // Define as an arrow func property to always bind this to the class instance.
862
903
  private handleLocationChange = () => {
863
- const href = window.location.href;
904
+ const win = checkForWindow();
905
+ if (!win?.location) return;
906
+
907
+ const href = win.location.href;
864
908
  if (this.store.state.location === href) return;
865
909
 
866
910
  this.knock.log(`[Guide] Handle Location change: ${href}`);
@@ -868,19 +912,20 @@ export class KnockGuideClient {
868
912
  };
869
913
 
870
914
  private listenForLocationChangesFromWindow() {
871
- if (window?.history) {
915
+ const win = checkForWindow();
916
+ if (win?.history) {
872
917
  // 1. Listen for browser back/forward button clicks.
873
- window.addEventListener("popstate", this.handleLocationChange);
918
+ win.addEventListener("popstate", this.handleLocationChange);
874
919
 
875
920
  // 2. Listen for hash changes in case it's used for routing.
876
- window.addEventListener("hashchange", this.handleLocationChange);
921
+ win.addEventListener("hashchange", this.handleLocationChange);
877
922
 
878
923
  // 3. Monkey-patch history methods to catch programmatic navigation.
879
- const pushStateFn = window.history.pushState;
880
- const replaceStateFn = window.history.replaceState;
924
+ const pushStateFn = win.history.pushState;
925
+ const replaceStateFn = win.history.replaceState;
881
926
 
882
927
  // Use setTimeout to allow the browser state to potentially settle.
883
- window.history.pushState = new Proxy(pushStateFn, {
928
+ win.history.pushState = new Proxy(pushStateFn, {
884
929
  apply: (target, history, args) => {
885
930
  Reflect.apply(target, history, args);
886
931
  setTimeout(() => {
@@ -888,7 +933,7 @@ export class KnockGuideClient {
888
933
  }, 0);
889
934
  },
890
935
  });
891
- window.history.replaceState = new Proxy(replaceStateFn, {
936
+ win.history.replaceState = new Proxy(replaceStateFn, {
892
937
  apply: (target, history, args) => {
893
938
  Reflect.apply(target, history, args);
894
939
  setTimeout(() => {
@@ -908,15 +953,18 @@ export class KnockGuideClient {
908
953
  }
909
954
 
910
955
  private removeEventListeners() {
911
- window.removeEventListener("popstate", this.handleLocationChange);
912
- window.removeEventListener("hashchange", this.handleLocationChange);
956
+ const win = checkForWindow();
957
+ if (!win?.history) return;
958
+
959
+ win.removeEventListener("popstate", this.handleLocationChange);
960
+ win.removeEventListener("hashchange", this.handleLocationChange);
913
961
 
914
962
  if (this.pushStateFn) {
915
- window.history.pushState = this.pushStateFn;
963
+ win.history.pushState = this.pushStateFn;
916
964
  this.pushStateFn = undefined;
917
965
  }
918
966
  if (this.replaceStateFn) {
919
- window.history.replaceState = this.replaceStateFn;
967
+ win.history.replaceState = this.replaceStateFn;
920
968
  this.replaceStateFn = undefined;
921
969
  }
922
970
  }