22 lines
906 B
TypeScript
22 lines
906 B
TypeScript
/** Stable across database Dates, JSON SSE payloads, and Nuxt hydration. */
|
|
export const serializeTopicSnapshot = (topic: unknown): string => JSON.stringify(topic, (_key, value) => {
|
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
return value;
|
|
}
|
|
|
|
return Object.fromEntries(Object.keys(value).sort().map(key => [key, value[key]]));
|
|
});
|
|
|
|
/** Absolute offsets make replay across a snapshot boundary idempotent. */
|
|
export const appendTextDelta = (current: string, content: string, offset?: number): string => {
|
|
if (offset === undefined) {
|
|
return current + content;
|
|
}
|
|
if (offset > current.length) {
|
|
// A gap requires reconciliation, not silently concatenating corrupt text.
|
|
return current;
|
|
}
|
|
const overlap = current.length - offset;
|
|
return overlap >= content.length ? current : current + content.slice(overlap);
|
|
};
|