@fabriktor/fx 0.0.184 → 0.0.185
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.
|
@@ -5,6 +5,7 @@ import { type NotificationsFXOperator } from "./notifications.js";
|
|
|
5
5
|
export type NotificationInboxState = SearchState<Notification> & {
|
|
6
6
|
readonly identity: string;
|
|
7
7
|
readonly reading: readonly string[];
|
|
8
|
+
readonly deleting: readonly string[];
|
|
8
9
|
readonly has_unread: boolean;
|
|
9
10
|
};
|
|
10
11
|
export type NotificationInboxOptions = {
|
|
@@ -17,6 +18,7 @@ export declare class NotificationInboxController {
|
|
|
17
18
|
private readonly options;
|
|
18
19
|
private readonly search;
|
|
19
20
|
private readonly stopRead;
|
|
21
|
+
private readonly stopDelete;
|
|
20
22
|
private ownerID;
|
|
21
23
|
private identity;
|
|
22
24
|
private session;
|
|
@@ -25,6 +27,8 @@ export declare class NotificationInboxController {
|
|
|
25
27
|
private unreadRequest?;
|
|
26
28
|
private hasUnread;
|
|
27
29
|
private readonly reads;
|
|
30
|
+
private readonly deletes;
|
|
31
|
+
private readonly deleted;
|
|
28
32
|
constructor(options: NotificationInboxOptions);
|
|
29
33
|
get state(): NotificationInboxState;
|
|
30
34
|
setOwner(owner_id?: string, session_id?: string): void;
|
|
@@ -32,6 +36,7 @@ export declare class NotificationInboxController {
|
|
|
32
36
|
loadMore(): Promise<void>;
|
|
33
37
|
retry(): Promise<void>;
|
|
34
38
|
markRead(id: string): Promise<void>;
|
|
39
|
+
delete(id: string): Promise<boolean>;
|
|
35
40
|
dispose(): void;
|
|
36
41
|
private refreshUnread;
|
|
37
42
|
private clear;
|
|
@@ -7,6 +7,7 @@ export class NotificationInboxController {
|
|
|
7
7
|
options;
|
|
8
8
|
search;
|
|
9
9
|
stopRead;
|
|
10
|
+
stopDelete;
|
|
10
11
|
ownerID = "";
|
|
11
12
|
identity = "";
|
|
12
13
|
session = new AbortController();
|
|
@@ -15,6 +16,8 @@ export class NotificationInboxController {
|
|
|
15
16
|
unreadRequest;
|
|
16
17
|
hasUnread = false;
|
|
17
18
|
reads = new Map();
|
|
19
|
+
deletes = new Map();
|
|
20
|
+
deleted = new Set();
|
|
18
21
|
constructor(options) {
|
|
19
22
|
this.options = options;
|
|
20
23
|
this.search = new SearchController({
|
|
@@ -27,12 +30,29 @@ export class NotificationInboxController {
|
|
|
27
30
|
this.search.patchResult(id, { is_read: true });
|
|
28
31
|
void this.refreshUnread();
|
|
29
32
|
});
|
|
33
|
+
this.stopDelete = options.events.subscribe(NotificationsEvents.Deleted, ({ owner_id, id }) => {
|
|
34
|
+
if (owner_id !== this.ownerID)
|
|
35
|
+
return;
|
|
36
|
+
this.deleted.add(id);
|
|
37
|
+
this.unreadRequest?.abort();
|
|
38
|
+
this.publish();
|
|
39
|
+
// Refill the displayed pages: deleting a row shifts later page offsets.
|
|
40
|
+
void this.refresh();
|
|
41
|
+
});
|
|
30
42
|
}
|
|
31
43
|
get state() {
|
|
44
|
+
const state = this.search.state;
|
|
45
|
+
// An older in-flight response must never restore a confirmed deletion.
|
|
46
|
+
const results = state.results.filter((item) => !this.deleted.has(item.id));
|
|
32
47
|
return {
|
|
33
|
-
...
|
|
48
|
+
...state,
|
|
49
|
+
results,
|
|
50
|
+
total_count: state.total_count === undefined
|
|
51
|
+
? undefined
|
|
52
|
+
: Math.max(0, state.total_count - (state.results.length - results.length)),
|
|
34
53
|
identity: this.identity,
|
|
35
54
|
reading: [...this.reads.keys()],
|
|
55
|
+
deleting: [...this.deletes.keys()],
|
|
36
56
|
has_unread: this.hasUnread,
|
|
37
57
|
};
|
|
38
58
|
}
|
|
@@ -88,7 +108,7 @@ export class NotificationInboxController {
|
|
|
88
108
|
await Promise.all([this.search.retry(), this.refreshUnread()]);
|
|
89
109
|
}
|
|
90
110
|
markRead(id) {
|
|
91
|
-
if (!this.ownerID)
|
|
111
|
+
if (!this.ownerID || this.deleted.has(id) || this.deletes.has(id))
|
|
92
112
|
return Promise.resolve();
|
|
93
113
|
const pending = this.reads.get(id);
|
|
94
114
|
if (pending)
|
|
@@ -115,8 +135,47 @@ export class NotificationInboxController {
|
|
|
115
135
|
this.publish();
|
|
116
136
|
return run;
|
|
117
137
|
}
|
|
138
|
+
delete(id) {
|
|
139
|
+
if (!this.ownerID)
|
|
140
|
+
return Promise.resolve(false);
|
|
141
|
+
if (this.deleted.has(id))
|
|
142
|
+
return Promise.resolve(true);
|
|
143
|
+
const pending = this.deletes.get(id);
|
|
144
|
+
if (pending)
|
|
145
|
+
return pending;
|
|
146
|
+
const session = this.session;
|
|
147
|
+
const owner_id = this.ownerID;
|
|
148
|
+
const run = Promise.resolve()
|
|
149
|
+
.then(async () => {
|
|
150
|
+
// Finish an already-started read before deleting the same document.
|
|
151
|
+
await this.reads.get(id)?.catch(() => undefined);
|
|
152
|
+
if (session.signal.aborted)
|
|
153
|
+
return false;
|
|
154
|
+
await this.options.notifications.deleteNotification({
|
|
155
|
+
id,
|
|
156
|
+
owner_id,
|
|
157
|
+
signal: session.signal,
|
|
158
|
+
});
|
|
159
|
+
return !session.signal.aborted;
|
|
160
|
+
})
|
|
161
|
+
.catch((error) => {
|
|
162
|
+
if (session.signal.aborted)
|
|
163
|
+
return false;
|
|
164
|
+
throw error;
|
|
165
|
+
})
|
|
166
|
+
.finally(() => {
|
|
167
|
+
if (this.deletes.get(id) !== run)
|
|
168
|
+
return;
|
|
169
|
+
this.deletes.delete(id);
|
|
170
|
+
this.publish();
|
|
171
|
+
});
|
|
172
|
+
this.deletes.set(id, run);
|
|
173
|
+
this.publish();
|
|
174
|
+
return run;
|
|
175
|
+
}
|
|
118
176
|
dispose() {
|
|
119
177
|
this.stopRead();
|
|
178
|
+
this.stopDelete();
|
|
120
179
|
this.clear();
|
|
121
180
|
}
|
|
122
181
|
async refreshUnread() {
|
|
@@ -155,6 +214,8 @@ export class NotificationInboxController {
|
|
|
155
214
|
this.refreshing = undefined;
|
|
156
215
|
this.refreshPending = false;
|
|
157
216
|
this.reads.clear();
|
|
217
|
+
this.deletes.clear();
|
|
218
|
+
this.deleted.clear();
|
|
158
219
|
this.search.clear();
|
|
159
220
|
}
|
|
160
221
|
publish() {
|
|
@@ -4,6 +4,7 @@ import { EventBus } from "../core/events.js";
|
|
|
4
4
|
import { type OwnerSearchOptions, type SearchPage } from "../core/search.js";
|
|
5
5
|
export declare const NotificationsEvents: {
|
|
6
6
|
readonly Read: import("../index.js").EventKey<Pick<Notification, "id" | "owner_id">>;
|
|
7
|
+
readonly Deleted: import("../index.js").EventKey<Pick<Notification, "id" | "owner_id">>;
|
|
7
8
|
};
|
|
8
9
|
export type DeviceTokenProvider = () => Promise<string>;
|
|
9
10
|
export type NotificationsFXOptions = {
|
|
@@ -14,6 +15,7 @@ export type NotificationsFXOptions = {
|
|
|
14
15
|
export type SearchNotificationsOptions = OwnerSearchOptions & Partial<Pick<Notification, "is_read" | "notif_k" | "key">>;
|
|
15
16
|
export type SearchNotificationsPage = SearchPage<Notification>;
|
|
16
17
|
export type MarkNotificationReadOptions = RequestOptions & Pick<Notification, "id" | "owner_id">;
|
|
18
|
+
export type DeleteNotificationOptions = Pick<RequestOptions, "signal"> & Pick<Notification, "id" | "owner_id">;
|
|
17
19
|
export type SyncDeviceTokenOptions = {
|
|
18
20
|
owner_id: string;
|
|
19
21
|
previous_id?: string;
|
|
@@ -28,6 +30,7 @@ export type SyncDeviceTokenResult = Awaited<ReturnType<NotificationsOperator["up
|
|
|
28
30
|
export interface NotificationsFXOperator {
|
|
29
31
|
searchNotifications(opts: SearchNotificationsOptions): Promise<SearchNotificationsPage>;
|
|
30
32
|
markNotificationRead(opts: MarkNotificationReadOptions): Promise<void>;
|
|
33
|
+
deleteNotification(opts: DeleteNotificationOptions): Promise<void>;
|
|
31
34
|
syncDeviceToken(opts: SyncDeviceTokenOptions): Promise<SyncDeviceTokenResult>;
|
|
32
35
|
unregisterDeviceToken(opts: UnregisterDeviceTokenOptions): Promise<void>;
|
|
33
36
|
}
|
|
@@ -38,6 +41,7 @@ export declare class NotificationsFX implements NotificationsFXOperator {
|
|
|
38
41
|
constructor(opts: NotificationsFXOptions);
|
|
39
42
|
searchNotifications(opts: SearchNotificationsOptions): Promise<SearchNotificationsPage>;
|
|
40
43
|
markNotificationRead(opts: MarkNotificationReadOptions): Promise<void>;
|
|
44
|
+
deleteNotification(opts: DeleteNotificationOptions): Promise<void>;
|
|
41
45
|
syncDeviceToken(opts: SyncDeviceTokenOptions): Promise<SyncDeviceTokenResult>;
|
|
42
46
|
unregisterDeviceToken(opts: UnregisterDeviceTokenOptions): Promise<void>;
|
|
43
47
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { IDKey, OwnerIDKey, ZeroID, PushTransportFCM, } from "@fabriktor/schema";
|
|
2
|
-
import { queryParams, } from "@fabriktor/client";
|
|
2
|
+
import { isClientErr, queryParams, } from "@fabriktor/client";
|
|
3
3
|
import { errRequired } from "../core/err.js";
|
|
4
4
|
import { createEvent, EventBus } from "../core/events.js";
|
|
5
5
|
import { requiredOperationID, resolvedID } from "../core/id.js";
|
|
6
6
|
import { runOwnerSearch, } from "../core/search.js";
|
|
7
7
|
export const NotificationsEvents = {
|
|
8
8
|
Read: createEvent(),
|
|
9
|
+
Deleted: createEvent(),
|
|
9
10
|
};
|
|
10
11
|
const msgOwnerIDRequired = "Owner ID is required.";
|
|
11
12
|
const msgNotificationIDRequired = "Notification ID is required.";
|
|
@@ -60,6 +61,35 @@ export class NotificationsFX {
|
|
|
60
61
|
});
|
|
61
62
|
this.events.publish(NotificationsEvents.Read, { owner_id, id });
|
|
62
63
|
}
|
|
64
|
+
async deleteNotification(opts) {
|
|
65
|
+
const owner_id = resolvedID(opts.owner_id);
|
|
66
|
+
if (owner_id === undefined) {
|
|
67
|
+
throw errRequired({
|
|
68
|
+
field: OwnerIDKey,
|
|
69
|
+
msg: msgOwnerIDRequired,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const id = resolvedID(opts.id);
|
|
73
|
+
if (id === undefined) {
|
|
74
|
+
throw errRequired({
|
|
75
|
+
field: IDKey,
|
|
76
|
+
msg: msgNotificationIDRequired,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
opts.signal?.throwIfAborted();
|
|
80
|
+
try {
|
|
81
|
+
await this.notifications.deleteOneNotification({
|
|
82
|
+
id,
|
|
83
|
+
signal: opts.signal,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (!isClientErr(error) || error.status !== 404)
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
opts.signal?.throwIfAborted();
|
|
91
|
+
this.events.publish(NotificationsEvents.Deleted, { owner_id, id });
|
|
92
|
+
}
|
|
63
93
|
async syncDeviceToken(opts) {
|
|
64
94
|
const owner_id = opts.owner_id.trim();
|
|
65
95
|
if (owner_id === "" || owner_id === ZeroID) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabriktor/fx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.185",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"typescript": "^6.0.3"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@fabriktor/client": "0.0.
|
|
20
|
+
"@fabriktor/client": "0.0.113",
|
|
21
21
|
"@fabriktor/fx": "0.0.94",
|
|
22
22
|
"@fabriktor/schema": "0.0.84",
|
|
23
23
|
"convert": "8.0.2"
|