@banou/social 0.0.4 → 0.0.5

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.d.mts CHANGED
@@ -184,6 +184,9 @@ type Participant = {
184
184
  name: string;
185
185
  displayName: string;
186
186
  avatar: string | null;
187
+ identicon?: string;
188
+ signKey?: string;
189
+ nickname?: string | null;
187
190
  } | null;
188
191
  self: boolean;
189
192
  };
@@ -245,7 +248,10 @@ type Hub = {
245
248
  text: string;
246
249
  }) => Promise<CommentAnswer>;
247
250
  state: () => Promise<Availability>;
248
- attach: (binding: Omit<RoomBinding, 'send'>) => Promise<{
251
+ attach: (binding: Omit<RoomBinding, 'send'> & {
252
+ send?: (frame: string) => Promise<void>;
253
+ onParticipants?: (participants: Participant[]) => void;
254
+ }) => Promise<{
249
255
  participants: () => Promise<Participant[]>;
250
256
  ingest: (from: string, frame: string, at: number, seq: number) => Promise<boolean>;
251
257
  memberJoined: (memberId: string) => Promise<void>;
package/dist/index.mjs CHANGED
@@ -109,62 +109,92 @@ const inertPresence = (selfMemberId) => {
109
109
  const createSocial = (connect) => {
110
110
  let hub = null;
111
111
  let negotiated = null;
112
- let reach = null;
112
+ let outdated = false;
113
113
  const ensure = async () => {
114
- if (reach !== null && reach !== "ready") return reach;
115
- if (hub !== null && negotiated?.usable === true) return reach ?? "ready";
114
+ if (outdated) return "outdated";
116
115
  try {
117
- const connected = await connect();
118
- const negotiation = negotiate(await connected.hello());
119
- if (!negotiation.usable) {
120
- reach = "outdated";
121
- return reach;
116
+ if (hub === null || negotiated === null) {
117
+ const connected = await connect();
118
+ const negotiation = negotiate(await connected.hello());
119
+ if (!negotiation.usable) {
120
+ outdated = true;
121
+ return "outdated";
122
+ }
123
+ hub = connected;
124
+ negotiated = negotiation;
122
125
  }
123
- hub = connected;
124
- negotiated = negotiation;
125
- reach = await connected.state();
126
- return reach;
126
+ return await hub.state();
127
127
  } catch {
128
- reach = "unavailable";
129
- return reach;
128
+ hub = null;
129
+ negotiated = null;
130
+ return "unavailable";
130
131
  }
131
132
  };
132
133
  const available = async () => ensure();
133
134
  const attach = async (binding) => {
134
- if (await ensure() !== "ready" || hub === null || negotiated === null || !supports(negotiated, SINCE.attach)) return inertPresence(binding.selfMemberId);
135
+ const state = await ensure();
136
+ if (state === "unavailable" || state === "outdated" || state === "app-not-connected" || hub === null || negotiated === null || !supports(negotiated, SINCE.attach)) return inertPresence(binding.selfMemberId);
135
137
  try {
138
+ let cached = [];
139
+ const listeners = /* @__PURE__ */ new Set();
140
+ let detached = false;
141
+ let version = 0;
142
+ let pushVersion = 0;
143
+ const emit = (next) => {
144
+ if (detached) return;
145
+ cached = next;
146
+ for (const listener of listeners) listener(cached);
147
+ };
136
148
  const remote = await hub.attach({
137
149
  roomId: binding.roomId,
138
150
  selfMemberId: binding.selfMemberId,
139
- observe: binding.observe
151
+ observe: binding.observe,
152
+ send: async (frame) => {
153
+ await binding.send(frame);
154
+ },
155
+ onParticipants: (next) => {
156
+ version += 1;
157
+ pushVersion = version;
158
+ emit(next);
159
+ }
140
160
  });
141
- let cached = [];
142
- const listeners = /* @__PURE__ */ new Set();
143
161
  const refresh = async () => {
144
- cached = await remote.participants();
145
- for (const listener of listeners) listener(cached);
162
+ const reqVersion = ++version;
163
+ try {
164
+ const next = await remote.participants();
165
+ if (detached || reqVersion < pushVersion || reqVersion < version) return;
166
+ emit(next);
167
+ } catch {}
146
168
  };
147
169
  refresh();
148
170
  return {
149
171
  participants: () => cached,
150
172
  of: (memberId) => cached.find((p) => p.memberId === memberId) ?? silentParticipant(memberId, binding.selfMemberId),
151
173
  on: (listener) => {
174
+ if (detached) return () => {};
152
175
  listeners.add(listener);
153
176
  return () => listeners.delete(listener);
154
177
  },
155
178
  ingest: (from, frame, at, seq) => {
156
- if (!isSocialFrame(frame)) return false;
179
+ if (detached || !isSocialFrame(frame)) return false;
157
180
  remote.ingest(from, frame, at, seq).then(refresh, () => {});
158
181
  return true;
159
182
  },
160
183
  memberJoined: (memberId) => {
184
+ if (detached) return;
161
185
  remote.memberJoined(memberId).then(refresh, () => {});
162
186
  },
163
187
  memberLeft: (memberId) => {
188
+ if (detached) return;
164
189
  remote.memberLeft(memberId).then(refresh, () => {});
165
190
  },
166
- announce: () => remote.announce(),
167
- detach: () => remote.detach()
191
+ announce: () => detached ? Promise.resolve() : remote.announce(),
192
+ detach: async () => {
193
+ if (detached) return;
194
+ detached = true;
195
+ listeners.clear();
196
+ await remote.detach().catch(() => {});
197
+ }
168
198
  };
169
199
  } catch {
170
200
  return inertPresence(binding.selfMemberId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banou/social",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Identity and friends across FKN apps. One signing key, a proven announcement inside a room, and a friend list that syncs with the account.",
5
5
  "license": "MIT",
6
6
  "type": "module",