UNPKG

@yoyo-org/progressive-json

Version:

Stream and render JSON data as it arrives - perfect for AI responses, large datasets, and real-time updates

34 lines (33 loc) 967 B
/** * Server-side message creator for increment plugin */ export function increment(key, value) { return { type: "increment", key, value }; } /** * Increment Plugin * * Handles "increment" message type that increments a numeric value. * * Example server usage: * ```ts * writer(increment(counterRef)); // Increments by 1 * writer(increment(counterRef, 5)); // Increments by 5 * ``` */ export const incrementPlugin = { type: "increment", handleMessage: (message, store, context) => { var _a; const incrementAmount = (_a = message.value) !== null && _a !== void 0 ? _a : 1; return context.updateAtPath(store, message.key, (obj, lastKey) => { const currentValue = obj[lastKey]; if (typeof currentValue === "number") { obj[lastKey] = currentValue + incrementAmount; } else { obj[lastKey] = incrementAmount; } }); }, };