@fabriktor/fx 0.0.191 → 0.0.193

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.
@@ -55,19 +55,36 @@ export function projectQueuedChats(chats, snapshot, ownerID, roomID) {
55
55
  const sequences = new Map(entries
56
56
  .filter((entry) => entry.server_id && entry.sequence !== undefined)
57
57
  .map((entry) => [entry.server_id, entry.sequence]));
58
+ let afterPending = 0;
58
59
  for (const entry of entries) {
59
60
  // A room event may arrive before this entry's independent confirmation
60
61
  // read. Match the server ID so identical messages remain distinct.
61
62
  if (!entry.chat && (!entry.server_id || !confirmed.has(entry.server_id))) {
62
63
  // Out-of-order reads must not move a later outgoing bubble ahead of an
63
64
  // earlier pending one. Canonical messages keep their server ordering.
64
- const before = result.findIndex((chat) => {
65
- const sequence = sequences.get(chat.id) ?? snapshot.confirmed_sequences?.[chat.id];
66
- return (sequence !== undefined &&
65
+ let after = afterPending;
66
+ for (let index = after; index < result.length; index++) {
67
+ const sequence = sequences.get(result[index].id) ??
68
+ snapshot.confirmed_sequences?.[result[index].id];
69
+ if (sequence !== undefined &&
67
70
  entry.sequence !== undefined &&
68
- sequence > entry.sequence);
71
+ sequence < entry.sequence)
72
+ after = index + 1;
73
+ }
74
+ const before = result.findIndex((chat, index) => {
75
+ if (index < after)
76
+ return false;
77
+ const sequence = sequences.get(chat.id) ?? snapshot.confirmed_sequences?.[chat.id];
78
+ if (sequence !== undefined && entry.sequence !== undefined) {
79
+ return sequence > entry.sequence;
80
+ }
81
+ // Completed entries are no longer in the durable queue after a reload.
82
+ // Keep unresolved messages at their original time, not at the live tail.
83
+ return (Date.parse(chat.created_at) > Date.parse(entry.optimistic.created_at));
69
84
  });
70
- result.splice(before < 0 ? result.length : before, 0, entry.optimistic);
85
+ const index = before < 0 ? result.length : before;
86
+ result.splice(index, 0, entry.optimistic);
87
+ afterPending = index + 1;
71
88
  }
72
89
  }
73
90
  return result.length === chats.length &&
@@ -1,4 +1,4 @@
1
- import type { ChatOperator } from "@fabriktor/client";
1
+ import { type ChatOperator } from "@fabriktor/client";
2
2
  import type { Chat, InChat } from "@fabriktor/schema";
3
3
  import { type EventListener } from "../core/events.js";
4
4
  import { type QueueRetry, type QueueStore, type QueueLock } from "../core/queue.js";
@@ -1,3 +1,4 @@
1
+ import { ClientErrKind, isClientErr, } from "@fabriktor/client";
1
2
  import { requiredOperationID } from "../core/id.js";
2
3
  import { createEvent, EventBus, notifyObserver, } from "../core/events.js";
3
4
  import { PersistentQueue, QueueWakeup, nextQueueRetry, } from "../core/queue.js";
@@ -438,6 +439,8 @@ export class ChatSendQueue {
438
439
  // lock coordinates retries/recovery across tabs without holding later POSTs.
439
440
  startConfirmation(entry, owner, signal) {
440
441
  const known = entry.server_id && this.confirmed.get(entry.server_id);
442
+ if (!known && this.waitForRetry(entry))
443
+ return;
441
444
  if (this.confirmations.has(entry.id) ||
442
445
  (!known && this.opts.online?.() === false)) {
443
446
  return;
@@ -455,6 +458,8 @@ export class ChatSendQueue {
455
458
  (!chatSendCanConfirm(stored) && !this.confirmed.has(stored.server_id))) {
456
459
  return;
457
460
  }
461
+ if (!this.confirmed.has(stored.server_id) && this.waitForRetry(stored))
462
+ return;
458
463
  let current = stored;
459
464
  try {
460
465
  const chat = this.confirmed.get(current.server_id) ??
@@ -500,15 +505,20 @@ export class ChatSendQueue {
500
505
  catch (error) {
501
506
  if (this.current(owner, signal))
502
507
  this.report(error);
508
+ const retry = !signal.aborted && !current.chat && retryableConfirmation(error)
509
+ ? nextQueueRetry(current.retry)
510
+ : undefined;
503
511
  await this.save({
504
512
  ...current,
505
- retry: undefined,
513
+ retry,
506
514
  status: current.chat
507
515
  ? ChatSendStatus.UploadInterrupted
508
516
  : signal.aborted
509
517
  ? ChatSendStatus.Interrupted
510
518
  : ChatSendStatus.Failed,
511
519
  });
520
+ if (retry && this.current(owner, signal))
521
+ this.scheduleRecovery(retry.after);
512
522
  }
513
523
  })
514
524
  .catch((err) => {
@@ -723,7 +733,15 @@ function chatSendCanConfirm(entry) {
723
733
  (entry.status === ChatSendStatus.Confirming ||
724
734
  entry.status === ChatSendStatus.Sending ||
725
735
  entry.status === ChatSendStatus.Queued ||
726
- entry.status === ChatSendStatus.Interrupted));
736
+ entry.status === ChatSendStatus.Interrupted ||
737
+ (entry.status === ChatSendStatus.Failed && !!entry.retry)));
738
+ }
739
+ function retryableConfirmation(error) {
740
+ return (isClientErr(error) &&
741
+ (error.kind === ClientErrKind.HTTPRequestFailed ||
742
+ error.status === 408 ||
743
+ error.status === 429 ||
744
+ (error.status !== undefined && error.status >= 500)));
727
745
  }
728
746
  export function chatSendNeedsRetry(entry) {
729
747
  return (entry.status === ChatSendStatus.Failed ||
@@ -1,6 +1,10 @@
1
1
  import { type ContactsOperator, type UsersOperator } from "@fabriktor/client";
2
2
  import { Friend, Hired, Manager, Na, OldHire, type Contact, type ID, type OperationResult, type User } from "@fabriktor/schema";
3
3
  import { PollScheduler } from "../core/poll.js";
4
+ import { EventBus } from "../core/events.js";
5
+ export declare const ContactsEvents: {
6
+ readonly Changed: import("../index.js").EventKey<void>;
7
+ };
4
8
  type ContactInsertOptions = Parameters<ContactsOperator["insertOneContact"]>[0];
5
9
  type ContactReplaceOptions = Parameters<ContactsOperator["replaceOneContact"]>[0];
6
10
  export type CreateContactOptions = ContactInsertOptions;
@@ -11,6 +15,7 @@ export type ContactsFXOptions = {
11
15
  contacts: ContactsOperator;
12
16
  users: UsersOperator;
13
17
  polling?: PollScheduler;
18
+ events?: EventBus;
14
19
  };
15
20
  export type LoadContactIndexOptions = {
16
21
  owner_id: string;
@@ -76,6 +81,7 @@ export declare class ContactsFX implements ContactsFXOperator {
76
81
  private contacts;
77
82
  private users;
78
83
  private readonly polling;
84
+ private readonly events;
79
85
  constructor(opts: ContactsFXOptions);
80
86
  createContact(opts: CreateContactOptions): Promise<Contact>;
81
87
  replaceContact(opts: ReplaceContactOptions): Promise<Contact>;
@@ -5,6 +5,10 @@ import { normalizeIDs, requiredOperationID } from "../core/id.js";
5
5
  import { mergeRecordsByID } from "../core/scope.js";
6
6
  import { runSearch } from "../core/search.js";
7
7
  import { PollScheduler } from "../core/poll.js";
8
+ import { createEvent, EventBus } from "../core/events.js";
9
+ export const ContactsEvents = {
10
+ Changed: createEvent(),
11
+ };
8
12
  const employmentUserBatchSize = 30;
9
13
  const resourceContact = "contact";
10
14
  const msgCreatedContactIDMissing = "Created contact response did not contain an ID.";
@@ -19,18 +23,22 @@ export class ContactsFX {
19
23
  contacts;
20
24
  users;
21
25
  polling;
26
+ events;
22
27
  constructor(opts) {
23
28
  this.contacts = opts.contacts;
24
29
  this.users = opts.users;
25
30
  this.polling = opts.polling ?? new PollScheduler();
31
+ this.events = opts.events ?? new EventBus();
26
32
  }
27
33
  async createContact(opts) {
28
34
  const out = await this.contacts.insertOneContact(opts);
35
+ this.events.publish(ContactsEvents.Changed, undefined);
29
36
  const id = requiredOperationID(out, msgCreatedContactIDMissing);
30
37
  return await this.findContact(id);
31
38
  }
32
39
  async replaceContact(opts) {
33
40
  await this.contacts.replaceOneContact(opts);
41
+ this.events.publish(ContactsEvents.Changed, undefined);
34
42
  return await this.findContact(opts.id);
35
43
  }
36
44
  async loadContactIndex(opts) {
@@ -162,7 +170,7 @@ export class ContactsFX {
162
170
  msg: msgContactIDRequired,
163
171
  });
164
172
  }
165
- return await this.contacts.insertOneContact({
173
+ const out = await this.contacts.insertOneContact({
166
174
  in: {
167
175
  contact_id,
168
176
  is_pending: true,
@@ -171,6 +179,8 @@ export class ContactsFX {
171
179
  is_archived: false,
172
180
  },
173
181
  });
182
+ this.events.publish(ContactsEvents.Changed, undefined);
183
+ return out;
174
184
  }
175
185
  async requestRelationUpgrade(opts) {
176
186
  if (opts.requested_relation <= opts.contact.relation) {
@@ -228,7 +238,7 @@ export class ContactsFX {
228
238
  });
229
239
  }
230
240
  async replaceContactResult(contact, overrides) {
231
- return await this.contacts.replaceOneContact({
241
+ const out = await this.contacts.replaceOneContact({
232
242
  id: contact.id,
233
243
  in: {
234
244
  contact_id: contact.contact_id,
@@ -239,6 +249,8 @@ export class ContactsFX {
239
249
  ...overrides,
240
250
  },
241
251
  });
252
+ this.events.publish(ContactsEvents.Changed, undefined);
253
+ return out;
242
254
  }
243
255
  async usersByIDs(uid, ids) {
244
256
  const values = normalizeIDs(ids);
package/dist/src/fx.js CHANGED
@@ -49,6 +49,7 @@ export class FX {
49
49
  contacts: opts.contacts,
50
50
  users: opts.users,
51
51
  polling: this.polling,
52
+ events: this.events,
52
53
  });
53
54
  this.billable = newBillableFX({
54
55
  billable: opts.billable,
@@ -1,6 +1,6 @@
1
1
  import { type InUser, type User } from "@fabriktor/schema";
2
2
  export type UserCardInput = Pick<InUser, "bio" | "urls" | "show_as_company" | "banner_id" | "banner_preview">;
3
- export type UserCardIdentity = Pick<User, "id" | "username" | "full_name" | "company_name"> & Partial<Pick<User, "show_as_company">>;
3
+ export type UserCardIdentity = Pick<User, "id" | "username" | "full_name" | "company_name"> & Partial<Pick<User, "account_identity" | "show_as_company">>;
4
4
  export type BannerFrame = {
5
5
  x: number;
6
6
  y: number;
@@ -1,4 +1,4 @@
1
- import { ZeroID, HTTPSScheme, UserURLHostPattern, UserBioMaxLength, UserURLsMaxCount, UserURLMaxLength, UserBannerMaxBytes, UserBannerWidth, UserBannerHeight, } from "@fabriktor/schema";
1
+ import { ZeroID, HTTPSScheme, UserURLHostPattern, UserBioMaxLength, UserURLsMaxCount, UserURLMaxLength, UserBannerMaxBytes, UserBannerWidth, UserBannerHeight, UserTypeEmployer, } from "@fabriktor/schema";
2
2
  import { errValidation } from "../core/err.js";
3
3
  import { publicLinkURL } from "../core/links.js";
4
4
  import { resolvedID } from "../core/id.js";
@@ -9,7 +9,7 @@ const msgBannerRequired = "A valid banner preview is required.";
9
9
  export const defaultBannerFrame = { x: 0.5, y: 0.5, zoom: 1 };
10
10
  export const bannerMaxZoom = 4;
11
11
  export function userDisplayName(user) {
12
- return ((user.show_as_company
12
+ return ((user.account_identity === UserTypeEmployer && user.show_as_company
13
13
  ? user.company_name?.trim()
14
14
  : user.full_name?.trim()) ||
15
15
  user.full_name?.trim() ||
@@ -35,6 +35,7 @@ export type UpdateUserProfileOptions = GetByUIDOptions & {
35
35
  export type PublicUserProfileOptions = Parameters<UsersOperator["publicProfile"]>[0];
36
36
  export type UpdateUserCardOptions = GetByUIDOptions & {
37
37
  id: string;
38
+ account_identity: User["account_identity"];
38
39
  in: UserCardInput;
39
40
  signal?: AbortSignal;
40
41
  };
@@ -165,6 +165,8 @@ export class UsersFX {
165
165
  }
166
166
  async updateUserCard(opts) {
167
167
  const input = normalizeUserCardInput(opts.in);
168
+ input.show_as_company =
169
+ opts.account_identity === UserTypeEmployer && input.show_as_company;
168
170
  validateUserCardInput(input);
169
171
  if (!resolvedID(opts.id) || !opts.uid.trim())
170
172
  throw errValidation({ msg: msgProfileRequired });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.191",
3
+ "version": "0.0.193",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {