@oblax/notifications 0.0.0-stage → 2.0.0

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.
@@ -0,0 +1,335 @@
1
+ import type { RealtimeConfig, RealtimeStream } from '@oblax/realtime';
2
+ /**
3
+ * Module-wide defaults for `onChange()`. The realtime tuning
4
+ * (`reconnectDelay`, `maxReconnectDelay`, `idleTimeout`, as `Realtime(config)`
5
+ * takes it, with its defaults) applies to the streams `onChange()` opens
6
+ * itself; `onChange({ ... })` overrides each.
7
+ */
8
+ export interface NotificationsConfig extends RealtimeConfig {
9
+ /**
10
+ * How long after a ring `onChange()` calls its handler a second time, in
11
+ * milliseconds. The list and the count are read from a replica, which can
12
+ * lag the write behind the ring by a moment: the second call keeps a stale
13
+ * first answer from sticking. `0` turns the second call off. Overridden per
14
+ * call by `onChange({ handler, recheckDelay })`.
15
+ * @default 1500
16
+ */
17
+ recheckDelay?: number;
18
+ }
19
+ /** A list response: every item, and how many there are. */
20
+ export interface NotificationsList<T> {
21
+ data: T[];
22
+ count: number;
23
+ }
24
+ export interface IdParams {
25
+ id: string;
26
+ }
27
+ export interface KeyParams {
28
+ /** The category's key. */
29
+ key: string;
30
+ }
31
+ /** What a notification is about: both or neither, each 1–64 characters without spaces. */
32
+ export interface NotificationSubject {
33
+ /** @example 'workflows.item' */
34
+ type: string;
35
+ id: string;
36
+ }
37
+ /** One rule a 400 `VALIDATION_FAILED` names, in `body.validation_errors`. */
38
+ export interface ValidationError {
39
+ /** The field, e.g. `title`, `to.users`, `subject.type`, `1.channel`. */
40
+ field: string;
41
+ /** The rule, e.g. `required`, `max`, `format`, `unknown`, `locked`, `reserved`. */
42
+ validation_check: string;
43
+ /** The value refused, when the server echoes it (`format` on a preference doesn't). */
44
+ value: unknown;
45
+ }
46
+ /**
47
+ * The body of an `OblaxHttpError` from the notifications routes:
48
+ * `(err.body as NotificationsErrorBody).validation_errors`.
49
+ */
50
+ export interface NotificationsErrorBody {
51
+ status: number;
52
+ /**
53
+ * `VALIDATION_FAILED`, `UNABLE_TO_PARSE_PAYLOAD`, `UNAUTHORIZED`,
54
+ * `AUTH_ACL_VALIDATION_FAILED`, `NOT_FOUND`, `NOTIFICATIONS_ALREADY_EXISTS`,
55
+ * `REQUEST_CANCELED`, `NOTIFICATIONS_UNAVAILABLE`, `UNABLE_TO_…`.
56
+ */
57
+ type: string;
58
+ message: string;
59
+ /** Every rule the request broke, on some 400s. */
60
+ validation_errors?: ValidationError[];
61
+ }
62
+ /**
63
+ * A notification in the signed-in user's inbox. Fields the send left out are
64
+ * `''` (text), `null` (`data`, `subject`, `expires_at`) or `false`.
65
+ */
66
+ export interface Notification {
67
+ /** The notification's own id (not the send's): what `read()`, `archive()`, … take. */
68
+ id: string;
69
+ category: string;
70
+ title: string;
71
+ body: string;
72
+ /** `''`, a path starting with a single `/`, or an `https://` URL: safe as an `href`. */
73
+ link: string;
74
+ /** The send's `data`, as sent; `null` for none. */
75
+ data: unknown;
76
+ actor_id: string;
77
+ organization_id: string;
78
+ subject: NotificationSubject | null;
79
+ /** Kept until the user reads, archives or deletes it. */
80
+ critical: boolean;
81
+ /** RFC 3339; past it, the notification leaves the list and the count. */
82
+ expires_at: string | null;
83
+ /** `null` until the user reads it. */
84
+ read_at: string | null;
85
+ /** `null` until the user archives it. */
86
+ archived_at: string | null;
87
+ /** When it was sent. RFC 3339. */
88
+ _created: string;
89
+ }
90
+ /** A page of the inbox, newest first. Pass `next` as `after` for the next page; it's `''` on the last. */
91
+ export interface NotificationPage {
92
+ data: Notification[];
93
+ next: string;
94
+ }
95
+ /** Which notifications `list()` returns. Each filter narrows the others. */
96
+ export interface ListNotificationsParams {
97
+ /** `true`: only the unread ones; `false`: only the read ones. */
98
+ unread?: boolean;
99
+ /** Only that category's. */
100
+ category?: string;
101
+ /** Only those about that organization. */
102
+ organizationId?: string;
103
+ /** Only those with that subject type. Give `subjectId` too to match one subject. */
104
+ subjectType?: string;
105
+ /** Only those with that subject id. */
106
+ subjectId?: string;
107
+ /** `true`: the archived ones instead of the others. */
108
+ archived?: boolean;
109
+ /** `true`: only the critical ones; `false`: only the others. */
110
+ critical?: boolean;
111
+ /** The previous page's `next`; left out for the first page. */
112
+ after?: string;
113
+ /** 20 by default, at most 100 (a larger one is cut to 100). */
114
+ limit?: number;
115
+ }
116
+ export interface CountParams {
117
+ /** Only that category's unread notifications. */
118
+ category?: string;
119
+ }
120
+ /** The unread count, for the badge. */
121
+ export interface CountResult {
122
+ count: number;
123
+ }
124
+ /**
125
+ * Which unread notifications `readAll()` marks: every one with neither,
126
+ * a category's, a subject's, or a subject's in a category. Archived unread
127
+ * ones are marked too.
128
+ */
129
+ export interface ReadAllParams {
130
+ category?: string;
131
+ subject?: NotificationSubject;
132
+ }
133
+ export interface ReadAllResult {
134
+ /** How many it marked. */
135
+ changed: number;
136
+ }
137
+ /** A category on the user's settings screen, with the user's effective value per channel. */
138
+ export interface PreferenceCategory {
139
+ /** The category's key. */
140
+ category: string;
141
+ name: string;
142
+ description: string;
143
+ /** The user can't change a locked category's switches: show them disabled. */
144
+ locked: boolean;
145
+ /** Each channel the category lists, on or off for the user. */
146
+ channels: Record<string, boolean>;
147
+ }
148
+ /** Switches to set: up to 500; one is `changes: [change]`. */
149
+ export interface SetPreferencesParams {
150
+ changes: PreferenceChange[];
151
+ }
152
+ /** One switch to set. */
153
+ export interface PreferenceChange {
154
+ /** The category's key. */
155
+ category: string;
156
+ /** A channel the category lists, e.g. `inbox`, `email`. */
157
+ channel: string;
158
+ enabled: boolean;
159
+ }
160
+ /**
161
+ * A kind of notification. The built-in `general` has no row: its `id` is
162
+ * `''` and it has no timestamps.
163
+ */
164
+ export interface Category {
165
+ id: string;
166
+ _created?: string;
167
+ _modified?: string;
168
+ /** What senders name it by: `^[a-z][a-z0-9_.-]{0,63}$`. Can't change once created. */
169
+ key: string;
170
+ name: string;
171
+ description: string;
172
+ /** Each channel it may go out on, with its default for users who haven't chosen. Always holds `inbox`. */
173
+ channels: Record<string, boolean>;
174
+ /** Users can't change their choices: everyone gets the defaults. */
175
+ locked: boolean;
176
+ }
177
+ /** The fields of a category, in the API's own shape. */
178
+ export interface CategoryFields {
179
+ /** 1–200 characters. */
180
+ name?: string;
181
+ /** Up to 1,000 characters. */
182
+ description?: string;
183
+ /**
184
+ * Channel names (`^[a-z][a-z0-9_-]{0,20}$`, at most 20) to their default.
185
+ * Must hold `inbox`.
186
+ */
187
+ channels?: Record<string, boolean>;
188
+ locked?: boolean;
189
+ }
190
+ /** A new category. Left out, `channels` is `{ inbox: true }`, `locked` false. */
191
+ export interface CreateCategoryParams extends CategoryFields {
192
+ key: string;
193
+ name: string;
194
+ }
195
+ /**
196
+ * A category's new fields; `key` names it. The server binds the body onto
197
+ * the stored category, so fields left out keep their values; `channels`,
198
+ * when given, replaces them whole. A category read from `list()` can be
199
+ * changed and passed back as it is: its `id` and timestamps aren't sent.
200
+ */
201
+ export interface UpdateCategoryParams extends CategoryFields {
202
+ key: string;
203
+ }
204
+ /**
205
+ * Who gets a send, OR'd: a person matched twice gets one notification. At
206
+ * most 5,000 people once roles and organizations are expanded.
207
+ */
208
+ export interface NotificationTarget {
209
+ /** Person ids. Not checked against identity: send ids you got from Oblax. */
210
+ users?: string[];
211
+ /** Role names, in any realm. */
212
+ roles?: string[];
213
+ /** Organization ids: their live members. */
214
+ organizations?: string[];
215
+ }
216
+ /** A send. Only `to`, `category` and `title` are required. */
217
+ export interface SendParams {
218
+ to: NotificationTarget;
219
+ /** A category's key; `general` always exists. */
220
+ category: string;
221
+ /** Plain text, up to 200 characters. */
222
+ title: string;
223
+ /** Plain text, up to 2,000 characters. */
224
+ body?: string;
225
+ /** A path of the app starting with a single `/`, or an `https://` URL. */
226
+ link?: string;
227
+ /** Any JSON for the app's own UI, up to 4,096 bytes as compact JSON. Sent as-is. */
228
+ data?: unknown;
229
+ /** Who caused it. Not left out of the recipients: leave them out of `to` yourself. */
230
+ actorId?: string;
231
+ /** The organization it's about (doesn't change who gets it). */
232
+ organizationId?: string;
233
+ /** What it's about (doesn't change who gets it). */
234
+ subject?: NotificationSubject;
235
+ /** A user who already has a notification with this key is skipped. Up to 200 characters. */
236
+ dedupKey?: string;
237
+ /** Kept until the user reads, archives or deletes it. */
238
+ critical?: boolean;
239
+ /** RFC 3339, in the future, at most 2 years ahead. */
240
+ expiresAt?: string;
241
+ }
242
+ /** The answer to a send. */
243
+ export interface SendResult {
244
+ /** The send's id (each recipient's notification has its own). */
245
+ id: string;
246
+ /** Notifications written, users with the inbox off included. */
247
+ recipients: number;
248
+ /** Users the dedup key skipped. */
249
+ skipped: number;
250
+ }
251
+ /** A send as an admin reads it back: its content and counts, never its `to`. */
252
+ export interface Send {
253
+ id: string;
254
+ _created?: string;
255
+ _modified?: string;
256
+ category: string;
257
+ title: string;
258
+ body: string;
259
+ link: string;
260
+ data: unknown;
261
+ actor_id: string;
262
+ organization_id: string;
263
+ subject: NotificationSubject | null;
264
+ dedup_key: string;
265
+ critical: boolean;
266
+ expires_at: string | null;
267
+ recipient_count: number;
268
+ skipped_count: number;
269
+ }
270
+ export interface RecipientsParams {
271
+ /** The send's id. */
272
+ id: string;
273
+ /** The channel whose switch must be on, e.g. `email`; `inbox` for those whose notification went to their inbox. */
274
+ channel: string;
275
+ /** The previous page's `next`. */
276
+ after?: string;
277
+ /** Rows to read: 100 by default, at most 500. */
278
+ limit?: number;
279
+ }
280
+ /**
281
+ * A page of a send's recipients with the channel on, in id order. A page may
282
+ * hold fewer users than `limit`, even none, while `next` is set: page on
283
+ * until `next` is `''`.
284
+ */
285
+ export interface RecipientsPage {
286
+ users: string[];
287
+ next: string;
288
+ }
289
+ /**
290
+ * Why `onChange()` called its handler:
291
+ *
292
+ * - `connected` — the stream opened (or, for a shared `stream`, was open already);
293
+ * - `reconnected` — the stream opened again: a ring may have been missed;
294
+ * - `changed` — the user's inbox changed (`oblax.notifications.changed`);
295
+ * - `recheck` — `recheckDelay` after the last ring, in case the first
296
+ * refetch read a replica that hadn't caught up.
297
+ */
298
+ export type ChangeReason = 'connected' | 'reconnected' | 'changed' | 'recheck';
299
+ export interface ChangeInfo {
300
+ reason: ChangeReason;
301
+ /**
302
+ * 1 for the watch's first call, one more for each call after it. Refetches
303
+ * can answer out of order: keep the answer of the latest `seq` and drop
304
+ * one whose `seq` is older than an answer already shown.
305
+ */
306
+ seq: number;
307
+ }
308
+ /** What `oblax.notifications.onChange()` takes. */
309
+ export interface OnChangeParams extends RealtimeConfig {
310
+ /**
311
+ * Called whenever the inbox may have changed: refetch the count here. What
312
+ * it throws, or the rejection of the promise it returns, goes to `onError`.
313
+ */
314
+ handler: (info: ChangeInfo) => unknown;
315
+ /**
316
+ * A stream the app already has, to share (one stream per tab serves the
317
+ * bell and the app's own events). Without one, `onChange()` opens its own
318
+ * with `realtime.connect()`, tuned by the module's config and the
319
+ * `RealtimeConfig` options given here. Tuning given with `stream` throws:
320
+ * a shared stream keeps its own. `close()` leaves a shared stream open; one
321
+ * already closed ends the watch at once.
322
+ */
323
+ stream?: RealtimeStream<any>;
324
+ /** Overrides the module's `recheckDelay`; `0` turns the second call off. */
325
+ recheckDelay?: number;
326
+ /** Aborting it ends the watch, as `close()` does. */
327
+ signal?: AbortSignal;
328
+ /**
329
+ * Gets what the handler threw, or the rejection of the promise it
330
+ * returned. Without it, those are ignored: a failed refetch doesn't stop
331
+ * the bell.
332
+ */
333
+ onError?: (error: unknown) => void;
334
+ }
335
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAQrE;;;;;GAKG;AACH,MAAM,WAAW,mBAAoB,SAAQ,cAAc;IACzD;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAID,2DAA2D;AAC3D,MAAM,WAAW,iBAAiB,CAAC,CAAC;IA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,KAAK,IAAI,CAAA;CACnC"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,63 @@
1
+ import type { RealtimeStream } from '@oblax/realtime';
2
+ import type { ChangeInfo } from './types.js';
3
+ /** The realtime event notifications-dom sends a user's streams when their inbox changes. It carries no data. */
4
+ export declare const CHANGED_EVENT = "oblax.notifications.changed";
5
+ /** A watch's resolved settings. */
6
+ export interface WatchSettings {
7
+ recheckDelay: number;
8
+ /** `onChange()` opened the stream: `close()` closes it too. */
9
+ owned: boolean;
10
+ signal?: AbortSignal | undefined;
11
+ onError?: ((error: unknown) => void) | undefined;
12
+ }
13
+ /**
14
+ * Calls a handler whenever the signed-in user's inbox may have changed: when
15
+ * the stream opens (first time and every reconnect: a ring may have been
16
+ * missed), on every `oblax.notifications.changed`, and once more
17
+ * `recheckDelay` after the last ring. Created by
18
+ * `oblax.notifications.onChange()`; the reconnecting is the stream's.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * let shown = 0
23
+ * const bell = oblax.notifications.onChange({
24
+ * handler: async ({ seq }) => {
25
+ * const { count } = await oblax.notifications.count()
26
+ * if (seq < shown) return // a later refetch answered first
27
+ * shown = seq
28
+ * setBadge(count)
29
+ * },
30
+ * })
31
+ * bell.stream.onClose(({ reason }) => { if (reason !== 'closed') startPolling() })
32
+ * // on sign-out
33
+ * bell.close()
34
+ * ```
35
+ */
36
+ export declare class NotificationsWatch {
37
+ /** The stream the watch listens on: its own, or the one passed as `stream`. */
38
+ readonly stream: RealtimeStream<any>;
39
+ private readonly handler;
40
+ private readonly settings;
41
+ private readonly offs;
42
+ private recheck;
43
+ private stopped;
44
+ private seq;
45
+ private readonly onSignalAbort;
46
+ /** @internal Use `oblax.notifications.onChange()`. */
47
+ constructor(stream: RealtimeStream<any>, handler: (info: ChangeInfo) => unknown, settings: WatchSettings);
48
+ /** Whether the watch ended: `close()`, the `signal`, or its stream closed for good. */
49
+ get closed(): boolean;
50
+ /**
51
+ * Ends the watch: removes its handlers from the stream, drops a pending
52
+ * recheck and, when `onChange()` opened the stream, closes it. A shared
53
+ * stream stays open. Safe to call more than once.
54
+ */
55
+ close(): void;
56
+ private stop;
57
+ /** One recheck, `recheckDelay` after the last ring: a new ring starts it over. */
58
+ private scheduleRecheck;
59
+ /** Calls the handler; what it throws or rejects with goes to `onError`. */
60
+ private call;
61
+ private fail;
62
+ }
63
+ //# sourceMappingURL=watch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch.d.ts","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,KAAK,EAAE,UAAU,EAAgB,MAAM,YAAY,CAAA;AAE1D,gHAAgH;AAChH,eAAO,MAAM,aAAa,gCAAgC,CAAA;AAE1D,mCAAmC;AACnC,MAAM,WAAW,aAAa;IAC5B,YAAY,EAAE,MAAM,CAAA;IACpB,+DAA+D;IAC/D,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;IAChC,OAAO,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;CACjD;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,kBAAkB;IAC7B,+EAA+E;IAE/E,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,CAAC,CAAA;IAEpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAwB;IAC7C,OAAO,CAAC,OAAO,CAA2C;IAC1D,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,GAAG,CAAI;IACf,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA2B;IAEzD,sDAAsD;gBAE1C,MAAM,EAAE,cAAc,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,OAAO,EAAE,QAAQ,EAAE,aAAa;IAiCxG,uFAAuF;IACvF,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED;;;;OAIG;IACH,KAAK,IAAI,IAAI;IAMb,OAAO,CAAC,IAAI;IAUZ,kFAAkF;IAClF,OAAO,CAAC,eAAe;IASvB,2EAA2E;IAC3E,OAAO,CAAC,IAAI;IAUZ,OAAO,CAAC,IAAI;CAOb"}
package/dist/watch.js ADDED
@@ -0,0 +1,123 @@
1
+ /** The realtime event notifications-dom sends a user's streams when their inbox changes. It carries no data. */
2
+ export const CHANGED_EVENT = 'oblax.notifications.changed';
3
+ /**
4
+ * Calls a handler whenever the signed-in user's inbox may have changed: when
5
+ * the stream opens (first time and every reconnect: a ring may have been
6
+ * missed), on every `oblax.notifications.changed`, and once more
7
+ * `recheckDelay` after the last ring. Created by
8
+ * `oblax.notifications.onChange()`; the reconnecting is the stream's.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * let shown = 0
13
+ * const bell = oblax.notifications.onChange({
14
+ * handler: async ({ seq }) => {
15
+ * const { count } = await oblax.notifications.count()
16
+ * if (seq < shown) return // a later refetch answered first
17
+ * shown = seq
18
+ * setBadge(count)
19
+ * },
20
+ * })
21
+ * bell.stream.onClose(({ reason }) => { if (reason !== 'closed') startPolling() })
22
+ * // on sign-out
23
+ * bell.close()
24
+ * ```
25
+ */
26
+ export class NotificationsWatch {
27
+ /** @internal Use `oblax.notifications.onChange()`. */
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
+ constructor(stream, handler, settings) {
30
+ this.offs = [];
31
+ this.stopped = false;
32
+ this.seq = 0;
33
+ this.onSignalAbort = () => this.close();
34
+ this.stream = stream;
35
+ this.handler = handler;
36
+ this.settings = settings;
37
+ const signal = settings.signal;
38
+ if (signal?.aborted) {
39
+ this.close();
40
+ return;
41
+ }
42
+ // a stream closed for good takes no handlers and never closes again: nothing will ring
43
+ if (stream.state === 'closed') {
44
+ this.stop();
45
+ return;
46
+ }
47
+ signal?.addEventListener('abort', this.onSignalAbort, { once: true });
48
+ this.offs.push(stream.onOpen(({ reconnected }) => this.call(reconnected ? 'reconnected' : 'connected')), stream.on(CHANGED_EVENT, () => {
49
+ this.call('changed');
50
+ this.scheduleRecheck();
51
+ }),
52
+ // closed for good (by the app, replaced, fatal): nothing more will ring
53
+ stream.onClose(() => this.stop()));
54
+ // a shared stream that's open already won't open again until it reconnects
55
+ if (!settings.owned && stream.state === 'open') {
56
+ void Promise.resolve().then(() => this.call('connected'));
57
+ }
58
+ }
59
+ /** Whether the watch ended: `close()`, the `signal`, or its stream closed for good. */
60
+ get closed() {
61
+ return this.stopped;
62
+ }
63
+ /**
64
+ * Ends the watch: removes its handlers from the stream, drops a pending
65
+ * recheck and, when `onChange()` opened the stream, closes it. A shared
66
+ * stream stays open. Safe to call more than once.
67
+ */
68
+ close() {
69
+ if (this.stopped)
70
+ return;
71
+ this.stop();
72
+ if (this.settings.owned)
73
+ this.stream.close();
74
+ }
75
+ stop() {
76
+ if (this.stopped)
77
+ return;
78
+ this.stopped = true;
79
+ if (this.recheck !== undefined)
80
+ clearTimeout(this.recheck);
81
+ this.recheck = undefined;
82
+ for (const off of this.offs)
83
+ off();
84
+ this.offs.length = 0;
85
+ this.settings.signal?.removeEventListener('abort', this.onSignalAbort);
86
+ }
87
+ /** One recheck, `recheckDelay` after the last ring: a new ring starts it over. */
88
+ scheduleRecheck() {
89
+ if (this.stopped || this.settings.recheckDelay <= 0)
90
+ return;
91
+ if (this.recheck !== undefined)
92
+ clearTimeout(this.recheck);
93
+ this.recheck = setTimeout(() => {
94
+ this.recheck = undefined;
95
+ this.call('recheck');
96
+ }, this.settings.recheckDelay);
97
+ }
98
+ /** Calls the handler; what it throws or rejects with goes to `onError`. */
99
+ call(reason) {
100
+ if (this.stopped)
101
+ return;
102
+ try {
103
+ const result = this.handler({ reason, seq: ++this.seq });
104
+ if (isThenable(result))
105
+ result.then(undefined, (error) => this.fail(error));
106
+ }
107
+ catch (error) {
108
+ this.fail(error);
109
+ }
110
+ }
111
+ fail(error) {
112
+ try {
113
+ this.settings.onError?.(error);
114
+ }
115
+ catch {
116
+ // swallowed: there's nowhere left to report it
117
+ }
118
+ }
119
+ }
120
+ function isThenable(v) {
121
+ return typeof v === 'object' && v !== null && typeof v.then === 'function';
122
+ }
123
+ //# sourceMappingURL=watch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"watch.js","sourceRoot":"","sources":["../src/watch.ts"],"names":[],"mappings":"AAGA,gHAAgH;AAChH,MAAM,CAAC,MAAM,aAAa,GAAG,6BAA6B,CAAA;AAW1D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,kBAAkB;IAa7B,sDAAsD;IACtD,8DAA8D;IAC9D,YAAY,MAA2B,EAAE,OAAsC,EAAE,QAAuB;QARvF,SAAI,GAAsB,EAAE,CAAA;QAErC,YAAO,GAAG,KAAK,CAAA;QACf,QAAG,GAAG,CAAC,CAAA;QACE,kBAAa,GAAG,GAAS,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;QAKvD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QAExB,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAA;QAC9B,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;YACpB,IAAI,CAAC,KAAK,EAAE,CAAA;YACZ,OAAM;QACR,CAAC;QACD,uFAAuF;QACvF,IAAI,MAAM,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,IAAI,CAAC,IAAI,EAAE,CAAA;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KAAK,CAAC,CAAA;QAClB,CAAC;IACH,CAAC;IAEO,IAAI,CAAC,KAAc;QACzB,IAAI,CAAC;YACH,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;QAAC,MAAM,CAAC;YACP,+CAA+C;QACjD,CAAC;IACH,CAAC;CACF;AAED,SAAS,UAAU,CAAC,CAAU;IAC5B,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,OAAQ,CAAwB,CAAC,IAAI,KAAK,UAAU,CAAA;AACpG,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@oblax/notifications",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "2.0.0",
4
+ "description": "Oblax SDK — notifications module (the user's inbox, preferences, categories, sends, the bell)",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Oblax SDK for JavaScript Team",
8
+ "url": "https://docs.oblax.io/docs/getting-started/your-first-sdk-call"
9
+ },
10
+ "homepage": "https://github.com/oblaxio/oblax-sdk-js#readme",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/oblaxio/oblax-sdk-js.git",
14
+ "directory": "packages/notifications"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/oblaxio/oblax-sdk-js/issues"
18
+ },
19
+ "keywords": [
20
+ "oblax",
21
+ "sdk",
22
+ "notifications"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "type": "module",
28
+ "main": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "import": "./dist/index.js",
33
+ "types": "./dist/index.d.ts"
34
+ }
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "peerDependencies": {
40
+ "@oblax/core": "^2.0.0",
41
+ "@oblax/realtime": "^2.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "typescript": "^5.5.0",
45
+ "@oblax/realtime": "2.0.0",
46
+ "@oblax/core": "2.0.0"
47
+ },
48
+ "scripts": {
49
+ "build": "tsc --build tsconfig.json",
50
+ "typecheck": "tsc --build tsconfig.json --dry",
51
+ "test": "tsc --build tsconfig.json && node --test \"test/*.test.ts\"",
52
+ "clean": "rm -rf dist tsconfig.tsbuildinfo"
53
+ }
6
54
  }