@fabriktor/fx 0.0.185 → 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.
- package/dist/src/contacts/contacts.d.ts +3 -0
- package/dist/src/contacts/contacts.js +5 -5
- package/dist/src/core/poll.d.ts +15 -0
- package/dist/src/core/poll.js +59 -0
- package/dist/src/fx.d.ts +3 -0
- package/dist/src/fx.js +3 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/preferences/app.d.ts +41 -0
- package/dist/src/preferences/app.js +186 -0
- package/dist/src/preferences/preferences.d.ts +12 -1
- package/dist/src/preferences/preferences.js +93 -1
- package/package.json +3 -3
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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,
|
package/dist/src/index.d.ts
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";
|
|
@@ -42,6 +43,7 @@ export * from "./notifications/route.js";
|
|
|
42
43
|
export * from "./payments/payments.js";
|
|
43
44
|
export * from "./payrolls/payrolls.js";
|
|
44
45
|
export * from "./payrolls/workspace.js";
|
|
46
|
+
export * from "./preferences/app.js";
|
|
45
47
|
export * from "./preferences/preferences.js";
|
|
46
48
|
export * from "./privacy/privacy.js";
|
|
47
49
|
export * from "./processes/processes.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";
|
|
@@ -42,6 +43,7 @@ export * from "./notifications/route.js";
|
|
|
42
43
|
export * from "./payments/payments.js";
|
|
43
44
|
export * from "./payrolls/payrolls.js";
|
|
44
45
|
export * from "./payrolls/workspace.js";
|
|
46
|
+
export * from "./preferences/app.js";
|
|
45
47
|
export * from "./preferences/preferences.js";
|
|
46
48
|
export * from "./privacy/privacy.js";
|
|
47
49
|
export * from "./processes/processes.js";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type ColorMode, type InPrefApp, type PrefApp } from "@fabriktor/schema";
|
|
2
|
+
import type { PollScheduler } from "../core/poll.js";
|
|
3
|
+
import type { PreferencesFXOperator } from "./preferences.js";
|
|
4
|
+
export declare const colorModes: readonly ["light", "dark"];
|
|
5
|
+
export declare function isColorMode(value: unknown): value is ColorMode;
|
|
6
|
+
export type AppPreferencesInput = Pick<InPrefApp, "color_mode" | "quiet_mode">;
|
|
7
|
+
export type AppPreferencesState = AppPreferencesInput & {
|
|
8
|
+
readonly ready: boolean;
|
|
9
|
+
readonly loading: boolean;
|
|
10
|
+
readonly saving: boolean;
|
|
11
|
+
readonly show_unread: boolean;
|
|
12
|
+
readonly error?: unknown;
|
|
13
|
+
};
|
|
14
|
+
export type AppPreferencesOptions = {
|
|
15
|
+
preferences: Pick<PreferencesFXOperator, "getPrefApp" | "savePrefApp">;
|
|
16
|
+
polling?: PollScheduler;
|
|
17
|
+
on_change: (state: AppPreferencesState) => void;
|
|
18
|
+
on_error?: (error: unknown) => void;
|
|
19
|
+
};
|
|
20
|
+
export declare class AppPreferencesController {
|
|
21
|
+
private readonly options;
|
|
22
|
+
private ownerID;
|
|
23
|
+
private identity;
|
|
24
|
+
private session;
|
|
25
|
+
private current?;
|
|
26
|
+
private pending?;
|
|
27
|
+
private saving;
|
|
28
|
+
private error?;
|
|
29
|
+
private stopPolling?;
|
|
30
|
+
private refreshRequested;
|
|
31
|
+
private channel?;
|
|
32
|
+
constructor(options: AppPreferencesOptions);
|
|
33
|
+
get state(): AppPreferencesState;
|
|
34
|
+
setOwner(ownerID?: string, identity?: string, pref?: PrefApp): void;
|
|
35
|
+
startSync(name: string): () => void;
|
|
36
|
+
refresh(): Promise<void>;
|
|
37
|
+
save(input: Partial<AppPreferencesInput>): Promise<void>;
|
|
38
|
+
private publish;
|
|
39
|
+
private invalidate;
|
|
40
|
+
private flushRefresh;
|
|
41
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { ColorModeDark, ColorModeLight, } from "@fabriktor/schema";
|
|
2
|
+
import { notifyObserver } from "../core/events.js";
|
|
3
|
+
import { errRequired, errValidation } from "../core/err.js";
|
|
4
|
+
export const colorModes = [ColorModeLight, ColorModeDark];
|
|
5
|
+
export function isColorMode(value) {
|
|
6
|
+
return colorModes.some((mode) => mode === value);
|
|
7
|
+
}
|
|
8
|
+
// One account's presentation preferences. Notification delivery and unread data
|
|
9
|
+
// remain independent of this state.
|
|
10
|
+
export class AppPreferencesController {
|
|
11
|
+
options;
|
|
12
|
+
ownerID = "";
|
|
13
|
+
identity = "";
|
|
14
|
+
session = new AbortController();
|
|
15
|
+
current;
|
|
16
|
+
pending;
|
|
17
|
+
saving = false;
|
|
18
|
+
error;
|
|
19
|
+
stopPolling;
|
|
20
|
+
refreshRequested = false;
|
|
21
|
+
channel;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
}
|
|
25
|
+
get state() {
|
|
26
|
+
const quiet = this.current?.quiet_mode ?? false;
|
|
27
|
+
const mode = this.current?.color_mode;
|
|
28
|
+
return {
|
|
29
|
+
color_mode: isColorMode(mode) ? mode : ColorModeDark,
|
|
30
|
+
quiet_mode: quiet,
|
|
31
|
+
ready: this.current !== undefined,
|
|
32
|
+
loading: this.pending !== undefined,
|
|
33
|
+
saving: this.saving,
|
|
34
|
+
show_unread: this.current !== undefined && !quiet,
|
|
35
|
+
error: this.error,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Bootstrap may supply the preference already fetched for this account.
|
|
39
|
+
setOwner(ownerID = "", identity = "", pref) {
|
|
40
|
+
if (ownerID === this.ownerID && identity === this.identity)
|
|
41
|
+
return;
|
|
42
|
+
this.session.abort();
|
|
43
|
+
this.stopPolling?.();
|
|
44
|
+
this.stopPolling = undefined;
|
|
45
|
+
this.session = new AbortController();
|
|
46
|
+
this.ownerID = ownerID;
|
|
47
|
+
this.identity = identity;
|
|
48
|
+
this.current = ownerID ? pref : undefined;
|
|
49
|
+
this.pending = undefined;
|
|
50
|
+
this.saving = false;
|
|
51
|
+
this.error = undefined;
|
|
52
|
+
this.refreshRequested = false;
|
|
53
|
+
this.publish();
|
|
54
|
+
if (ownerID && !this.current)
|
|
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
|
+
};
|
|
82
|
+
}
|
|
83
|
+
refresh() {
|
|
84
|
+
if (this.pending)
|
|
85
|
+
return this.pending;
|
|
86
|
+
if (!this.ownerID)
|
|
87
|
+
return Promise.resolve();
|
|
88
|
+
if (this.saving) {
|
|
89
|
+
this.refreshRequested = true;
|
|
90
|
+
return Promise.resolve();
|
|
91
|
+
}
|
|
92
|
+
const signal = this.session.signal;
|
|
93
|
+
const ownerID = this.ownerID;
|
|
94
|
+
const pending = Promise.resolve().then(async () => {
|
|
95
|
+
try {
|
|
96
|
+
const pref = await this.options.preferences.getPrefApp({
|
|
97
|
+
owner_id: ownerID,
|
|
98
|
+
signal,
|
|
99
|
+
});
|
|
100
|
+
if (signal.aborted)
|
|
101
|
+
return;
|
|
102
|
+
this.current = pref;
|
|
103
|
+
this.error = undefined;
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (signal.aborted)
|
|
107
|
+
return;
|
|
108
|
+
this.error = error;
|
|
109
|
+
notifyObserver(() => this.options.on_error?.(error));
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
if (!signal.aborted) {
|
|
113
|
+
this.pending = undefined;
|
|
114
|
+
this.publish();
|
|
115
|
+
this.flushRefresh();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
this.pending = pending;
|
|
120
|
+
this.publish();
|
|
121
|
+
return pending;
|
|
122
|
+
}
|
|
123
|
+
async save(input) {
|
|
124
|
+
if (!this.ownerID)
|
|
125
|
+
throw errRequired({ field: "owner_id", msg: "An account is required." });
|
|
126
|
+
if (this.saving)
|
|
127
|
+
throw errValidation({
|
|
128
|
+
msg: "Application preferences are already being saved.",
|
|
129
|
+
});
|
|
130
|
+
const signal = this.session.signal;
|
|
131
|
+
const ownerID = this.ownerID;
|
|
132
|
+
this.saving = true;
|
|
133
|
+
this.publish();
|
|
134
|
+
try {
|
|
135
|
+
await this.pending;
|
|
136
|
+
if (signal.aborted)
|
|
137
|
+
return;
|
|
138
|
+
const pref = await this.options.preferences.savePrefApp({
|
|
139
|
+
owner_id: ownerID,
|
|
140
|
+
in: input,
|
|
141
|
+
signal,
|
|
142
|
+
});
|
|
143
|
+
if (signal.aborted)
|
|
144
|
+
return;
|
|
145
|
+
this.current = pref;
|
|
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
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
if (!signal.aborted)
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
if (!signal.aborted) {
|
|
164
|
+
this.saving = false;
|
|
165
|
+
this.publish();
|
|
166
|
+
this.flushRefresh();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
publish() {
|
|
171
|
+
notifyObserver(() => this.options.on_change(this.state));
|
|
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
|
+
}
|
|
186
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import type { PreferencesOperator } from "@fabriktor/client";
|
|
2
|
-
import { type InPrefClient, type InPrefCompany, type InPrefEmployee, type OperationResult, type PrefPayment, type PrefClient, type PrefEmployee, type PrefCompany, type PrefVisibility, type PrefLanguage, type PrefNotif, type PrefEmail, type PrefTutorial } from "@fabriktor/schema";
|
|
2
|
+
import { type InPrefApp, type PrefApp, type InPrefClient, type InPrefCompany, type InPrefEmployee, type OperationResult, type PrefPayment, type PrefClient, type PrefEmployee, type PrefCompany, type PrefVisibility, type PrefLanguage, type PrefNotif, type PrefEmail, type PrefTutorial } from "@fabriktor/schema";
|
|
3
3
|
import { EventBus } from "../core/events.js";
|
|
4
|
+
export type GetPrefAppOptions = {
|
|
5
|
+
owner_id: string;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
};
|
|
8
|
+
export type SavePrefAppOptions = GetPrefAppOptions & {
|
|
9
|
+
in: Partial<Pick<InPrefApp, "color_mode" | "quiet_mode">>;
|
|
10
|
+
};
|
|
4
11
|
export declare const PreferencesEvents: {
|
|
5
12
|
readonly CompanySaved: import("../index.js").EventKey<PrefCompany>;
|
|
6
13
|
};
|
|
@@ -85,6 +92,8 @@ export type SaveSpeechToTextSourceLanguageOptions = {
|
|
|
85
92
|
speech_to_text_source_language: string;
|
|
86
93
|
};
|
|
87
94
|
export interface PreferencesFXOperator {
|
|
95
|
+
getPrefApp(opts: GetPrefAppOptions): Promise<PrefApp>;
|
|
96
|
+
savePrefApp(opts: SavePrefAppOptions): Promise<PrefApp>;
|
|
88
97
|
createPrefPayment(opts: CreatePrefPaymentOptions): Promise<PrefPayment>;
|
|
89
98
|
replacePrefPayment(opts: ReplacePrefPaymentOptions): Promise<PrefPayment>;
|
|
90
99
|
createPrefClient(opts: CreatePrefClientOptions): Promise<PrefClient>;
|
|
@@ -122,6 +131,8 @@ export declare class PreferencesFX implements PreferencesFXOperator {
|
|
|
122
131
|
private preferences;
|
|
123
132
|
private readonly events;
|
|
124
133
|
constructor(opts: PreferencesFXOptions);
|
|
134
|
+
getPrefApp(opts: GetPrefAppOptions): Promise<PrefApp>;
|
|
135
|
+
savePrefApp(opts: SavePrefAppOptions): Promise<PrefApp>;
|
|
125
136
|
createPrefPayment(opts: CreatePrefPaymentOptions): Promise<PrefPayment>;
|
|
126
137
|
replacePrefPayment(opts: ReplacePrefPaymentOptions): Promise<PrefPayment>;
|
|
127
138
|
createPrefClient(opts: CreatePrefClientOptions): Promise<PrefClient>;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { queryParam } from "@fabriktor/client";
|
|
2
2
|
import { OwnerIDKey, } from "@fabriktor/schema";
|
|
3
|
-
import { errResolveFailed } from "../core/err.js";
|
|
3
|
+
import { errResolveFailed, errRequired, errValidation } from "../core/err.js";
|
|
4
4
|
import { createEvent, EventBus } from "../core/events.js";
|
|
5
5
|
import { requiredOperationID } from "../core/id.js";
|
|
6
|
+
import { isColorMode } from "./app.js";
|
|
6
7
|
export const PreferencesEvents = {
|
|
7
8
|
CompanySaved: createEvent(),
|
|
8
9
|
};
|
|
@@ -44,6 +45,97 @@ export class PreferencesFX {
|
|
|
44
45
|
this.preferences = opts.preferences;
|
|
45
46
|
this.events = opts.events ?? new EventBus();
|
|
46
47
|
}
|
|
48
|
+
async getPrefApp(opts) {
|
|
49
|
+
if (!opts.owner_id.trim())
|
|
50
|
+
throw errRequired({ field: "owner_id", msg: "An account is required." });
|
|
51
|
+
const out = await this.preferences.queryPrefApps({
|
|
52
|
+
query: queryParam(OwnerIDKey, opts.owner_id),
|
|
53
|
+
signal: opts.signal,
|
|
54
|
+
});
|
|
55
|
+
const pref = out.value?.[0];
|
|
56
|
+
if (!pref)
|
|
57
|
+
throw errResolveFailed({
|
|
58
|
+
resource: "pref_app",
|
|
59
|
+
msg: "Application preferences could not be resolved.",
|
|
60
|
+
});
|
|
61
|
+
return pref;
|
|
62
|
+
}
|
|
63
|
+
async savePrefApp(opts) {
|
|
64
|
+
if (opts.in.color_mode !== undefined && !isColorMode(opts.in.color_mode)) {
|
|
65
|
+
throw errValidation({
|
|
66
|
+
field: "color_mode",
|
|
67
|
+
msg: "Unsupported color mode.",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (opts.in.quiet_mode !== undefined &&
|
|
71
|
+
typeof opts.in.quiet_mode !== "boolean") {
|
|
72
|
+
throw errValidation({
|
|
73
|
+
field: "quiet_mode",
|
|
74
|
+
msg: "Quiet mode must be a boolean.",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
let pref = await this.getPrefApp(opts);
|
|
78
|
+
opts.signal?.throwIfAborted();
|
|
79
|
+
// Patch only the requested fields; another device may change the other setting.
|
|
80
|
+
const input = { is_default: false };
|
|
81
|
+
if (opts.in.color_mode !== undefined)
|
|
82
|
+
input.color_mode = opts.in.color_mode;
|
|
83
|
+
if (opts.in.quiet_mode !== undefined)
|
|
84
|
+
input.quiet_mode = opts.in.quiet_mode;
|
|
85
|
+
if (opts.in.color_mode === undefined && opts.in.quiet_mode === undefined)
|
|
86
|
+
return pref;
|
|
87
|
+
let id = pref.id;
|
|
88
|
+
if (pref.is_default) {
|
|
89
|
+
try {
|
|
90
|
+
const out = await this.preferences.insertOnePrefApp({
|
|
91
|
+
in: {
|
|
92
|
+
is_default: false,
|
|
93
|
+
color_mode: opts.in.color_mode ?? pref.color_mode,
|
|
94
|
+
quiet_mode: opts.in.quiet_mode ?? pref.quiet_mode,
|
|
95
|
+
},
|
|
96
|
+
signal: opts.signal,
|
|
97
|
+
});
|
|
98
|
+
id = requiredOperationID(out, "Created application preference response did not contain an ID.");
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
// A concurrent first save or a lost response can leave a persisted record.
|
|
102
|
+
opts.signal?.throwIfAborted();
|
|
103
|
+
try {
|
|
104
|
+
pref = await this.getPrefApp(opts);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
opts.signal?.throwIfAborted();
|
|
110
|
+
if (pref.is_default)
|
|
111
|
+
throw error;
|
|
112
|
+
id = pref.id;
|
|
113
|
+
await this.preferences.replaceOnePrefApp({
|
|
114
|
+
id,
|
|
115
|
+
in: input,
|
|
116
|
+
signal: opts.signal,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
await this.preferences.replaceOnePrefApp({
|
|
122
|
+
id,
|
|
123
|
+
in: input,
|
|
124
|
+
signal: opts.signal,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
opts.signal?.throwIfAborted();
|
|
128
|
+
const out = await this.preferences.findOnePrefApp({
|
|
129
|
+
id,
|
|
130
|
+
signal: opts.signal,
|
|
131
|
+
});
|
|
132
|
+
if (!out.value)
|
|
133
|
+
throw errResolveFailed({
|
|
134
|
+
resource: "pref_app",
|
|
135
|
+
msg: "Application preferences could not be resolved after saving.",
|
|
136
|
+
});
|
|
137
|
+
return out.value;
|
|
138
|
+
}
|
|
47
139
|
async createPrefPayment(opts) {
|
|
48
140
|
const out = await this.preferences.insertOnePrefPayment(opts);
|
|
49
141
|
const id = requiredOperationID(out, msgCreatedPrefPaymentIDMissing);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabriktor/fx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.187",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"typescript": "^6.0.3"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@fabriktor/client": "0.0.
|
|
20
|
+
"@fabriktor/client": "0.0.114",
|
|
21
21
|
"@fabriktor/fx": "0.0.94",
|
|
22
|
-
"@fabriktor/schema": "0.0.
|
|
22
|
+
"@fabriktor/schema": "0.0.85",
|
|
23
23
|
"convert": "8.0.2"
|
|
24
24
|
}
|
|
25
25
|
}
|