@spoosh/plugin-refetch 0.1.0-beta.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Spoosh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @spoosh/plugin-refetch
2
+
3
+ Auto-refetch plugin for Spoosh - refetch on window focus and network reconnect.
4
+
5
+ **[Documentation](https://spoosh.dev/docs/plugins/refetch)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @spoosh/plugin-refetch
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { refetchPlugin } from "@spoosh/plugin-refetch";
17
+
18
+ const plugins = [
19
+ refetchPlugin({
20
+ refetchOnFocus: true,
21
+ refetchOnReconnect: true,
22
+ }),
23
+ ];
24
+
25
+ // Uses plugin defaults
26
+ useRead((api) => api.posts.$get());
27
+
28
+ // Per-query override
29
+ useRead((api) => api.posts.$get(), {
30
+ refetchOnFocus: false, // Disable for this query
31
+ });
32
+ ```
33
+
34
+ ## Options
35
+
36
+ ### Plugin Config
37
+
38
+ | Option | Type | Default | Description |
39
+ | -------------------- | --------- | ------- | --------------------------------- |
40
+ | `refetchOnFocus` | `boolean` | `false` | Refetch when window regains focus |
41
+ | `refetchOnReconnect` | `boolean` | `false` | Refetch when network reconnects |
42
+
43
+ ### Per-Request Options
44
+
45
+ | Option | Type | Description |
46
+ | -------------------- | --------- | ----------------------------------- |
47
+ | `refetchOnFocus` | `boolean` | Override focus refetch behavior |
48
+ | `refetchOnReconnect` | `boolean` | Override reconnect refetch behavior |
@@ -0,0 +1,49 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ interface RefetchPluginConfig {
4
+ /** Automatically refetch when window regains focus. Defaults to `false`. */
5
+ refetchOnFocus?: boolean;
6
+ /** Automatically refetch when network reconnects. Defaults to `false`. */
7
+ refetchOnReconnect?: boolean;
8
+ }
9
+ interface RefetchReadOptions {
10
+ /** Automatically refetch when window regains focus. Overrides plugin default. */
11
+ refetchOnFocus?: boolean;
12
+ /** Automatically refetch when network reconnects. Overrides plugin default. */
13
+ refetchOnReconnect?: boolean;
14
+ }
15
+ type RefetchWriteOptions = object;
16
+ type RefetchInfiniteReadOptions = object;
17
+ type RefetchReadResult = object;
18
+ type RefetchWriteResult = object;
19
+
20
+ /**
21
+ * Enables automatic refetching on window focus and network reconnect.
22
+ *
23
+ * @param config - Plugin configuration
24
+ *
25
+ * @see {@link https://spoosh.dev/docs/plugins/refetch | Refetch Plugin Documentation}
26
+ *
27
+ * @returns Refetch plugin instance
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const plugins = [
32
+ * refetchPlugin({ refetchOnFocus: true, refetchOnReconnect: true }),
33
+ * ];
34
+ *
35
+ * // Per-query override
36
+ * useRead((api) => api.posts.$get(), {
37
+ * refetchOnFocus: false, // Disable for this query
38
+ * });
39
+ * ```
40
+ */
41
+ declare function refetchPlugin(config?: RefetchPluginConfig): SpooshPlugin<{
42
+ readOptions: RefetchReadOptions;
43
+ writeOptions: RefetchWriteOptions;
44
+ infiniteReadOptions: RefetchInfiniteReadOptions;
45
+ readResult: RefetchReadResult;
46
+ writeResult: RefetchWriteResult;
47
+ }>;
48
+
49
+ export { type RefetchInfiniteReadOptions, type RefetchPluginConfig, type RefetchReadOptions, type RefetchReadResult, type RefetchWriteOptions, type RefetchWriteResult, refetchPlugin };
@@ -0,0 +1,49 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ interface RefetchPluginConfig {
4
+ /** Automatically refetch when window regains focus. Defaults to `false`. */
5
+ refetchOnFocus?: boolean;
6
+ /** Automatically refetch when network reconnects. Defaults to `false`. */
7
+ refetchOnReconnect?: boolean;
8
+ }
9
+ interface RefetchReadOptions {
10
+ /** Automatically refetch when window regains focus. Overrides plugin default. */
11
+ refetchOnFocus?: boolean;
12
+ /** Automatically refetch when network reconnects. Overrides plugin default. */
13
+ refetchOnReconnect?: boolean;
14
+ }
15
+ type RefetchWriteOptions = object;
16
+ type RefetchInfiniteReadOptions = object;
17
+ type RefetchReadResult = object;
18
+ type RefetchWriteResult = object;
19
+
20
+ /**
21
+ * Enables automatic refetching on window focus and network reconnect.
22
+ *
23
+ * @param config - Plugin configuration
24
+ *
25
+ * @see {@link https://spoosh.dev/docs/plugins/refetch | Refetch Plugin Documentation}
26
+ *
27
+ * @returns Refetch plugin instance
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const plugins = [
32
+ * refetchPlugin({ refetchOnFocus: true, refetchOnReconnect: true }),
33
+ * ];
34
+ *
35
+ * // Per-query override
36
+ * useRead((api) => api.posts.$get(), {
37
+ * refetchOnFocus: false, // Disable for this query
38
+ * });
39
+ * ```
40
+ */
41
+ declare function refetchPlugin(config?: RefetchPluginConfig): SpooshPlugin<{
42
+ readOptions: RefetchReadOptions;
43
+ writeOptions: RefetchWriteOptions;
44
+ infiniteReadOptions: RefetchInfiniteReadOptions;
45
+ readResult: RefetchReadResult;
46
+ writeResult: RefetchWriteResult;
47
+ }>;
48
+
49
+ export { type RefetchInfiniteReadOptions, type RefetchPluginConfig, type RefetchReadOptions, type RefetchReadResult, type RefetchWriteOptions, type RefetchWriteResult, refetchPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ refetchPlugin: () => refetchPlugin
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/plugin.ts
28
+ function refetchPlugin(config = {}) {
29
+ const { refetchOnFocus = false, refetchOnReconnect = false } = config;
30
+ const listenersByHook = /* @__PURE__ */ new Map();
31
+ const isBrowser = typeof window !== "undefined";
32
+ const setupFocusListener = (hookId, queryKey, eventEmitter) => {
33
+ if (!isBrowser) return;
34
+ const visibilityHandler = () => {
35
+ if (document.visibilityState === "visible") {
36
+ eventEmitter.emit("refetch", {
37
+ queryKey,
38
+ reason: "focus"
39
+ });
40
+ }
41
+ };
42
+ const focusHandler = () => {
43
+ eventEmitter.emit("refetch", {
44
+ queryKey,
45
+ reason: "focus"
46
+ });
47
+ };
48
+ document.addEventListener("visibilitychange", visibilityHandler);
49
+ window.addEventListener("focus", focusHandler);
50
+ const entry = listenersByHook.get(hookId) ?? { queryKey };
51
+ entry.queryKey = queryKey;
52
+ entry.focusCleanup = () => {
53
+ document.removeEventListener("visibilitychange", visibilityHandler);
54
+ window.removeEventListener("focus", focusHandler);
55
+ };
56
+ listenersByHook.set(hookId, entry);
57
+ };
58
+ const setupReconnectListener = (hookId, queryKey, eventEmitter) => {
59
+ if (!isBrowser) return;
60
+ const handler = () => {
61
+ eventEmitter.emit("refetch", {
62
+ queryKey,
63
+ reason: "reconnect"
64
+ });
65
+ };
66
+ window.addEventListener("online", handler);
67
+ const entry = listenersByHook.get(hookId) ?? { queryKey };
68
+ entry.queryKey = queryKey;
69
+ entry.reconnectCleanup = () => {
70
+ window.removeEventListener("online", handler);
71
+ };
72
+ listenersByHook.set(hookId, entry);
73
+ };
74
+ const cleanupHook = (hookId) => {
75
+ const entry = listenersByHook.get(hookId);
76
+ if (entry) {
77
+ entry.focusCleanup?.();
78
+ entry.reconnectCleanup?.();
79
+ listenersByHook.delete(hookId);
80
+ }
81
+ };
82
+ const hasFocusListener = (hookId) => {
83
+ return listenersByHook.get(hookId)?.focusCleanup !== void 0;
84
+ };
85
+ const hasReconnectListener = (hookId) => {
86
+ return listenersByHook.get(hookId)?.reconnectCleanup !== void 0;
87
+ };
88
+ const removeFocusListener = (hookId) => {
89
+ const entry = listenersByHook.get(hookId);
90
+ if (entry?.focusCleanup) {
91
+ entry.focusCleanup();
92
+ entry.focusCleanup = void 0;
93
+ }
94
+ };
95
+ const removeReconnectListener = (hookId) => {
96
+ const entry = listenersByHook.get(hookId);
97
+ if (entry?.reconnectCleanup) {
98
+ entry.reconnectCleanup();
99
+ entry.reconnectCleanup = void 0;
100
+ }
101
+ };
102
+ return {
103
+ name: "spoosh:refetch",
104
+ operations: ["read", "infiniteRead"],
105
+ lifecycle: {
106
+ onMount(context) {
107
+ const { queryKey, eventEmitter, hookId } = context;
108
+ if (!hookId) return;
109
+ const pluginOptions = context.pluginOptions;
110
+ const shouldRefetchOnFocus = pluginOptions?.refetchOnFocus ?? refetchOnFocus;
111
+ const shouldRefetchOnReconnect = pluginOptions?.refetchOnReconnect ?? refetchOnReconnect;
112
+ if (shouldRefetchOnFocus) {
113
+ setupFocusListener(hookId, queryKey, eventEmitter);
114
+ }
115
+ if (shouldRefetchOnReconnect) {
116
+ setupReconnectListener(hookId, queryKey, eventEmitter);
117
+ }
118
+ },
119
+ onUpdate(context) {
120
+ const { queryKey, eventEmitter, hookId } = context;
121
+ if (!hookId) return;
122
+ const pluginOptions = context.pluginOptions;
123
+ const shouldRefetchOnFocus = pluginOptions?.refetchOnFocus ?? refetchOnFocus;
124
+ const shouldRefetchOnReconnect = pluginOptions?.refetchOnReconnect ?? refetchOnReconnect;
125
+ const entry = listenersByHook.get(hookId);
126
+ const queryKeyChanged = entry && entry.queryKey !== queryKey;
127
+ if (queryKeyChanged) {
128
+ cleanupHook(hookId);
129
+ }
130
+ if (shouldRefetchOnFocus && !hasFocusListener(hookId)) {
131
+ setupFocusListener(hookId, queryKey, eventEmitter);
132
+ } else if (!shouldRefetchOnFocus && hasFocusListener(hookId)) {
133
+ removeFocusListener(hookId);
134
+ }
135
+ if (shouldRefetchOnReconnect && !hasReconnectListener(hookId)) {
136
+ setupReconnectListener(hookId, queryKey, eventEmitter);
137
+ } else if (!shouldRefetchOnReconnect && hasReconnectListener(hookId)) {
138
+ removeReconnectListener(hookId);
139
+ }
140
+ },
141
+ onUnmount(context) {
142
+ if (context.hookId) {
143
+ cleanupHook(context.hookId);
144
+ }
145
+ }
146
+ }
147
+ };
148
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,125 @@
1
+ // src/plugin.ts
2
+ function refetchPlugin(config = {}) {
3
+ const { refetchOnFocus = false, refetchOnReconnect = false } = config;
4
+ const listenersByHook = /* @__PURE__ */ new Map();
5
+ const isBrowser = typeof window !== "undefined";
6
+ const setupFocusListener = (hookId, queryKey, eventEmitter) => {
7
+ if (!isBrowser) return;
8
+ const visibilityHandler = () => {
9
+ if (document.visibilityState === "visible") {
10
+ eventEmitter.emit("refetch", {
11
+ queryKey,
12
+ reason: "focus"
13
+ });
14
+ }
15
+ };
16
+ const focusHandler = () => {
17
+ eventEmitter.emit("refetch", {
18
+ queryKey,
19
+ reason: "focus"
20
+ });
21
+ };
22
+ document.addEventListener("visibilitychange", visibilityHandler);
23
+ window.addEventListener("focus", focusHandler);
24
+ const entry = listenersByHook.get(hookId) ?? { queryKey };
25
+ entry.queryKey = queryKey;
26
+ entry.focusCleanup = () => {
27
+ document.removeEventListener("visibilitychange", visibilityHandler);
28
+ window.removeEventListener("focus", focusHandler);
29
+ };
30
+ listenersByHook.set(hookId, entry);
31
+ };
32
+ const setupReconnectListener = (hookId, queryKey, eventEmitter) => {
33
+ if (!isBrowser) return;
34
+ const handler = () => {
35
+ eventEmitter.emit("refetch", {
36
+ queryKey,
37
+ reason: "reconnect"
38
+ });
39
+ };
40
+ window.addEventListener("online", handler);
41
+ const entry = listenersByHook.get(hookId) ?? { queryKey };
42
+ entry.queryKey = queryKey;
43
+ entry.reconnectCleanup = () => {
44
+ window.removeEventListener("online", handler);
45
+ };
46
+ listenersByHook.set(hookId, entry);
47
+ };
48
+ const cleanupHook = (hookId) => {
49
+ const entry = listenersByHook.get(hookId);
50
+ if (entry) {
51
+ entry.focusCleanup?.();
52
+ entry.reconnectCleanup?.();
53
+ listenersByHook.delete(hookId);
54
+ }
55
+ };
56
+ const hasFocusListener = (hookId) => {
57
+ return listenersByHook.get(hookId)?.focusCleanup !== void 0;
58
+ };
59
+ const hasReconnectListener = (hookId) => {
60
+ return listenersByHook.get(hookId)?.reconnectCleanup !== void 0;
61
+ };
62
+ const removeFocusListener = (hookId) => {
63
+ const entry = listenersByHook.get(hookId);
64
+ if (entry?.focusCleanup) {
65
+ entry.focusCleanup();
66
+ entry.focusCleanup = void 0;
67
+ }
68
+ };
69
+ const removeReconnectListener = (hookId) => {
70
+ const entry = listenersByHook.get(hookId);
71
+ if (entry?.reconnectCleanup) {
72
+ entry.reconnectCleanup();
73
+ entry.reconnectCleanup = void 0;
74
+ }
75
+ };
76
+ return {
77
+ name: "spoosh:refetch",
78
+ operations: ["read", "infiniteRead"],
79
+ lifecycle: {
80
+ onMount(context) {
81
+ const { queryKey, eventEmitter, hookId } = context;
82
+ if (!hookId) return;
83
+ const pluginOptions = context.pluginOptions;
84
+ const shouldRefetchOnFocus = pluginOptions?.refetchOnFocus ?? refetchOnFocus;
85
+ const shouldRefetchOnReconnect = pluginOptions?.refetchOnReconnect ?? refetchOnReconnect;
86
+ if (shouldRefetchOnFocus) {
87
+ setupFocusListener(hookId, queryKey, eventEmitter);
88
+ }
89
+ if (shouldRefetchOnReconnect) {
90
+ setupReconnectListener(hookId, queryKey, eventEmitter);
91
+ }
92
+ },
93
+ onUpdate(context) {
94
+ const { queryKey, eventEmitter, hookId } = context;
95
+ if (!hookId) return;
96
+ const pluginOptions = context.pluginOptions;
97
+ const shouldRefetchOnFocus = pluginOptions?.refetchOnFocus ?? refetchOnFocus;
98
+ const shouldRefetchOnReconnect = pluginOptions?.refetchOnReconnect ?? refetchOnReconnect;
99
+ const entry = listenersByHook.get(hookId);
100
+ const queryKeyChanged = entry && entry.queryKey !== queryKey;
101
+ if (queryKeyChanged) {
102
+ cleanupHook(hookId);
103
+ }
104
+ if (shouldRefetchOnFocus && !hasFocusListener(hookId)) {
105
+ setupFocusListener(hookId, queryKey, eventEmitter);
106
+ } else if (!shouldRefetchOnFocus && hasFocusListener(hookId)) {
107
+ removeFocusListener(hookId);
108
+ }
109
+ if (shouldRefetchOnReconnect && !hasReconnectListener(hookId)) {
110
+ setupReconnectListener(hookId, queryKey, eventEmitter);
111
+ } else if (!shouldRefetchOnReconnect && hasReconnectListener(hookId)) {
112
+ removeReconnectListener(hookId);
113
+ }
114
+ },
115
+ onUnmount(context) {
116
+ if (context.hookId) {
117
+ cleanupHook(context.hookId);
118
+ }
119
+ }
120
+ }
121
+ };
122
+ }
123
+ export {
124
+ refetchPlugin
125
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@spoosh/plugin-refetch",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Auto-refetch plugin for Spoosh - refetch on focus and reconnect",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nxnom/spoosh.git",
9
+ "directory": "packages/plugin-refetch"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nxnom/spoosh/issues"
13
+ },
14
+ "homepage": "https://spoosh.dev/docs/plugins/refetch",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "keywords": [
19
+ "spoosh",
20
+ "plugin",
21
+ "refetch",
22
+ "api-client",
23
+ "focus",
24
+ "reconnect"
25
+ ],
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.mjs",
33
+ "require": "./dist/index.js"
34
+ }
35
+ },
36
+ "peerDependencies": {
37
+ "@spoosh/core": ">=0.1.0"
38
+ },
39
+ "devDependencies": {
40
+ "@spoosh/core": "0.1.0-beta.0",
41
+ "@spoosh/test-utils": "0.1.0-beta.0"
42
+ },
43
+ "scripts": {
44
+ "dev": "tsup --watch",
45
+ "build": "tsup",
46
+ "typecheck": "tsc --noEmit",
47
+ "lint": "eslint src --max-warnings 0",
48
+ "format": "prettier --write 'src/**/*.ts'"
49
+ }
50
+ }