From d30d40909760a55d83366a2d4fbe25c8f9580877 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 18 Jun 2026 12:58:13 +0800 Subject: [PATCH] refactor(plugin-state): dedupe update serialization --- src/plugin-state/plugin-state-store.ts | 43 +++++++++++++------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/plugin-state/plugin-state-store.ts b/src/plugin-state/plugin-state-store.ts index d06e3392e5fb..8cef64b49f1a 100644 --- a/src/plugin-state/plugin-state-store.ts +++ b/src/plugin-state/plugin-state-store.ts @@ -228,6 +228,25 @@ function prepareRegisterParams( }; } +function prepareUpdateValueJson( + key: string, + updateValue: (current: T | undefined) => T | undefined, + defaultTtlMs?: number, + opts?: { ttlMs?: number }, +): (current: unknown) => { valueJson: string; ttlMs?: number } | undefined { + return (current) => { + const next = updateValue(current as T | undefined); + if (next === undefined) { + return undefined; + } + const prepared = prepareRegisterParams(key, next, defaultTtlMs, opts); + return { + valueJson: prepared.valueJson, + ...(prepared.ttlMs != null ? { ttlMs: prepared.ttlMs } : {}), + }; + }; +} + function assertConsistentOptions( pluginId: string, namespace: string, @@ -294,17 +313,7 @@ function createKeyedStoreForPluginId( namespace, key: normalizedKey, maxEntries, - updateValueJson: (current) => { - const next = updateValue(current as T | undefined); - if (next === undefined) { - return undefined; - } - const params = prepareRegisterParams(normalizedKey, next, defaultTtlMs, opts); - return { - valueJson: params.valueJson, - ...(params.ttlMs != null ? { ttlMs: params.ttlMs } : {}), - }; - }, + updateValueJson: prepareUpdateValueJson(normalizedKey, updateValue, defaultTtlMs, opts), ...(env ? { env } : {}), }); }, @@ -390,17 +399,7 @@ function createSyncKeyedStoreForPluginId( namespace, key: normalizedKey, maxEntries, - updateValueJson: (current) => { - const next = updateValue(current as T | undefined); - if (next === undefined) { - return undefined; - } - const params = prepareRegisterParams(normalizedKey, next, defaultTtlMs, opts); - return { - valueJson: params.valueJson, - ...(params.ttlMs != null ? { ttlMs: params.ttlMs } : {}), - }; - }, + updateValueJson: prepareUpdateValueJson(normalizedKey, updateValue, defaultTtlMs, opts), ...(env ? { env } : {}), }); },