@spoosh/plugin-polling 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,7 +16,7 @@ npm install @spoosh/plugin-polling
16
16
  import { Spoosh } from "@spoosh/core";
17
17
  import { pollingPlugin } from "@spoosh/plugin-polling";
18
18
 
19
- const client = new Spoosh<ApiSchema, Error>("/api").use([pollingPlugin()]);
19
+ const spoosh = new Spoosh<ApiSchema, Error>("/api").use([pollingPlugin()]);
20
20
 
21
21
  // Static polling interval (5 seconds)
22
22
  useRead((api) => api("posts").GET(), { pollingInterval: 5000 });
package/dist/index.d.mts CHANGED
@@ -29,7 +29,7 @@ declare module "@spoosh/core" {
29
29
  * ```ts
30
30
  * import { Spoosh } from "@spoosh/core";
31
31
  *
32
- * const client = new Spoosh<ApiSchema, Error>("/api")
32
+ * const spoosh = new Spoosh<ApiSchema, Error>("/api")
33
33
  * .use([
34
34
  * // ... other plugins
35
35
  * pollingPlugin(),
package/dist/index.d.ts CHANGED
@@ -29,7 +29,7 @@ declare module "@spoosh/core" {
29
29
  * ```ts
30
30
  * import { Spoosh } from "@spoosh/core";
31
31
  *
32
- * const client = new Spoosh<ApiSchema, Error>("/api")
32
+ * const spoosh = new Spoosh<ApiSchema, Error>("/api")
33
33
  * .use([
34
34
  * // ... other plugins
35
35
  * pollingPlugin(),
package/dist/index.js CHANGED
@@ -25,26 +25,47 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/plugin.ts
28
+ var PLUGIN_NAME = "spoosh:polling";
28
29
  function pollingPlugin() {
29
30
  const timeouts = /* @__PURE__ */ new Map();
30
- const clearPolling = (queryKey) => {
31
+ const eventTracers = /* @__PURE__ */ new Map();
32
+ const clearPolling = (queryKey, reason) => {
31
33
  const timeout = timeouts.get(queryKey);
32
34
  if (timeout) {
33
35
  clearTimeout(timeout);
34
36
  timeouts.delete(queryKey);
37
+ const et = eventTracers.get(queryKey);
38
+ if (et && reason) {
39
+ et.emit(reason, { queryKey });
40
+ }
41
+ eventTracers.delete(queryKey);
35
42
  }
36
43
  };
37
44
  const scheduleNextPoll = (context, response) => {
38
45
  const { queryKey, eventEmitter } = context;
46
+ const et = context.eventTracer?.(PLUGIN_NAME);
39
47
  const pluginOptions = context.pluginOptions;
40
48
  const pollingInterval = pluginOptions?.pollingInterval;
41
49
  if (!pollingInterval) return;
42
50
  const source = response ?? context.stateManager.getCache(queryKey)?.state;
43
51
  const resolvedInterval = typeof pollingInterval === "function" ? pollingInterval(source?.data, source?.error) : pollingInterval;
44
- if (resolvedInterval === false || resolvedInterval <= 0) return;
52
+ if (resolvedInterval === false || resolvedInterval <= 0) {
53
+ et?.emit("Polling disabled", { queryKey, color: "muted" });
54
+ return;
55
+ }
45
56
  clearPolling(queryKey);
57
+ if (et) {
58
+ eventTracers.set(queryKey, et);
59
+ }
60
+ et?.emit(`Scheduled next poll in ${resolvedInterval}ms`, {
61
+ queryKey,
62
+ color: "info",
63
+ meta: { interval: resolvedInterval }
64
+ });
46
65
  const timeout = setTimeout(() => {
47
66
  timeouts.delete(queryKey);
67
+ const storedTracer = eventTracers.get(queryKey);
68
+ storedTracer?.emit("Poll triggered", { queryKey, color: "success" });
48
69
  eventEmitter.emit("refetch", {
49
70
  queryKey,
50
71
  reason: "polling"
@@ -53,7 +74,7 @@ function pollingPlugin() {
53
74
  timeouts.set(queryKey, timeout);
54
75
  };
55
76
  return {
56
- name: "spoosh:polling",
77
+ name: PLUGIN_NAME,
57
78
  operations: ["read", "infiniteRead"],
58
79
  afterResponse(context, response) {
59
80
  scheduleNextPoll(context, response);
@@ -61,13 +82,13 @@ function pollingPlugin() {
61
82
  lifecycle: {
62
83
  onUpdate(context, previousContext) {
63
84
  if (previousContext.queryKey !== context.queryKey) {
64
- clearPolling(previousContext.queryKey);
85
+ clearPolling(previousContext.queryKey, "Query key changed");
65
86
  }
66
87
  const { queryKey } = context;
67
88
  const pluginOptions = context.pluginOptions;
68
89
  const pollingInterval = pluginOptions?.pollingInterval;
69
90
  if (!pollingInterval) {
70
- clearPolling(queryKey);
91
+ clearPolling(queryKey, "Polling disabled");
71
92
  return;
72
93
  }
73
94
  const currentTimeout = timeouts.get(queryKey);
@@ -76,7 +97,7 @@ function pollingPlugin() {
76
97
  }
77
98
  },
78
99
  onUnmount(context) {
79
- clearPolling(context.queryKey);
100
+ clearPolling(context.queryKey, "Component unmounted");
80
101
  }
81
102
  }
82
103
  };
package/dist/index.mjs CHANGED
@@ -1,24 +1,45 @@
1
1
  // src/plugin.ts
2
+ var PLUGIN_NAME = "spoosh:polling";
2
3
  function pollingPlugin() {
3
4
  const timeouts = /* @__PURE__ */ new Map();
4
- const clearPolling = (queryKey) => {
5
+ const eventTracers = /* @__PURE__ */ new Map();
6
+ const clearPolling = (queryKey, reason) => {
5
7
  const timeout = timeouts.get(queryKey);
6
8
  if (timeout) {
7
9
  clearTimeout(timeout);
8
10
  timeouts.delete(queryKey);
11
+ const et = eventTracers.get(queryKey);
12
+ if (et && reason) {
13
+ et.emit(reason, { queryKey });
14
+ }
15
+ eventTracers.delete(queryKey);
9
16
  }
10
17
  };
11
18
  const scheduleNextPoll = (context, response) => {
12
19
  const { queryKey, eventEmitter } = context;
20
+ const et = context.eventTracer?.(PLUGIN_NAME);
13
21
  const pluginOptions = context.pluginOptions;
14
22
  const pollingInterval = pluginOptions?.pollingInterval;
15
23
  if (!pollingInterval) return;
16
24
  const source = response ?? context.stateManager.getCache(queryKey)?.state;
17
25
  const resolvedInterval = typeof pollingInterval === "function" ? pollingInterval(source?.data, source?.error) : pollingInterval;
18
- if (resolvedInterval === false || resolvedInterval <= 0) return;
26
+ if (resolvedInterval === false || resolvedInterval <= 0) {
27
+ et?.emit("Polling disabled", { queryKey, color: "muted" });
28
+ return;
29
+ }
19
30
  clearPolling(queryKey);
31
+ if (et) {
32
+ eventTracers.set(queryKey, et);
33
+ }
34
+ et?.emit(`Scheduled next poll in ${resolvedInterval}ms`, {
35
+ queryKey,
36
+ color: "info",
37
+ meta: { interval: resolvedInterval }
38
+ });
20
39
  const timeout = setTimeout(() => {
21
40
  timeouts.delete(queryKey);
41
+ const storedTracer = eventTracers.get(queryKey);
42
+ storedTracer?.emit("Poll triggered", { queryKey, color: "success" });
22
43
  eventEmitter.emit("refetch", {
23
44
  queryKey,
24
45
  reason: "polling"
@@ -27,7 +48,7 @@ function pollingPlugin() {
27
48
  timeouts.set(queryKey, timeout);
28
49
  };
29
50
  return {
30
- name: "spoosh:polling",
51
+ name: PLUGIN_NAME,
31
52
  operations: ["read", "infiniteRead"],
32
53
  afterResponse(context, response) {
33
54
  scheduleNextPoll(context, response);
@@ -35,13 +56,13 @@ function pollingPlugin() {
35
56
  lifecycle: {
36
57
  onUpdate(context, previousContext) {
37
58
  if (previousContext.queryKey !== context.queryKey) {
38
- clearPolling(previousContext.queryKey);
59
+ clearPolling(previousContext.queryKey, "Query key changed");
39
60
  }
40
61
  const { queryKey } = context;
41
62
  const pluginOptions = context.pluginOptions;
42
63
  const pollingInterval = pluginOptions?.pollingInterval;
43
64
  if (!pollingInterval) {
44
- clearPolling(queryKey);
65
+ clearPolling(queryKey, "Polling disabled");
45
66
  return;
46
67
  }
47
68
  const currentTimeout = timeouts.get(queryKey);
@@ -50,7 +71,7 @@ function pollingPlugin() {
50
71
  }
51
72
  },
52
73
  onUnmount(context) {
53
- clearPolling(context.queryKey);
74
+ clearPolling(context.queryKey, "Component unmounted");
54
75
  }
55
76
  }
56
77
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-polling",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "Automatic polling/refetching plugin for Spoosh",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,11 +34,11 @@
34
34
  }
35
35
  },
36
36
  "peerDependencies": {
37
- "@spoosh/core": ">=0.11.1"
37
+ "@spoosh/core": ">=0.13.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@spoosh/core": "0.11.1",
41
- "@spoosh/test-utils": "0.1.6"
40
+ "@spoosh/test-utils": "0.2.0",
41
+ "@spoosh/core": "0.13.0"
42
42
  },
43
43
  "scripts": {
44
44
  "dev": "tsup --watch",