@knocklabs/client 0.21.1 → 0.21.2

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cjs/api.js +1 -1
  3. package/dist/cjs/api.js.map +1 -1
  4. package/dist/cjs/clients/feed/feed.js +1 -1
  5. package/dist/cjs/clients/feed/feed.js.map +1 -1
  6. package/dist/cjs/clients/guide/client.js +1 -1
  7. package/dist/cjs/clients/guide/client.js.map +1 -1
  8. package/dist/cjs/clients/guide/helpers.js +1 -1
  9. package/dist/cjs/clients/guide/helpers.js.map +1 -1
  10. package/dist/cjs/clients/guide/types.js +2 -0
  11. package/dist/cjs/clients/guide/types.js.map +1 -0
  12. package/dist/cjs/helpers.js +1 -1
  13. package/dist/cjs/helpers.js.map +1 -1
  14. package/dist/cjs/index.js +1 -1
  15. package/dist/cjs/knock.js +2 -2
  16. package/dist/cjs/knock.js.map +1 -1
  17. package/dist/cjs/pageVisibility.js +2 -0
  18. package/dist/cjs/pageVisibility.js.map +1 -0
  19. package/dist/esm/api.mjs +27 -12
  20. package/dist/esm/api.mjs.map +1 -1
  21. package/dist/esm/clients/feed/feed.mjs +60 -87
  22. package/dist/esm/clients/feed/feed.mjs.map +1 -1
  23. package/dist/esm/clients/guide/client.mjs +191 -147
  24. package/dist/esm/clients/guide/client.mjs.map +1 -1
  25. package/dist/esm/clients/guide/helpers.mjs +34 -44
  26. package/dist/esm/clients/guide/helpers.mjs.map +1 -1
  27. package/dist/esm/clients/guide/types.mjs +13 -0
  28. package/dist/esm/clients/guide/types.mjs.map +1 -0
  29. package/dist/esm/helpers.mjs +19 -4
  30. package/dist/esm/helpers.mjs.map +1 -1
  31. package/dist/esm/index.mjs +14 -12
  32. package/dist/esm/index.mjs.map +1 -1
  33. package/dist/esm/knock.mjs +31 -29
  34. package/dist/esm/knock.mjs.map +1 -1
  35. package/dist/esm/pageVisibility.mjs +31 -0
  36. package/dist/esm/pageVisibility.mjs.map +1 -0
  37. package/dist/types/api.d.ts +4 -0
  38. package/dist/types/api.d.ts.map +1 -1
  39. package/dist/types/clients/feed/feed.d.ts +1 -11
  40. package/dist/types/clients/feed/feed.d.ts.map +1 -1
  41. package/dist/types/clients/feed/interfaces.d.ts +0 -4
  42. package/dist/types/clients/feed/interfaces.d.ts.map +1 -1
  43. package/dist/types/clients/feed/utils.d.ts +0 -2
  44. package/dist/types/clients/feed/utils.d.ts.map +1 -1
  45. package/dist/types/clients/guide/client.d.ts +3 -1
  46. package/dist/types/clients/guide/client.d.ts.map +1 -1
  47. package/dist/types/clients/guide/helpers.d.ts +1 -7
  48. package/dist/types/clients/guide/helpers.d.ts.map +1 -1
  49. package/dist/types/clients/guide/index.d.ts +2 -1
  50. package/dist/types/clients/guide/index.d.ts.map +1 -1
  51. package/dist/types/clients/guide/types.d.ts +21 -0
  52. package/dist/types/clients/guide/types.d.ts.map +1 -1
  53. package/dist/types/helpers.d.ts +19 -0
  54. package/dist/types/helpers.d.ts.map +1 -1
  55. package/dist/types/interfaces.d.ts +2 -0
  56. package/dist/types/interfaces.d.ts.map +1 -1
  57. package/dist/types/knock.d.ts +1 -0
  58. package/dist/types/knock.d.ts.map +1 -1
  59. package/dist/types/pageVisibility.d.ts +22 -0
  60. package/dist/types/pageVisibility.d.ts.map +1 -0
  61. package/package.json +1 -1
  62. package/src/api.ts +30 -0
  63. package/src/clients/feed/feed.ts +0 -73
  64. package/src/clients/feed/interfaces.ts +0 -7
  65. package/src/clients/guide/client.ts +117 -32
  66. package/src/clients/guide/helpers.ts +0 -12
  67. package/src/clients/guide/index.ts +3 -0
  68. package/src/clients/guide/types.ts +34 -0
  69. package/src/helpers.ts +39 -0
  70. package/src/interfaces.ts +2 -0
  71. package/src/knock.ts +4 -3
  72. package/src/pageVisibility.ts +70 -0
