@fabriktor/fx 0.0.186 → 0.0.187

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.
@@ -1,5 +1,6 @@
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
+ import { PollScheduler } from "../core/poll.js";
3
4
  type ContactInsertOptions = Parameters<ContactsOperator["insertOneContact"]>[0];
4
5
  type ContactReplaceOptions = Parameters<ContactsOperator["replaceOneContact"]>[0];
5
6
  export type CreateContactOptions = ContactInsertOptions;
@@ -9,6 +10,7 @@ export type ActiveContactRelation = typeof Friend | typeof Hired | typeof Manage
9
10
  export type ContactsFXOptions = {
10
11
  contacts: ContactsOperator;
11
12
  users: UsersOperator;
13
+ polling?: PollScheduler;
12
14
  };
13
15
  export type LoadContactIndexOptions = {
14
16
  owner_id: string;
@@ -73,6 +75,7 @@ export interface ContactsFXOperator {
73
75
  export declare class ContactsFX implements ContactsFXOperator {
74
76
  private contacts;
75
77
  private users;
78
+ private readonly polling;
76
79
  constructor(opts: ContactsFXOptions);
77
80
  createContact(opts: CreateContactOptions): Promise<Contact>;
78
81
  replaceContact(opts: ReplaceContactOptions): Promise<Contact>;
@@ -4,7 +4,7 @@ import { errRequired, errResolveFailed, errValidation } from "../core/err.js";
4
4
  import { normalizeIDs, requiredOperationID } from "../core/id.js";
5
5
  import { mergeRecordsByID } from "../core/scope.js";
6
6
  import { runSearch } from "../core/search.js";
7
- const presenceIntervalMS = 120_000;
7
+ import { PollScheduler } from "../core/poll.js";
8
8
  const employmentUserBatchSize = 30;
9
9
  const resourceContact = "contact";
10
10
  const msgCreatedContactIDMissing = "Created contact response did not contain an ID.";
@@ -18,9 +18,11 @@ const msgPendingRelationRequired = "Contact request does not contain a pending r
18
18
  export class ContactsFX {
19
19
  contacts;
20
20
  users;
21
+ polling;
21
22
  constructor(opts) {
22
23
  this.contacts = opts.contacts;
23
24
  this.users = opts.users;
25
+ this.polling = opts.polling ?? new PollScheduler();
24
26
  }
25
27
  async createContact(opts) {
26
28
  const out = await this.contacts.insertOneContact(opts);
@@ -141,16 +143,14 @@ export class ContactsFX {
141
143
  running = false;
142
144
  }
143
145
  };
144
- const timer = setInterval(() => {
145
- void tick();
146
- }, presenceIntervalMS);
146
+ const stopPolling = this.polling.subscribe(tick);
147
147
  return {
148
148
  stop() {
149
149
  if (stopped) {
150
150
  return;
151
151
  }
152
152
  stopped = true;
153
- clearInterval(timer);
153
+ stopPolling();
154
154
  },
155
155
  };
156
156
  }
@@ -0,0 +1,15 @@
1
+ type PollTask = {
2
+ run: () => void | Promise<void>;
3
+ pending?: Promise<void>;
4
+ };
5
+ export declare class PollScheduler {
6
+ private readonly tasks;
7
+ private timer?;
8
+ private active;
9
+ subscribe(run: PollTask["run"]): () => void;
10
+ setActive(active: boolean): void;
11
+ refresh(): Promise<void>;
12
+ private run;
13
+ private schedule;
14
+ }
15
+ export {};
@@ -0,0 +1,59 @@
1
+ import { notifyObserver } from "./events.js";
2
+ const pollIntervalMS = 120_000;
3
+ // One timer for account background reads. Jobs run independently and never overlap themselves.
4
+ export class PollScheduler {
5
+ tasks = new Set();
6
+ timer;
7
+ active = true;
8
+ subscribe(run) {
9
+ const task = { run };
10
+ this.tasks.add(task);
11
+ this.schedule();
12
+ return () => {
13
+ this.tasks.delete(task);
14
+ this.schedule();
15
+ };
16
+ }
17
+ // Browser/native bindings own visibility and connectivity; FX owns the timer.
18
+ setActive(active) {
19
+ if (active === this.active)
20
+ return;
21
+ this.active = active;
22
+ this.schedule();
23
+ if (active)
24
+ void this.refresh();
25
+ }
26
+ async refresh() {
27
+ if (!this.active)
28
+ return;
29
+ await Promise.all([...this.tasks].map((task) => this.run(task)));
30
+ }
31
+ run(task) {
32
+ if (task.pending)
33
+ return task.pending;
34
+ const pending = Promise.resolve()
35
+ .then(() => {
36
+ if (this.active && this.tasks.has(task))
37
+ return task.run();
38
+ })
39
+ .catch((error) => notifyObserver(() => {
40
+ throw error;
41
+ }))
42
+ .finally(() => {
43
+ task.pending = undefined;
44
+ });
45
+ task.pending = pending;
46
+ return pending;
47
+ }
48
+ schedule() {
49
+ if (!this.active || !this.tasks.size) {
50
+ globalThis.clearInterval(this.timer);
51
+ this.timer = undefined;
52
+ }
53
+ else if (this.timer === undefined) {
54
+ this.timer = globalThis.setInterval(() => {
55
+ void this.refresh();
56
+ }, pollIntervalMS);
57
+ }
58
+ }
59
+ }
package/dist/src/fx.d.ts CHANGED
@@ -6,6 +6,7 @@ import type { CallsFX, CallsFXOperator } from "./calls/calls.js";
6
6
  import type { ChatFX, ChatFXOperator } from "./chat/chat.js";
7
7
  import type { ContactsFX, ContactsFXOperator } from "./contacts/contacts.js";
8
8
  import { EventBus } from "./core/events.js";
9
+ import { PollScheduler } from "./core/poll.js";
9
10
  import type { FeedFX, FeedFXOperator } from "./feed/feed.js";
10
11
  import type { FulfillmentsFX, FulfillmentsFXOperator } from "./fulfillments/fulfillments.js";
11
12
  import type { JobsFX, JobsFXOperator } from "./jobs/jobs.js";
@@ -62,6 +63,7 @@ export type DefaultFXOptions = {
62
63
  };
63
64
  export interface FXOperator {
64
65
  readonly events: EventBus;
66
+ readonly polling: PollScheduler;
65
67
  billable: BillableFXOperator;
66
68
  calls: CallsFXOperator;
67
69
  chat: ChatFXOperator;
@@ -85,6 +87,7 @@ export interface FXOperator {
85
87
  }
86
88
  export declare class FX implements FXOperator {
87
89
  readonly events: EventBus;
90
+ readonly polling: PollScheduler;
88
91
  billable: BillableFX;
89
92
  calls: CallsFX;
90
93
  chat: ChatFX;
package/dist/src/fx.js CHANGED
@@ -4,6 +4,7 @@ import { newCallsFX } from "./calls/calls.js";
4
4
  import { newChatFX } from "./chat/chat.js";
5
5
  import { newContactsFX } from "./contacts/contacts.js";
6
6
  import { EventBus } from "./core/events.js";
7
+ import { PollScheduler } from "./core/poll.js";
7
8
  import { newFeedFX } from "./feed/feed.js";
8
9
  import { newFulfillmentsFX } from "./fulfillments/fulfillments.js";
9
10
  import { newJobsFX } from "./jobs/jobs.js";
@@ -22,6 +23,7 @@ import { newTimesheetsFX } from "./timesheets/timesheets.js";
22
23
  import { newUsersFX } from "./users/users.js";
23
24
  export class FX {
24
25
  events = new EventBus();
26
+ polling = new PollScheduler();
25
27
  billable;
26
28
  calls;
27
29
  chat;
@@ -46,6 +48,7 @@ export class FX {
46
48
  this.contacts = newContactsFX({
47
49
  contacts: opts.contacts,
48
50
  users: opts.users,
51
+ polling: this.polling,
49
52
  });
50
53
  this.billable = newBillableFX({
51
54
  billable: opts.billable,
@@ -18,6 +18,7 @@ export * from "./core/id.js";
18
18
  export * from "./core/links.js";
19
19
  export * from "./core/measurement.js";
20
20
  export * from "./core/multi-create.js";
21
+ export * from "./core/poll.js";
21
22
  export * from "./core/pre-action.js";
22
23
  export * from "./core/pull.js";
23
24
  export * from "./core/query.js";
package/dist/src/index.js CHANGED
@@ -18,6 +18,7 @@ export * from "./core/id.js";
18
18
  export * from "./core/links.js";
19
19
  export * from "./core/measurement.js";
20
20
  export * from "./core/multi-create.js";
21
+ export * from "./core/poll.js";
21
22
  export * from "./core/pre-action.js";
22
23
  export * from "./core/pull.js";
23
24
  export * from "./core/query.js";
@@ -1,6 +1,7 @@
1
- import { type ColorMode, type InPrefApp } from "@fabriktor/schema";
1
+ import { type ColorMode, type InPrefApp, type PrefApp } from "@fabriktor/schema";
2
+ import type { PollScheduler } from "../core/poll.js";
2
3
  import type { PreferencesFXOperator } from "./preferences.js";
3
- export declare const colorModes: readonly ["light", "concrete", "dark"];
4
+ export declare const colorModes: readonly ["light", "dark"];
4
5
  export declare function isColorMode(value: unknown): value is ColorMode;
5
6
  export type AppPreferencesInput = Pick<InPrefApp, "color_mode" | "quiet_mode">;
6
7
  export type AppPreferencesState = AppPreferencesInput & {
@@ -12,6 +13,7 @@ export type AppPreferencesState = AppPreferencesInput & {
12
13
  };
13
14
  export type AppPreferencesOptions = {
14
15
  preferences: Pick<PreferencesFXOperator, "getPrefApp" | "savePrefApp">;
16
+ polling?: PollScheduler;
15
17
  on_change: (state: AppPreferencesState) => void;
16
18
  on_error?: (error: unknown) => void;
17
19
  };
@@ -24,10 +26,16 @@ export declare class AppPreferencesController {
24
26
  private pending?;
25
27
  private saving;
26
28
  private error?;
29
+ private stopPolling?;
30
+ private refreshRequested;
31
+ private channel?;
27
32
  constructor(options: AppPreferencesOptions);
28
33
  get state(): AppPreferencesState;
29
- setOwner(ownerID?: string, identity?: string): void;
34
+ setOwner(ownerID?: string, identity?: string, pref?: PrefApp): void;
35
+ startSync(name: string): () => void;
30
36
  refresh(): Promise<void>;
31
37
  save(input: Partial<AppPreferencesInput>): Promise<void>;
32
38
  private publish;
39
+ private invalidate;
40
+ private flushRefresh;
33
41
  }
@@ -1,11 +1,7 @@
1
- import { ColorModeConcrete, ColorModeDark, ColorModeLight, } from "@fabriktor/schema";
1
+ import { ColorModeDark, ColorModeLight, } from "@fabriktor/schema";
2
2
  import { notifyObserver } from "../core/events.js";
3
3
  import { errRequired, errValidation } from "../core/err.js";
4
- export const colorModes = [
5
- ColorModeLight,
6
- ColorModeConcrete,
7
- ColorModeDark,
8
- ];
4
+ export const colorModes = [ColorModeLight, ColorModeDark];
9
5
  export function isColorMode(value) {
10
6
  return colorModes.some((mode) => mode === value);
11
7
  }
@@ -20,13 +16,17 @@ export class AppPreferencesController {
20
16
  pending;
21
17
  saving = false;
22
18
  error;
19
+ stopPolling;
20
+ refreshRequested = false;
21
+ channel;
23
22
  constructor(options) {
24
23
  this.options = options;
25
24
  }
26
25
  get state() {
27
26
  const quiet = this.current?.quiet_mode ?? false;
27
+ const mode = this.current?.color_mode;
28
28
  return {
29
- color_mode: this.current?.color_mode ?? ColorModeDark,
29
+ color_mode: isColorMode(mode) ? mode : ColorModeDark,
30
30
  quiet_mode: quiet,
31
31
  ready: this.current !== undefined,
32
32
  loading: this.pending !== undefined,
@@ -35,26 +35,60 @@ export class AppPreferencesController {
35
35
  error: this.error,
36
36
  };
37
37
  }
38
- setOwner(ownerID = "", identity = "") {
38
+ // Bootstrap may supply the preference already fetched for this account.
39
+ setOwner(ownerID = "", identity = "", pref) {
39
40
  if (ownerID === this.ownerID && identity === this.identity)
40
41
  return;
41
42
  this.session.abort();
43
+ this.stopPolling?.();
44
+ this.stopPolling = undefined;
42
45
  this.session = new AbortController();
43
46
  this.ownerID = ownerID;
44
47
  this.identity = identity;
45
- this.current = undefined;
48
+ this.current = ownerID ? pref : undefined;
46
49
  this.pending = undefined;
47
50
  this.saving = false;
48
51
  this.error = undefined;
52
+ this.refreshRequested = false;
49
53
  this.publish();
50
- if (ownerID)
54
+ if (ownerID && !this.current)
51
55
  void this.refresh();
56
+ if (ownerID)
57
+ this.stopPolling = this.options.polling?.subscribe(() => this.refresh());
58
+ }
59
+ // Broadcast only invalidation; every tab still reads the authoritative backend.
60
+ startSync(name) {
61
+ this.channel?.close();
62
+ this.channel = undefined;
63
+ try {
64
+ if (typeof BroadcastChannel !== "undefined") {
65
+ this.channel = new BroadcastChannel(name);
66
+ this.channel.onmessage = ({ data }) => {
67
+ if (data?.type === "preferences-changed" &&
68
+ this.ownerID &&
69
+ data.owner_id === this.ownerID &&
70
+ data.identity === this.identity)
71
+ this.invalidate();
72
+ };
73
+ }
74
+ }
75
+ catch (error) {
76
+ notifyObserver(() => this.options.on_error?.(error));
77
+ }
78
+ return () => {
79
+ this.channel?.close();
80
+ this.channel = undefined;
81
+ };
52
82
  }
53
83
  refresh() {
54
84
  if (this.pending)
55
85
  return this.pending;
56
- if (!this.ownerID || this.saving)
86
+ if (!this.ownerID)
87
+ return Promise.resolve();
88
+ if (this.saving) {
89
+ this.refreshRequested = true;
57
90
  return Promise.resolve();
91
+ }
58
92
  const signal = this.session.signal;
59
93
  const ownerID = this.ownerID;
60
94
  const pending = Promise.resolve().then(async () => {
@@ -78,6 +112,7 @@ export class AppPreferencesController {
78
112
  if (!signal.aborted) {
79
113
  this.pending = undefined;
80
114
  this.publish();
115
+ this.flushRefresh();
81
116
  }
82
117
  }
83
118
  });
@@ -109,6 +144,16 @@ export class AppPreferencesController {
109
144
  return;
110
145
  this.current = pref;
111
146
  this.error = undefined;
147
+ try {
148
+ this.channel?.postMessage({
149
+ type: "preferences-changed",
150
+ owner_id: ownerID,
151
+ identity: this.identity,
152
+ });
153
+ }
154
+ catch (error) {
155
+ notifyObserver(() => this.options.on_error?.(error));
156
+ }
112
157
  }
113
158
  catch (error) {
114
159
  if (!signal.aborted)
@@ -118,10 +163,24 @@ export class AppPreferencesController {
118
163
  if (!signal.aborted) {
119
164
  this.saving = false;
120
165
  this.publish();
166
+ this.flushRefresh();
121
167
  }
122
168
  }
123
169
  }
124
170
  publish() {
125
171
  notifyObserver(() => this.options.on_change(this.state));
126
172
  }
173
+ // An invalidation received during a request must survive that request's result.
174
+ invalidate() {
175
+ if (this.pending || this.saving)
176
+ this.refreshRequested = true;
177
+ else
178
+ void this.refresh();
179
+ }
180
+ flushRefresh() {
181
+ if (this.refreshRequested && !this.saving) {
182
+ this.refreshRequested = false;
183
+ void this.refresh();
184
+ }
185
+ }
127
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.186",
3
+ "version": "0.0.187",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {