@knocklabs/client 0.10.17 → 0.11.0-rc-2.0
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 +6 -15
- package/dist/cjs/api.js +1 -1
- package/dist/cjs/api.js.map +1 -1
- package/dist/cjs/clients/feed/feed.js +1 -1
- package/dist/cjs/clients/feed/feed.js.map +1 -1
- package/dist/cjs/clients/feed/store.js +1 -1
- package/dist/cjs/clients/feed/store.js.map +1 -1
- package/dist/cjs/clients/in-app-messages/channel-client.js +2 -0
- package/dist/cjs/clients/in-app-messages/channel-client.js.map +1 -0
- package/dist/cjs/clients/in-app-messages/message-client.js +2 -0
- package/dist/cjs/clients/in-app-messages/message-client.js.map +1 -0
- package/dist/cjs/clients/in-app-messages/socket-manager.js +2 -0
- package/dist/cjs/clients/in-app-messages/socket-manager.js.map +1 -0
- package/dist/cjs/clients/in-app-messages/store.js +2 -0
- package/dist/cjs/clients/in-app-messages/store.js.map +1 -0
- package/dist/cjs/clients/users/index.js +1 -1
- package/dist/cjs/clients/users/index.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/esm/api.mjs +14 -9
- package/dist/esm/api.mjs.map +1 -1
- package/dist/esm/clients/in-app-messages/channel-client.mjs +80 -0
- package/dist/esm/clients/in-app-messages/channel-client.mjs.map +1 -0
- package/dist/esm/clients/in-app-messages/message-client.mjs +157 -0
- package/dist/esm/clients/in-app-messages/message-client.mjs.map +1 -0
- package/dist/esm/clients/in-app-messages/socket-manager.mjs +83 -0
- package/dist/esm/clients/in-app-messages/socket-manager.mjs.map +1 -0
- package/dist/esm/clients/in-app-messages/store.mjs +11 -0
- package/dist/esm/clients/in-app-messages/store.mjs.map +1 -0
- package/dist/esm/clients/users/index.mjs +28 -20
- package/dist/esm/clients/users/index.mjs.map +1 -1
- package/dist/esm/index.mjs +11 -7
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/api.d.ts +1 -0
- package/dist/types/api.d.ts.map +1 -1
- package/dist/types/clients/feed/feed.d.ts +1 -0
- package/dist/types/clients/feed/index.d.ts +1 -0
- package/dist/types/clients/feed/interfaces.d.ts +1 -0
- package/dist/types/clients/feed/store.d.ts +1 -0
- package/dist/types/clients/feed/types.d.ts +1 -0
- package/dist/types/clients/feed/utils.d.ts +1 -0
- package/dist/types/clients/in-app-messages/channel-client.d.ts +23 -0
- package/dist/types/clients/in-app-messages/channel-client.d.ts.map +1 -0
- package/dist/types/clients/in-app-messages/index.d.ts +4 -0
- package/dist/types/clients/in-app-messages/index.d.ts.map +1 -0
- package/dist/types/clients/in-app-messages/message-client.d.ts +52 -0
- package/dist/types/clients/in-app-messages/message-client.d.ts.map +1 -0
- package/dist/types/clients/in-app-messages/socket-manager.d.ts +27 -0
- package/dist/types/clients/in-app-messages/socket-manager.d.ts.map +1 -0
- package/dist/types/clients/in-app-messages/store.d.ts +6 -0
- package/dist/types/clients/in-app-messages/store.d.ts.map +1 -0
- package/dist/types/clients/in-app-messages/types.d.ts +54 -0
- package/dist/types/clients/in-app-messages/types.d.ts.map +1 -0
- package/dist/types/clients/messages/index.d.ts +1 -0
- package/dist/types/clients/messages/interfaces.d.ts +2 -1
- package/dist/types/clients/messages/interfaces.d.ts.map +1 -1
- package/dist/types/clients/objects/index.d.ts +1 -0
- package/dist/types/clients/preferences/index.d.ts +1 -0
- package/dist/types/clients/preferences/interfaces.d.ts +7 -9
- package/dist/types/clients/preferences/interfaces.d.ts.map +1 -1
- package/dist/types/clients/slack/index.d.ts +1 -0
- package/dist/types/clients/users/index.d.ts +4 -1
- package/dist/types/clients/users/index.d.ts.map +1 -1
- package/dist/types/clients/users/interfaces.d.ts +9 -0
- package/dist/types/clients/users/interfaces.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/interfaces.d.ts +1 -0
- package/dist/types/knock.d.ts +1 -0
- package/package.json +15 -13
- package/src/api.ts +9 -2
- package/src/clients/in-app-messages/channel-client.ts +129 -0
- package/src/clients/in-app-messages/index.ts +3 -0
- package/src/clients/in-app-messages/message-client.ts +275 -0
- package/src/clients/in-app-messages/socket-manager.ts +187 -0
- package/src/clients/in-app-messages/store.ts +15 -0
- package/src/clients/in-app-messages/types.ts +82 -0
- package/src/clients/messages/interfaces.ts +1 -9
- package/src/clients/preferences/interfaces.ts +6 -11
- package/src/clients/users/index.ts +19 -1
- package/src/clients/users/interfaces.ts +10 -0
- package/src/index.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,25 +1,16 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 0.11.0-rc-2.0
|
|
4
4
|
|
|
5
|
-
###
|
|
6
|
-
|
|
7
|
-
- 26db496: fix: ensure feed can render with empty/missing userId values
|
|
8
|
-
- 988aaf9: fix: engagement_status in BulkUpdateMessagesInChannelProperties type
|
|
9
|
-
|
|
10
|
-
## 0.10.16
|
|
11
|
-
|
|
12
|
-
### Patch Changes
|
|
13
|
-
|
|
14
|
-
- bc99374: fix: bundle client package using "compat" interop
|
|
5
|
+
### Minor Changes
|
|
15
6
|
|
|
16
|
-
|
|
7
|
+
- f442962: feat: add in app messages client, hooks, provider, and components
|
|
17
8
|
|
|
18
9
|
### Patch Changes
|
|
19
10
|
|
|
20
|
-
-
|
|
21
|
-
- Updated dependencies [
|
|
22
|
-
- @knocklabs/types@0.1.5
|
|
11
|
+
- 1b3ad4b: fix: ensure feed can render with empty/missing userId values
|
|
12
|
+
- Updated dependencies [f442962]
|
|
13
|
+
- @knocklabs/types@0.1.5-rc-2.0
|
|
23
14
|
|
|
24
15
|
## 0.10.14
|
|
25
16
|
|
package/dist/cjs/api.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var a=Object.defineProperty;var n=(s,e,t)=>e in s?a(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var r=(s,e,t)=>n(s,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("axios"),o=require("axios-retry"),u=require("phoenix");class c{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;const t=i.default?i.default:i;this.axiosClient=t.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken}}),typeof window<"u"&&(this.socket=new u.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.exponentialDelay})}async makeRequest(e){try{const t=await this.axiosClient(e);return{statusCode:t.status<300?"ok":"error",body:t.data,error:void 0,status:t.status}}catch(t){return console.error(t),{statusCode:"error",status:500,body:void 0,error:t}}}canRetryRequest(e){return o.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}}exports.default=c;
|
|
2
2
|
//# sourceMappingURL=api.js.map
|
package/dist/cjs/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sources":["../../src/api.ts"],"sourcesContent":["import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport axiosRetry from \"axios-retry\";\nimport { Socket } from \"phoenix\";\n\ntype ApiClientOptions = {\n host: string;\n apiKey: string;\n userToken: string | undefined;\n};\n\nexport interface ApiResponse {\n // eslint-disable-next-line\n error?: any;\n // eslint-disable-next-line\n body?: any;\n statusCode: \"ok\" | \"error\";\n status: number;\n}\n\nclass ApiClient {\n private host: string;\n private apiKey: string;\n private userToken: string | null;\n private axiosClient: AxiosInstance;\n\n public socket: Socket | undefined;\n\n constructor(options: ApiClientOptions) {\n this.host = options.host;\n this.apiKey = options.apiKey;\n this.userToken = options.userToken || null;\n\n // Create a retryable axios client\n this.
|
|
1
|
+
{"version":3,"file":"api.js","sources":["../../src/api.ts"],"sourcesContent":["import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport axiosRetry from \"axios-retry\";\nimport { Socket } from \"phoenix\";\n\ntype ApiClientOptions = {\n host: string;\n apiKey: string;\n userToken: string | undefined;\n};\n\nexport interface ApiResponse {\n // eslint-disable-next-line\n error?: any;\n // eslint-disable-next-line\n body?: any;\n statusCode: \"ok\" | \"error\";\n status: number;\n}\n\nclass ApiClient {\n private host: string;\n private apiKey: string;\n private userToken: string | null;\n private axiosClient: AxiosInstance;\n\n public socket: Socket | undefined;\n\n constructor(options: ApiClientOptions) {\n this.host = options.host;\n this.apiKey = options.apiKey;\n this.userToken = options.userToken || null;\n\n // Create a retryable axios client, but account for issues where the axios export is not\n // the default in certain bundlers (Webpack).\n //\n // NOTE: This is a temporary fix that exists because of this issue:\n // https://github.com/axios/axios/issues/6591\n const axiosInstance = // @ts-expect-error Fixing the issue described above\n (axios.default ? axios.default : axios) as AxiosStatic;\n\n this.axiosClient = axiosInstance.create({\n baseURL: this.host,\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.apiKey}`,\n \"X-Knock-User-Token\": this.userToken,\n },\n });\n\n if (typeof window !== \"undefined\") {\n this.socket = new Socket(`${this.host.replace(\"http\", \"ws\")}/ws/v1`, {\n params: {\n user_token: this.userToken,\n api_key: this.apiKey,\n },\n });\n }\n\n axiosRetry(this.axiosClient, {\n retries: 3,\n retryCondition: this.canRetryRequest,\n retryDelay: axiosRetry.exponentialDelay,\n });\n }\n\n async makeRequest(req: AxiosRequestConfig): Promise<ApiResponse> {\n try {\n const result = await this.axiosClient(req);\n\n return {\n statusCode: result.status < 300 ? \"ok\" : \"error\",\n body: result.data,\n error: undefined,\n status: result.status,\n };\n\n // eslint:disable-next-line\n } catch (e: unknown) {\n console.error(e);\n\n return {\n statusCode: \"error\",\n status: 500,\n body: undefined,\n error: e,\n };\n }\n }\n\n private canRetryRequest(error: AxiosError) {\n // Retry Network Errors.\n if (axiosRetry.isNetworkError(error)) {\n return true;\n }\n\n if (!error.response) {\n // Cannot determine if the request can be retried\n return false;\n }\n\n // Retry Server Errors (5xx).\n if (error.response.status >= 500 && error.response.status <= 599) {\n return true;\n }\n\n // Retry if rate limited.\n if (error.response.status === 429) {\n return true;\n }\n\n return false;\n }\n}\n\nexport default ApiClient;\n"],"names":["ApiClient","options","__publicField","axiosInstance","axios","Socket","axiosRetry","req","result","e","error"],"mappings":"uVAmBA,MAAMA,CAAU,CAQd,YAAYC,EAA2B,CAP/BC,EAAA,aACAA,EAAA,eACAA,EAAA,kBACAA,EAAA,oBAEDA,EAAA,eAGL,KAAK,KAAOD,EAAQ,KACpB,KAAK,OAASA,EAAQ,OACjB,KAAA,UAAYA,EAAQ,WAAa,KAOhC,MAAAE,EACHC,EAAM,QAAUA,EAAM,QAAUA,EAE9B,KAAA,YAAcD,EAAc,OAAO,CACtC,QAAS,KAAK,KACd,QAAS,CACP,OAAQ,mBACR,eAAgB,mBAChB,cAAe,UAAU,KAAK,MAAM,GACpC,qBAAsB,KAAK,SAC7B,CAAA,CACD,EAEG,OAAO,OAAW,MACf,KAAA,OAAS,IAAIE,EAAA,OAAO,GAAG,KAAK,KAAK,QAAQ,OAAQ,IAAI,CAAC,SAAU,CACnE,OAAQ,CACN,WAAY,KAAK,UACjB,QAAS,KAAK,MAChB,CAAA,CACD,GAGHC,EAAW,KAAK,YAAa,CAC3B,QAAS,EACT,eAAgB,KAAK,gBACrB,WAAYA,EAAW,gBAAA,CACxB,CACH,CAEA,MAAM,YAAYC,EAA+C,CAC3D,GAAA,CACF,MAAMC,EAAS,MAAM,KAAK,YAAYD,CAAG,EAElC,MAAA,CACL,WAAYC,EAAO,OAAS,IAAM,KAAO,QACzC,KAAMA,EAAO,KACb,MAAO,OACP,OAAQA,EAAO,MAAA,QAIVC,EAAY,CACnB,eAAQ,MAAMA,CAAC,EAER,CACL,WAAY,QACZ,OAAQ,IACR,KAAM,OACN,MAAOA,CAAA,CAEX,CACF,CAEQ,gBAAgBC,EAAmB,CAErC,OAAAJ,EAAW,eAAeI,CAAK,EAC1B,GAGJA,EAAM,SAMPA,EAAM,SAAS,QAAU,KAAOA,EAAM,SAAS,QAAU,KAKzDA,EAAM,SAAS,SAAW,IATrB,EAcX,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var f=Object.defineProperty;var p=(c,t,e)=>t in c?f(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e;var d=(c,t,e)=>p(c,typeof t!="symbol"?t+"":t,e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const k=require("eventemitter2"),m=require("../../networkStatus.js"),g=require("./store.js"),_=c=>c&&typeof c=="object"&&"default"in c?c:{default:c},S=_(k),v={archived:"exclude"},y=2e3;class b{constructor(t,e,s){d(this,"userFeedId");d(this,"channel");d(this,"broadcaster");d(this,"defaultOptions");d(this,"broadcastChannel");d(this,"disconnectTimer",null);d(this,"hasSubscribedToRealTimeUpdates",!1);d(this,"visibilityChangeHandler",()=>{});d(this,"visibilityChangeListenerConnected",!1);d(this,"store");this.knock=t,this.feedId=e,this.feedId=e,this.userFeedId=this.buildUserFeedId(),this.store=g.default(),this.broadcaster=new S.default({wildcard:!0,delimiter:"."}),this.defaultOptions={...v,...s},this.knock.log(`[Feed] Initialized a feed on channel ${e}`),this.initializeRealtimeConnection(),this.setupBroadcastChannel()}reinitialize(){this.userFeedId=this.buildUserFeedId(),this.initializeRealtimeConnection(),this.setupBroadcastChannel()}teardown(){this.knock.log("[Feed] Tearing down feed instance"),this.channel&&(this.channel.leave(),this.channel.off("new-message")),this.teardownAutoSocketManager(),this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.broadcastChannel&&this.broadcastChannel.close()}dispose(){this.knock.log("[Feed] Disposing of feed instance"),this.teardown(),this.broadcaster.removeAllListeners(),this.knock.feeds.removeInstance(this)}listenForUpdates(){if(this.knock.log("[Feed] Connecting to real-time service"),this.hasSubscribedToRealTimeUpdates=!0,!this.knock.isAuthenticated()){this.knock.log("[Feed] User is not authenticated, skipping listening for updates");return}const t=this.knock.client().socket;t&&!t.isConnected()&&t.connect(),this.channel&&["closed","errored"].includes(this.channel.state)&&this.channel.join()}on(t,e){this.broadcaster.on(t,e)}off(t,e){this.broadcaster.off(t,e)}getState(){return this.store.getState()}async markAsSeen(t){const e=new Date().toISOString();return this.optimisticallyPerformStatusUpdate(t,"seen",{seen_at:e},"unseen_count"),this.makeStatusUpdate(t,"seen")}async markAllAsSeen(){const{metadata:t,items:e,...s}=this.store.getState();if(this.defaultOptions.status==="unseen")s.resetStore({...t,total_count:0,unseen_count:0});else{s.setMetadata({...t,unseen_count:0});const i={seen_at:new Date().toISOString()},o=e.map(r=>r.id);s.setItemAttrs(o,i)}const n=await this.makeBulkStatusUpdate("seen");return this.emitEvent("all_seen",e),n}async markAsUnseen(t){return this.optimisticallyPerformStatusUpdate(t,"unseen",{seen_at:null},"unseen_count"),this.makeStatusUpdate(t,"unseen")}async markAsRead(t){const e=new Date().toISOString();return this.optimisticallyPerformStatusUpdate(t,"read",{read_at:e},"unread_count"),this.makeStatusUpdate(t,"read")}async markAllAsRead(){const{metadata:t,items:e,...s}=this.store.getState();if(this.defaultOptions.status==="unread")s.resetStore({...t,total_count:0,unread_count:0});else{s.setMetadata({...t,unread_count:0});const i={read_at:new Date().toISOString()},o=e.map(r=>r.id);s.setItemAttrs(o,i)}const n=await this.makeBulkStatusUpdate("read");return this.emitEvent("all_read",e),n}async markAsUnread(t){return this.optimisticallyPerformStatusUpdate(t,"unread",{read_at:null},"unread_count"),this.makeStatusUpdate(t,"unread")}async markAsInteracted(t,e){const s=new Date().toISOString();return this.optimisticallyPerformStatusUpdate(t,"interacted",{read_at:s,interacted_at:s},"unread_count"),this.makeStatusUpdate(t,"interacted",e)}async markAsArchived(t){const e=this.store.getState(),s=this.defaultOptions.archived==="exclude",a=Array.isArray(t)?t:[t],n=a.map(i=>i.id);if(s){const i=a.filter(u=>!u.seen_at).length,o=a.filter(u=>!u.read_at).length,r={...e.metadata,total_count:Math.max(0,e.metadata.total_count-a.length),unseen_count:Math.max(0,e.metadata.unseen_count-i),unread_count:Math.max(0,e.metadata.unread_count-o)},l=e.items.filter(u=>!n.includes(u.id));e.setResult({entries:l,meta:r,page_info:e.pageInfo})}else e.setItemAttrs(n,{archived_at:new Date().toISOString()});return this.makeStatusUpdate(t,"archived")}async markAllAsArchived(){const{items:t,...e}=this.store.getState();if(this.defaultOptions.archived==="exclude")e.resetStore();else{const n=t.map(i=>i.id);e.setItemAttrs(n,{archived_at:new Date().toISOString()})}const a=await this.makeBulkStatusUpdate("archive");return this.emitEvent("all_archived",t),a}async markAllReadAsArchived(){const{items:t,...e}=this.store.getState(),a=t.filter(o=>o.read_at===null).map(o=>o.id);if(e.setItemAttrs(a,{archived_at:new Date().toISOString()}),this.defaultOptions.archived==="exclude"){const o=t.filter(l=>!a.includes(l.id)),r={...e.metadata,total_count:o.length,unread_count:0};e.setResult({entries:o,meta:r,page_info:e.pageInfo})}return await this.makeBulkStatusUpdate("archive")}async markAsUnarchived(t){return this.optimisticallyPerformStatusUpdate(t,"unarchived",{archived_at:null}),this.makeStatusUpdate(t,"unarchived")}async fetch(t={}){const{networkStatus:e,...s}=this.store.getState();if(!this.knock.isAuthenticated()){this.knock.log("[Feed] User is not authenticated, skipping fetch");return}if(m.isRequestInFlight(e)){this.knock.log("[Feed] Request is in flight, skipping fetch");return}s.setNetworkStatus(t.__loadingType??m.NetworkStatus.loading);const a={...this.defaultOptions,...t,__loadingType:void 0,__fetchSource:void 0,__experimentalCrossBrowserUpdates:void 0,auto_manage_socket_connection:void 0,auto_manage_socket_connection_delay:void 0},n=await this.knock.client().makeRequest({method:"GET",url:`/v1/users/${this.knock.userId}/feeds/${this.feedId}`,params:a});if(n.statusCode==="error"||!n.body)return s.setNetworkStatus(m.NetworkStatus.error),{status:n.statusCode,data:n.error||n.body};const i={entries:n.body.entries,meta:n.body.meta,page_info:n.body.page_info};if(t.before){const l={shouldSetPage:!1,shouldAppend:!0};s.setResult(i,l)}else if(t.after){const l={shouldSetPage:!0,shouldAppend:!0};s.setResult(i,l)}else s.setResult(i);this.broadcast("messages.new",i);const o=t.__fetchSource==="socket"?"items.received.realtime":"items.received.page",r={items:i.entries,metadata:i.meta,event:o};return this.broadcast(r.event,r),{data:i,status:n.statusCode}}async fetchNextPage(){const{pageInfo:t}=this.store.getState();t.after&&this.fetch({after:t.after,__loadingType:m.NetworkStatus.fetchMore})}broadcast(t,e){this.broadcaster.emit(t,e)}async onNewMessageReceived({metadata:t}){this.knock.log("[Feed] Received new real-time message");const{items:e,...s}=this.store.getState(),a=e[0];s.setMetadata(t),this.fetch({before:a==null?void 0:a.__cursor,__fetchSource:"socket"})}buildUserFeedId(){return`${this.feedId}:${this.knock.userId}`}optimisticallyPerformStatusUpdate(t,e,s,a){const n=this.store.getState(),i=Array.isArray(t)?t:[t],o=i.map(r=>r.id);if(a){const{metadata:r}=n,l=i.filter(h=>{switch(e){case"seen":return h.seen_at===null;case"unseen":return h.seen_at!==null;case"read":case"interacted":return h.read_at===null;case"unread":return h.read_at!==null;default:return!0}}),u=e.startsWith("un")?l.length:-l.length;n.setMetadata({...r,[a]:Math.max(0,r[a]+u)})}n.setItemAttrs(o,s)}async makeStatusUpdate(t,e,s){const a=Array.isArray(t)?t:[t],n=a.map(o=>o.id),i=await this.knock.messages.batchUpdateStatuses(n,e,{metadata:s});return this.emitEvent(e,a),i}async makeBulkStatusUpdate(t){const e={user_ids:[this.knock.userId],engagement_status:this.defaultOptions.status!=="all"?this.defaultOptions.status:void 0,archived:this.defaultOptions.archived,has_tenant:this.defaultOptions.has_tenant,tenants:this.defaultOptions.tenant?[this.defaultOptions.tenant]:void 0};return await this.knock.messages.bulkUpdateAllStatusesInChannel({channelId:this.feedId,status:t,options:e})}setupBroadcastChannel(){this.broadcastChannel=typeof self<"u"&&"BroadcastChannel"in self?new BroadcastChannel(`knock:feed:${this.userFeedId}`):null,this.broadcastChannel&&this.defaultOptions.__experimentalCrossBrowserUpdates===!0&&(this.broadcastChannel.onmessage=t=>{switch(t.data.type){case"items:archived":case"items:unarchived":case"items:seen":case"items:unseen":case"items:read":case"items:unread":case"items:all_read":case"items:all_seen":case"items:all_archived":return this.fetch();default:return null}})}broadcastOverChannel(t,e){if(this.broadcastChannel)try{const s=JSON.parse(JSON.stringify(e));this.broadcastChannel.postMessage({type:t,payload:s})}catch(s){console.warn(`Could not broadcast ${t}, got error: ${s}`)}}initializeRealtimeConnection(){const{socket:t}=this.knock.client();t&&(this.channel=t.channel(`feeds:${this.userFeedId}`,this.defaultOptions),this.channel.on("new-message",e=>this.onNewMessageReceived(e)),this.defaultOptions.auto_manage_socket_connection&&this.setupAutoSocketManager(),this.hasSubscribedToRealTimeUpdates&&this.knock.isAuthenticated()&&(t.isConnected()||t.connect(),this.channel.join()))}setupAutoSocketManager(){typeof document>"u"||this.visibilityChangeListenerConnected||(this.visibilityChangeHandler=this.handleVisibilityChange.bind(this),this.visibilityChangeListenerConnected=!0,document.addEventListener("visibilitychange",this.visibilityChangeHandler))}teardownAutoSocketManager(){typeof document>"u"||(document.removeEventListener("visibilitychange",this.visibilityChangeHandler),this.visibilityChangeListenerConnected=!1)}emitEvent(t,e){this.broadcaster.emit(`items.${t}`,{items:e}),this.broadcaster.emit(`items:${t}`,{items:e}),this.broadcastOverChannel(`items:${t}`,{items:e})}handleVisibilityChange(){var s;const t=this.defaultOptions.auto_manage_socket_connection_delay??y,e=this.knock.client();document.visibilityState==="hidden"?this.disconnectTimer=setTimeout(()=>{var a;(a=e.socket)==null||a.disconnect(),this.disconnectTimer=null},t):document.visibilityState==="visible"&&(this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),(s=e.socket)!=null&&s.isConnected()||this.initializeRealtimeConnection())}}exports.default=b;
|
|
1
|
+
"use strict";var f=Object.defineProperty;var k=(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)=>k(u,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=require("eventemitter2"),m=require("../../networkStatus.js"),g=require("./store.js"),_={archived:"exclude"},S=2e3;class v{constructor(e,t,s){c(this,"userFeedId");c(this,"channel");c(this,"broadcaster");c(this,"defaultOptions");c(this,"broadcastChannel");c(this,"disconnectTimer",null);c(this,"hasSubscribedToRealTimeUpdates",!1);c(this,"visibilityChangeHandler",()=>{});c(this,"visibilityChangeListenerConnected",!1);c(this,"store");this.knock=e,this.feedId=t,this.feedId=t,this.userFeedId=this.buildUserFeedId(),this.store=g.default(),this.broadcaster=new p({wildcard:!0,delimiter:"."}),this.defaultOptions={..._,...s},this.knock.log(`[Feed] Initialized a feed on channel ${t}`),this.initializeRealtimeConnection(),this.setupBroadcastChannel()}reinitialize(){this.userFeedId=this.buildUserFeedId(),this.initializeRealtimeConnection(),this.setupBroadcastChannel()}teardown(){this.knock.log("[Feed] Tearing down feed instance"),this.channel&&(this.channel.leave(),this.channel.off("new-message")),this.teardownAutoSocketManager(),this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),this.broadcastChannel&&this.broadcastChannel.close()}dispose(){this.knock.log("[Feed] Disposing of feed instance"),this.teardown(),this.broadcaster.removeAllListeners(),this.knock.feeds.removeInstance(this)}listenForUpdates(){if(this.knock.log("[Feed] Connecting to real-time service"),this.hasSubscribedToRealTimeUpdates=!0,!this.knock.isAuthenticated()){this.knock.log("[Feed] User is not authenticated, skipping listening for updates");return}const e=this.knock.client().socket;e&&!e.isConnected()&&e.connect(),this.channel&&["closed","errored"].includes(this.channel.state)&&this.channel.join()}on(e,t){this.broadcaster.on(e,t)}off(e,t){this.broadcaster.off(e,t)}getState(){return this.store.getState()}async markAsSeen(e){const t=new Date().toISOString();return this.optimisticallyPerformStatusUpdate(e,"seen",{seen_at:t},"unseen_count"),this.makeStatusUpdate(e,"seen")}async markAllAsSeen(){const{metadata:e,items:t,...s}=this.store.getState();if(this.defaultOptions.status==="unseen")s.resetStore({...e,total_count:0,unseen_count:0});else{s.setMetadata({...e,unseen_count:0});const i={seen_at:new Date().toISOString()},o=t.map(r=>r.id);s.setItemAttrs(o,i)}const n=await this.makeBulkStatusUpdate("seen");return this.emitEvent("all_seen",t),n}async markAsUnseen(e){return this.optimisticallyPerformStatusUpdate(e,"unseen",{seen_at:null},"unseen_count"),this.makeStatusUpdate(e,"unseen")}async markAsRead(e){const t=new Date().toISOString();return this.optimisticallyPerformStatusUpdate(e,"read",{read_at:t},"unread_count"),this.makeStatusUpdate(e,"read")}async markAllAsRead(){const{metadata:e,items:t,...s}=this.store.getState();if(this.defaultOptions.status==="unread")s.resetStore({...e,total_count:0,unread_count:0});else{s.setMetadata({...e,unread_count:0});const i={read_at:new Date().toISOString()},o=t.map(r=>r.id);s.setItemAttrs(o,i)}const n=await this.makeBulkStatusUpdate("read");return this.emitEvent("all_read",t),n}async markAsUnread(e){return this.optimisticallyPerformStatusUpdate(e,"unread",{read_at:null},"unread_count"),this.makeStatusUpdate(e,"unread")}async markAsInteracted(e,t){const s=new Date().toISOString();return this.optimisticallyPerformStatusUpdate(e,"interacted",{read_at:s,interacted_at:s},"unread_count"),this.makeStatusUpdate(e,"interacted",t)}async markAsArchived(e){const t=this.store.getState(),s=this.defaultOptions.archived==="exclude",a=Array.isArray(e)?e:[e],n=a.map(i=>i.id);if(s){const i=a.filter(l=>!l.seen_at).length,o=a.filter(l=>!l.read_at).length,r={...t.metadata,total_count:Math.max(0,t.metadata.total_count-a.length),unseen_count:Math.max(0,t.metadata.unseen_count-i),unread_count:Math.max(0,t.metadata.unread_count-o)},d=t.items.filter(l=>!n.includes(l.id));t.setResult({entries:d,meta:r,page_info:t.pageInfo})}else t.setItemAttrs(n,{archived_at:new Date().toISOString()});return this.makeStatusUpdate(e,"archived")}async markAllAsArchived(){const{items:e,...t}=this.store.getState();if(this.defaultOptions.archived==="exclude")t.resetStore();else{const n=e.map(i=>i.id);t.setItemAttrs(n,{archived_at:new Date().toISOString()})}const a=await this.makeBulkStatusUpdate("archive");return this.emitEvent("all_archived",e),a}async markAllReadAsArchived(){const{items:e,...t}=this.store.getState(),a=e.filter(o=>o.read_at===null).map(o=>o.id);if(t.setItemAttrs(a,{archived_at:new Date().toISOString()}),this.defaultOptions.archived==="exclude"){const o=e.filter(d=>!a.includes(d.id)),r={...t.metadata,total_count:o.length,unread_count:0};t.setResult({entries:o,meta:r,page_info:t.pageInfo})}return await this.makeBulkStatusUpdate("archive")}async markAsUnarchived(e){return this.optimisticallyPerformStatusUpdate(e,"unarchived",{archived_at:null}),this.makeStatusUpdate(e,"unarchived")}async fetch(e={}){const{networkStatus:t,...s}=this.store.getState();if(!this.knock.isAuthenticated()){this.knock.log("[Feed] User is not authenticated, skipping fetch");return}if(m.isRequestInFlight(t)){this.knock.log("[Feed] Request is in flight, skipping fetch");return}s.setNetworkStatus(e.__loadingType??m.NetworkStatus.loading);const a={...this.defaultOptions,...e,__loadingType:void 0,__fetchSource:void 0,__experimentalCrossBrowserUpdates:void 0,auto_manage_socket_connection:void 0,auto_manage_socket_connection_delay:void 0},n=await this.knock.client().makeRequest({method:"GET",url:`/v1/users/${this.knock.userId}/feeds/${this.feedId}`,params:a});if(n.statusCode==="error"||!n.body)return s.setNetworkStatus(m.NetworkStatus.error),{status:n.statusCode,data:n.error||n.body};const i={entries:n.body.entries,meta:n.body.meta,page_info:n.body.page_info};if(e.before){const d={shouldSetPage:!1,shouldAppend:!0};s.setResult(i,d)}else if(e.after){const d={shouldSetPage:!0,shouldAppend:!0};s.setResult(i,d)}else s.setResult(i);this.broadcast("messages.new",i);const o=e.__fetchSource==="socket"?"items.received.realtime":"items.received.page",r={items:i.entries,metadata:i.meta,event:o};return this.broadcast(r.event,r),{data:i,status:n.statusCode}}async fetchNextPage(){const{pageInfo:e}=this.store.getState();e.after&&this.fetch({after:e.after,__loadingType:m.NetworkStatus.fetchMore})}broadcast(e,t){this.broadcaster.emit(e,t)}async onNewMessageReceived({metadata:e}){this.knock.log("[Feed] Received new real-time message");const{items:t,...s}=this.store.getState(),a=t[0];s.setMetadata(e),this.fetch({before:a==null?void 0:a.__cursor,__fetchSource:"socket"})}buildUserFeedId(){return`${this.feedId}:${this.knock.userId}`}optimisticallyPerformStatusUpdate(e,t,s,a){const n=this.store.getState(),i=Array.isArray(e)?e:[e],o=i.map(r=>r.id);if(a){const{metadata:r}=n,d=i.filter(h=>{switch(t){case"seen":return h.seen_at===null;case"unseen":return h.seen_at!==null;case"read":case"interacted":return h.read_at===null;case"unread":return h.read_at!==null;default:return!0}}),l=t.startsWith("un")?d.length:-d.length;n.setMetadata({...r,[a]:Math.max(0,r[a]+l)})}n.setItemAttrs(o,s)}async makeStatusUpdate(e,t,s){const a=Array.isArray(e)?e:[e],n=a.map(o=>o.id),i=await this.knock.messages.batchUpdateStatuses(n,t,{metadata:s});return this.emitEvent(t,a),i}async makeBulkStatusUpdate(e){const t={user_ids:[this.knock.userId],engagement_status:this.defaultOptions.status!=="all"?this.defaultOptions.status:void 0,archived:this.defaultOptions.archived,has_tenant:this.defaultOptions.has_tenant,tenants:this.defaultOptions.tenant?[this.defaultOptions.tenant]:void 0};return await this.knock.messages.bulkUpdateAllStatusesInChannel({channelId:this.feedId,status:e,options:t})}setupBroadcastChannel(){this.broadcastChannel=typeof self<"u"&&"BroadcastChannel"in self?new BroadcastChannel(`knock:feed:${this.userFeedId}`):null,this.broadcastChannel&&this.defaultOptions.__experimentalCrossBrowserUpdates===!0&&(this.broadcastChannel.onmessage=e=>{switch(e.data.type){case"items:archived":case"items:unarchived":case"items:seen":case"items:unseen":case"items:read":case"items:unread":case"items:all_read":case"items:all_seen":case"items:all_archived":return this.fetch();default:return null}})}broadcastOverChannel(e,t){if(this.broadcastChannel)try{const s=JSON.parse(JSON.stringify(t));this.broadcastChannel.postMessage({type:e,payload:s})}catch(s){console.warn(`Could not broadcast ${e}, got error: ${s}`)}}initializeRealtimeConnection(){const{socket:e}=this.knock.client();e&&(this.channel=e.channel(`feeds:${this.userFeedId}`,this.defaultOptions),this.channel.on("new-message",t=>this.onNewMessageReceived(t)),this.defaultOptions.auto_manage_socket_connection&&this.setupAutoSocketManager(),this.hasSubscribedToRealTimeUpdates&&this.knock.isAuthenticated()&&(e.isConnected()||e.connect(),this.channel.join()))}setupAutoSocketManager(){typeof document>"u"||this.visibilityChangeListenerConnected||(this.visibilityChangeHandler=this.handleVisibilityChange.bind(this),this.visibilityChangeListenerConnected=!0,document.addEventListener("visibilitychange",this.visibilityChangeHandler))}teardownAutoSocketManager(){typeof document>"u"||(document.removeEventListener("visibilitychange",this.visibilityChangeHandler),this.visibilityChangeListenerConnected=!1)}emitEvent(e,t){this.broadcaster.emit(`items.${e}`,{items:t}),this.broadcaster.emit(`items:${e}`,{items:t}),this.broadcastOverChannel(`items:${e}`,{items:t})}handleVisibilityChange(){var s;const e=this.defaultOptions.auto_manage_socket_connection_delay??S,t=this.knock.client();document.visibilityState==="hidden"?this.disconnectTimer=setTimeout(()=>{var a;(a=t.socket)==null||a.disconnect(),this.disconnectTimer=null},e):document.visibilityState==="visible"&&(this.disconnectTimer&&(clearTimeout(this.disconnectTimer),this.disconnectTimer=null),(s=t.socket)!=null&&s.isConnected()||this.initializeRealtimeConnection())}}exports.default=v;
|
|
2
2
|
//# sourceMappingURL=feed.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feed.js","sources":["../../../../src/clients/feed/feed.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport EventEmitter from \"eventemitter2\";\nimport { Channel } from \"phoenix\";\nimport { StoreApi } from \"zustand\";\n\nimport Knock from \"../../knock\";\nimport { NetworkStatus, isRequestInFlight } from \"../../networkStatus\";\nimport {\n BulkUpdateMessagesInChannelProperties,\n MessageEngagementStatus,\n} from \"../messages/interfaces\";\n\nimport {\n FeedClientOptions,\n FeedItem,\n FeedMetadata,\n FeedResponse,\n FetchFeedOptions,\n} from \"./interfaces\";\nimport createStore from \"./store\";\nimport {\n BindableFeedEvent,\n FeedEvent,\n FeedEventCallback,\n FeedEventPayload,\n FeedItemOrItems,\n FeedMessagesReceivedPayload,\n FeedRealTimeCallback,\n FeedStoreState,\n} from \"./types\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\"> = {\n archived: \"exclude\",\n};\n\nconst DEFAULT_DISCONNECT_DELAY = 2000;\n\nclass Feed {\n private userFeedId: string;\n private channel?: Channel;\n private broadcaster: EventEmitter;\n private defaultOptions: FeedClientOptions;\n private broadcastChannel!: BroadcastChannel | null;\n private disconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private hasSubscribedToRealTimeUpdates: boolean = false;\n private visibilityChangeHandler: () => void = () => {};\n private visibilityChangeListenerConnected: boolean = false;\n\n // The raw store instance, used for binding in React and other environments\n public store: StoreApi<FeedStoreState>;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n ) {\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = { ...feedClientDefaults, ...options };\n\n this.knock.log(`[Feed] Initialized a feed on channel ${feedId}`);\n\n // Attempt to setup a realtime connection (does not join)\n this.initializeRealtimeConnection();\n\n this.setupBroadcastChannel();\n }\n\n /**\n * Used to reinitialize a current feed instance, which is useful when reauthenticating users\n */\n reinitialize() {\n // Reinitialize the user feed id incase the userId changed\n this.userFeedId = this.buildUserFeedId();\n\n // Reinitialize the real-time connection\n this.initializeRealtimeConnection();\n\n // Reinitialize our broadcast channel\n this.setupBroadcastChannel();\n }\n\n /**\n * Cleans up a feed instance by destroying the store and disconnecting\n * an open socket connection.\n */\n teardown() {\n this.knock.log(\"[Feed] Tearing down feed instance\");\n\n if (this.channel) {\n this.channel.leave();\n this.channel.off(\"new-message\");\n }\n\n this.teardownAutoSocketManager();\n\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n if (this.broadcastChannel) {\n this.broadcastChannel.close();\n }\n }\n\n /** Tears down an instance and removes it entirely from the feed manager */\n dispose() {\n this.knock.log(\"[Feed] Disposing of feed instance\");\n this.teardown();\n this.broadcaster.removeAllListeners();\n this.knock.feeds.removeInstance(this);\n }\n\n /*\n Initializes a real-time connection to Knock, connecting the websocket for the\n current ApiClient instance if the socket is not already connected.\n */\n listenForUpdates() {\n this.knock.log(\"[Feed] Connecting to real-time service\");\n\n this.hasSubscribedToRealTimeUpdates = true;\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\n \"[Feed] User is not authenticated, skipping listening for updates\",\n );\n return;\n }\n\n const maybeSocket = this.knock.client().socket;\n\n // Connect the socket only if we don't already have a connection\n if (maybeSocket && !maybeSocket.isConnected()) {\n maybeSocket.connect();\n }\n\n // Only join the channel if we're not already in a joining state\n if (this.channel && [\"closed\", \"errored\"].includes(this.channel.state)) {\n this.channel.join();\n }\n }\n\n /* Binds a handler to be invoked when event occurs */\n on(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.on(eventName, callback);\n }\n\n off(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.off(eventName, callback);\n }\n\n getState() {\n return this.store.getState();\n }\n\n async markAsSeen(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"seen\",\n { seen_at: now },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAllAsSeen() {\n // To mark all of the messages as seen we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unseen_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnseen = this.defaultOptions.status === \"unseen\";\n\n // If we're looking at the unseen view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnseen) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unseen_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unseen_count: 0 });\n\n const attrs = { seen_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"seen\");\n this.emitEvent(\"all_seen\", items);\n\n return result;\n }\n\n async markAsUnseen(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unseen\",\n { seen_at: null },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"read\",\n { read_at: now },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAllAsRead() {\n // To mark all of the messages as read we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unread_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unread_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnread = this.defaultOptions.status === \"unread\";\n\n // If we're looking at the unread view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnread) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unread_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unread_count: 0 });\n\n const attrs = { read_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"read\");\n this.emitEvent(\"all_read\", items);\n\n return result;\n }\n\n async markAsUnread(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unread\",\n { read_at: null },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n async markAsInteracted(\n itemOrItems: FeedItemOrItems,\n metadata?: Record<string, string>,\n ) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"interacted\",\n {\n read_at: now,\n interacted_at: now,\n },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"interacted\", metadata);\n }\n\n /*\n Marking one or more items as archived should:\n\n - Decrement the badge count for any unread / unseen items\n - Remove the item from the feed list when the `archived` flag is \"exclude\" (default)\n\n TODO: how do we handle rollbacks?\n */\n async markAsArchived(itemOrItems: FeedItemOrItems) {\n const state = this.store.getState();\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n /*\n In the code here we want to optimistically update counts and items\n that are persisted such that we can display updates immediately on the feed\n without needing to make a network request.\n\n Note: right now this does *not* take into account offline handling or any extensive retry\n logic, so rollbacks aren't considered. That probably needs to be a future consideration for\n this library.\n\n Scenarios to consider:\n\n ## Feed scope to archived *only*\n\n - Counts should not be decremented\n - Items should not be removed\n\n ## Feed scoped to exclude archived items (the default)\n\n - Counts should be decremented\n - Items should be removed\n\n ## Feed scoped to include archived items as well\n\n - Counts should not be decremented\n - Items should not be removed\n */\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = items.filter((i) => !i.seen_at).length;\n const unreadCount = items.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n // Ensure that the counts don't ever go below 0 on archiving where the client state\n // gets out of sync with the server state\n total_count: Math.max(0, state.metadata.total_count - items.length),\n unseen_count: Math.max(0, state.metadata.unseen_count - unseenCount),\n unread_count: Math.max(0, state.metadata.unread_count - unreadCount),\n };\n\n // Remove the archiving entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAllAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Reset the store to clear out all of items and reset the badge count\n state.resetStore();\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n const itemIds = items.map((i) => i.id);\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n this.emitEvent(\"all_archived\", items);\n\n return result;\n }\n\n async markAllReadAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n // Filter items to only include those that are unread\n const unreadItems = items.filter((item) => item.read_at === null);\n // Mark all the unread items as archived and read\n const itemIds = unreadItems.map((i) => i.id);\n state.setItemAttrs(itemIds, {\n archived_at: new Date().toISOString(),\n });\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Remove all the read items from the store and reset the badge count\n const remainingItems = items.filter((item) => !itemIds.includes(item.id));\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n total_count: remainingItems.length,\n unread_count: 0,\n };\n\n state.setResult({\n entries: remainingItems,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n // this.emitEvent(\"all_archived\", readItems);\n\n return result;\n }\n\n async markAsUnarchived(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n /* Fetches the feed content, appending it to the store */\n async fetch(options: FetchFeedOptions = {}) {\n const { networkStatus, ...state } = this.store.getState();\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\"[Feed] User is not authenticated, skipping fetch\");\n return;\n }\n\n // If there's an existing request in flight, then do nothing\n if (isRequestInFlight(networkStatus)) {\n this.knock.log(\"[Feed] Request is in flight, skipping fetch\");\n return;\n }\n\n // Set the loading type based on the request type it is\n state.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading);\n\n // Always include the default params, if they have been set\n const queryParams = {\n ...this.defaultOptions,\n ...options,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: undefined,\n auto_manage_socket_connection: undefined,\n auto_manage_socket_connection_delay: undefined,\n };\n\n const result = await this.knock.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,\n params: queryParams,\n });\n\n if (result.statusCode === \"error\" || !result.body) {\n state.setNetworkStatus(NetworkStatus.error);\n\n return {\n status: result.statusCode,\n data: result.error || result.body,\n };\n }\n\n const response = {\n entries: result.body.entries,\n meta: result.body.meta,\n page_info: result.body.page_info,\n };\n\n if (options.before) {\n const opts = { shouldSetPage: false, shouldAppend: true };\n state.setResult(response, opts);\n } else if (options.after) {\n const opts = { shouldSetPage: true, shouldAppend: true };\n state.setResult(response, opts);\n } else {\n state.setResult(response);\n }\n\n // Legacy `messages.new` event, should be removed in a future version\n this.broadcast(\"messages.new\", response);\n\n // Broadcast the appropriate event type depending on the fetch source\n const feedEventType: FeedEvent =\n options.__fetchSource === \"socket\"\n ? \"items.received.realtime\"\n : \"items.received.page\";\n\n const eventPayload = {\n items: response.entries as FeedItem[],\n metadata: response.meta as FeedMetadata,\n event: feedEventType,\n };\n\n this.broadcast(eventPayload.event, eventPayload);\n\n return { data: response, status: result.statusCode };\n }\n\n async fetchNextPage() {\n // Attempts to fetch the next page of results (if we have any)\n const { pageInfo } = this.store.getState();\n\n if (!pageInfo.after) {\n // Nothing more to fetch\n return;\n }\n\n this.fetch({\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n private broadcast(\n eventName: FeedEvent,\n data: FeedResponse | FeedEventPayload,\n ) {\n this.broadcaster.emit(eventName, data);\n }\n\n // Invoked when a new real-time message comes in from the socket\n private async onNewMessageReceived({\n metadata,\n }: FeedMessagesReceivedPayload) {\n this.knock.log(\"[Feed] Received new real-time message\");\n\n // Handle the new message coming in\n const { items, ...state } = this.store.getState();\n const currentHead: FeedItem | undefined = items[0];\n // Optimistically set the badge counts\n state.setMetadata(metadata);\n // Fetch the items before the current head (if it exists)\n this.fetch({ before: currentHead?.__cursor, __fetchSource: \"socket\" });\n }\n\n private buildUserFeedId() {\n return `${this.feedId}:${this.knock.userId}`;\n }\n\n private optimisticallyPerformStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n attrs: object,\n badgeCountAttr?: \"unread_count\" | \"unseen_count\",\n ) {\n const state = this.store.getState();\n const normalizedItems = Array.isArray(itemOrItems)\n ? itemOrItems\n : [itemOrItems];\n const itemIds = normalizedItems.map((item) => item.id);\n\n if (badgeCountAttr) {\n const { metadata } = state;\n\n // We only want to update the counts of items that have not already been counted towards the\n // badge count total to avoid updating the badge count unnecessarily.\n const itemsToUpdate = normalizedItems.filter((item) => {\n switch (type) {\n case \"seen\":\n return item.seen_at === null;\n case \"unseen\":\n return item.seen_at !== null;\n case \"read\":\n case \"interacted\":\n return item.read_at === null;\n case \"unread\":\n return item.read_at !== null;\n default:\n return true;\n }\n });\n\n // Tnis is a hack to determine the direction of whether we're\n // adding or removing from the badge count\n const direction = type.startsWith(\"un\")\n ? itemsToUpdate.length\n : -itemsToUpdate.length;\n\n state.setMetadata({\n ...metadata,\n [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),\n });\n }\n\n // Update the items with the given attributes\n state.setItemAttrs(itemIds, attrs);\n }\n\n private async makeStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n metadata?: Record<string, string>,\n ) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n const itemIds = items.map((item) => item.id);\n\n const result = await this.knock.messages.batchUpdateStatuses(\n itemIds,\n type,\n { metadata },\n );\n\n // Emit the event that these items had their statuses changed\n // Note: we do this after the update to ensure that the server event actually completed\n this.emitEvent(type, items);\n\n return result;\n }\n\n private async makeBulkStatusUpdate(\n status: BulkUpdateMessagesInChannelProperties[\"status\"],\n ) {\n // The base scope for the call should take into account all of the options currently\n // set on the feed, as well as being scoped for the current user. We do this so that\n // we ONLY make changes to the messages that are currently in view on this feed, and not\n // all messages that exist.\n const options = {\n user_ids: [this.knock.userId!],\n engagement_status:\n this.defaultOptions.status !== \"all\"\n ? this.defaultOptions.status\n : undefined,\n archived: this.defaultOptions.archived,\n has_tenant: this.defaultOptions.has_tenant,\n tenants: this.defaultOptions.tenant\n ? [this.defaultOptions.tenant]\n : undefined,\n };\n\n return await this.knock.messages.bulkUpdateAllStatusesInChannel({\n channelId: this.feedId,\n status,\n options,\n });\n }\n\n private setupBroadcastChannel() {\n // Attempt to bind to listen to other events from this feed in different tabs\n // Note: here we ensure `self` is available (it's not in server rendered envs)\n this.broadcastChannel =\n typeof self !== \"undefined\" && \"BroadcastChannel\" in self\n ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)\n : null;\n\n // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast\n // channel (iff it's enabled and exists)\n if (\n this.broadcastChannel &&\n this.defaultOptions.__experimentalCrossBrowserUpdates === true\n ) {\n this.broadcastChannel.onmessage = (e) => {\n switch (e.data.type) {\n case \"items:archived\":\n case \"items:unarchived\":\n case \"items:seen\":\n case \"items:unseen\":\n case \"items:read\":\n case \"items:unread\":\n case \"items:all_read\":\n case \"items:all_seen\":\n case \"items:all_archived\":\n // When items are updated in any other tab, simply refetch to get the latest state\n // to make sure that the state gets updated accordingly. In the future here we could\n // maybe do this optimistically without the fetch.\n return this.fetch();\n default:\n return null;\n }\n };\n }\n }\n\n private broadcastOverChannel(type: string, payload: GenericData) {\n // The broadcastChannel may not be available in non-browser environments\n if (!this.broadcastChannel) {\n return;\n }\n\n // Here we stringify our payload and try and send as JSON such that we\n // don't get any `An object could not be cloned` errors when trying to broadcast\n try {\n const stringifiedPayload = JSON.parse(JSON.stringify(payload));\n\n this.broadcastChannel.postMessage({\n type,\n payload: stringifiedPayload,\n });\n } catch (e) {\n console.warn(`Could not broadcast ${type}, got error: ${e}`);\n }\n }\n\n private initializeRealtimeConnection() {\n const { socket: maybeSocket } = this.knock.client();\n\n // In server environments we might not have a socket connection\n if (!maybeSocket) return;\n\n // Reinitialize channel connections incase the socket changed\n this.channel = maybeSocket.channel(\n `feeds:${this.userFeedId}`,\n this.defaultOptions,\n );\n\n this.channel.on(\"new-message\", (resp) => this.onNewMessageReceived(resp));\n\n if (this.defaultOptions.auto_manage_socket_connection) {\n this.setupAutoSocketManager();\n }\n\n // If we're initializing but they have previously opted to listen to real-time updates\n // then we will automatically reconnect on their behalf\n if (this.hasSubscribedToRealTimeUpdates && this.knock.isAuthenticated()) {\n if (!maybeSocket.isConnected()) maybeSocket.connect();\n this.channel.join();\n }\n }\n\n /**\n * Listen for changes to document visibility and automatically disconnect\n * or reconnect the socket after a delay\n */\n private setupAutoSocketManager() {\n if (\n typeof document === \"undefined\" ||\n this.visibilityChangeListenerConnected\n ) {\n return;\n }\n\n this.visibilityChangeHandler = this.handleVisibilityChange.bind(this);\n this.visibilityChangeListenerConnected = true;\n document.addEventListener(\"visibilitychange\", this.visibilityChangeHandler);\n }\n\n private teardownAutoSocketManager() {\n if (typeof document === \"undefined\") return;\n\n document.removeEventListener(\n \"visibilitychange\",\n this.visibilityChangeHandler,\n );\n this.visibilityChangeListenerConnected = false;\n }\n\n private emitEvent(\n type:\n | MessageEngagementStatus\n | \"all_read\"\n | \"all_seen\"\n | \"all_archived\"\n | \"unread\"\n | \"unseen\"\n | \"unarchived\",\n items: FeedItem[],\n ) {\n // Handle both `items.` and `items:` format for events for compatibility reasons\n this.broadcaster.emit(`items.${type}`, { items });\n this.broadcaster.emit(`items:${type}`, { items });\n // Internal events only need `items:`\n this.broadcastOverChannel(`items:${type}`, { items });\n }\n\n private handleVisibilityChange() {\n const disconnectDelay =\n this.defaultOptions.auto_manage_socket_connection_delay ??\n DEFAULT_DISCONNECT_DELAY;\n\n const client = this.knock.client();\n\n if (document.visibilityState === \"hidden\") {\n // When the tab is hidden, clean up the socket connection after a delay\n this.disconnectTimer = setTimeout(() => {\n client.socket?.disconnect();\n this.disconnectTimer = null;\n }, disconnectDelay);\n } else if (document.visibilityState === \"visible\") {\n // When the tab is visible, clear the disconnect timer if active to cancel disconnecting\n // This handles cases where the tab is only briefly hidden to avoid unnecessary disconnects\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n // If the socket is not connected, try to reconnect\n if (!client.socket?.isConnected()) {\n this.initializeRealtimeConnection();\n }\n }\n }\n}\n\nexport default Feed;\n"],"names":["feedClientDefaults","DEFAULT_DISCONNECT_DELAY","Feed","knock","feedId","options","__publicField","createStore","EventEmitter","maybeSocket","eventName","callback","itemOrItems","now","metadata","items","state","attrs","itemIds","item","result","shouldOptimisticallyRemoveItems","unseenCount","i","unreadCount","updatedMetadata","entriesToSet","remainingItems","networkStatus","isRequestInFlight","NetworkStatus","queryParams","response","opts","feedEventType","eventPayload","pageInfo","data","currentHead","type","badgeCountAttr","normalizedItems","itemsToUpdate","direction","status","e","payload","stringifiedPayload","resp","disconnectDelay","client","_a"],"mappings":"4aAgCMA,EAA0D,CAC9D,SAAU,SACZ,EAEMC,EAA2B,IAEjC,MAAMC,CAAK,CAcT,YACWC,EACAC,EACTC,EACA,CAjBMC,EAAA,mBACAA,EAAA,gBACAA,EAAA,oBACAA,EAAA,uBACAA,EAAA,yBACAA,EAAA,uBAAwD,MACxDA,EAAA,sCAA0C,IAC1CA,EAAA,+BAAsC,IAAM,CAAA,GAC5CA,EAAA,yCAA6C,IAG9CA,EAAA,cAGI,KAAA,MAAAH,EACA,KAAA,OAAAC,EAGT,KAAK,OAASA,EACT,KAAA,WAAa,KAAK,kBACvB,KAAK,MAAQG,EAAAA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pBE,EAAAD,EAAO,SAAP,MAAAC,EAAe,eAClB,KAAK,6BAA6B,EAGxC,CACF"}
|
|
1
|
+
{"version":3,"file":"feed.js","sources":["../../../../src/clients/feed/feed.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport EventEmitter from \"eventemitter2\";\nimport { Channel } from \"phoenix\";\nimport { StoreApi } from \"zustand\";\n\nimport Knock from \"../../knock\";\nimport { NetworkStatus, isRequestInFlight } from \"../../networkStatus\";\nimport {\n BulkUpdateMessagesInChannelProperties,\n MessageEngagementStatus,\n} from \"../messages/interfaces\";\n\nimport {\n FeedClientOptions,\n FeedItem,\n FeedMetadata,\n FeedResponse,\n FetchFeedOptions,\n} from \"./interfaces\";\nimport createStore from \"./store\";\nimport {\n BindableFeedEvent,\n FeedEvent,\n FeedEventCallback,\n FeedEventPayload,\n FeedItemOrItems,\n FeedMessagesReceivedPayload,\n FeedRealTimeCallback,\n FeedStoreState,\n} from \"./types\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\"> = {\n archived: \"exclude\",\n};\n\nconst DEFAULT_DISCONNECT_DELAY = 2000;\n\nclass Feed {\n private userFeedId: string;\n private channel?: Channel;\n private broadcaster: EventEmitter;\n private defaultOptions: FeedClientOptions;\n private broadcastChannel!: BroadcastChannel | null;\n private disconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private hasSubscribedToRealTimeUpdates: boolean = false;\n private visibilityChangeHandler: () => void = () => {};\n private visibilityChangeListenerConnected: boolean = false;\n\n // The raw store instance, used for binding in React and other environments\n public store: StoreApi<FeedStoreState>;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n ) {\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = { ...feedClientDefaults, ...options };\n\n this.knock.log(`[Feed] Initialized a feed on channel ${feedId}`);\n\n // Attempt to setup a realtime connection (does not join)\n this.initializeRealtimeConnection();\n\n this.setupBroadcastChannel();\n }\n\n /**\n * Used to reinitialize a current feed instance, which is useful when reauthenticating users\n */\n reinitialize() {\n // Reinitialize the user feed id incase the userId changed\n this.userFeedId = this.buildUserFeedId();\n\n // Reinitialize the real-time connection\n this.initializeRealtimeConnection();\n\n // Reinitialize our broadcast channel\n this.setupBroadcastChannel();\n }\n\n /**\n * Cleans up a feed instance by destroying the store and disconnecting\n * an open socket connection.\n */\n teardown() {\n this.knock.log(\"[Feed] Tearing down feed instance\");\n\n if (this.channel) {\n this.channel.leave();\n this.channel.off(\"new-message\");\n }\n\n this.teardownAutoSocketManager();\n\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n if (this.broadcastChannel) {\n this.broadcastChannel.close();\n }\n }\n\n /** Tears down an instance and removes it entirely from the feed manager */\n dispose() {\n this.knock.log(\"[Feed] Disposing of feed instance\");\n this.teardown();\n this.broadcaster.removeAllListeners();\n this.knock.feeds.removeInstance(this);\n }\n\n /*\n Initializes a real-time connection to Knock, connecting the websocket for the\n current ApiClient instance if the socket is not already connected.\n */\n listenForUpdates() {\n this.knock.log(\"[Feed] Connecting to real-time service\");\n\n this.hasSubscribedToRealTimeUpdates = true;\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\n \"[Feed] User is not authenticated, skipping listening for updates\",\n );\n return;\n }\n\n const maybeSocket = this.knock.client().socket;\n\n // Connect the socket only if we don't already have a connection\n if (maybeSocket && !maybeSocket.isConnected()) {\n maybeSocket.connect();\n }\n\n // Only join the channel if we're not already in a joining state\n if (this.channel && [\"closed\", \"errored\"].includes(this.channel.state)) {\n this.channel.join();\n }\n }\n\n /* Binds a handler to be invoked when event occurs */\n on(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.on(eventName, callback);\n }\n\n off(\n eventName: BindableFeedEvent,\n callback: FeedEventCallback | FeedRealTimeCallback,\n ) {\n this.broadcaster.off(eventName, callback);\n }\n\n getState() {\n return this.store.getState();\n }\n\n async markAsSeen(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"seen\",\n { seen_at: now },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAllAsSeen() {\n // To mark all of the messages as seen we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unseen_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnseen = this.defaultOptions.status === \"unseen\";\n\n // If we're looking at the unseen view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnseen) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unseen_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unseen_count: 0 });\n\n const attrs = { seen_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"seen\");\n this.emitEvent(\"all_seen\", items);\n\n return result;\n }\n\n async markAsUnseen(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unseen\",\n { seen_at: null },\n \"unseen_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: FeedItemOrItems) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"read\",\n { read_at: now },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAllAsRead() {\n // To mark all of the messages as read we:\n // 1. Optimistically update *everything* we have in the store\n // 2. We decrement the `unread_count` to zero optimistically\n // 3. We issue the API call to the endpoint\n //\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unread_count` to be what it was.\n //\n // Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`\n // items by removing everything from view.\n const { metadata, items, ...state } = this.store.getState();\n\n const isViewingOnlyUnread = this.defaultOptions.status === \"unread\";\n\n // If we're looking at the unread view, then we want to remove all of the items optimistically\n // from the store given that nothing should be visible. We do this by resetting the store state\n // and setting the current metadata counts to 0\n if (isViewingOnlyUnread) {\n state.resetStore({\n ...metadata,\n total_count: 0,\n unread_count: 0,\n });\n } else {\n // Otherwise we want to update the metadata and mark all of the items in the store as seen\n state.setMetadata({ ...metadata, unread_count: 0 });\n\n const attrs = { read_at: new Date().toISOString() };\n const itemIds = items.map((item) => item.id);\n\n state.setItemAttrs(itemIds, attrs);\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"read\");\n this.emitEvent(\"all_read\", items);\n\n return result;\n }\n\n async markAsUnread(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"unread\",\n { read_at: null },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n async markAsInteracted(\n itemOrItems: FeedItemOrItems,\n metadata?: Record<string, string>,\n ) {\n const now = new Date().toISOString();\n this.optimisticallyPerformStatusUpdate(\n itemOrItems,\n \"interacted\",\n {\n read_at: now,\n interacted_at: now,\n },\n \"unread_count\",\n );\n\n return this.makeStatusUpdate(itemOrItems, \"interacted\", metadata);\n }\n\n /*\n Marking one or more items as archived should:\n\n - Decrement the badge count for any unread / unseen items\n - Remove the item from the feed list when the `archived` flag is \"exclude\" (default)\n\n TODO: how do we handle rollbacks?\n */\n async markAsArchived(itemOrItems: FeedItemOrItems) {\n const state = this.store.getState();\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n /*\n In the code here we want to optimistically update counts and items\n that are persisted such that we can display updates immediately on the feed\n without needing to make a network request.\n\n Note: right now this does *not* take into account offline handling or any extensive retry\n logic, so rollbacks aren't considered. That probably needs to be a future consideration for\n this library.\n\n Scenarios to consider:\n\n ## Feed scope to archived *only*\n\n - Counts should not be decremented\n - Items should not be removed\n\n ## Feed scoped to exclude archived items (the default)\n\n - Counts should be decremented\n - Items should be removed\n\n ## Feed scoped to include archived items as well\n\n - Counts should not be decremented\n - Items should not be removed\n */\n\n if (shouldOptimisticallyRemoveItems) {\n // If any of the items are unseen or unread, then capture as we'll want to decrement\n // the counts for these in the metadata we have\n const unseenCount = items.filter((i) => !i.seen_at).length;\n const unreadCount = items.filter((i) => !i.read_at).length;\n\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n // Ensure that the counts don't ever go below 0 on archiving where the client state\n // gets out of sync with the server state\n total_count: Math.max(0, state.metadata.total_count - items.length),\n unseen_count: Math.max(0, state.metadata.unseen_count - unseenCount),\n unread_count: Math.max(0, state.metadata.unread_count - unreadCount),\n };\n\n // Remove the archiving entries\n const entriesToSet = state.items.filter(\n (item) => !itemIds.includes(item.id),\n );\n\n state.setResult({\n entries: entriesToSet,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAllAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Reset the store to clear out all of items and reset the badge count\n state.resetStore();\n } else {\n // Mark all the entries being updated as archived either way so the state is correct\n const itemIds = items.map((i) => i.id);\n state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n this.emitEvent(\"all_archived\", items);\n\n return result;\n }\n\n async markAllReadAsArchived() {\n // Note: there is the potential for a race condition here because the bulk\n // update is an async method, so if a new message comes in during this window before\n // the update has been processed we'll effectively reset the `unseen_count` to be what it was.\n const { items, ...state } = this.store.getState();\n // Filter items to only include those that are unread\n const unreadItems = items.filter((item) => item.read_at === null);\n // Mark all the unread items as archived and read\n const itemIds = unreadItems.map((i) => i.id);\n state.setItemAttrs(itemIds, {\n archived_at: new Date().toISOString(),\n });\n\n // Here if we're looking at a feed that excludes all of the archived items by default then we\n // will want to optimistically remove all of the items from the feed as they are now all excluded\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"exclude\";\n\n if (shouldOptimisticallyRemoveItems) {\n // Remove all the read items from the store and reset the badge count\n const remainingItems = items.filter((item) => !itemIds.includes(item.id));\n // Build the new metadata\n const updatedMetadata = {\n ...state.metadata,\n total_count: remainingItems.length,\n unread_count: 0,\n };\n\n state.setResult({\n entries: remainingItems,\n meta: updatedMetadata,\n page_info: state.pageInfo,\n });\n }\n\n // Issue the API request to the bulk status change API\n const result = await this.makeBulkStatusUpdate(\"archive\");\n // this.emitEvent(\"all_archived\", readItems);\n\n return result;\n }\n\n async markAsUnarchived(itemOrItems: FeedItemOrItems) {\n this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n /* Fetches the feed content, appending it to the store */\n async fetch(options: FetchFeedOptions = {}) {\n const { networkStatus, ...state } = this.store.getState();\n\n // If the user is not authenticated, then do nothing\n if (!this.knock.isAuthenticated()) {\n this.knock.log(\"[Feed] User is not authenticated, skipping fetch\");\n return;\n }\n\n // If there's an existing request in flight, then do nothing\n if (isRequestInFlight(networkStatus)) {\n this.knock.log(\"[Feed] Request is in flight, skipping fetch\");\n return;\n }\n\n // Set the loading type based on the request type it is\n state.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading);\n\n // Always include the default params, if they have been set\n const queryParams = {\n ...this.defaultOptions,\n ...options,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: undefined,\n auto_manage_socket_connection: undefined,\n auto_manage_socket_connection_delay: undefined,\n };\n\n const result = await this.knock.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,\n params: queryParams,\n });\n\n if (result.statusCode === \"error\" || !result.body) {\n state.setNetworkStatus(NetworkStatus.error);\n\n return {\n status: result.statusCode,\n data: result.error || result.body,\n };\n }\n\n const response = {\n entries: result.body.entries,\n meta: result.body.meta,\n page_info: result.body.page_info,\n };\n\n if (options.before) {\n const opts = { shouldSetPage: false, shouldAppend: true };\n state.setResult(response, opts);\n } else if (options.after) {\n const opts = { shouldSetPage: true, shouldAppend: true };\n state.setResult(response, opts);\n } else {\n state.setResult(response);\n }\n\n // Legacy `messages.new` event, should be removed in a future version\n this.broadcast(\"messages.new\", response);\n\n // Broadcast the appropriate event type depending on the fetch source\n const feedEventType: FeedEvent =\n options.__fetchSource === \"socket\"\n ? \"items.received.realtime\"\n : \"items.received.page\";\n\n const eventPayload = {\n items: response.entries as FeedItem[],\n metadata: response.meta as FeedMetadata,\n event: feedEventType,\n };\n\n this.broadcast(eventPayload.event, eventPayload);\n\n return { data: response, status: result.statusCode };\n }\n\n async fetchNextPage() {\n // Attempts to fetch the next page of results (if we have any)\n const { pageInfo } = this.store.getState();\n\n if (!pageInfo.after) {\n // Nothing more to fetch\n return;\n }\n\n this.fetch({\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n private broadcast(\n eventName: FeedEvent,\n data: FeedResponse | FeedEventPayload,\n ) {\n this.broadcaster.emit(eventName, data);\n }\n\n // Invoked when a new real-time message comes in from the socket\n private async onNewMessageReceived({\n metadata,\n }: FeedMessagesReceivedPayload) {\n this.knock.log(\"[Feed] Received new real-time message\");\n\n // Handle the new message coming in\n const { items, ...state } = this.store.getState();\n const currentHead: FeedItem | undefined = items[0];\n // Optimistically set the badge counts\n state.setMetadata(metadata);\n // Fetch the items before the current head (if it exists)\n this.fetch({ before: currentHead?.__cursor, __fetchSource: \"socket\" });\n }\n\n private buildUserFeedId() {\n return `${this.feedId}:${this.knock.userId}`;\n }\n\n private optimisticallyPerformStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n attrs: object,\n badgeCountAttr?: \"unread_count\" | \"unseen_count\",\n ) {\n const state = this.store.getState();\n const normalizedItems = Array.isArray(itemOrItems)\n ? itemOrItems\n : [itemOrItems];\n const itemIds = normalizedItems.map((item) => item.id);\n\n if (badgeCountAttr) {\n const { metadata } = state;\n\n // We only want to update the counts of items that have not already been counted towards the\n // badge count total to avoid updating the badge count unnecessarily.\n const itemsToUpdate = normalizedItems.filter((item) => {\n switch (type) {\n case \"seen\":\n return item.seen_at === null;\n case \"unseen\":\n return item.seen_at !== null;\n case \"read\":\n case \"interacted\":\n return item.read_at === null;\n case \"unread\":\n return item.read_at !== null;\n default:\n return true;\n }\n });\n\n // Tnis is a hack to determine the direction of whether we're\n // adding or removing from the badge count\n const direction = type.startsWith(\"un\")\n ? itemsToUpdate.length\n : -itemsToUpdate.length;\n\n state.setMetadata({\n ...metadata,\n [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),\n });\n }\n\n // Update the items with the given attributes\n state.setItemAttrs(itemIds, attrs);\n }\n\n private async makeStatusUpdate(\n itemOrItems: FeedItemOrItems,\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n metadata?: Record<string, string>,\n ) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n const itemIds = items.map((item) => item.id);\n\n const result = await this.knock.messages.batchUpdateStatuses(\n itemIds,\n type,\n { metadata },\n );\n\n // Emit the event that these items had their statuses changed\n // Note: we do this after the update to ensure that the server event actually completed\n this.emitEvent(type, items);\n\n return result;\n }\n\n private async makeBulkStatusUpdate(\n status: BulkUpdateMessagesInChannelProperties[\"status\"],\n ) {\n // The base scope for the call should take into account all of the options currently\n // set on the feed, as well as being scoped for the current user. We do this so that\n // we ONLY make changes to the messages that are currently in view on this feed, and not\n // all messages that exist.\n const options = {\n user_ids: [this.knock.userId!],\n engagement_status:\n this.defaultOptions.status !== \"all\"\n ? this.defaultOptions.status\n : undefined,\n archived: this.defaultOptions.archived,\n has_tenant: this.defaultOptions.has_tenant,\n tenants: this.defaultOptions.tenant\n ? [this.defaultOptions.tenant]\n : undefined,\n };\n\n return await this.knock.messages.bulkUpdateAllStatusesInChannel({\n channelId: this.feedId,\n status,\n options,\n });\n }\n\n private setupBroadcastChannel() {\n // Attempt to bind to listen to other events from this feed in different tabs\n // Note: here we ensure `self` is available (it's not in server rendered envs)\n this.broadcastChannel =\n typeof self !== \"undefined\" && \"BroadcastChannel\" in self\n ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)\n : null;\n\n // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast\n // channel (iff it's enabled and exists)\n if (\n this.broadcastChannel &&\n this.defaultOptions.__experimentalCrossBrowserUpdates === true\n ) {\n this.broadcastChannel.onmessage = (e) => {\n switch (e.data.type) {\n case \"items:archived\":\n case \"items:unarchived\":\n case \"items:seen\":\n case \"items:unseen\":\n case \"items:read\":\n case \"items:unread\":\n case \"items:all_read\":\n case \"items:all_seen\":\n case \"items:all_archived\":\n // When items are updated in any other tab, simply refetch to get the latest state\n // to make sure that the state gets updated accordingly. In the future here we could\n // maybe do this optimistically without the fetch.\n return this.fetch();\n default:\n return null;\n }\n };\n }\n }\n\n private broadcastOverChannel(type: string, payload: GenericData) {\n // The broadcastChannel may not be available in non-browser environments\n if (!this.broadcastChannel) {\n return;\n }\n\n // Here we stringify our payload and try and send as JSON such that we\n // don't get any `An object could not be cloned` errors when trying to broadcast\n try {\n const stringifiedPayload = JSON.parse(JSON.stringify(payload));\n\n this.broadcastChannel.postMessage({\n type,\n payload: stringifiedPayload,\n });\n } catch (e) {\n console.warn(`Could not broadcast ${type}, got error: ${e}`);\n }\n }\n\n private initializeRealtimeConnection() {\n const { socket: maybeSocket } = this.knock.client();\n\n // In server environments we might not have a socket connection\n if (!maybeSocket) return;\n\n // Reinitialize channel connections incase the socket changed\n this.channel = maybeSocket.channel(\n `feeds:${this.userFeedId}`,\n this.defaultOptions,\n );\n\n this.channel.on(\"new-message\", (resp) => this.onNewMessageReceived(resp));\n\n if (this.defaultOptions.auto_manage_socket_connection) {\n this.setupAutoSocketManager();\n }\n\n // If we're initializing but they have previously opted to listen to real-time updates\n // then we will automatically reconnect on their behalf\n if (this.hasSubscribedToRealTimeUpdates && this.knock.isAuthenticated()) {\n if (!maybeSocket.isConnected()) maybeSocket.connect();\n this.channel.join();\n }\n }\n\n /**\n * Listen for changes to document visibility and automatically disconnect\n * or reconnect the socket after a delay\n */\n private setupAutoSocketManager() {\n if (\n typeof document === \"undefined\" ||\n this.visibilityChangeListenerConnected\n ) {\n return;\n }\n\n this.visibilityChangeHandler = this.handleVisibilityChange.bind(this);\n this.visibilityChangeListenerConnected = true;\n document.addEventListener(\"visibilitychange\", this.visibilityChangeHandler);\n }\n\n private teardownAutoSocketManager() {\n if (typeof document === \"undefined\") return;\n\n document.removeEventListener(\n \"visibilitychange\",\n this.visibilityChangeHandler,\n );\n this.visibilityChangeListenerConnected = false;\n }\n\n private emitEvent(\n type:\n | MessageEngagementStatus\n | \"all_read\"\n | \"all_seen\"\n | \"all_archived\"\n | \"unread\"\n | \"unseen\"\n | \"unarchived\",\n items: FeedItem[],\n ) {\n // Handle both `items.` and `items:` format for events for compatibility reasons\n this.broadcaster.emit(`items.${type}`, { items });\n this.broadcaster.emit(`items:${type}`, { items });\n // Internal events only need `items:`\n this.broadcastOverChannel(`items:${type}`, { items });\n }\n\n private handleVisibilityChange() {\n const disconnectDelay =\n this.defaultOptions.auto_manage_socket_connection_delay ??\n DEFAULT_DISCONNECT_DELAY;\n\n const client = this.knock.client();\n\n if (document.visibilityState === \"hidden\") {\n // When the tab is hidden, clean up the socket connection after a delay\n this.disconnectTimer = setTimeout(() => {\n client.socket?.disconnect();\n this.disconnectTimer = null;\n }, disconnectDelay);\n } else if (document.visibilityState === \"visible\") {\n // When the tab is visible, clear the disconnect timer if active to cancel disconnecting\n // This handles cases where the tab is only briefly hidden to avoid unnecessary disconnects\n if (this.disconnectTimer) {\n clearTimeout(this.disconnectTimer);\n this.disconnectTimer = null;\n }\n\n // If the socket is not connected, try to reconnect\n if (!client.socket?.isConnected()) {\n this.initializeRealtimeConnection();\n }\n }\n }\n}\n\nexport default Feed;\n"],"names":["feedClientDefaults","DEFAULT_DISCONNECT_DELAY","Feed","knock","feedId","options","__publicField","createStore","EventEmitter","maybeSocket","eventName","callback","itemOrItems","now","metadata","items","state","attrs","itemIds","item","result","shouldOptimisticallyRemoveItems","unseenCount","i","unreadCount","updatedMetadata","entriesToSet","remainingItems","networkStatus","isRequestInFlight","NetworkStatus","queryParams","response","opts","feedEventType","eventPayload","pageInfo","data","currentHead","type","badgeCountAttr","normalizedItems","itemsToUpdate","direction","status","payload","stringifiedPayload","e","resp","disconnectDelay","client","_a"],"mappings":"6WAgCMA,EAA0D,CAC9D,SAAU,SACZ,EAEMC,EAA2B,IAEjC,MAAMC,CAAK,CAcT,YACW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,CACtD,CAEQ,wBAAyB,OACzB,MAAAkC,EACJ,KAAK,eAAe,qCACpBhD,EAEIiD,EAAS,KAAK,MAAM,OAAO,EAE7B,SAAS,kBAAoB,SAE1B,KAAA,gBAAkB,WAAW,IAAM,QACtCC,EAAAD,EAAO,SAAP,MAAAC,EAAe,aACf,KAAK,gBAAkB,MACtBF,CAAe,EACT,SAAS,kBAAoB,YAGlC,KAAK,kBACP,aAAa,KAAK,eAAe,EACjC,KAAK,gBAAkB,OAIpBE,EAAAD,EAAO,SAAP,MAAAC,EAAe,eAClB,KAAK,6BAA6B,EAGxC,CACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("zustand/vanilla"),d=require("../../networkStatus.js"),i=require("./utils.js"),c=typeof u=="function"?u:u.default;function f(e){const t=i.deduplicateItems(e);return i.sortItems(t)}const p={shouldSetPage:!0,shouldAppend:!1},l={items:[],metadata:{total_count:0,unread_count:0,unseen_count:0},pageInfo:{before:null,after:null,page_size:50}};function S(){return c(e=>({...l,networkStatus:d.NetworkStatus.ready,loading:!1,setNetworkStatus:t=>e(()=>({networkStatus:t,loading:t===d.NetworkStatus.loading})),setResult:({entries:t,meta:r,page_info:n},a=p)=>e(s=>({items:a.shouldAppend?f(s.items.concat(t)):t,metadata:r,pageInfo:a.shouldSetPage?n:s.pageInfo,loading:!1,networkStatus:d.NetworkStatus.ready})),setMetadata:t=>e(()=>({metadata:t})),resetStore:(t=l.metadata)=>e(()=>({...l,metadata:t})),setItemAttrs:(t,r)=>{const n=t.reduce((a,s)=>({...a,[s]:r}),{});return e(a=>({items:a.items.map(o=>n[o.id]?{...o,...n[o.id]}:o)}))}}))}exports.default=S;
|
|
2
2
|
//# sourceMappingURL=store.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.js","sources":["../../../../src/clients/feed/store.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport zustand from \"zustand/vanilla\";\n\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { FeedItem } from \"./interfaces\";\nimport { FeedStoreState } from \"./types\";\nimport { deduplicateItems, sortItems } from \"./utils\";\n\n// Get the correct Zustand function. Caused by some issues in v3 exports\n// https://github.com/pmndrs/zustand/issues/334\nconst create: typeof zustand =\n // @ts-expect-error workaround for issue above\n typeof zustand === \"function\" ? zustand : zustand.default;\n\nfunction processItems(items: FeedItem[]) {\n const deduped = deduplicateItems(items);\n const sorted = sortItems(deduped);\n\n return sorted;\n}\n\nconst defaultSetResultOptions = {\n shouldSetPage: true,\n shouldAppend: false,\n};\n\nconst initialStoreState = {\n items: [],\n metadata: {\n total_count: 0,\n unread_count: 0,\n unseen_count: 0,\n },\n pageInfo: {\n before: null,\n after: null,\n page_size: 50,\n },\n};\n\nexport default function createStore() {\n return create<FeedStoreState>((set) => ({\n // Keeps track of all of the items loaded\n ...initialStoreState,\n // The network status indicates what's happening with the request\n networkStatus: NetworkStatus.ready,\n loading: false,\n\n setNetworkStatus: (networkStatus: NetworkStatus) =>\n set(() => ({\n networkStatus,\n loading: networkStatus === NetworkStatus.loading,\n })),\n\n setResult: (\n { entries, meta, page_info },\n options = defaultSetResultOptions,\n ) =>\n set((state) => {\n // We resort the list on set, so concating everything is fine (if a bit suboptimal)\n const items = options.shouldAppend\n ? processItems(state.items.concat(entries as FeedItem<GenericData>[]))\n : entries;\n\n return {\n items,\n metadata: meta,\n pageInfo: options.shouldSetPage ? page_info : state.pageInfo,\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n }),\n\n setMetadata: (metadata) => set(() => ({ metadata })),\n\n resetStore: (metadata = initialStoreState.metadata) =>\n set(() => ({ ...initialStoreState, metadata })),\n\n setItemAttrs: (itemIds, attrs) => {\n // Create a map for the items to the updates to be made\n const itemUpdatesMap: { [id: string]: object } = itemIds.reduce(\n (acc, itemId) => ({ ...acc, [itemId]: attrs }),\n {},\n );\n\n return set((state) => {\n const items = state.items.map((item) => {\n if (itemUpdatesMap[item.id]) {\n return { ...item, ...itemUpdatesMap[item.id] };\n }\n\n return item;\n });\n\n return { items };\n });\n },\n }));\n}\n"],"names":["create","zustand","processItems","items","deduped","deduplicateItems","sortItems","defaultSetResultOptions","initialStoreState","createStore","set","NetworkStatus","networkStatus","entries","meta","page_info","options","state","metadata","itemIds","attrs","itemUpdatesMap","acc","itemId","item"],"mappings":"
|
|
1
|
+
{"version":3,"file":"store.js","sources":["../../../../src/clients/feed/store.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport zustand from \"zustand/vanilla\";\n\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { FeedItem } from \"./interfaces\";\nimport { FeedStoreState } from \"./types\";\nimport { deduplicateItems, sortItems } from \"./utils\";\n\n// Get the correct Zustand function. Caused by some issues in v3 exports\n// https://github.com/pmndrs/zustand/issues/334\nconst create: typeof zustand =\n // @ts-expect-error workaround for issue above\n typeof zustand === \"function\" ? zustand : zustand.default;\n\nfunction processItems(items: FeedItem[]) {\n const deduped = deduplicateItems(items);\n const sorted = sortItems(deduped);\n\n return sorted;\n}\n\nconst defaultSetResultOptions = {\n shouldSetPage: true,\n shouldAppend: false,\n};\n\nconst initialStoreState = {\n items: [],\n metadata: {\n total_count: 0,\n unread_count: 0,\n unseen_count: 0,\n },\n pageInfo: {\n before: null,\n after: null,\n page_size: 50,\n },\n};\n\nexport default function createStore() {\n return create<FeedStoreState>((set) => ({\n // Keeps track of all of the items loaded\n ...initialStoreState,\n // The network status indicates what's happening with the request\n networkStatus: NetworkStatus.ready,\n loading: false,\n\n setNetworkStatus: (networkStatus: NetworkStatus) =>\n set(() => ({\n networkStatus,\n loading: networkStatus === NetworkStatus.loading,\n })),\n\n setResult: (\n { entries, meta, page_info },\n options = defaultSetResultOptions,\n ) =>\n set((state) => {\n // We resort the list on set, so concating everything is fine (if a bit suboptimal)\n const items = options.shouldAppend\n ? processItems(state.items.concat(entries as FeedItem<GenericData>[]))\n : entries;\n\n return {\n items,\n metadata: meta,\n pageInfo: options.shouldSetPage ? page_info : state.pageInfo,\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n }),\n\n setMetadata: (metadata) => set(() => ({ metadata })),\n\n resetStore: (metadata = initialStoreState.metadata) =>\n set(() => ({ ...initialStoreState, metadata })),\n\n setItemAttrs: (itemIds, attrs) => {\n // Create a map for the items to the updates to be made\n const itemUpdatesMap: { [id: string]: object } = itemIds.reduce(\n (acc, itemId) => ({ ...acc, [itemId]: attrs }),\n {},\n );\n\n return set((state) => {\n const items = state.items.map((item) => {\n if (itemUpdatesMap[item.id]) {\n return { ...item, ...itemUpdatesMap[item.id] };\n }\n\n return item;\n });\n\n return { items };\n });\n },\n }));\n}\n"],"names":["create","zustand","processItems","items","deduped","deduplicateItems","sortItems","defaultSetResultOptions","initialStoreState","createStore","set","NetworkStatus","networkStatus","entries","meta","page_info","options","state","metadata","itemIds","attrs","itemUpdatesMap","acc","itemId","item"],"mappings":"2MAWMA,EAEJ,OAAOC,GAAY,WAAaA,EAAUA,EAAQ,QAEpD,SAASC,EAAaC,EAAmB,CACjC,MAAAC,EAAUC,mBAAiBF,CAAK,EAG/B,OAFQG,YAAUF,CAAO,CAGlC,CAEA,MAAMG,EAA0B,CAC9B,cAAe,GACf,aAAc,EAChB,EAEMC,EAAoB,CACxB,MAAO,CAAC,EACR,SAAU,CACR,YAAa,EACb,aAAc,EACd,aAAc,CAChB,EACA,SAAU,CACR,OAAQ,KACR,MAAO,KACP,UAAW,EACb,CACF,EAEA,SAAwBC,GAAc,CAC7B,OAAAT,EAAwBU,IAAS,CAEtC,GAAGF,EAEH,cAAeG,EAAc,cAAA,MAC7B,QAAS,GAET,iBAAmBC,GACjBF,EAAI,KAAO,CAAA,cACTE,EACA,QAASA,IAAkBD,EAAAA,cAAc,OAAA,EACzC,EAEJ,UAAW,CACT,CAAE,QAAAE,EAAS,KAAAC,EAAM,UAAAC,GACjBC,EAAUT,IAEVG,EAAKO,IAMI,CACL,MALYD,EAAQ,aAClBd,EAAae,EAAM,MAAM,OAAOJ,CAAkC,CAAC,EACnEA,EAIF,SAAUC,EACV,SAAUE,EAAQ,cAAgBD,EAAYE,EAAM,SACpD,QAAS,GACT,cAAeN,EAAc,cAAA,KAAA,EAEhC,EAEH,YAAcO,GAAaR,EAAI,KAAO,CAAE,SAAAQ,CAAW,EAAA,EAEnD,WAAY,CAACA,EAAWV,EAAkB,WACxCE,EAAI,KAAO,CAAE,GAAGF,EAAmB,SAAAU,CAAA,EAAW,EAEhD,aAAc,CAACC,EAASC,IAAU,CAEhC,MAAMC,EAA2CF,EAAQ,OACvD,CAACG,EAAKC,KAAY,CAAE,GAAGD,EAAK,CAACC,CAAM,EAAGH,IACtC,CAAC,CAAA,EAGI,OAAAV,EAAKO,IASH,CAAE,MARKA,EAAM,MAAM,IAAKO,GACzBH,EAAeG,EAAK,EAAE,EACjB,CAAE,GAAGA,EAAM,GAAGH,EAAeG,EAAK,EAAE,GAGtCA,CACR,CAEc,EAChB,CACH,CACA,EAAA,CACJ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var c=Object.defineProperty;var u=(n,e,s)=>e in n?c(n,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[e]=s;var a=(n,e,s)=>u(n,typeof e!="symbol"?e+"":e,s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("../../networkStatus.js"),k=require("./socket-manager.js"),l=require("./store.js");class h{constructor(e,s,t={}){a(this,"store");a(this,"socketManager");this.knock=e,this.channelId=s,this.defaultOptions=t,this.store=l.createStore();const{socket:r}=this.knock.client();r&&(this.socketManager=new k.InAppMessageSocketManager(r)),this.knock.log(`[IAM] Initialized a client on channel ${s}`)}setMessageAttrs(e,s){this.store.setState(t=>({...t,messages:{...t.messages,...e.reduce((r,i)=>(t.messages[i]&&(r[i]={...t.messages[i],...s}),r),{})}}))}setQueryResponse(e,s){const t={loading:!1,networkStatus:g.NetworkStatus.ready,data:{messageIds:s.entries.map(r=>r.id),pageInfo:s.pageInfo}};this.store.setState(r=>({...r,messages:s.entries.reduce((i,o)=>(i[o.id]=o,i),r.messages),queries:{...r.queries,[e]:t}}))}setQueryStatus(e,s){this.store.setState(t=>({...t,queries:{...t.queries,[e]:{...t.queries[e],...s}}}))}subscribe(e){if(this.socketManager)return this.knock.log(`[InAppMessagesClient] Subscribe a client ${e.referenceId} to socket events`),this.socketManager.join(e)}unsubscribe(e){this.socketManager&&(this.knock.log(`[InAppMessagesClient] Unsubscribe a client ${e.referenceId}`),this.socketManager.leave(e))}}exports.InAppMessagesChannelClient=h;
|
|
2
|
+
//# sourceMappingURL=channel-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel-client.js","sources":["../../../../src/clients/in-app-messages/channel-client.ts"],"sourcesContent":["import Knock from \"../../knock\";\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { InAppMessagesClient } from \"./message-client\";\nimport { InAppMessageSocketManager } from \"./socket-manager\";\nimport { InAppMessagesStore, createStore } from \"./store\";\nimport {\n InAppMessage,\n InAppMessagesClientOptions,\n InAppMessagesQueryInfo,\n InAppMessagesResponse,\n} from \"./types\";\n\n/**\n * Manages the configuration for an in app channel.\n * Stores all fetched messages to support optimistic updates.\n */\nexport class InAppMessagesChannelClient {\n public store: InAppMessagesStore;\n\n private socketManager: InAppMessageSocketManager | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly defaultOptions: InAppMessagesClientOptions = {},\n ) {\n this.store = createStore();\n\n // Initialize a socket manager for the in-app channel client, which there\n // should be one per in-app channel client but it's abstracted out as a\n // separate module for the organization/encapsulation purposes.\n const { socket } = this.knock.client();\n if (socket) {\n this.socketManager = new InAppMessageSocketManager(socket);\n }\n\n this.knock.log(`[IAM] Initialized a client on channel ${channelId}`);\n }\n\n // ----------------------------------------------\n // Store helpers\n // ----------------------------------------------\n\n setMessageAttrs(messageIds: string[], attrs: Partial<InAppMessage>) {\n this.store.setState((state) => ({\n ...state,\n messages: {\n ...state.messages,\n ...messageIds.reduce<Record<string, InAppMessage>>((messages, id) => {\n if (state.messages[id]) {\n messages[id] = {\n ...state.messages[id],\n ...attrs,\n };\n }\n return messages;\n }, {}),\n },\n }));\n }\n\n setQueryResponse(queryKey: string, response: InAppMessagesResponse) {\n const queryInfo: InAppMessagesQueryInfo = {\n loading: false,\n networkStatus: NetworkStatus.ready,\n data: {\n messageIds: response.entries.map((iam) => iam.id),\n pageInfo: response.pageInfo,\n },\n };\n\n this.store.setState((state) => {\n return {\n ...state,\n // Store new messages in store\n messages: response.entries.reduce((messages, message) => {\n messages[message.id] = message;\n return messages;\n }, state.messages),\n // Store query results\n queries: {\n ...state.queries,\n [queryKey]: queryInfo,\n },\n };\n });\n }\n\n setQueryStatus(\n queryKey: string,\n status: Pick<InAppMessagesQueryInfo, \"loading\" | \"networkStatus\">,\n ) {\n this.store.setState((state) => ({\n ...state,\n queries: {\n ...state.queries,\n [queryKey]: {\n ...state.queries[queryKey],\n ...status,\n },\n },\n }));\n }\n\n /*\n * Socket\n */\n\n subscribe(client: InAppMessagesClient) {\n if (!this.socketManager) return;\n this.knock.log(\n `[InAppMessagesClient] Subscribe a client ${client.referenceId} to socket events`,\n );\n\n // Pass the unsub func back to iam client so it can be used when\n // unsubscribing.\n return this.socketManager.join(client);\n }\n\n unsubscribe(client: InAppMessagesClient) {\n if (!this.socketManager) return;\n this.knock.log(\n `[InAppMessagesClient] Unsubscribe a client ${client.referenceId}`,\n );\n\n this.socketManager.leave(client);\n }\n}\n"],"names":["InAppMessagesChannelClient","knock","channelId","defaultOptions","__publicField","createStore","socket","InAppMessageSocketManager","messageIds","attrs","state","messages","id","queryKey","response","queryInfo","NetworkStatus","iam","message","status","client"],"mappings":"uVAiBO,MAAMA,CAA2B,CAKtC,YACWC,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ACV,YAAK,MAAM,IACT,4CAA4CA,EAAO,WAAW,mBAAA,EAKzD,KAAK,cAAc,KAAKA,CAAM,CACvC,CAEA,YAAYA,EAA6B,CAClC,KAAK,gBACV,KAAK,MAAM,IACT,8CAA8CA,EAAO,WAAW,EAAA,EAG7D,KAAA,cAAc,MAAMA,CAAM,EACjC,CACF"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var o=Object.defineProperty;var l=(a,t,e)=>t in a?o(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var r=(a,t,e)=>l(a,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("nanoid"),i=require("../../networkStatus.js"),k=require("./socket-manager.js");class y{constructor(t,e,s={}){r(this,"knock");r(this,"queryKey");r(this,"referenceId");r(this,"unsub");this.channelClient=t,this.messageType=e,this.defaultOptions=s,this.defaultOptions={...t.defaultOptions,...s},this.knock=t.knock,this.queryKey=this.buildQueryKey(this.defaultOptions),this.referenceId=g.nanoid(),this.knock.log(`[IAM] Initialized a client for message ${e}`)}async fetch(){const t={...this.defaultOptions,trigger_data:JSON.stringify(this.defaultOptions.trigger_data)};this.queryKey=this.buildQueryKey(t);const s=(this.channelClient.store.state.queries[this.queryKey]??{loading:!1,networkStatus:i.NetworkStatus.ready}).networkStatus;if(!(s&&i.isRequestInFlight(s))){this.channelClient.setQueryStatus(this.queryKey,{networkStatus:i.NetworkStatus.loading,loading:!0});try{const n=await this.knock.user.getInAppMessages({channelId:this.channelClient.channelId,messageType:this.messageType,params:t});return this.channelClient.setQueryResponse(this.queryKey,n),{data:n,status:"ok"}}catch(n){return this.channelClient.setQueryStatus(this.queryKey,{networkStatus:i.NetworkStatus.error,loading:!1}),{status:"error",error:n}}}}getQueryInfoSelector(t){var c;const e=t.queries[this.queryKey];return{messages:(((c=e==null?void 0:e.data)==null?void 0:c.messageIds)??[]).reduce((h,d)=>{const u=t.messages[d];return u&&h.push(u),h},[]),networkStatus:(e==null?void 0:e.networkStatus)??i.NetworkStatus.ready,loading:(e==null?void 0:e.loading)??!1}}async markAsSeen(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{seen_at:new Date().toISOString()}),this.makeStatusUpdate(t,"seen")}async markAsUnseen(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{seen_at:null}),this.makeStatusUpdate(t,"unseen")}async markAsRead(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{read_at:new Date().toISOString()}),this.makeStatusUpdate(t,"read")}async markAsUnread(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{read_at:null}),this.makeStatusUpdate(t,"unread")}async markAsInteracted(t,e){const s=new Date().toISOString(),n=this.getItemIds(t);return this.channelClient.setMessageAttrs(n,{read_at:s,interacted_at:s}),this.makeStatusUpdate(t,"interacted",e)}async markAsArchived(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{archived_at:new Date().toISOString()}),this.makeStatusUpdate(t,"archived")}async markAsUnarchived(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{archived_at:null}),this.makeStatusUpdate(t,"unarchived")}async makeStatusUpdate(t,e,s){const n=this.getItemIds(t);return await this.knock.messages.batchUpdateStatuses(n,e,{metadata:s})}buildQueryKey(t){const e=`/v1/users/${this.knock.userId}/in-app-messages/${this.channelClient.channelId}/${this.messageType}`,s=new URLSearchParams(t).toString();return s?`${e}?${s}`:e}subscribe(){this.unsub=this.channelClient.subscribe(this)}unsubscribe(){this.channelClient.unsubscribe(this)}async handleSocketEvent(t){switch(t.event){case k.SocketEventType.MessageCreated:return await this.fetch();default:throw new Error(`Unhandled socket event: ${t.event}`)}}socketChannelTopic(){return`in_app:${this.messageType}:${this.channelClient.channelId}:${this.knock.userId}`}getItemIds(t){return(Array.isArray(t)?t:[t]).map(s=>s.id)}}exports.InAppMessagesClient=y;
|
|
2
|
+
//# sourceMappingURL=message-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-client.js","sources":["../../../../src/clients/in-app-messages/message-client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { nanoid } from \"nanoid\";\n\nimport Knock from \"../../knock\";\nimport { NetworkStatus, isRequestInFlight } from \"../../networkStatus\";\nimport { MessageEngagementStatus } from \"../messages/interfaces\";\n\nimport { InAppMessagesChannelClient } from \"./channel-client\";\nimport { SocketEventPayload, SocketEventType } from \"./socket-manager\";\nimport {\n InAppMessage,\n InAppMessagesClientOptions,\n InAppMessagesResponse,\n InAppMessagesStoreState,\n} from \"./types\";\n\n/**\n * Manages realtime connection to in app messages service.\n */\nexport class InAppMessagesClient {\n private knock: Knock;\n\n public queryKey: string;\n public referenceId: string;\n public unsub?: () => void;\n\n constructor(\n readonly channelClient: InAppMessagesChannelClient,\n readonly messageType: string,\n readonly defaultOptions: InAppMessagesClientOptions = {},\n ) {\n this.defaultOptions = {\n ...channelClient.defaultOptions,\n ...defaultOptions,\n };\n this.knock = channelClient.knock;\n this.queryKey = this.buildQueryKey(this.defaultOptions);\n this.referenceId = nanoid();\n\n this.knock.log(`[IAM] Initialized a client for message ${messageType}`);\n }\n\n // ----------------------------------------------\n // Data fetching\n // ----------------------------------------------\n async fetch<\n TContent extends GenericData = GenericData,\n TData extends GenericData = GenericData,\n >(): Promise<\n | {\n status: \"ok\";\n data: InAppMessagesResponse<TContent, TData>;\n }\n | {\n status: \"error\";\n error: Error;\n }\n | undefined\n > {\n const params = {\n ...this.defaultOptions,\n // Convert trigger_data to a string, which the API expects\n trigger_data: JSON.stringify(this.defaultOptions.trigger_data),\n };\n\n this.queryKey = this.buildQueryKey(params);\n\n const queryState = this.channelClient.store.state.queries[\n this.queryKey\n ] ?? {\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n const networkStatus = queryState.networkStatus;\n\n // If there's an existing request in flight, then do nothing\n if (networkStatus && isRequestInFlight(networkStatus)) {\n return;\n }\n\n // Set the loading type based on the request type it is\n this.channelClient.setQueryStatus(this.queryKey, {\n networkStatus: NetworkStatus.loading,\n loading: true,\n });\n\n try {\n const response = await this.knock.user.getInAppMessages<TContent, TData>({\n channelId: this.channelClient.channelId,\n messageType: this.messageType,\n params,\n });\n\n this.channelClient.setQueryResponse(this.queryKey, response);\n\n return { data: response, status: \"ok\" };\n } catch (error) {\n this.channelClient.setQueryStatus(this.queryKey, {\n networkStatus: NetworkStatus.error,\n loading: false,\n });\n\n return {\n status: \"error\",\n error: error as Error,\n };\n }\n }\n\n getQueryInfoSelector<\n TContent extends GenericData = GenericData,\n TData extends GenericData = GenericData,\n >(\n state: InAppMessagesStoreState,\n ): {\n messages: InAppMessage<TContent, TData>[];\n loading: boolean;\n networkStatus: NetworkStatus;\n } {\n const queryInfo = state.queries[this.queryKey];\n const messageIds = queryInfo?.data?.messageIds ?? [];\n\n const messages = messageIds.reduce<InAppMessage<TContent, TData>[]>(\n (messages, messageId) => {\n const message = state.messages[messageId];\n if (message) {\n messages.push(message as InAppMessage<TContent, TData>);\n }\n return messages;\n },\n [],\n );\n\n return {\n messages,\n networkStatus: queryInfo?.networkStatus ?? NetworkStatus.ready,\n loading: queryInfo?.loading ?? false,\n };\n }\n\n // ----------------------------------------------\n // Message engagement\n // ----------------------------------------------\n async markAsSeen(itemOrItems: InAppMessage | InAppMessage[]) {\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n seen_at: new Date().toISOString(),\n });\n\n return this.makeStatusUpdate(itemOrItems, \"seen\");\n }\n\n async markAsUnseen(itemOrItems: InAppMessage | InAppMessage[]) {\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n seen_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unseen\");\n }\n\n async markAsRead(itemOrItems: InAppMessage | InAppMessage[]) {\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n read_at: new Date().toISOString(),\n });\n\n return this.makeStatusUpdate(itemOrItems, \"read\");\n }\n\n async markAsUnread(itemOrItems: InAppMessage | InAppMessage[]) {\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n read_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unread\");\n }\n\n async markAsInteracted(\n itemOrItems: InAppMessage | InAppMessage[],\n metadata?: Record<string, string>,\n ) {\n const now = new Date().toISOString();\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n read_at: now,\n interacted_at: now,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"interacted\", metadata);\n }\n\n async markAsArchived(itemOrItems: InAppMessage | InAppMessage[]) {\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n archived_at: new Date().toISOString(),\n });\n\n return this.makeStatusUpdate(itemOrItems, \"archived\");\n }\n\n async markAsUnarchived(itemOrItems: InAppMessage | InAppMessage[]) {\n const itemIds = this.getItemIds(itemOrItems);\n\n this.channelClient.setMessageAttrs(itemIds, {\n archived_at: null,\n });\n\n return this.makeStatusUpdate(itemOrItems, \"unarchived\");\n }\n\n private async makeStatusUpdate(\n itemOrItems: InAppMessage | InAppMessage[],\n type: MessageEngagementStatus | \"unread\" | \"unseen\" | \"unarchived\",\n metadata?: Record<string, string>,\n ) {\n // Always treat items as a batch to use the corresponding batch endpoint\n const itemIds = this.getItemIds(itemOrItems);\n\n const result = await this.knock.messages.batchUpdateStatuses(\n itemIds,\n type,\n { metadata },\n );\n\n return result;\n }\n\n // ----------------------------------------------\n // Helpers\n // ----------------------------------------------\n private buildQueryKey(params: GenericData): string {\n const baseKey = `/v1/users/${this.knock.userId}/in-app-messages/${this.channelClient.channelId}/${this.messageType}`;\n const paramsString = new URLSearchParams(params).toString();\n return paramsString ? `${baseKey}?${paramsString}` : baseKey;\n }\n\n subscribe() {\n this.unsub = this.channelClient.subscribe(this);\n }\n\n unsubscribe() {\n this.channelClient.unsubscribe(this);\n }\n\n // This is a callback function that will be invoked when a new socket event\n // relevant for this message client is received (and if subscribed).\n async handleSocketEvent(payload: SocketEventPayload) {\n switch (payload.event) {\n case SocketEventType.MessageCreated:\n // TODO(KNO-7169): Explore using an in-app message in the socket event\n // directly instead of re-fetching.\n return await this.fetch();\n\n default:\n throw new Error(`Unhandled socket event: ${payload.event}`);\n }\n }\n\n socketChannelTopic() {\n return `in_app:${this.messageType}:${this.channelClient.channelId}:${this.knock.userId}`;\n }\n\n private getItemIds(itemOrItems: InAppMessage | InAppMessage[]): string[] {\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n return items.map((item) => item.id);\n }\n}\n"],"names":["InAppMessagesClient","channelClient","messageType","defaultOptions","__publicField","nanoid","params","networkStatus","NetworkStatus","isRequestInFlight","response","error","state","queryInfo","_a","messages","messageId","message","itemOrItems","itemIds","metadata","now","type","baseKey","paramsString","payload","SocketEventType","item"],"mappings":"mVAmBO,MAAMA,CAAoB,CAO/B,YACWC,EACAC,EACAC,EAA6C,CAAA,EACtD,CAVMC,EAAA,cAEDA,EAAA,iBACAA,EAAA,oBACAA,EAAA,cAGI,KAAA,cAAAH,EACA,KAAA,YAAAC,EACA,KAAA,eAAAC,EAET,KAAK,eAAiB,CACpB,GAAGF,EAAc,eACjB,GAAGE,CAAA,EAEL,KAAK,MAAQF,EAAc,MAC3B,KAAK,SAAW,KAAK,cAAc,KAAK,cAAc,EACtD,KAAK,YAAcI,EAAAA,SAEnB,KAAK,MAAM,IAAI,0CAA0CH,CAAW,EAAE,CACxE,CAKA,MAAM,OAaJ,CACA,MAAMI,EAAS,CACb,GAAG,KAAK,eAER,aAAc,KAAK,UAAU,KAAK,eAAe,YAAY,CAAA,EAG1D,KAAA,SAAW,KAAK,cAAcA,CAAM,EAQzC,MAAMC,GANa,KAAK,cAAc,MAAM,MAAM,QAChD,KAAK,QACP,GAAK,CACH,QAAS,GACT,cAAeC,EAAc,cAAA,KAAA,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GAC5GC,EAAe,IAAI,gBAAgBlB,CAAM,EAAE,SAAS,EAC1D,OAAOkB,EAAe,GAAGD,CAAO,IAAIC,CAAY,GAAKD,CACvD,CAEA,WAAY,CACV,KAAK,MAAQ,KAAK,cAAc,UAAU,IAAI,CAChD,CAEA,aAAc,CACP,KAAA,cAAc,YAAY,IAAI,CACrC,CAIA,MAAM,kBAAkBE,EAA6B,CACnD,OAAQA,EAAQ,MAAO,CACrB,KAAKC,EAAgB,gBAAA,eAGZ,OAAA,MAAM,KAAK,QAEpB,QACE,MAAM,IAAI,MAAM,2BAA2BD,EAAQ,KAAK,EAAE,CAC9D,CACF,CAEA,oBAAqB,CACZ,MAAA,UAAU,KAAK,WAAW,IAAI,KAAK,cAAc,SAAS,IAAI,KAAK,MAAM,MAAM,EACxF,CAEQ,WAAWP,EAAsD,CAEvE,OADc,MAAM,QAAQA,CAAW,EAAIA,EAAc,CAACA,CAAW,GACxD,IAAKS,GAASA,EAAK,EAAE,CACpC,CACF"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var b=Object.defineProperty;var m=(t,e,s)=>e in t?b(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var h=(t,e,s)=>m(t,typeof e!="symbol"?e+"":e,s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("@tanstack/store");var l=(t=>(t.MessageCreated="message.created",t))(l||{});const f=["message.created"];class S{constructor(e){h(this,"channels");h(this,"params");h(this,"inbox");this.socket=e,this.channels={},this.params={},this.inbox=new g.Store({})}join(e){const s=e.socketChannelTopic(),n=e.referenceId,o=e.defaultOptions;this.socket.isConnected()||this.socket.connect(),this.params[s]||(this.params[s]={});const a=this.params[s][n],p=!a||JSON.stringify(a)!==JSON.stringify(o);if(p&&(this.params[s]={...this.params[s],[n]:o}),!this.channels[s]||p){const c=this.socket.channel(s,this.params[s]);for(const d of f)c.on(d,u=>this.setInbox(u));this.channels[s]=c}const r=this.channels[s];return["closed","errored"].includes(r.state)&&r.join(),this.inbox.subscribe(()=>{const c=this.inbox.state[n];c&&e.handleSocketEvent(c)})}leave(e){e.unsub&&e.unsub();const s=e.socketChannelTopic(),n=e.referenceId,o={...this.params},a=o[s]||{};a[n]&&delete a[n];const r={...this.channels},i=r[s];if(i&&Object.keys(a).length===0){for(const c of f)i.off(c);i.leave(),delete r[s]}this.params=o,this.channels=r}setInbox(e){const{attn:s,...n}=e;this.inbox.setState(()=>s.reduce((o,a)=>({...o,[a]:n}),{}))}}exports.InAppMessageSocketManager=S;exports.SocketEventType=l;
|
|
2
|
+
//# sourceMappingURL=socket-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socket-manager.js","sources":["../../../../src/clients/in-app-messages/socket-manager.ts"],"sourcesContent":["import { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\n\nimport { InAppMessagesClient } from \"./message-client\";\nimport { InAppMessage, InAppMessagesClientOptions } from \"./types\";\n\nexport enum SocketEventType {\n MessageCreated = \"message.created\",\n}\n\nconst SOCKET_EVENT_TYPES = [SocketEventType.MessageCreated];\n\ntype ClientQueryParams = InAppMessagesClientOptions;\n\n// e.g. in_app:<message-type>:<channel_id>:<user_id>\ntype ChannelTopic = string;\n\n// Unique reference id of an in-app message client\ntype ClientReferenceId = string;\n\ntype MessageCreatedEventPayload = {\n event: SocketEventType.MessageCreated;\n topic: string;\n data: {\n in_app_message: InAppMessage;\n };\n};\n\nexport type SocketEventPayload = MessageCreatedEventPayload;\n\n// \"attn\" field contains a list of client reference ids that should be notified\n// of a socket event.\ntype WithAttn<P> = P & { attn: Array<ClientReferenceId> };\n\ntype InAppMessageSocketInbox = Record<ClientReferenceId, SocketEventPayload>;\n\n/*\n * Manages socket subscriptions for in-app messages, allowing multiple in-app\n * message clients or components to listen for real time updates from the socket\n * API via a single socket connection. It's expected to be instantiated once\n * per an in-app channel.\n */\nexport class InAppMessageSocketManager {\n // Mapping of live channels by topic. Note, there can be one or more in-app\n // message client(s) that can subscribe.\n private channels: Record<ChannelTopic, Channel>;\n\n // Mapping of query params for each in-app message client, partitioned by its\n // reference id, and grouped by channel topic. It's a double nested object\n // that looks like:\n // {\n // \"in_app:card:...\": {\n // \"ref-1\": {\n // \"workflow_key\": \"foo\",\n // },\n // \"ref-2\": {\n // \"workflow_key\": \"bar\",\n // },\n // },\n // \"in_app:banner:...\": {\n // \"ref-3\": {\n // \"workflow_key\": \"baz\",\n // },\n // }\n // }\n //\n // Each time a new in-app message client joins a channel, we send all cumulated\n // params such that the socket API can apply filtering rules and figure out\n // which in-app message clients should be notified basd on reference ids in\n // \"attn\" field of the event payload when sending out an event.\n private params: Record<\n ChannelTopic,\n Record<ClientReferenceId, ClientQueryParams>\n >;\n\n // A reactive store that captures a new socket event, that notifies any in-app\n // message clients that have subscribed.\n private inbox: Store<\n InAppMessageSocketInbox,\n (cb: InAppMessageSocketInbox) => InAppMessageSocketInbox\n >;\n\n constructor(readonly socket: Socket) {\n this.channels = {};\n this.params = {};\n this.inbox = new Store<InAppMessageSocketInbox>({});\n }\n\n join(iamClient: InAppMessagesClient) {\n const topic = iamClient.socketChannelTopic();\n const referenceId = iamClient.referenceId;\n const params = iamClient.defaultOptions;\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If a new in-app message client joins, or has updated query params then\n // track the updated params and (re)join with the latest query params.\n // Note, each time we send combined params of all in-app message clients that\n // have subscribed for a given message type (and an in-app channel and a\n // user), grouped by client's reference id.\n if (!this.params[topic]) {\n this.params[topic] = {};\n }\n\n const maybeParams = this.params[topic][referenceId];\n const hasNewOrUpdatedParams =\n !maybeParams || JSON.stringify(maybeParams) !== JSON.stringify(params);\n\n if (hasNewOrUpdatedParams) {\n // Tracks all subscribed client's params by its reference id and by topic.\n this.params[topic] = { ...this.params[topic], [referenceId]: params };\n }\n\n if (!this.channels[topic] || hasNewOrUpdatedParams) {\n const newChannel = this.socket.channel(topic, this.params[topic]);\n for (const eventType of SOCKET_EVENT_TYPES) {\n newChannel.on(eventType, (payload) => this.setInbox(payload));\n }\n // Tracks live channels by channel topic.\n this.channels[topic] = newChannel;\n }\n\n const channel = this.channels[topic];\n\n // Join the channel if not already joined or joining or leaving.\n if ([\"closed\", \"errored\"].includes(channel.state)) {\n channel.join();\n }\n\n // Let the in-app message client subscribe to the \"inbox\", so it can be\n // notified when there's a new socket event that is relevant for the client.\n const unsub = this.inbox.subscribe(() => {\n const payload = this.inbox.state[referenceId];\n if (!payload) return;\n\n iamClient.handleSocketEvent(payload);\n });\n\n return unsub;\n }\n\n leave(iamClient: InAppMessagesClient) {\n if (iamClient.unsub) {\n iamClient.unsub();\n }\n\n const topic = iamClient.socketChannelTopic();\n const referenceId = iamClient.referenceId;\n\n const partitionedParams = { ...this.params };\n const paramsForTopic = partitionedParams[topic] || {};\n const paramsForReferenceClient = paramsForTopic[referenceId];\n\n if (paramsForReferenceClient) {\n delete paramsForTopic[referenceId];\n }\n\n const channels = { ...this.channels };\n const channelForTopic = channels[topic];\n if (channelForTopic && Object.keys(paramsForTopic).length === 0) {\n for (const eventType of SOCKET_EVENT_TYPES) {\n channelForTopic.off(eventType);\n }\n channelForTopic.leave();\n delete channels[topic];\n }\n\n this.params = partitionedParams;\n this.channels = channels;\n }\n\n private setInbox(payload: WithAttn<SocketEventPayload>) {\n const { attn, ...rest } = payload;\n\n // Set the incoming socket event into the inbox, keyed by relevant client\n // reference ids provided by the server (via attn field), so we can notify\n // only the clients that need to be notified.\n this.inbox.setState(() =>\n attn.reduce((acc, referenceId) => {\n return { ...acc, [referenceId]: rest };\n }, {}),\n );\n }\n}\n"],"names":["SocketEventType","SOCKET_EVENT_TYPES","InAppMessageSocketManager","socket","__publicField","Store","iamClient","topic","referenceId","params","maybeParams","hasNewOrUpdatedParams","newChannel","eventType","payload","channel","partitionedParams","paramsForTopic","channels","channelForTopic","attn","rest","acc"],"mappings":"uRAMY,IAAAA,GAAAA,IACVA,EAAA,eA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D,EAAU,qBAClBE,EAAcF,EAAU,YAExBU,EAAoB,CAAE,GAAG,KAAK,MAAO,EACrCC,EAAiBD,EAAkBT,CAAK,GAAK,CAAA,EAClBU,EAAeT,CAAW,GAGzD,OAAOS,EAAeT,CAAW,EAGnC,MAAMU,EAAW,CAAE,GAAG,KAAK,QAAS,EAC9BC,EAAkBD,EAASX,CAAK,EACtC,GAAIY,GAAmB,OAAO,KAAKF,CAAc,EAAE,SAAW,EAAG,CAC/D,UAAWJ,KAAaZ,EACtBkB,EAAgB,IAAIN,CAAS,EAE/BM,EAAgB,MAAM,EACtB,OAAOD,EAASX,CAAK,CACvB,CAEA,KAAK,OAASS,EACd,KAAK,SAAWE,CAClB,CAEQ,SAASJ,EAAuC,CACtD,KAAM,CAAE,KAAAM,EAAM,GAAGC,CAAA,EAASP,EAK1B,KAAK,MAAM,SAAS,IAClBM,EAAK,OAAO,CAACE,EAAKd,KACT,CAAE,GAAGc,EAAK,CAACd,CAAW,EAAGa,CAAK,GACpC,EAAE,CAAA,CAET,CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sources":["../../../../src/clients/in-app-messages/store.ts"],"sourcesContent":["import { Store } from \"@tanstack/store\";\n\nimport { InAppMessagesStoreState } from \"./types\";\n\nexport type InAppMessagesStore = Store<\n InAppMessagesStoreState,\n (cb: InAppMessagesStoreState) => InAppMessagesStoreState\n>;\n\nexport function createStore() {\n return new Store<InAppMessagesStoreState>({\n messages: {},\n queries: {},\n });\n}\n"],"names":["createStore","Store"],"mappings":"mHASO,SAASA,GAAc,CAC5B,OAAO,IAAIC,EAAAA,MAA+B,CACxC,SAAU,CAAC,EACX,QAAS,CAAC,CAAA,CACX,CACH"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var c=Object.defineProperty;var u=(
|
|
1
|
+
"use strict";var c=Object.defineProperty;var u=(n,e,t)=>e in n?c(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var r=(n,e,t)=>u(n,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i="default";class l{constructor(e){r(this,"instance");this.instance=e}async get(){const e=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}`});return this.handleResponse(e)}async identify(e={}){const t=await this.instance.client().makeRequest({method:"PUT",url:`/v1/users/${this.instance.userId}`,params:e});return this.handleResponse(t)}async getAllPreferences(){const e=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/preferences`});return this.handleResponse(e)}async getPreferences(e={}){const t=e.preferenceSet||i,s=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/preferences/${t}`,params:{tenant:e.tenant}});return this.handleResponse(s)}async setPreferences(e,t={}){const s=t.preferenceSet||i,a=await this.instance.client().makeRequest({method:"PUT",url:`/v1/users/${this.instance.userId}/preferences/${s}`,data:e});return this.handleResponse(a)}async getChannelData(e){const t=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/channel_data/${e.channelId}`});return this.handleResponse(t)}async setChannelData({channelId:e,channelData:t}){const s=await this.instance.client().makeRequest({method:"PUT",url:`/v1/users/${this.instance.userId}/channel_data/${e}`,data:{data:t}});return this.handleResponse(s)}async getInAppMessages({channelId:e,messageType:t,params:s}){const a=await this.instance.client().makeRequest({method:"GET",url:`/v1/users/${this.instance.userId}/in-app-messages/${e}/${t}`,params:s});return this.handleResponse(a)}handleResponse(e){if(e.statusCode==="error")throw new Error(e.error||e.body);return e.body}}exports.default=l;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/clients/users/index.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\n\nimport { ApiResponse } from \"../../api\";\nimport { ChannelData, User } from \"../../interfaces\";\nimport Knock from \"../../knock\";\nimport {\n GetPreferencesOptions,\n PreferenceOptions,\n PreferenceSet,\n SetPreferencesProperties,\n} from \"../preferences/interfaces\";\n\nimport {
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/clients/users/index.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\n\nimport { ApiResponse } from \"../../api\";\nimport { ChannelData, User } from \"../../interfaces\";\nimport Knock from \"../../knock\";\nimport { InAppMessagesResponse } from \"../in-app-messages\";\nimport {\n GetPreferencesOptions,\n PreferenceOptions,\n PreferenceSet,\n SetPreferencesProperties,\n} from \"../preferences/interfaces\";\n\nimport {\n GetChannelDataInput,\n GetInAppMessagesInput,\n SetChannelDataInput,\n} from \"./interfaces\";\n\nconst DEFAULT_PREFERENCE_SET_ID = \"default\";\n\nclass UserClient {\n private instance: Knock;\n\n constructor(instance: Knock) {\n this.instance = instance;\n }\n\n async get() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}`,\n });\n\n return this.handleResponse<User>(result);\n }\n\n async identify(props: GenericData = {}) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}`,\n params: props,\n });\n\n return this.handleResponse<User>(result);\n }\n\n async getAllPreferences() {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences`,\n });\n\n return this.handleResponse<PreferenceSet[]>(result);\n }\n\n async getPreferences(\n options: GetPreferencesOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n params: { tenant: options.tenant },\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async setPreferences(\n preferenceSet: SetPreferencesProperties,\n options: PreferenceOptions = {},\n ): Promise<PreferenceSet> {\n const preferenceSetId = options.preferenceSet || DEFAULT_PREFERENCE_SET_ID;\n\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/preferences/${preferenceSetId}`,\n data: preferenceSet,\n });\n\n return this.handleResponse<PreferenceSet>(result);\n }\n\n async getChannelData<T = GenericData>(params: GetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/channel_data/${params.channelId}`,\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n async setChannelData<T = GenericData>({\n channelId,\n channelData,\n }: SetChannelDataInput) {\n const result = await this.instance.client().makeRequest({\n method: \"PUT\",\n url: `/v1/users/${this.instance.userId}/channel_data/${channelId}`,\n data: { data: channelData },\n });\n\n return this.handleResponse<ChannelData<T>>(result);\n }\n\n async getInAppMessages<\n TContent extends GenericData = GenericData,\n TData extends GenericData = GenericData,\n >({ channelId, messageType, params }: GetInAppMessagesInput) {\n const result = await this.instance.client().makeRequest({\n method: \"GET\",\n url: `/v1/users/${this.instance.userId}/in-app-messages/${channelId}/${messageType}`,\n params,\n });\n\n return this.handleResponse<InAppMessagesResponse<TContent, TData>>(result);\n }\n\n private handleResponse<T>(response: ApiResponse) {\n if (response.statusCode === \"error\") {\n throw new Error(response.error || response.body);\n }\n\n return response.body as T;\n }\n}\n\nexport default UserClient;\n"],"names":["DEFAULT_PREFERENCE_SET_ID","UserClient","instance","__publicField","result","props","options","preferenceSetId","preferenceSet","params","channelId","channelData","messageType","response"],"mappings":"gRAmBA,MAAMA,EAA4B,UAElC,MAAMC,CAAW,CAGf,YAAYC,EAAiB,CAFrBC,EAAA,iBAGN,KAAK,SAAWD,CAClB,CAEA,MAAM,KAAM,CACV,MAAME,EAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY,CACtD,OAAQ,MACR,IAAK,aAAa,KAAK,SAAS,MAAM,EAAA,CACvC,EAEM,OAAA,KAAK,eAAqBA,CAAM,CACzC,CAEA,MAAM,SAASC,EAAqB,GAAI,CACtC,MAAMD,EAAS,MAAM,KAAK,S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C,CAAY,CAAA,CAC3B,EAEM,OAAA,KAAK,eAA+BP,CAAM,CACnD,CAEA,MAAM,iBAGJ,CAAE,UAAAM,EAAW,YAAAE,EAAa,OAAAH,GAAiC,CAC3D,MAAML,EAAS,MAAM,KAAK,SAAS,OAAA,EAAS,YAAY,CACtD,OAAQ,MACR,IAAK,aAAa,KAAK,SAAS,MAAM,oBAAoBM,CAAS,IAAIE,CAAW,GAClF,OAAAH,CAAA,CACD,EAEM,OAAA,KAAK,eAAuDL,CAAM,CAC3E,CAEQ,eAAkBS,EAAuB,CAC3C,GAAAA,EAAS,aAAe,QAC1B,MAAM,IAAI,MAAMA,EAAS,OAASA,EAAS,IAAI,EAGjD,OAAOA,EAAS,IAClB,CACF"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("./clients/feed/index.js"),
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("./clients/feed/index.js"),s=require("./knock.js"),n=require("./clients/objects/constants.js"),i=require("./clients/in-app-messages/channel-client.js"),r=require("./clients/in-app-messages/message-client.js"),e=require("./networkStatus.js"),u=require("./clients/feed/feed.js");exports.FeedClient=t.default;exports.default=s.default;exports.TENANT_OBJECT_COLLECTION=n.TENANT_OBJECT_COLLECTION;exports.InAppMessagesChannelClient=i.InAppMessagesChannelClient;exports.InAppMessagesClient=r.InAppMessagesClient;exports.NetworkStatus=e.NetworkStatus;exports.isRequestInFlight=e.isRequestInFlight;exports.Feed=u.default;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/api.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var r = (s, e, t) =>
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
var a = Object.defineProperty;
|
|
2
|
+
var n = (s, e, t) => e in s ? a(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var r = (s, e, t) => n(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import o from "axios";
|
|
5
|
+
import i from "axios-retry";
|
|
6
6
|
import { Socket as u } from "phoenix";
|
|
7
7
|
class y {
|
|
8
8
|
constructor(e) {
|
|
@@ -11,7 +11,12 @@ class y {
|
|
|
11
11
|
r(this, "userToken");
|
|
12
12
|
r(this, "axiosClient");
|
|
13
13
|
r(this, "socket");
|
|
14
|
-
this.host = e.host, this.apiKey = e.apiKey, this.userToken = e.userToken || null
|
|
14
|
+
this.host = e.host, this.apiKey = e.apiKey, this.userToken = e.userToken || null;
|
|
15
|
+
const t = (
|
|
16
|
+
// @ts-expect-error Fixing the issue described above
|
|
17
|
+
o.default ? o.default : o
|
|
18
|
+
);
|
|
19
|
+
this.axiosClient = t.create({
|
|
15
20
|
baseURL: this.host,
|
|
16
21
|
headers: {
|
|
17
22
|
Accept: "application/json",
|
|
@@ -24,10 +29,10 @@ class y {
|
|
|
24
29
|
user_token: this.userToken,
|
|
25
30
|
api_key: this.apiKey
|
|
26
31
|
}
|
|
27
|
-
})),
|
|
32
|
+
})), i(this.axiosClient, {
|
|
28
33
|
retries: 3,
|
|
29
34
|
retryCondition: this.canRetryRequest,
|
|
30
|
-
retryDelay:
|
|
35
|
+
retryDelay: i.exponentialDelay
|
|
31
36
|
});
|
|
32
37
|
}
|
|
33
38
|
async makeRequest(e) {
|
|
@@ -49,7 +54,7 @@ class y {
|
|
|
49
54
|
}
|
|
50
55
|
}
|
|
51
56
|
canRetryRequest(e) {
|
|
52
|
-
return
|
|
57
|
+
return i.isNetworkError(e) ? !0 : e.response ? e.response.status >= 500 && e.response.status <= 599 || e.response.status === 429 : !1;
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
export {
|