@@ -1,30 +1,31 @@
1
1
  var I = Object.defineProperty;
2
2
  var C = (u, e, t) => e in u ? I(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
- var c = (u, e, t) => C(u, typeof e != "symbol" ? e + "" : e, t);
3
+ var l = (u, e, t) => C(u, typeof e != "symbol" ? e + "" : e, t);
4
4
  import { Store as E } from "@tanstack/store";
5
5
  import { URLPattern as w } from "urlpattern-polyfill";
6
- import { byKey as G, mockDefaultGroup as R, formatFilters as S, formatState as m, checkStateIfThrottled as y, formatGroupStage as v, DEFAULT_GROUP_KEY as P, newUrl as D, predicateUrlRules as A, predicateUrlPatterns as L, SelectionResult as $, findDefaultGroup as T } from "./helpers.mjs";
7
- const K = 50, O = 30 * 1e3, U = 3, g = {
6
+ import { byKey as y, mockDefaultGroup as R, formatFilters as G, formatState as S, checkStateIfThrottled as m, formatGroupStage as v, DEFAULT_GROUP_KEY as P, newUrl as D, predicateUrlRules as A, predicateUrlPatterns as L, findDefaultGroup as T } from "./helpers.mjs";
7
+ import { SelectionResult as $ } from "./types.mjs";
8
+ const K = 50, O = 30 * 1e3, U = 3, p = {
8
9
  GUIDE_KEY: "knock_guide_key",
9
10
  PREVIEW_SESSION_ID: "knock_preview_session_id"
10
- }, f = "knock_guide_debug", l = () => {
11
+ }, f = "knock_guide_debug", k = () => {
11
12
  if (typeof window < "u")
12
13
  return window;
13
14
  }, F = (u) => `/v1/users/${u}/guides`, b = () => {
14
- const u = l();
15
+ const u = k();
15
16
  if (!u || !u.location)
16
17
  return { forcedGuideKey: null, previewSessionId: null };
17
- const e = new URLSearchParams(u.location.search), t = e.get(g.GUIDE_KEY), s = e.get(
18
- g.PREVIEW_SESSION_ID
18
+ const e = new URLSearchParams(u.location.search), t = e.get(p.GUIDE_KEY), s = e.get(
19
+ p.PREVIEW_SESSION_ID
19
20
  );
20
21
  if (t || s) {
21
22
  if (u.localStorage)
22
23
  try {
23
- const n = {
24
+ const r = {
24
25
  forcedGuideKey: t,
25
26
  previewSessionId: s
26
27
  };
27
- u.localStorage.setItem(f, JSON.stringify(n));
28
+ u.localStorage.setItem(f, JSON.stringify(r));
28
29
  } catch {
29
30
  }
30
31
  return {
@@ -32,21 +33,21 @@ const K = 50, O = 30 * 1e3, U = 3, g = {
32
33
  previewSessionId: s
33
34
  };
34
35
  }
35
- let i = null, r = null;
36
+ let i = null, n = null;
36
37
  if (u.localStorage)
37
38
  try {
38
- const n = u.localStorage.getItem(f);
39
- if (n) {
40
- const o = N(n);
41
- i = o.forcedGuideKey, r = o.previewSessionId;
39
+ const r = u.localStorage.getItem(f);
40
+ if (r) {
41
+ const a = q(r);
42
+ i = a.forcedGuideKey, n = a.previewSessionId;
42
43
  }
43
44
  } catch {
44
45
  }
45
46
  return {
46
47
  forcedGuideKey: i,
47
- previewSessionId: r
48
+ previewSessionId: n
48
49
  };
49
- }, N = (u) => {
50
+ }, q = (u) => {
50
51
  try {
51
52
  const e = JSON.parse(u);
52
53
  return {
@@ -59,20 +60,20 @@ const K = 50, O = 30 * 1e3, U = 3, g = {
59
60
  previewSessionId: null
60
61
  };
61
62
  }
62
- }, _ = (u, e = {}) => {
63
- const t = new $(), s = T(u.guideGroups);
64
- if (!s) return t;
65
- const i = s.display_sequence, r = u.location;
66
- for (const [n, o] of i.entries()) {
67
- const a = u.previewGuides[o] || u.guides[o];
68
- !a || !q(a, e, {
63
+ }, _ = (u, e, t) => {
64
+ const s = new $(), i = T(u.guideGroups);
65
+ if (!i) return s;
66
+ const n = i.display_sequence, r = u.location;
67
+ for (const [a, o] of n.entries()) {
68
+ const c = u.previewGuides[o] || u.guides[o];
69
+ !c || !N(c, e, {
69
70
  location: r,
70
71
  ineligibleGuides: u.ineligibleGuides,
71
72
  debug: u.debug
72
- }) || t.set(n, a);
73
+ }) || s.set(a, c);
73
74
  }
74
- return t.metadata = { guideGroup: s }, t;
75
- }, q = (u, e, { location: t, ineligibleGuides: s = {}, debug: i = {} }) => e.type && e.type !== u.type || e.key && e.key !== u.key ? !1 : i.forcedGuideKey ? i.forcedGuideKey === u.key : s[u.key] || !u.active || u.steps.every((n) => !!n.message.archived_at) ? !1 : j(u, t), j = (u, e) => {
75
+ return s.metadata = { guideGroup: i, filters: e, ...t }, s;
76
+ }, N = (u, e, { location: t, ineligibleGuides: s = {}, debug: i = {} }) => e.type && e.type !== u.type || e.key && e.key !== u.key ? !1 : i.forcedGuideKey ? i.forcedGuideKey === u.key : s[u.key] || !u.active || u.steps.every((r) => !!r.message.archived_at) ? !1 : j(u, t), j = (u, e) => {
76
77
  const t = e ? D(e) : void 0, s = u.activation_url_rules || [], i = u.activation_url_patterns || [];
77
78
  if (t && s.length > 0) {
78
79
  if (!A(t, s)) return !1;
@@ -82,12 +83,12 @@ const K = 50, O = 30 * 1e3, U = 3, g = {
82
83
  };
83
84
  class W {
84
85
  constructor(e, t, s = {}, i = {}) {
85
- c(this, "store");
86
+ l(this, "store");
86
87
  // Phoenix channels for real time guide updates over websocket
87
- c(this, "socket");
88
- c(this, "socketChannel");
89
- c(this, "socketChannelTopic");
90
- c(this, "socketEventTypes", [
88
+ l(this, "socket");
89
+ l(this, "socketChannel");
90
+ l(this, "socketChannelTopic");
91
+ l(this, "socketEventTypes", [
91
92
  "guide.added",
92
93
  "guide.updated",
93
94
  "guide.removed",
@@ -95,19 +96,19 @@ class W {
95
96
  "guide_group.updated",
96
97
  "guide.live_preview_updated"
97
98
  ]);
98
- c(this, "subscribeRetryCount", 0);
99
+ l(this, "subscribeRetryCount", 0);
99
100
  // Original history methods to monkey patch, or restore in cleanups.
100
- c(this, "pushStateFn");
101
- c(this, "replaceStateFn");
101
+ l(this, "pushStateFn");
102
+ l(this, "replaceStateFn");
102
103
  // Guides that are competing to render are "staged" first without rendering
103
104
  // and ranked based on its relative order in the group over a duration of time
104
105
  // to resolve and render the prevailing one.
105
- c(this, "stage");
106
- c(this, "counterIntervalId");
106
+ l(this, "stage");
107
+ l(this, "counterIntervalId");
107
108
  // Define as an arrow func property to always bind this to the class instance.
108
- c(this, "handleLocationChange", () => {
109
+ l(this, "handleLocationChange", () => {
109
110
  this.knock.log("[Guide] .handleLocationChange");
110
- const e = l();
111
+ const e = k();
111
112
  if (!(e != null && e.location)) return;
112
113
  const t = e.location.href;
113
114
  if (this.store.state.location === t) return;
@@ -123,15 +124,15 @@ class W {
123
124
  "[Guide] Debug state changed, refetching guides and resubscribing to the websocket channel"
124
125
  ), this.fetch(), this.subscribe());
125
126
  });
126
- var p;
127
+ var g;
127
128
  this.knock = e, this.channelId = t, this.targetParams = s, this.options = i;
128
129
  const {
129
- trackLocationFromWindow: r = !0,
130
+ trackLocationFromWindow: n = !0,
130
131
  // TODO(KNO-11523): Remove once we ship guide toolbar v2, and offload as
131
132
  // much debugging specific logic and responsibilities to toolbar.
132
- trackDebugParams: n = !1,
133
- throttleCheckInterval: o = O
134
- } = i, a = l(), d = r ? (p = a == null ? void 0 : a.location) == null ? void 0 : p.href : void 0, h = n ? b() : void 0;
133
+ trackDebugParams: r = !1,
134
+ throttleCheckInterval: a = O
135
+ } = i, o = k(), c = n ? (g = o == null ? void 0 : o.location) == null ? void 0 : g.href : void 0, d = r ? b() : void 0;
135
136
  this.store = new E({
136
137
  guideGroups: [],
137
138
  guideGroupDisplayLogs: {},
@@ -139,13 +140,13 @@ class W {
139
140
  ineligibleGuides: {},
140
141
  previewGuides: {},
141
142
  queries: {},
142
- location: d,
143
+ location: c,
143
144
  // Increment to update the state store and trigger re-selection.
144
145
  counter: 0,
145
- debug: h
146
+ debug: d
146
147
  });
147
- const { socket: k } = this.knock.client();
148
- this.socket = k, this.socketChannelTopic = `guides:${t}`, r && this.listenForLocationChangesFromWindow(), o && this.startCounterInterval(o), this.knock.log("[Guide] Initialized a guide client");
148
+ const { socket: h } = this.knock.client();
149
+ this.socket = h, this.socketChannelTopic = `guides:${t}`, n && this.listenForLocationChangesFromWindow(), a && this.startCounterInterval(a), this.knock.log("[Guide] Initialized a guide client");
149
150
  }
150
151
  incrementCounter() {
151
152
  this.knock.log("[Guide] Incrementing the counter"), this.store.setState((e) => ({ ...e, counter: e.counter + 1 }));
@@ -166,36 +167,36 @@ class W {
166
167
  const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), i = this.store.state.queries[s];
167
168
  if (i)
168
169
  return i;
169
- this.store.setState((n) => ({
170
- ...n,
171
- queries: { ...n.queries, [s]: { status: "loading" } }
170
+ this.store.setState((r) => ({
171
+ ...r,
172
+ queries: { ...r.queries, [s]: { status: "loading" } }
172
173
  }));
173
- let r;
174
+ let n;
174
175
  try {
175
176
  this.knock.log("[Guide] Fetching all eligible guides");
176
- const n = await this.knock.user.getGuides(this.channelId, t);
177
- r = { status: "ok" };
177
+ const r = await this.knock.user.getGuides(this.channelId, t);
178
+ n = { status: "ok" };
178
179
  const {
179
- entries: o,
180
- guide_groups: a,
181
- guide_group_display_logs: d,
182
- ineligible_guides: h
183
- } = n;
184
- this.knock.log("[Guide] Loading fetched guides"), this.store.setState((k) => ({
185
- ...k,
186
- guideGroups: (a == null ? void 0 : a.length) > 0 ? a : [R(o)],
187
- guideGroupDisplayLogs: d || {},
188
- guides: G(o.map((p) => this.localCopy(p))),
189
- ineligibleGuides: G(h || []),
190
- queries: { ...k.queries, [s]: r }
180
+ entries: a,
181
+ guide_groups: o,
182
+ guide_group_display_logs: c,
183
+ ineligible_guides: d
184
+ } = r;
185
+ this.knock.log("[Guide] Loading fetched guides"), this.store.setState((h) => ({
186
+ ...h,
187
+ guideGroups: (o == null ? void 0 : o.length) > 0 ? o : [R(a)],
188
+ guideGroupDisplayLogs: c || {},
189
+ guides: y(a.map((g) => this.localCopy(g))),
190
+ ineligibleGuides: y(d || []),
191
+ queries: { ...h.queries, [s]: n }
191
192
  }));
192
- } catch (n) {
193
- r = { status: "error", error: n }, this.store.setState((o) => ({
194
- ...o,
195
- queries: { ...o.queries, [s]: r }
193
+ } catch (r) {
194
+ n = { status: "error", error: r }, this.store.setState((a) => ({
195
+ ...a,
196
+ queries: { ...a.queries, [s]: n }
196
197
  }));
197
198
  }
198
- return r;
199
+ return n;
199
200
  }
200
201
  subscribe() {
201
202
  if (!this.socket) return;
@@ -207,7 +208,7 @@ class W {
207
208
  preview_session_id: (e == null ? void 0 : e.previewSessionId) || void 0
208
209
  }, s = this.socket.channel(this.socketChannelTopic, t);
209
210
  for (const i of this.socketEventTypes)
210
- s.on(i, (r) => this.handleSocketEvent(r));
211
+ s.on(i, (n) => this.handleSocketEvent(n));
211
212
  ["closed", "errored"].includes(s.state) && (this.subscribeRetryCount = 0, s.join().receive("ok", () => {
212
213
  this.knock.log("[Guide] Successfully joined channel");
213
214
  }).receive("error", (i) => {
@@ -255,8 +256,8 @@ class W {
255
256
  }
256
257
  setLocation(e, t = {}) {
257
258
  this.knock.log(`[Guide] .setLocation (loc=${e})`), this.clearGroupStage(), this.knock.log("[Guide] Updating the tracked location"), this.store.setState((s) => {
258
- var r;
259
- const i = (r = t == null ? void 0 : t.debug) != null && r.previewSessionId ? s.previewGuides : {};
259
+ var n;
260
+ const i = (n = t == null ? void 0 : t.debug) != null && n.previewSessionId ? s.previewGuides : {};
260
261
  return {
261
262
  ...s,
262
263
  ...t,
@@ -267,7 +268,7 @@ class W {
267
268
  }
268
269
  exitDebugMode() {
269
270
  this.knock.log("[Guide] Exiting debug mode");
270
- const e = l();
271
+ const e = k();
271
272
  if (e != null && e.localStorage)
272
273
  try {
273
274
  e.localStorage.removeItem(f);
@@ -280,7 +281,7 @@ class W {
280
281
  // Clear preview guides when exiting debug mode
281
282
  })), e != null && e.location) {
282
283
  const t = new URL(e.location.href);
283
- (t.searchParams.has(g.GUIDE_KEY) || t.searchParams.has(g.PREVIEW_SESSION_ID)) && (t.searchParams.delete(g.GUIDE_KEY), t.searchParams.delete(g.PREVIEW_SESSION_ID), e.location.href = t.toString());
284
+ (t.searchParams.has(p.GUIDE_KEY) || t.searchParams.has(p.PREVIEW_SESSION_ID)) && (t.searchParams.delete(p.GUIDE_KEY), t.searchParams.delete(p.PREVIEW_SESSION_ID), e.location.href = t.toString());
284
285
  }
285
286
  }
286
287
  setDebug(e) {
@@ -306,62 +307,104 @@ class W {
306
307
  // Store selector
307
308
  //
308
309
  selectGuides(e, t = {}, s = {}) {
309
- if (this.knock.log(
310
- `[Guide] .selectGuides (filters: ${S(t)}; state: ${m(e)})`
311
- ), !this.selectGuide(e, t, s))
310
+ var c;
311
+ this.knock.log(
312
+ `[Guide] .selectGuides (filters: ${G(t)}; state: ${S(e)})`
313
+ );
314
+ const i = this.selectGuide(e, t, {
315
+ ...s,
316
+ // Don't record this result, not the actual query result we need.
317
+ recordSelectQuery: !1
318
+ }), { recordSelectQuery: n = !!((c = e.debug) != null && c.debugging) } = s, r = {
319
+ limit: "all",
320
+ opts: { ...s, recordSelectQuery: n }
321
+ }, a = _(e, t, r);
322
+ if (this.maybeRecordSelectResult(a), !i && !s.includeThrottled)
312
323
  return [];
313
- const r = [..._(e, t).values()];
314
- if (!s.includeThrottled && y(e)) {
315
- const n = r.filter(
316
- (a) => a.bypass_global_group_limit
317
- ), o = r.length - n.length;
324
+ const o = [...a.values()];
325
+ if (!s.includeThrottled && m(e)) {
326
+ const d = o.filter(
327
+ (g) => g.bypass_global_group_limit
328
+ ), h = o.length - d.length;
318
329
  return this.knock.log(
319
- `[Guide] Throttling ${o} guides from selection, and returning ${n.length} guides`
320
- ), n;
330
+ `[Guide] Throttling ${h} guides from selection, and returning ${d.length} guides`
331
+ ), d;
321
332
  }
322
- return this.knock.log(`[Guide] Returning ${r.length} guides from selection`), r;
333
+ return this.knock.log(`[Guide] Returning ${o.length} guides from selection`), o;
323
334
  }
324
335
  selectGuide(e, t = {}, s = {}) {
336
+ var d;
325
337
  if (this.knock.log(
326
- `[Guide] .selectGuide (filters: ${S(t)}; state: ${m(e)})`
338
+ `[Guide] .selectGuide (filters: ${G(t)}; state: ${S(e)})`
327
339
  ), Object.keys(e.guides).length === 0 && Object.keys(e.previewGuides).length === 0) {
328
340
  this.knock.log("[Guide] Exiting selection (no guides)");
329
341
  return;
330
342
  }
331
- const i = _(e, t);
332
- if (i.size === 0) {
343
+ this.stage || (this.stage = this.openGroupStage());
344
+ const { recordSelectQuery: i = !!((d = e.debug) != null && d.debugging) } = s, n = {
345
+ limit: "one",
346
+ opts: { ...s, recordSelectQuery: i }
347
+ }, r = _(e, t, n);
348
+ if (this.maybeRecordSelectResult(r), r.size === 0) {
333
349
  this.knock.log("[Guide] Selection found zero result");
334
350
  return;
335
351
  }
336
- const [r, n] = [...i][0];
352
+ const [a, o] = [...r][0];
337
353
  if (this.knock.log(
338
- `[Guide] Selection found: \`${n.key}\` (total: ${i.size})`
339
- ), n.bypass_global_group_limit)
340
- return this.knock.log(`[Guide] Returning the unthrottled guide: ${n.key}`), n;
341
- if (!s.includeThrottled && y(e)) {
342
- this.knock.log(`[Guide] Throttling the selected guide: ${n.key}`);
343
- return;
344
- }
345
- switch (this.stage || (this.stage = this.openGroupStage()), this.stage.status) {
354
+ `[Guide] Selection found: \`${o.key}\` (total: ${r.size})`
355
+ ), o.bypass_global_group_limit)
356
+ return this.knock.log(`[Guide] Returning the unthrottled guide: ${o.key}`), o;
357
+ const c = !s.includeThrottled && m(e);
358
+ switch (this.stage.status) {
346
359
  case "open": {
347
- this.knock.log(`[Guide] Adding to the group stage: ${n.key}`), this.stage.ordered[r] = n.key;
360
+ this.knock.log(`[Guide] Adding to the group stage: ${o.key}`), this.stage.ordered[a] = o.key;
348
361
  return;
349
362
  }
350
363
  case "patch": {
351
- this.knock.log(`[Guide] Patching the group stage: ${n.key}`), this.stage.ordered[r] = n.key;
352
- const o = this.stage.resolved === n.key ? n : void 0;
364
+ if (this.knock.log(`[Guide] Patching the group stage: ${o.key}`), this.stage.ordered[a] = o.key, c) {
365
+ this.knock.log(`[Guide] Throttling the selected guide: ${o.key}`);
366
+ return;
367
+ }
368
+ const h = this.stage.resolved === o.key ? o : void 0;
353
369
  return this.knock.log(
354
- `[Guide] Returning \`${o == null ? void 0 : o.key}\` (stage: ${v(this.stage)})`
355
- ), o;
370
+ `[Guide] Returning \`${h == null ? void 0 : h.key}\` (stage: ${v(this.stage)})`
371
+ ), h;
356
372
  }
357
373
  case "closed": {
358
- const o = this.stage.resolved === n.key ? n : void 0;
374
+ if (c) {
375
+ this.knock.log(`[Guide] Throttling the selected guide: ${o.key}`);
376
+ return;
377
+ }
378
+ const h = this.stage.resolved === o.key ? o : void 0;
359
379
  return this.knock.log(
360
- `[Guide] Returning \`${o == null ? void 0 : o.key}\` (stage: ${v(this.stage)})`
361
- ), o;
380
+ `[Guide] Returning \`${h == null ? void 0 : h.key}\` (stage: ${v(this.stage)})`
381
+ ), h;
362
382
  }
363
383
  }
364
384
  }
385
+ // Record select query results by accumulating them by 1) key or type first,
386
+ // and then 2) "one" or "all".
387
+ maybeRecordSelectResult(e) {
388
+ if (!e.metadata) return;
389
+ const { opts: t, filters: s, limit: i } = e.metadata;
390
+ if (!t.recordSelectQuery || !s.key && !s.type || !this.stage || this.stage.status === "closed") return;
391
+ const n = this.stage.results.key || {};
392
+ s.key && (n[s.key] = {
393
+ ...n[s.key] || {},
394
+ [i]: e
395
+ });
396
+ const r = this.stage.results.type || {};
397
+ s.type && (r[s.type] = {
398
+ ...r[s.type] || {},
399
+ [i]: e
400
+ }), this.stage = {
401
+ ...this.stage,
402
+ results: { key: n, type: r }
403
+ };
404
+ }
405
+ getStage() {
406
+ return this.stage;
407
+ }
365
408
  openGroupStage() {
366
409
  this.knock.log("[Guide] Opening a new group stage");
367
410
  const {
@@ -372,6 +415,7 @@ class W {
372
415
  return this.stage = {
373
416
  status: "open",
374
417
  ordered: [],
418
+ results: {},
375
419
  timeoutId: t
376
420
  }, this.stage;
377
421
  }
@@ -452,16 +496,16 @@ class W {
452
496
  this.knock.log(
453
497
  `[Guide] Marking as interacted (Guide key: ${e.key}; Step ref:${t.ref})`
454
498
  );
455
- const i = (/* @__PURE__ */ new Date()).toISOString(), r = this.setStepMessageAttrs(e.key, t.ref, {
499
+ const i = (/* @__PURE__ */ new Date()).toISOString(), n = this.setStepMessageAttrs(e.key, t.ref, {
456
500
  read_at: i,
457
501
  interacted_at: i
458
502
  });
459
- if (!r) return;
460
- const n = {
461
- ...this.buildEngagementEventBaseParams(e, r),
503
+ if (!n) return;
504
+ const r = {
505
+ ...this.buildEngagementEventBaseParams(e, n),
462
506
  metadata: s
463
507
  };
464
- return this.knock.user.markGuideStepAs("interacted", n), r;
508
+ return this.knock.user.markGuideStepAs("interacted", r), n;
465
509
  }
466
510
  async markAsArchived(e, t) {
467
511
  if (t.message.archived_at) return;
@@ -490,24 +534,24 @@ class W {
490
534
  // Get the next unarchived step.
491
535
  getStep() {
492
536
  var i;
493
- return ((i = t.store.state.debug) == null ? void 0 : i.forcedGuideKey) === this.key ? this.steps[0] : this.steps.find((r) => !r.message.archived_at);
537
+ return ((i = t.store.state.debug) == null ? void 0 : i.forcedGuideKey) === this.key ? this.steps[0] : this.steps.find((n) => !n.message.archived_at);
494
538
  }
495
539
  };
496
- return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: i, ...r }) => {
497
- const n = {
498
- ...r,
540
+ return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: i, ...n }) => {
541
+ const r = {
542
+ ...n,
499
543
  message: { ...i },
500
544
  markAsSeen() {
501
545
  return t.markAsSeen(s, this);
502
546
  },
503
- markAsInteracted({ metadata: o } = {}) {
504
- return t.markAsInteracted(s, this, o);
547
+ markAsInteracted({ metadata: a } = {}) {
548
+ return t.markAsInteracted(s, this, a);
505
549
  },
506
550
  markAsArchived() {
507
551
  return t.markAsArchived(s, this);
508
552
  }
509
553
  };
510
- return n.markAsSeen = n.markAsSeen.bind(n), n.markAsInteracted = n.markAsInteracted.bind(n), n.markAsArchived = n.markAsArchived.bind(n), n;
554
+ return r.markAsSeen = r.markAsSeen.bind(r), r.markAsInteracted = r.markAsInteracted.bind(r), r.markAsArchived = r.markAsArchived.bind(r), r;
511
555
  }), s.activation_url_patterns = e.activation_url_patterns.map((i) => ({
512
556
  ...i,
513
557
  pattern: new w({
@@ -524,29 +568,29 @@ class W {
524
568
  (s != null && s.forcedGuideKey || s != null && s.debugging) && (t.force_all_guides = !0);
525
569
  let i = Object.fromEntries(
526
570
  Object.entries(t).filter(
527
- ([r, n]) => n != null
571
+ ([n, r]) => r != null
528
572
  )
529
573
  );
530
574
  return i = i.data ? { ...i, data: JSON.stringify(i.data) } : i, i;
531
575
  }
532
576
  formatQueryKey(e) {
533
577
  const s = Object.keys(e).sort().map(
534
- (r) => `${encodeURIComponent(r)}=${encodeURIComponent(e[r])}`
578
+ (n) => `${encodeURIComponent(n)}=${encodeURIComponent(e[n])}`
535
579
  ).join("&"), i = F(this.knock.userId);
536
580
  return s ? `${i}?${s}` : i;
537
581
  }
538
582
  setStepMessageAttrs(e, t, s) {
539
583
  let i;
540
- return s.archived_at && this.clearGroupStage(), this.store.setState((r) => {
541
- let n = r.guides[e];
542
- if (!n) return r;
543
- const o = n.steps.map((h) => (h.ref !== t || (h.message = { ...h.message, ...s }, i = h), h));
544
- n = i ? { ...n, steps: o } : n;
545
- const a = { ...r.guides, [n.key]: n }, d = s.archived_at && !n.bypass_global_group_limit ? {
546
- ...r.guideGroupDisplayLogs,
584
+ return s.archived_at && this.clearGroupStage(), this.store.setState((n) => {
585
+ let r = n.guides[e];
586
+ if (!r) return n;
587
+ const a = r.steps.map((d) => (d.ref !== t || (d.message = { ...d.message, ...s }, i = d), d));
588
+ r = i ? { ...r, steps: a } : r;
589
+ const o = { ...n.guides, [r.key]: r }, c = s.archived_at && !r.bypass_global_group_limit ? {
590
+ ...n.guideGroupDisplayLogs,
547
591
  [P]: s.archived_at
548
- } : r.guideGroupDisplayLogs;
549
- return { ...r, guides: a, guideGroupDisplayLogs: d };
592
+ } : n.guideGroupDisplayLogs;
593
+ return { ...n, guides: o, guideGroupDisplayLogs: c };
550
594
  }), i;
551
595
  }
552
596
  buildEngagementEventBaseParams(e, t) {
@@ -577,17 +621,17 @@ class W {
577
621
  data: e
578
622
  }) {
579
623
  this.patchClosedGroupStage(), this.store.setState((t) => {
580
- const s = [e.guide_group], i = e.guide_group.display_sequence_unthrottled || [], r = e.guide_group.display_sequence_throttled || [];
581
- let n = t.guides;
582
- return n = i.reduce((o, a) => {
583
- if (!o[a]) return o;
584
- const d = { ...o[a], bypass_global_group_limit: !0 };
585
- return { ...o, [a]: d };
586
- }, n), n = r.reduce((o, a) => {
587
- if (!o[a]) return o;
588
- const d = { ...o[a], bypass_global_group_limit: !1 };
589
- return { ...o, [a]: d };
590
- }, n), { ...t, guides: n, guideGroups: s };
624
+ const s = [e.guide_group], i = e.guide_group.display_sequence_unthrottled || [], n = e.guide_group.display_sequence_throttled || [];
625
+ let r = t.guides;
626
+ return r = i.reduce((a, o) => {
627
+ if (!a[o]) return a;
628
+ const c = { ...a[o], bypass_global_group_limit: !0 };
629
+ return { ...a, [o]: c };
630
+ }, r), r = n.reduce((a, o) => {
631
+ if (!a[o]) return a;
632
+ const c = { ...a[o], bypass_global_group_limit: !1 };
633
+ return { ...a, [o]: c };
634
+ }, r), { ...t, guides: r, guideGroups: s };
591
635
  });
592
636
  }
593
637
  updatePreviewGuide({ data: e }) {
@@ -603,19 +647,19 @@ class W {
603
647
  return !!e.forcedGuideKey != !!t.forcedGuideKey || e.previewSessionId !== t.previewSessionId;
604
648
  }
605
649
  listenForLocationChangesFromWindow() {
606
- const e = l();
650
+ const e = k();
607
651
  if (e != null && e.history && (e != null && e.addEventListener)) {
608
652
  e.addEventListener("popstate", this.handleLocationChange), e.addEventListener("hashchange", this.handleLocationChange);
609
653
  const t = e.history.pushState, s = e.history.replaceState;
610
654
  e.history.pushState = new Proxy(t, {
611
- apply: (i, r, n) => {
612
- Reflect.apply(i, r, n), setTimeout(() => {
655
+ apply: (i, n, r) => {
656
+ Reflect.apply(i, n, r), setTimeout(() => {
613
657
  this.handleLocationChange();
614
658
  }, 0);
615
659
  }
616
660
  }), e.history.replaceState = new Proxy(s, {
617
- apply: (i, r, n) => {
618
- Reflect.apply(i, r, n), setTimeout(() => {
661
+ apply: (i, n, r) => {
662
+ Reflect.apply(i, n, r), setTimeout(() => {
619
663
  this.handleLocationChange();
620
664
  }, 0);
621
665
  }
@@ -626,12 +670,12 @@ class W {
626
670
  );
627
671
  }
628
672
  removeLocationChangeEventListeners() {
629
- const e = l();
673
+ const e = k();
630
674
  !(e != null && e.history) || !(e != null && e.removeEventListener) || (e.removeEventListener("popstate", this.handleLocationChange), e.removeEventListener("hashchange", this.handleLocationChange), this.pushStateFn && (e.history.pushState = this.pushStateFn, this.pushStateFn = void 0), this.replaceStateFn && (e.history.replaceState = this.replaceStateFn, this.replaceStateFn = void 0));
631
675
  }
632
676
  }
633
677
  export {
634
- g as DEBUG_QUERY_PARAMS,
678
+ p as DEBUG_QUERY_PARAMS,
635
679
  W as KnockGuideClient,
636
680
  j as checkActivatable,
637
681
  F as guidesApiRootPath