@calimero-network/mero-js 23.4.1 → 23.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -2752,6 +2752,14 @@ var SseClient = class {
2752
2752
  this.reconnectTimer = null;
2753
2753
  this.subscribedContextIds = /* @__PURE__ */ new Set();
2754
2754
  this.subscribedGroupIds = /* @__PURE__ */ new Set();
2755
+ /**
2756
+ * The current presence of each subscribed context, as the node's replay and
2757
+ * its live deltas left it: author → that author's last `Ephemeral` data and
2758
+ * when it arrived. The node replays a context's presence once, when the
2759
+ * context is first subscribed on this connection, so a presence listener
2760
+ * that comes later would never see it; {@link presenceSeed} gives it this.
2761
+ */
2762
+ this.presence = /* @__PURE__ */ new Map();
2755
2763
  this.closed = false;
2756
2764
  this.listeners = { connect: [], event: [], error: [] };
2757
2765
  this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
@@ -2917,6 +2925,7 @@ var SseClient = class {
2917
2925
  if (msg.type === "connect" && msg.session_id) {
2918
2926
  this.sessionId = msg.session_id;
2919
2927
  this.emit("connect", msg.session_id);
2928
+ this.presence.clear();
2920
2929
  if (this.subscribedContextIds.size > 0 || this.subscribedGroupIds.size > 0) {
2921
2930
  this.sendSubscription("subscribe", {
2922
2931
  contextIds: [...this.subscribedContextIds],
@@ -2936,6 +2945,7 @@ var SseClient = class {
2936
2945
  }
2937
2946
  }
2938
2947
  if (msg.result.contextId) {
2948
+ if (msg.result.type === "Ephemeral") this.notePresence(msg.result.contextId, eventData);
2939
2949
  this.emit("event", {
2940
2950
  contextId: msg.result.contextId,
2941
2951
  // Forward the event tag (sibling of `data` in core's flattened
@@ -2969,12 +2979,38 @@ var SseClient = class {
2969
2979
  await this.sendSubscription("subscribe", { contextIds: newContextIds, groupIds: newGroupIds });
2970
2980
  }
2971
2981
  }
2982
+ /** Whether this connection holds `contextId`'s events. */
2983
+ isSubscribed(contextId) {
2984
+ return this.subscribedContextIds.has(contextId);
2985
+ }
2986
+ /**
2987
+ * The presence currently known for `contextId`, as `Ephemeral` event data,
2988
+ * each entry's `ageMs` advanced by how long ago it arrived. Empty for a
2989
+ * context this connection does not hold.
2990
+ */
2991
+ presenceSeed(contextId) {
2992
+ const now = Date.now();
2993
+ return [...this.presence.get(contextId)?.values() ?? []].map(({ data, at }) => ({
2994
+ ...data,
2995
+ ageMs: (typeof data.ageMs === "number" ? data.ageMs : 0) + (now - at)
2996
+ }));
2997
+ }
2998
+ notePresence(contextId, data) {
2999
+ if (!this.subscribedContextIds.has(contextId)) return;
3000
+ const d = data;
3001
+ if (!d || typeof d.author !== "string") return;
3002
+ let byAuthor = this.presence.get(contextId);
3003
+ if (!byAuthor) this.presence.set(contextId, byAuthor = /* @__PURE__ */ new Map());
3004
+ if (d.removed) byAuthor.delete(d.author);
3005
+ else byAuthor.set(d.author, { data: d, at: Date.now() });
3006
+ }
2972
3007
  async unsubscribe(idsOrOpts) {
2973
3008
  const opts = Array.isArray(idsOrOpts) ? { contextIds: idsOrOpts } : idsOrOpts;
2974
3009
  const hadContextIds = (opts.contextIds ?? []).filter((id) => this.subscribedContextIds.has(id));
2975
3010
  const hadGroupIds = (opts.groupIds ?? []).filter((id) => this.subscribedGroupIds.has(id));
2976
3011
  for (const id of opts.contextIds ?? []) {
2977
3012
  this.subscribedContextIds.delete(id);
3013
+ this.presence.delete(id);
2978
3014
  }
2979
3015
  for (const id of opts.groupIds ?? []) {
2980
3016
  this.subscribedGroupIds.delete(id);
@@ -3312,6 +3348,11 @@ function subscribePresence(sse, contextId, handler, codec = jsonCodec()) {
3312
3348
  handler(entry);
3313
3349
  };
3314
3350
  sse.on("event", listener);
3351
+ if (sse.isSubscribed?.(contextId)) {
3352
+ for (const data of sse.presenceSeed?.(contextId) ?? []) {
3353
+ listener({ contextId, type: "Ephemeral", data });
3354
+ }
3355
+ }
3315
3356
  void sse.connect().catch(() => void 0);
3316
3357
  void sse.subscribe([contextId]).catch(() => void 0);
3317
3358
  return () => {
@@ -10617,7 +10658,7 @@ function bytes32(value, label2) {
10617
10658
  }
10618
10659
 
10619
10660
  // src/namespace-op/namespace-op.ts
10620
- var SIGNED_NAMESPACE_OP_SCHEMA_VERSION = 20;
10661
+ var SIGNED_NAMESPACE_OP_SCHEMA_VERSION = 21;
10621
10662
  var NAMESPACE_SIGN_DOMAIN = new TextEncoder().encode("calimero.namespace.v1");
10622
10663
  var NAMESPACE_OP_ROOT = 0;
10623
10664
  var ROOT_OP2 = {