@spoosh/plugin-gc 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -37,7 +37,7 @@ type GcPluginExports = {
37
37
  * ```ts
38
38
  * import { Spoosh } from "@spoosh/core";
39
39
  *
40
- * const client = new Spoosh<ApiSchema, Error>("/api")
40
+ * const spoosh = new Spoosh<ApiSchema, Error>("/api")
41
41
  * .use([
42
42
  * gcPlugin({
43
43
  * maxAge: 60000, // Remove entries older than 1 minute
@@ -47,7 +47,7 @@ type GcPluginExports = {
47
47
  * ]);
48
48
  *
49
49
  * // Manual GC trigger
50
- * const { runGc } = createReactSpoosh(client);
50
+ * const { runGc } = create(client);
51
51
  * const removedCount = runGc();
52
52
  * ```
53
53
  */
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ type GcPluginExports = {
37
37
  * ```ts
38
38
  * import { Spoosh } from "@spoosh/core";
39
39
  *
40
- * const client = new Spoosh<ApiSchema, Error>("/api")
40
+ * const spoosh = new Spoosh<ApiSchema, Error>("/api")
41
41
  * .use([
42
42
  * gcPlugin({
43
43
  * maxAge: 60000, // Remove entries older than 1 minute
@@ -47,7 +47,7 @@ type GcPluginExports = {
47
47
  * ]);
48
48
  *
49
49
  * // Manual GC trigger
50
- * const { runGc } = createReactSpoosh(client);
50
+ * const { runGc } = create(client);
51
51
  * const removedCount = runGc();
52
52
  * ```
53
53
  */
package/dist/index.js CHANGED
@@ -25,7 +25,8 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/plugin.ts
28
- function runGarbageCollection(stateManager, options) {
28
+ var PLUGIN_NAME = "spoosh:gc";
29
+ function runGarbageCollection(stateManager, eventTracer, options) {
29
30
  const { maxAge, maxEntries } = options;
30
31
  const now = Date.now();
31
32
  let removedCount = 0;
@@ -61,23 +62,44 @@ function runGarbageCollection(stateManager, options) {
61
62
  }
62
63
  }
63
64
  }
65
+ if (removedCount > 0) {
66
+ eventTracer?.emit(`Cleaned ${removedCount} cache entries`, {
67
+ color: "success",
68
+ meta: { removed: removedCount, total: entries.length }
69
+ });
70
+ }
64
71
  return removedCount;
65
72
  }
66
73
  function gcPlugin(options = {}) {
67
74
  const { interval = 6e4 } = options;
75
+ let runGcFn;
68
76
  return {
69
- name: "spoosh:gc",
77
+ name: PLUGIN_NAME,
70
78
  operations: [],
71
- instanceApi(context) {
79
+ setup(context) {
72
80
  const { stateManager } = context;
73
- const runGc = () => {
74
- return runGarbageCollection(stateManager, options);
81
+ const et = context.eventTracer?.(PLUGIN_NAME);
82
+ runGcFn = () => {
83
+ return runGarbageCollection(stateManager, et, options);
75
84
  };
85
+ if (typeof window === "undefined") {
86
+ return;
87
+ }
88
+ et?.emit(`GC scheduled every ${interval}ms`, {
89
+ color: "info",
90
+ meta: {
91
+ interval,
92
+ maxAge: options.maxAge,
93
+ maxEntries: options.maxEntries
94
+ }
95
+ });
76
96
  setInterval(() => {
77
- runGc();
97
+ runGcFn?.();
78
98
  }, interval);
99
+ },
100
+ instanceApi() {
79
101
  return {
80
- runGc
102
+ runGc: () => runGcFn?.() ?? 0
81
103
  };
82
104
  }
83
105
  };
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/plugin.ts
2
- function runGarbageCollection(stateManager, options) {
2
+ var PLUGIN_NAME = "spoosh:gc";
3
+ function runGarbageCollection(stateManager, eventTracer, options) {
3
4
  const { maxAge, maxEntries } = options;
4
5
  const now = Date.now();
5
6
  let removedCount = 0;
@@ -35,23 +36,44 @@ function runGarbageCollection(stateManager, options) {
35
36
  }
36
37
  }
37
38
  }
39
+ if (removedCount > 0) {
40
+ eventTracer?.emit(`Cleaned ${removedCount} cache entries`, {
41
+ color: "success",
42
+ meta: { removed: removedCount, total: entries.length }
43
+ });
44
+ }
38
45
  return removedCount;
39
46
  }
40
47
  function gcPlugin(options = {}) {
41
48
  const { interval = 6e4 } = options;
49
+ let runGcFn;
42
50
  return {
43
- name: "spoosh:gc",
51
+ name: PLUGIN_NAME,
44
52
  operations: [],
45
- instanceApi(context) {
53
+ setup(context) {
46
54
  const { stateManager } = context;
47
- const runGc = () => {
48
- return runGarbageCollection(stateManager, options);
55
+ const et = context.eventTracer?.(PLUGIN_NAME);
56
+ runGcFn = () => {
57
+ return runGarbageCollection(stateManager, et, options);
49
58
  };
59
+ if (typeof window === "undefined") {
60
+ return;
61
+ }
62
+ et?.emit(`GC scheduled every ${interval}ms`, {
63
+ color: "info",
64
+ meta: {
65
+ interval,
66
+ maxAge: options.maxAge,
67
+ maxEntries: options.maxEntries
68
+ }
69
+ });
50
70
  setInterval(() => {
51
- runGc();
71
+ runGcFn?.();
52
72
  }, interval);
73
+ },
74
+ instanceApi() {
53
75
  return {
54
- runGc
76
+ runGc: () => runGcFn?.() ?? 0
55
77
  };
56
78
  }
57
79
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-gc",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "description": "Garbage collection plugin for Spoosh cache with time and size-based cleanup",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,11 +34,11 @@
34
34
  }
35
35
  },
36
36
  "peerDependencies": {
37
- "@spoosh/core": ">=0.12.0"
37
+ "@spoosh/core": ">=0.13.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@spoosh/core": "0.12.0",
41
- "@spoosh/test-utils": "0.1.8"
40
+ "@spoosh/test-utils": "0.2.0",
41
+ "@spoosh/core": "0.13.0"
42
42
  },
43
43
  "scripts": {
44
44
  "dev": "tsup --watch",