refactor(plugin-state): dedupe update serialization

This commit is contained in:
Vincent Koc
2026-06-18 12:58:13 +08:00
parent 77a877e869
commit d30d409097

View File

@@ -228,6 +228,25 @@ function prepareRegisterParams(
};
}
function prepareUpdateValueJson<T>(
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<T>(
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<T>(
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 } : {}),
});
},