@knocklabs/client 0.21.1 → 0.21.3
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 +20 -0
- 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/guide/client.js +1 -1
- package/dist/cjs/clients/guide/client.js.map +1 -1
- package/dist/cjs/clients/guide/helpers.js +1 -1
- package/dist/cjs/clients/guide/helpers.js.map +1 -1
- package/dist/cjs/clients/guide/types.js +2 -0
- package/dist/cjs/clients/guide/types.js.map +1 -0
- package/dist/cjs/helpers.js +1 -1
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/knock.js +2 -2
- package/dist/cjs/knock.js.map +1 -1
- package/dist/cjs/pageVisibility.js +2 -0
- package/dist/cjs/pageVisibility.js.map +1 -0
- package/dist/esm/api.mjs +27 -12
- package/dist/esm/api.mjs.map +1 -1
- package/dist/esm/clients/feed/feed.mjs +60 -87
- package/dist/esm/clients/feed/feed.mjs.map +1 -1
- package/dist/esm/clients/guide/client.mjs +346 -268
- package/dist/esm/clients/guide/client.mjs.map +1 -1
- package/dist/esm/clients/guide/helpers.mjs +50 -57
- package/dist/esm/clients/guide/helpers.mjs.map +1 -1
- package/dist/esm/clients/guide/types.mjs +13 -0
- package/dist/esm/clients/guide/types.mjs.map +1 -0
- package/dist/esm/helpers.mjs +19 -4
- package/dist/esm/helpers.mjs.map +1 -1
- package/dist/esm/index.mjs +14 -12
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/knock.mjs +31 -29
- package/dist/esm/knock.mjs.map +1 -1
- package/dist/esm/pageVisibility.mjs +31 -0
- package/dist/esm/pageVisibility.mjs.map +1 -0
- package/dist/types/api.d.ts +4 -0
- package/dist/types/api.d.ts.map +1 -1
- package/dist/types/clients/feed/feed.d.ts +1 -11
- package/dist/types/clients/feed/feed.d.ts.map +1 -1
- package/dist/types/clients/feed/interfaces.d.ts +0 -4
- package/dist/types/clients/feed/interfaces.d.ts.map +1 -1
- package/dist/types/clients/feed/utils.d.ts +0 -2
- package/dist/types/clients/feed/utils.d.ts.map +1 -1
- package/dist/types/clients/guide/client.d.ts +4 -1
- package/dist/types/clients/guide/client.d.ts.map +1 -1
- package/dist/types/clients/guide/helpers.d.ts +1 -7
- package/dist/types/clients/guide/helpers.d.ts.map +1 -1
- package/dist/types/clients/guide/index.d.ts +2 -1
- package/dist/types/clients/guide/index.d.ts.map +1 -1
- package/dist/types/clients/guide/types.d.ts +24 -0
- package/dist/types/clients/guide/types.d.ts.map +1 -1
- package/dist/types/helpers.d.ts +19 -0
- package/dist/types/helpers.d.ts.map +1 -1
- package/dist/types/interfaces.d.ts +2 -0
- package/dist/types/interfaces.d.ts.map +1 -1
- package/dist/types/knock.d.ts +1 -0
- package/dist/types/knock.d.ts.map +1 -1
- package/dist/types/pageVisibility.d.ts +22 -0
- package/dist/types/pageVisibility.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/api.ts +30 -0
- package/src/clients/feed/feed.ts +0 -73
- package/src/clients/feed/interfaces.ts +0 -7
- package/src/clients/guide/client.ts +182 -35
- package/src/clients/guide/helpers.ts +4 -12
- package/src/clients/guide/index.ts +3 -0
- package/src/clients/guide/types.ts +37 -0
- package/src/helpers.ts +39 -0
- package/src/interfaces.ts +2 -0
- package/src/knock.ts +4 -3
- package/src/pageVisibility.ts +70 -0
package/dist/esm/api.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
var n = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var s = (
|
|
4
|
-
import
|
|
5
|
-
import
|
|
2
|
+
var o = (i, e, t) => e in i ? n(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
|
|
3
|
+
var s = (i, e, t) => o(i, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import c from "axios";
|
|
5
|
+
import r from "axios-retry";
|
|
6
6
|
import { Socket as u } from "phoenix";
|
|
7
|
-
|
|
7
|
+
import { exponentialBackoffFullJitter as a } from "./helpers.mjs";
|
|
8
|
+
import { PageVisibilityManager as h } from "./pageVisibility.mjs";
|
|
9
|
+
class b {
|
|
8
10
|
constructor(e) {
|
|
9
11
|
s(this, "host");
|
|
10
12
|
s(this, "apiKey");
|
|
@@ -12,7 +14,8 @@ class k {
|
|
|
12
14
|
s(this, "branch");
|
|
13
15
|
s(this, "axiosClient");
|
|
14
16
|
s(this, "socket");
|
|
15
|
-
this
|
|
17
|
+
s(this, "pageVisibility");
|
|
18
|
+
this.host = e.host, this.apiKey = e.apiKey, this.userToken = e.userToken || null, this.branch = e.branch || null, this.axiosClient = c.create({
|
|
16
19
|
baseURL: this.host,
|
|
17
20
|
headers: {
|
|
18
21
|
Accept: "application/json",
|
|
@@ -27,11 +30,19 @@ class k {
|
|
|
27
30
|
user_token: this.userToken,
|
|
28
31
|
api_key: this.apiKey,
|
|
29
32
|
branch_slug: this.branch
|
|
30
|
-
}
|
|
31
|
-
|
|
33
|
+
},
|
|
34
|
+
reconnectAfterMs: (t) => a(t, {
|
|
35
|
+
baseDelayMs: 1e3,
|
|
36
|
+
maxDelayMs: 3e4
|
|
37
|
+
}),
|
|
38
|
+
rejoinAfterMs: (t) => a(t, {
|
|
39
|
+
baseDelayMs: 1e3,
|
|
40
|
+
maxDelayMs: 6e4
|
|
41
|
+
})
|
|
42
|
+
}), e.disconnectOnPageHidden !== !1 && (this.pageVisibility = new h(this.socket))), r(this.axiosClient, {
|
|
32
43
|
retries: 3,
|
|
33
44
|
retryCondition: this.canRetryRequest,
|
|
34
|
-
retryDelay:
|
|
45
|
+
retryDelay: r.exponentialDelay
|
|
35
46
|
});
|
|
36
47
|
}
|
|
37
48
|
async makeRequest(e) {
|
|
@@ -52,14 +63,18 @@ class k {
|
|
|
52
63
|
};
|
|
53
64
|
}
|
|
54
65
|
}
|
|
66
|
+
teardown() {
|
|
67
|
+
var e, t;
|
|
68
|
+
(e = this.pageVisibility) == null || e.teardown(), (t = this.socket) != null && t.isConnected() && this.socket.disconnect();
|
|
69
|
+
}
|
|
55
70
|
canRetryRequest(e) {
|
|
56
|
-
return
|
|
71
|
+
return r.isNetworkError(e) ? !0 : e.response ? e.response.status >= 500 && e.response.status <= 599 || e.response.status === 429 : !1;
|
|
57
72
|
}
|
|
58
73
|
getKnockClientHeader() {
|
|
59
|
-
return "Knock/ClientJS 0.21.
|
|
74
|
+
return "Knock/ClientJS 0.21.3";
|
|
60
75
|
}
|
|
61
76
|
}
|
|
62
77
|
export {
|
|
63
|
-
|
|
78
|
+
b as default
|
|
64
79
|
};
|
|
65
80
|
//# sourceMappingURL=api.mjs.map
|
package/dist/esm/api.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.mjs","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 branch?: string;\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 branch: 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 this.branch = options.branch || null;\n\n // Create a retryable axios client\n this.axiosClient = axios.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 \"X-Knock-Client\": this.getKnockClientHeader(),\n \"X-Knock-Branch\": this.branch,\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 branch_slug: this.branch,\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 private getKnockClientHeader() {\n // Note: we're following format used in our Stainless SDKs:\n // https://github.com/knocklabs/knock-node/blob/main/src/client.ts#L335\n // If we add the env var to turbo.json, it caches it so the version\n // never actually updates.\n // eslint-disable-next-line turbo/no-undeclared-env-vars\n return `Knock/ClientJS ${process.env.CLIENT_PACKAGE_VERSION}`;\n }\n}\n\nexport default ApiClient;\n"],"names":["ApiClient","options","__publicField","axios","Socket","axiosRetry","req","result","e","error"],"mappings":"
|
|
1
|
+
{"version":3,"file":"api.mjs","sources":["../../src/api.ts"],"sourcesContent":["import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport axiosRetry from \"axios-retry\";\nimport { Socket } from \"phoenix\";\n\nimport { exponentialBackoffFullJitter } from \"./helpers\";\nimport { PageVisibilityManager } from \"./pageVisibility\";\n\ntype ApiClientOptions = {\n host: string;\n apiKey: string;\n userToken: string | undefined;\n branch?: string;\n /** Automatically disconnect the socket when the page is hidden and reconnect when visible. Defaults to `true`. */\n disconnectOnPageHidden?: boolean;\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 branch: string | null;\n private axiosClient: AxiosInstance;\n\n public socket: Socket | undefined;\n private pageVisibility: PageVisibilityManager | undefined;\n\n constructor(options: ApiClientOptions) {\n this.host = options.host;\n this.apiKey = options.apiKey;\n this.userToken = options.userToken || null;\n this.branch = options.branch || null;\n\n // Create a retryable axios client\n this.axiosClient = axios.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 \"X-Knock-Client\": this.getKnockClientHeader(),\n \"X-Knock-Branch\": this.branch,\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 branch_slug: this.branch,\n },\n reconnectAfterMs: (tries: number) => {\n return exponentialBackoffFullJitter(tries, {\n baseDelayMs: 1000,\n maxDelayMs: 30_000,\n });\n },\n rejoinAfterMs: (tries: number) => {\n return exponentialBackoffFullJitter(tries, {\n baseDelayMs: 1000,\n maxDelayMs: 60_000,\n });\n },\n });\n\n if (options.disconnectOnPageHidden !== false) {\n this.pageVisibility = new PageVisibilityManager(this.socket);\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 teardown() {\n this.pageVisibility?.teardown();\n\n if (this.socket?.isConnected()) {\n this.socket.disconnect();\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 private getKnockClientHeader() {\n // Note: we're following format used in our Stainless SDKs:\n // https://github.com/knocklabs/knock-node/blob/main/src/client.ts#L335\n // If we add the env var to turbo.json, it caches it so the version\n // never actually updates.\n // eslint-disable-next-line turbo/no-undeclared-env-vars\n return `Knock/ClientJS ${process.env.CLIENT_PACKAGE_VERSION}`;\n }\n}\n\nexport default ApiClient;\n"],"names":["ApiClient","options","__publicField","axios","Socket","tries","exponentialBackoffFullJitter","PageVisibilityManager","axiosRetry","req","result","e","_a","_b","error"],"mappings":";;;;;;;;AAyBA,MAAMA,EAAU;AAAA,EAUd,YAAYC,GAA2B;AAT/B,IAAAC,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAED,IAAAA,EAAA;AACC,IAAAA,EAAA;AAGN,SAAK,OAAOD,EAAQ,MACpB,KAAK,SAASA,EAAQ,QACjB,KAAA,YAAYA,EAAQ,aAAa,MACjC,KAAA,SAASA,EAAQ,UAAU,MAG3B,KAAA,cAAcE,EAAM,OAAO;AAAA,MAC9B,SAAS,KAAK;AAAA,MACd,SAAS;AAAA,QACP,QAAQ;AAAA,QACR,gBAAgB;AAAA,QAChB,eAAe,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KAAK,OAAO,WAAW;AAAA,EACzB;AAAA,EAGM,gBAAgBC,GAAmB;AAErC,WAAAN,EAAW,eAAeM,CAAK,IAC1B,KAGJA,EAAM,WAMPA,EAAM,SAAS,UAAU,OAAOA,EAAM,SAAS,UAAU,OAKzDA,EAAM,SAAS,WAAW,MATrB;AAAA,EAaF;AAAA,EAGD,uBAAuB;AAMtB,WAAA;AAAA,EAAoD;AAE/D;"}
|
|
@@ -1,38 +1,34 @@
|
|
|
1
1
|
var p = Object.defineProperty;
|
|
2
|
-
var g = (
|
|
3
|
-
var
|
|
2
|
+
var g = (l, t, e) => t in l ? p(l, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : l[t] = e;
|
|
3
|
+
var u = (l, t, e) => g(l, typeof t != "symbol" ? t + "" : t, e);
|
|
4
4
|
import k from "eventemitter2";
|
|
5
5
|
import { nanoid as _ } from "nanoid";
|
|
6
|
-
import { isValidUuid as
|
|
7
|
-
import { isRequestInFlight as
|
|
6
|
+
import { isValidUuid as S } from "../../helpers.mjs";
|
|
7
|
+
import { isRequestInFlight as v, NetworkStatus as m } from "../../networkStatus.mjs";
|
|
8
8
|
import { SocketEventType as y } from "./socket-manager.mjs";
|
|
9
|
-
import
|
|
10
|
-
import { mergeDateRangeParams as f, getFormattedTriggerData as
|
|
11
|
-
const
|
|
9
|
+
import I from "./store.mjs";
|
|
10
|
+
import { mergeDateRangeParams as f, getFormattedTriggerData as b } from "./utils.mjs";
|
|
11
|
+
const U = {
|
|
12
12
|
archived: "exclude",
|
|
13
13
|
mode: "compact"
|
|
14
|
-
},
|
|
14
|
+
}, w = "client_";
|
|
15
15
|
class P {
|
|
16
16
|
constructor(t, e, s, n) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
c(this, "hasSubscribedToRealTimeUpdates", !1);
|
|
26
|
-
c(this, "visibilityChangeHandler", () => {
|
|
27
|
-
});
|
|
28
|
-
c(this, "visibilityChangeListenerConnected", !1);
|
|
17
|
+
u(this, "defaultOptions");
|
|
18
|
+
u(this, "referenceId");
|
|
19
|
+
u(this, "unsubscribeFromSocketEvents");
|
|
20
|
+
u(this, "socketManager");
|
|
21
|
+
u(this, "userFeedId");
|
|
22
|
+
u(this, "broadcaster");
|
|
23
|
+
u(this, "broadcastChannel");
|
|
24
|
+
u(this, "hasSubscribedToRealTimeUpdates", !1);
|
|
29
25
|
// The raw store instance, used for binding in React and other environments
|
|
30
|
-
|
|
31
|
-
this.knock = t, this.feedId = e, (!e || !
|
|
26
|
+
u(this, "store");
|
|
27
|
+
this.knock = t, this.feedId = e, (!e || !S(e)) && this.knock.log(
|
|
32
28
|
"[Feed] Invalid or missing feedId provided to the Feed constructor. The feed should be a UUID of an in-app feed channel (`in_app_feed`) found in the Knock dashboard. Please provide a valid feedId to the Feed constructor.",
|
|
33
29
|
!0
|
|
34
|
-
), this.feedId = e, this.userFeedId = this.buildUserFeedId(), this.referenceId = w + _(), this.socketManager = n, this.store =
|
|
35
|
-
...
|
|
30
|
+
), this.feedId = e, this.userFeedId = this.buildUserFeedId(), this.referenceId = w + _(), this.socketManager = n, this.store = I(), this.broadcaster = new k({ wildcard: !0, delimiter: "." }), this.defaultOptions = {
|
|
31
|
+
...U,
|
|
36
32
|
...f(s)
|
|
37
33
|
}, this.knock.log(`[Feed] Initialized a feed on channel ${e}`), this.initializeRealtimeConnection(), this.setupBroadcastChannel();
|
|
38
34
|
}
|
|
@@ -48,7 +44,7 @@ class P {
|
|
|
48
44
|
*/
|
|
49
45
|
teardown() {
|
|
50
46
|
var t;
|
|
51
|
-
this.knock.log("[Feed] Tearing down feed instance"), (t = this.socketManager) == null || t.leave(this), this.
|
|
47
|
+
this.knock.log("[Feed] Tearing down feed instance"), (t = this.socketManager) == null || t.leave(this), this.broadcastChannel && this.broadcastChannel.close();
|
|
52
48
|
}
|
|
53
49
|
/** Tears down an instance and removes it entirely from the feed manager */
|
|
54
50
|
dispose() {
|
|
@@ -100,8 +96,8 @@ class P {
|
|
|
100
96
|
const a = { seen_at: (/* @__PURE__ */ new Date()).toISOString() }, i = e.map((d) => d.id);
|
|
101
97
|
s.setItemAttrs(i, a);
|
|
102
98
|
}
|
|
103
|
-
const
|
|
104
|
-
return this.emitEvent("all_seen", e),
|
|
99
|
+
const r = await this.makeBulkStatusUpdate("seen");
|
|
100
|
+
return this.emitEvent("all_seen", e), r;
|
|
105
101
|
}
|
|
106
102
|
async markAsUnseen(t) {
|
|
107
103
|
return this.optimisticallyPerformStatusUpdate(
|
|
@@ -133,8 +129,8 @@ class P {
|
|
|
133
129
|
const a = { read_at: (/* @__PURE__ */ new Date()).toISOString() }, i = e.map((d) => d.id);
|
|
134
130
|
s.setItemAttrs(i, a);
|
|
135
131
|
}
|
|
136
|
-
const
|
|
137
|
-
return this.emitEvent("all_read", e),
|
|
132
|
+
const r = await this.makeBulkStatusUpdate("read");
|
|
133
|
+
return this.emitEvent("all_read", e), r;
|
|
138
134
|
}
|
|
139
135
|
async markAsUnread(t) {
|
|
140
136
|
return this.optimisticallyPerformStatusUpdate(
|
|
@@ -165,25 +161,25 @@ class P {
|
|
|
165
161
|
TODO: how do we handle rollbacks?
|
|
166
162
|
*/
|
|
167
163
|
async markAsArchived(t) {
|
|
168
|
-
const e = this.store.getState(), s = this.defaultOptions.archived === "exclude", n = Array.isArray(t) ? t : [t],
|
|
164
|
+
const e = this.store.getState(), s = this.defaultOptions.archived === "exclude", n = Array.isArray(t) ? t : [t], r = n.map((a) => a.id);
|
|
169
165
|
if (s) {
|
|
170
|
-
const a = n.filter((
|
|
166
|
+
const a = n.filter((o) => !o.seen_at).length, i = n.filter((o) => !o.read_at).length, d = {
|
|
171
167
|
...e.metadata,
|
|
172
168
|
// Ensure that the counts don't ever go below 0 on archiving where the client state
|
|
173
169
|
// gets out of sync with the server state
|
|
174
170
|
total_count: Math.max(0, e.metadata.total_count - n.length),
|
|
175
171
|
unseen_count: Math.max(0, e.metadata.unseen_count - a),
|
|
176
172
|
unread_count: Math.max(0, e.metadata.unread_count - i)
|
|
177
|
-
},
|
|
178
|
-
(
|
|
173
|
+
}, c = e.items.filter(
|
|
174
|
+
(o) => !r.includes(o.id)
|
|
179
175
|
);
|
|
180
176
|
e.setResult({
|
|
181
|
-
entries:
|
|
177
|
+
entries: c,
|
|
182
178
|
meta: d,
|
|
183
179
|
page_info: e.pageInfo
|
|
184
180
|
});
|
|
185
181
|
} else
|
|
186
|
-
e.setItemAttrs(
|
|
182
|
+
e.setItemAttrs(r, { archived_at: (/* @__PURE__ */ new Date()).toISOString() });
|
|
187
183
|
return this.makeStatusUpdate(t, "archived");
|
|
188
184
|
}
|
|
189
185
|
async markAllAsArchived() {
|
|
@@ -191,8 +187,8 @@ class P {
|
|
|
191
187
|
if (this.defaultOptions.archived === "exclude")
|
|
192
188
|
e.resetStore();
|
|
193
189
|
else {
|
|
194
|
-
const
|
|
195
|
-
e.setItemAttrs(
|
|
190
|
+
const r = t.map((a) => a.id);
|
|
191
|
+
e.setItemAttrs(r, { archived_at: (/* @__PURE__ */ new Date()).toISOString() });
|
|
196
192
|
}
|
|
197
193
|
const n = await this.makeBulkStatusUpdate("archive");
|
|
198
194
|
return this.emitEvent("all_archived", t), n;
|
|
@@ -202,7 +198,7 @@ class P {
|
|
|
202
198
|
if (e.setItemAttrs(n, {
|
|
203
199
|
archived_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
204
200
|
}), this.defaultOptions.archived === "exclude") {
|
|
205
|
-
const i = t.filter((
|
|
201
|
+
const i = t.filter((c) => !n.includes(c.id)), d = {
|
|
206
202
|
...e.metadata,
|
|
207
203
|
total_count: i.length,
|
|
208
204
|
unread_count: 0
|
|
@@ -218,18 +214,18 @@ class P {
|
|
|
218
214
|
async markAsUnarchived(t) {
|
|
219
215
|
const e = this.store.getState(), s = Array.isArray(t) ? t : [t], n = s.map((a) => a.id);
|
|
220
216
|
if (this.defaultOptions.archived === "only") {
|
|
221
|
-
const a = s.filter((
|
|
217
|
+
const a = s.filter((o) => !o.seen_at).length, i = s.filter((o) => !o.read_at).length, d = {
|
|
222
218
|
...e.metadata,
|
|
223
219
|
// Ensure that the counts don't ever go below 0 on unarchiving where the client state
|
|
224
220
|
// gets out of sync with the server state
|
|
225
221
|
total_count: Math.max(0, e.metadata.total_count - s.length),
|
|
226
222
|
unseen_count: Math.max(0, e.metadata.unseen_count - a),
|
|
227
223
|
unread_count: Math.max(0, e.metadata.unread_count - i)
|
|
228
|
-
},
|
|
229
|
-
(
|
|
224
|
+
}, c = e.items.filter(
|
|
225
|
+
(o) => !n.includes(o.id)
|
|
230
226
|
);
|
|
231
227
|
e.setResult({
|
|
232
|
-
entries:
|
|
228
|
+
entries: c,
|
|
233
229
|
meta: d,
|
|
234
230
|
page_info: e.pageInfo
|
|
235
231
|
});
|
|
@@ -246,28 +242,26 @@ class P {
|
|
|
246
242
|
this.knock.log("[Feed] User is not authenticated, skipping fetch");
|
|
247
243
|
return;
|
|
248
244
|
}
|
|
249
|
-
if (
|
|
245
|
+
if (v(e)) {
|
|
250
246
|
this.knock.log("[Feed] Request is in flight, skipping fetch");
|
|
251
247
|
return;
|
|
252
248
|
}
|
|
253
249
|
s.setNetworkStatus(t.__loadingType ?? m.loading);
|
|
254
|
-
const n =
|
|
250
|
+
const n = b({
|
|
255
251
|
...this.defaultOptions,
|
|
256
252
|
...t
|
|
257
|
-
}),
|
|
253
|
+
}), r = {
|
|
258
254
|
...this.defaultOptions,
|
|
259
255
|
...f(t),
|
|
260
256
|
trigger_data: n,
|
|
261
257
|
// Unset options that should not be sent to the API
|
|
262
258
|
__loadingType: void 0,
|
|
263
259
|
__fetchSource: void 0,
|
|
264
|
-
__experimentalCrossBrowserUpdates: void 0
|
|
265
|
-
auto_manage_socket_connection: void 0,
|
|
266
|
-
auto_manage_socket_connection_delay: void 0
|
|
260
|
+
__experimentalCrossBrowserUpdates: void 0
|
|
267
261
|
}, a = await this.knock.client().makeRequest({
|
|
268
262
|
method: "GET",
|
|
269
263
|
url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,
|
|
270
|
-
params:
|
|
264
|
+
params: r
|
|
271
265
|
});
|
|
272
266
|
if (a.statusCode === "error" || !a.body)
|
|
273
267
|
return s.setNetworkStatus(m.error), {
|
|
@@ -280,20 +274,20 @@ class P {
|
|
|
280
274
|
page_info: a.body.page_info
|
|
281
275
|
};
|
|
282
276
|
if (t.before) {
|
|
283
|
-
const
|
|
284
|
-
s.setResult(i,
|
|
277
|
+
const o = { shouldSetPage: !1, shouldAppend: !0 };
|
|
278
|
+
s.setResult(i, o);
|
|
285
279
|
} else if (t.after) {
|
|
286
|
-
const
|
|
287
|
-
s.setResult(i,
|
|
280
|
+
const o = { shouldSetPage: !0, shouldAppend: !0 };
|
|
281
|
+
s.setResult(i, o);
|
|
288
282
|
} else
|
|
289
283
|
s.setResult(i);
|
|
290
284
|
this.broadcast("messages.new", i);
|
|
291
|
-
const d = t.__fetchSource === "socket" ? "items.received.realtime" : "items.received.page",
|
|
285
|
+
const d = t.__fetchSource === "socket" ? "items.received.realtime" : "items.received.page", c = {
|
|
292
286
|
items: i.entries,
|
|
293
287
|
metadata: i.meta,
|
|
294
288
|
event: d
|
|
295
289
|
};
|
|
296
|
-
return this.broadcast(
|
|
290
|
+
return this.broadcast(c.event, c), { data: i, status: a.statusCode };
|
|
297
291
|
}
|
|
298
292
|
async fetchNextPage(t = {}) {
|
|
299
293
|
const { pageInfo: e } = this.store.getState();
|
|
@@ -313,16 +307,16 @@ class P {
|
|
|
313
307
|
async onNewMessageReceived({ data: t }) {
|
|
314
308
|
var a;
|
|
315
309
|
this.knock.log("[Feed] Received new real-time message");
|
|
316
|
-
const { items: e, ...s } = this.store.getState(), n = e[0],
|
|
317
|
-
|
|
310
|
+
const { items: e, ...s } = this.store.getState(), n = e[0], r = (a = t[this.referenceId]) == null ? void 0 : a.metadata;
|
|
311
|
+
r && s.setMetadata(r), this.fetch({ before: n == null ? void 0 : n.__cursor, __fetchSource: "socket" });
|
|
318
312
|
}
|
|
319
313
|
buildUserFeedId() {
|
|
320
314
|
return `${this.feedId}:${this.knock.userId}`;
|
|
321
315
|
}
|
|
322
316
|
optimisticallyPerformStatusUpdate(t, e, s, n) {
|
|
323
|
-
const
|
|
317
|
+
const r = this.store.getState(), a = Array.isArray(t) ? t : [t], i = a.map((d) => d.id);
|
|
324
318
|
if (n) {
|
|
325
|
-
const { metadata: d } =
|
|
319
|
+
const { metadata: d } = r, c = a.filter((h) => {
|
|
326
320
|
switch (e) {
|
|
327
321
|
case "seen":
|
|
328
322
|
return h.seen_at === null;
|
|
@@ -336,17 +330,17 @@ class P {
|
|
|
336
330
|
default:
|
|
337
331
|
return !0;
|
|
338
332
|
}
|
|
339
|
-
}),
|
|
340
|
-
|
|
333
|
+
}), o = e.startsWith("un") ? c.length : -c.length;
|
|
334
|
+
r.setMetadata({
|
|
341
335
|
...d,
|
|
342
|
-
[n]: Math.max(0, d[n] +
|
|
336
|
+
[n]: Math.max(0, d[n] + o)
|
|
343
337
|
});
|
|
344
338
|
}
|
|
345
|
-
|
|
339
|
+
r.setItemAttrs(i, s);
|
|
346
340
|
}
|
|
347
341
|
async makeStatusUpdate(t, e, s) {
|
|
348
|
-
const n = Array.isArray(t) ? t : [t],
|
|
349
|
-
|
|
342
|
+
const n = Array.isArray(t) ? t : [t], r = n.map((i) => i.id), a = await this.knock.messages.batchUpdateStatuses(
|
|
343
|
+
r,
|
|
350
344
|
e,
|
|
351
345
|
{ metadata: s }
|
|
352
346
|
);
|
|
@@ -398,7 +392,7 @@ class P {
|
|
|
398
392
|
}
|
|
399
393
|
initializeRealtimeConnection() {
|
|
400
394
|
var t;
|
|
401
|
-
this.socketManager &&
|
|
395
|
+
this.socketManager && this.hasSubscribedToRealTimeUpdates && this.knock.isAuthenticated() && (this.unsubscribeFromSocketEvents = (t = this.socketManager) == null ? void 0 : t.join(this));
|
|
402
396
|
}
|
|
403
397
|
async handleSocketEvent(t) {
|
|
404
398
|
switch (t.event) {
|
|
@@ -411,30 +405,9 @@ class P {
|
|
|
411
405
|
}
|
|
412
406
|
}
|
|
413
407
|
}
|
|
414
|
-
/**
|
|
415
|
-
* Listen for changes to document visibility and automatically disconnect
|
|
416
|
-
* or reconnect the socket after a delay
|
|
417
|
-
*/
|
|
418
|
-
setUpVisibilityListeners() {
|
|
419
|
-
typeof document > "u" || this.visibilityChangeListenerConnected || (this.visibilityChangeHandler = this.handleVisibilityChange.bind(this), this.visibilityChangeListenerConnected = !0, document.addEventListener("visibilitychange", this.visibilityChangeHandler));
|
|
420
|
-
}
|
|
421
|
-
tearDownVisibilityListeners() {
|
|
422
|
-
typeof document > "u" || (document.removeEventListener(
|
|
423
|
-
"visibilitychange",
|
|
424
|
-
this.visibilityChangeHandler
|
|
425
|
-
), this.visibilityChangeListenerConnected = !1);
|
|
426
|
-
}
|
|
427
408
|
emitEvent(t, e) {
|
|
428
409
|
this.broadcaster.emit(`items.${t}`, { items: e }), this.broadcaster.emit(`items:${t}`, { items: e }), this.broadcastOverChannel(`items:${t}`, { items: e });
|
|
429
410
|
}
|
|
430
|
-
handleVisibilityChange() {
|
|
431
|
-
var s, n;
|
|
432
|
-
const t = this.defaultOptions.auto_manage_socket_connection_delay ?? U, e = this.knock.client();
|
|
433
|
-
document.visibilityState === "hidden" ? this.disconnectTimer = setTimeout(() => {
|
|
434
|
-
var o;
|
|
435
|
-
(o = e.socket) == null || o.disconnect(), this.disconnectTimer = null;
|
|
436
|
-
}, t) : document.visibilityState === "visible" && (this.disconnectTimer && (clearTimeout(this.disconnectTimer), this.disconnectTimer = null), (s = e.socket) != null && s.isConnected() || (n = e.socket) == null || n.connect());
|
|
437
|
-
}
|
|
438
411
|
}
|
|
439
412
|
export {
|
|
440
413
|
P as default
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"feed.mjs","sources":["../../../../src/clients/feed/feed.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport EventEmitter from \"eventemitter2\";\nimport { nanoid } from \"nanoid\";\n\nimport { isValidUuid } from \"../../helpers\";\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 FetchFeedOptionsForRequest,\n} from \"./interfaces\";\nimport {\n FeedSocketManager,\n SocketEventPayload,\n SocketEventType,\n} from \"./socket-manager\";\nimport createStore, { FeedStore } from \"./store\";\nimport {\n BindableFeedEvent,\n FeedEvent,\n FeedEventCallback,\n FeedEventPayload,\n FeedItemOrItems,\n FeedMessagesReceivedPayload,\n FeedRealTimeCallback,\n} from \"./types\";\nimport { getFormattedTriggerData, mergeDateRangeParams } from \"./utils\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\" | \"mode\"> = {\n archived: \"exclude\",\n mode: \"compact\",\n};\n\nconst DEFAULT_DISCONNECT_DELAY = 2000;\n\nconst CLIENT_REF_ID_PREFIX = \"client_\";\n\nclass Feed {\n public readonly defaultOptions: FeedClientOptions;\n public readonly referenceId: string;\n public unsubscribeFromSocketEvents: (() => void) | undefined = undefined;\n private socketManager: FeedSocketManager | undefined;\n private userFeedId: string;\n private broadcaster: EventEmitter;\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: FeedStore;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n socketManager: FeedSocketManager | undefined,\n ) {\n if (!feedId || !isValidUuid(feedId)) {\n this.knock.log(\n \"[Feed] Invalid or missing feedId provided to the Feed constructor. The feed should be a UUID of an in-app feed channel (`in_app_feed`) found in the Knock dashboard. Please provide a valid feedId to the Feed constructor.\",\n true,\n );\n }\n\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.referenceId = CLIENT_REF_ID_PREFIX + nanoid();\n this.socketManager = socketManager;\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = {\n ...feedClientDefaults,\n ...mergeDateRangeParams(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(socketManager?: FeedSocketManager) {\n this.socketManager = socketManager;\n\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 this.socketManager?.leave(this);\n\n this.tearDownVisibilityListeners();\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 this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\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 const state = this.store.getState();\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"only\";\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 unarchiving 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 unarchived 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 this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\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 // trigger_data should be a JSON string for the API\n // this function will format the trigger data if it's an object\n // https://docs.knock.app/reference#get-feed\n const formattedTriggerData = getFormattedTriggerData({\n ...this.defaultOptions,\n ...options,\n });\n\n // Always include the default params, if they have been set\n const queryParams: FetchFeedOptionsForRequest = {\n ...this.defaultOptions,\n ...mergeDateRangeParams(options),\n trigger_data: formattedTriggerData,\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(options: FetchFeedOptions = {}) {\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 ...options,\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n get socketChannelTopic(): string {\n return `feeds:${this.userFeedId}`;\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({ data }: 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\n // Optimistically set the badge counts\n const metadata = data[this.referenceId]?.metadata;\n if (metadata) {\n state.setMetadata(metadata);\n }\n\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 // This 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 // In server environments we might not have a socket connection\n if (!this.socketManager) return;\n\n if (this.defaultOptions.auto_manage_socket_connection) {\n this.setUpVisibilityListeners();\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 this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\n }\n }\n\n async handleSocketEvent(payload: SocketEventPayload) {\n switch (payload.event) {\n case SocketEventType.NewMessage:\n this.onNewMessageReceived(payload);\n return;\n default: {\n const _exhaustiveCheck: never = payload.event;\n return;\n }\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 setUpVisibilityListeners() {\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 tearDownVisibilityListeners() {\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 client.socket?.connect();\n }\n }\n }\n}\n\nexport default Feed;\n"],"names":["feedClientDefaults","DEFAULT_DISCONNECT_DELAY","CLIENT_REF_ID_PREFIX","Feed","knock","feedId","options","socketManager","__publicField","isValidUuid","nanoid","createStore","EventEmitter","mergeDateRangeParams","_a","eventName","callback","itemOrItems","now","metadata","items","state","attrs","itemIds","item","result","shouldOptimisticallyRemoveItems","unseenCount","i","unreadCount","updatedMetadata","entriesToSet","remainingItems","networkStatus","isRequestInFlight","NetworkStatus","formattedTriggerData","getFormattedTriggerData","queryParams","response","opts","feedEventType","eventPayload","pageInfo","data","currentHead","type","badgeCountAttr","normalizedItems","itemsToUpdate","direction","status","e","payload","stringifiedPayload","SocketEventType","disconnectDelay","client","_b"],"mappings":";;;;;;;;;;AAsCA,MAAMA,IAAmE;AAAA,EACvE,UAAU;AAAA,EACV,MAAM;AACR,GAEMC,IAA2B,KAE3BC,IAAuB;AAE7B,MAAMC,EAAK;AAAA,EAgBT,YACWC,GACAC,GACTC,GACAC,GACA;AApB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;AAAA;AAAA;AAAA,EAOM,2BAA2B;AACjC,IACE,OAAO,WAAa,OACpB,KAAK,sCAKP,KAAK,0BAA0B,KAAK,uBAAuB,KAAK,IAAI,GACpE,KAAK,oCAAoC,IAChC,SAAA,iBAAiB,oBAAoB,KAAK,uBAAuB;AAAA,EAAA;AAAA,EAGpE,8BAA8B;AAChC,IAAA,OAAO,WAAa,QAEf,SAAA;AAAA,MACP;AAAA,MACA,KAAK;AAAA,IACP,GACA,KAAK,oCAAoC;AAAA,EAAA;AAAA,EAGnC,UACNP,GAQA1B,GACA;AAEA,SAAK,YAAY,KAAK,SAAS0B,CAAI,IAAI,EAAE,OAAA1B,GAAO,GAChD,KAAK,YAAY,KAAK,SAAS0B,CAAI,IAAI,EAAE,OAAA1B,GAAO,GAEhD,KAAK,qBAAqB,SAAS0B,CAAI,IAAI,EAAE,OAAA1B,GAAO;AAAA,EAAA;AAAA,EAG9C,yBAAyB;;AACzB,UAAAoC,IACJ,KAAK,eAAe,uCACpBvD,GAEIwD,IAAS,KAAK,MAAM,OAAO;AAE7B,IAAA,SAAS,oBAAoB,WAE1B,KAAA,kBAAkB,WAAW,MAAM;;AACtC,OAAA3C,IAAA2C,EAAO,WAAP,QAAA3C,EAAe,cACf,KAAK,kBAAkB;AAAA,OACtB0C,CAAe,IACT,SAAS,oBAAoB,cAGlC,KAAK,oBACP,aAAa,KAAK,eAAe,GACjC,KAAK,kBAAkB,QAIpB1C,IAAA2C,EAAO,WAAP,QAAA3C,EAAe,kBAClB4C,IAAAD,EAAO,WAAP,QAAAC,EAAe;AAAA,EAEnB;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"feed.mjs","sources":["../../../../src/clients/feed/feed.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport EventEmitter from \"eventemitter2\";\nimport { nanoid } from \"nanoid\";\n\nimport { isValidUuid } from \"../../helpers\";\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 FetchFeedOptionsForRequest,\n} from \"./interfaces\";\nimport {\n FeedSocketManager,\n SocketEventPayload,\n SocketEventType,\n} from \"./socket-manager\";\nimport createStore, { FeedStore } from \"./store\";\nimport {\n BindableFeedEvent,\n FeedEvent,\n FeedEventCallback,\n FeedEventPayload,\n FeedItemOrItems,\n FeedMessagesReceivedPayload,\n FeedRealTimeCallback,\n} from \"./types\";\nimport { getFormattedTriggerData, mergeDateRangeParams } from \"./utils\";\n\n// Default options to apply\nconst feedClientDefaults: Pick<FeedClientOptions, \"archived\" | \"mode\"> = {\n archived: \"exclude\",\n mode: \"compact\",\n};\n\nconst CLIENT_REF_ID_PREFIX = \"client_\";\n\nclass Feed {\n public readonly defaultOptions: FeedClientOptions;\n public readonly referenceId: string;\n public unsubscribeFromSocketEvents: (() => void) | undefined = undefined;\n private socketManager: FeedSocketManager | undefined;\n private userFeedId: string;\n private broadcaster: EventEmitter;\n private broadcastChannel!: BroadcastChannel | null;\n private hasSubscribedToRealTimeUpdates: boolean = false;\n\n // The raw store instance, used for binding in React and other environments\n public store: FeedStore;\n\n constructor(\n readonly knock: Knock,\n readonly feedId: string,\n options: FeedClientOptions,\n socketManager: FeedSocketManager | undefined,\n ) {\n if (!feedId || !isValidUuid(feedId)) {\n this.knock.log(\n \"[Feed] Invalid or missing feedId provided to the Feed constructor. The feed should be a UUID of an in-app feed channel (`in_app_feed`) found in the Knock dashboard. Please provide a valid feedId to the Feed constructor.\",\n true,\n );\n }\n\n this.feedId = feedId;\n this.userFeedId = this.buildUserFeedId();\n this.referenceId = CLIENT_REF_ID_PREFIX + nanoid();\n this.socketManager = socketManager;\n this.store = createStore();\n this.broadcaster = new EventEmitter({ wildcard: true, delimiter: \".\" });\n this.defaultOptions = {\n ...feedClientDefaults,\n ...mergeDateRangeParams(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(socketManager?: FeedSocketManager) {\n this.socketManager = socketManager;\n\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 this.socketManager?.leave(this);\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 this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\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 const state = this.store.getState();\n\n const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];\n\n const itemIds: string[] = items.map((item) => item.id);\n\n const shouldOptimisticallyRemoveItems =\n this.defaultOptions.archived === \"only\";\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 unarchiving 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 unarchived 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 this.optimisticallyPerformStatusUpdate(itemOrItems, \"unarchived\", {\n archived_at: null,\n });\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 // trigger_data should be a JSON string for the API\n // this function will format the trigger data if it's an object\n // https://docs.knock.app/reference#get-feed\n const formattedTriggerData = getFormattedTriggerData({\n ...this.defaultOptions,\n ...options,\n });\n\n // Always include the default params, if they have been set\n const queryParams: FetchFeedOptionsForRequest = {\n ...this.defaultOptions,\n ...mergeDateRangeParams(options),\n trigger_data: formattedTriggerData,\n // Unset options that should not be sent to the API\n __loadingType: undefined,\n __fetchSource: undefined,\n __experimentalCrossBrowserUpdates: 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(options: FetchFeedOptions = {}) {\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 ...options,\n after: pageInfo.after,\n __loadingType: NetworkStatus.fetchMore,\n });\n }\n\n get socketChannelTopic(): string {\n return `feeds:${this.userFeedId}`;\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({ data }: 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\n // Optimistically set the badge counts\n const metadata = data[this.referenceId]?.metadata;\n if (metadata) {\n state.setMetadata(metadata);\n }\n\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 // This 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 // In server environments we might not have a socket connection\n if (!this.socketManager) return;\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 this.unsubscribeFromSocketEvents = this.socketManager?.join(this);\n }\n }\n\n async handleSocketEvent(payload: SocketEventPayload) {\n switch (payload.event) {\n case SocketEventType.NewMessage:\n this.onNewMessageReceived(payload);\n return;\n default: {\n const _exhaustiveCheck: never = payload.event;\n return;\n }\n }\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\nexport default Feed;\n"],"names":["feedClientDefaults","CLIENT_REF_ID_PREFIX","Feed","knock","feedId","options","socketManager","__publicField","isValidUuid","nanoid","createStore","EventEmitter","mergeDateRangeParams","_a","eventName","callback","itemOrItems","now","metadata","items","state","attrs","itemIds","item","result","shouldOptimisticallyRemoveItems","unseenCount","i","unreadCount","updatedMetadata","entriesToSet","remainingItems","networkStatus","isRequestInFlight","NetworkStatus","formattedTriggerData","getFormattedTriggerData","queryParams","response","opts","feedEventType","eventPayload","pageInfo","data","currentHead","type","badgeCountAttr","normalizedItems","itemsToUpdate","direction","status","e","payload","stringifiedPayload","SocketEventType"],"mappings":";;;;;;;;;;AAsCA,MAAMA,IAAmE;AAAA,EACvE,UAAU;AAAA,EACV,MAAM;AACR,GAEMC,IAAuB;AAE7B,MAAMC,EAAK;AAAA,EAaT,YACWC,GACAC,GACTC,GACAC,GACA;AAjBc,IAAAC,EAAA;AACA,IAAAA,EAAA;AACT,IAAAA,EAAA;AACC,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA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