diff --git a/docs/nostr-patterns/replaceable-events.md b/docs/nostr-patterns/replaceable-events.md index 0cad379..f02158b 100644 --- a/docs/nostr-patterns/replaceable-events.md +++ b/docs/nostr-patterns/replaceable-events.md @@ -7,15 +7,19 @@ in this file follows from that single fact. ## Strictly-monotonic `created_at` per coord -**Canonical:** `src/modules/events/composables/useRSVP.ts` — -`lastPublishAt` map + the `Math.max(now, previous + 1)` line. +**Canonical helper:** `src/lib/nostr/timestamp.ts` — +`monotonicCreatedAt(lastCreatedAt, now?)` returns `max(now, last + 1)`. +Use it for **every** replaceable-event publish; track the last +`created_at` per coord (a `Map` when one composable +publishes many coords like `useRSVP.ts`, or a single field when there's +one coord per user like `useBookmarks.ts`' kind-10003 list). ```ts +import { monotonicCreatedAt } from '@/lib/nostr/timestamp' + const lastPublishAt = new Map() -const now = Math.floor(Date.now() / 1000) -const previous = lastPublishAt.get(coord) ?? 0 -const createdAt = Math.max(now, previous + 1) +const createdAt = monotonicCreatedAt(lastPublishAt.get(coord)) … lastPublishAt.set(coord, signedEvent.created_at) // only after publish success ```