dsh-bark-push 0.2.2 → 0.3.1

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/lib/index.js CHANGED
@@ -17,6 +17,7 @@ import { readFileSync } from 'node:fs';
17
17
 
18
18
  import { mergeConfig, normalizeConfig, objectOf, publicConfig } from './config.js';
19
19
  import { BarkClient } from './bark.js';
20
+ import { BrowserNotifyHub } from './browser-notify.js';
20
21
  import { ChannelSender } from './channels.js';
21
22
  import { installSessionBridge, installQuestionBridge } from './events.js';
22
23
  import { installHttpRoutes, nextConfigOverlay } from './http.js';
@@ -117,22 +118,38 @@ export function apply(ctx, config) {
117
118
  logger,
118
119
  version: packageVersion(),
119
120
  });
121
+ const browserNotify = new BrowserNotifyHub({
122
+ getConfig,
123
+ state,
124
+ notifier,
125
+ getSession: (sessionId) => {
126
+ try {
127
+ const sessions = typeof ctx.get === 'function' ? ctx.get('sessions') : ctx.sessions;
128
+ return sessions?.get?.(sessionId);
129
+ } catch {
130
+ return undefined;
131
+ }
132
+ },
133
+ logger,
134
+ });
120
135
 
121
136
  /** @type {Array<() => void>} */
122
137
  const disposers = [];
123
138
  disposers.push(
124
139
  installSessionBridge(ctx, {
125
140
  notifier,
141
+ browserNotifier: browserNotify,
126
142
  getConfig,
127
143
  logger,
128
144
  }),
129
145
  );
130
- disposers.push(installQuestionBridge(ctx, { notifier, logger }));
146
+ disposers.push(installQuestionBridge(ctx, { notifier, browserNotifier: browserNotify, logger }));
131
147
 
132
148
  const runtime = {
133
149
  version: packageVersion(),
134
150
  getConfig,
135
151
  getState: () => state.snapshot(),
152
+ browserNotify,
136
153
  warnings: () => [...new Set(warnings)],
137
154
  lastDelivery: () => notifier.lastDelivery,
138
155
  /**
@@ -165,19 +182,21 @@ export function apply(ctx, config) {
165
182
  * @param {'workspace'|'session'} kind - mute level.
166
183
  * @param {string} id - target id.
167
184
  * @param {boolean} muted - desired state.
185
+ * @param {'push'|'browser'} [channel] - notification channel.
168
186
  * @returns {Promise<void>} completion.
169
187
  */
170
- async setMute(kind, id, muted) {
171
- await state.setMuted(kind, id, muted);
188
+ async setMute(kind, id, muted, channel = 'push') {
189
+ await state.setMuted(kind, id, muted, channel);
172
190
  },
173
191
  /**
174
192
  * Toggle one mute flag.
175
193
  * @param {'workspace'|'session'} kind - mute level.
176
194
  * @param {string} id - target id.
195
+ * @param {'push'|'browser'} [channel] - notification channel.
177
196
  * @returns {Promise<{ muted: boolean }>} new value.
178
197
  */
179
- async toggleMute(kind, id) {
180
- const result = await state.toggleMuted(kind, id);
198
+ async toggleMute(kind, id, channel = 'push') {
199
+ const result = await state.toggleMuted(kind, id, channel);
181
200
  return { muted: result.muted };
182
201
  },
183
202
  /** @returns {Promise<object>} sanitized test outcome. */
@@ -203,6 +222,7 @@ export function apply(ctx, config) {
203
222
  () => () => {
204
223
  abort.abort();
205
224
  queue.dispose();
225
+ browserNotify.dispose();
206
226
  for (const dispose of disposers) {
207
227
  try {
208
228
  dispose();
package/lib/state.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Persistent plugin state: UI-managed config overrides, muted workspaces, muted
3
- * sessions, and the last test result.
2
+ * Persistent plugin state: UI-managed config overrides, muted workspaces,
3
+ * muted sessions, independent browser-notification mute sets, and the last test
4
+ * result.
4
5
  *
5
6
  * The file lives under `$DSH_HOME/bark-push/state.json`. It is written with
6
7
  * mode 0600 and an atomic rename, and a malformed file is moved aside instead
@@ -53,7 +54,7 @@ export function resolveStateFile(config, env = process.env) {
53
54
 
54
55
  /**
55
56
  * Return a fresh empty state document.
56
- * @returns {{ version: number, config: object, mutedWorkspaces: string[], mutedSessions: string[], lastTest: object|null, updatedAt: number }} state.
57
+ * @returns {{ version: number, config: object, mutedWorkspaces: string[], mutedSessions: string[], browserMutedWorkspaces: string[], browserMutedSessions: string[], lastTest: object|null, updatedAt: number }} state.
57
58
  */
58
59
  function emptyState() {
59
60
  return {
@@ -61,6 +62,8 @@ function emptyState() {
61
62
  config: {},
62
63
  mutedWorkspaces: [],
63
64
  mutedSessions: [],
65
+ browserMutedWorkspaces: [],
66
+ browserMutedSessions: [],
64
67
  lastTest: null,
65
68
  updatedAt: 0,
66
69
  };
@@ -134,6 +137,8 @@ export function normalizeState(value) {
134
137
  config: raw.config !== null && typeof raw.config === 'object' && !Array.isArray(raw.config) ? raw.config : {},
135
138
  mutedWorkspaces: idsOf(raw.mutedWorkspaces),
136
139
  mutedSessions: idsOf(raw.mutedSessions),
140
+ browserMutedWorkspaces: idsOf(raw.browserMutedWorkspaces),
141
+ browserMutedSessions: idsOf(raw.browserMutedSessions),
137
142
  lastTest: testRecordOf(raw.lastTest),
138
143
  updatedAt: Number.isFinite(raw.updatedAt) ? Number(raw.updatedAt) : 0,
139
144
  };
@@ -154,6 +159,8 @@ export class StateStore {
154
159
  this.data = emptyState();
155
160
  this.mutedWorkspaces = new Set();
156
161
  this.mutedSessions = new Set();
162
+ this.browserMutedWorkspaces = new Set();
163
+ this.browserMutedSessions = new Set();
157
164
  this.writeChain = Promise.resolve();
158
165
  this.loaded = false;
159
166
  this.mtimeMs = 0;
@@ -199,6 +206,8 @@ export class StateStore {
199
206
  this.data = state;
200
207
  this.mutedWorkspaces = new Set(state.mutedWorkspaces);
201
208
  this.mutedSessions = new Set(state.mutedSessions);
209
+ this.browserMutedWorkspaces = new Set(state.browserMutedWorkspaces);
210
+ this.browserMutedSessions = new Set(state.browserMutedSessions);
202
211
  }
203
212
 
204
213
  /**
@@ -230,6 +239,8 @@ export class StateStore {
230
239
  config: { ...this.data.config },
231
240
  mutedWorkspaces: [...this.mutedWorkspaces],
232
241
  mutedSessions: [...this.mutedSessions],
242
+ browserMutedWorkspaces: [...this.browserMutedWorkspaces],
243
+ browserMutedSessions: [...this.browserMutedSessions],
233
244
  lastTest: this.data.lastTest,
234
245
  updatedAt: this.data.updatedAt,
235
246
  };
@@ -249,15 +260,43 @@ export class StateStore {
249
260
  return false;
250
261
  }
251
262
 
263
+ /**
264
+ * Whether one workspace or Session is muted for the independent browser
265
+ * notification channel.
266
+ * @param {object} scope - mute scope.
267
+ * @param {string} [scope.sessionId] - Session id.
268
+ * @param {string} [scope.workspaceKey] - workspace id or `cwd:` fallback key.
269
+ * @returns {boolean} whether the browser notification must be suppressed.
270
+ */
271
+ isBrowserMuted({ sessionId, workspaceKey } = {}) {
272
+ if (workspaceKey && this.browserMutedWorkspaces.has(workspaceKey)) return true;
273
+ if (sessionId && this.browserMutedSessions.has(sessionId)) return true;
274
+ return false;
275
+ }
276
+
277
+ /**
278
+ * Return the mutable set for one channel and level.
279
+ * @param {'workspace'|'session'} kind - mute level.
280
+ * @param {'push'|'browser'} channel - notification channel.
281
+ * @returns {Set<string>} target set.
282
+ */
283
+ #muteSet(kind, channel) {
284
+ if (channel === 'browser') {
285
+ return kind === 'workspace' ? this.browserMutedWorkspaces : this.browserMutedSessions;
286
+ }
287
+ return kind === 'workspace' ? this.mutedWorkspaces : this.mutedSessions;
288
+ }
289
+
252
290
  /**
253
291
  * Set one mute flag and persist.
254
292
  * @param {'workspace'|'session'} kind - mute level.
255
293
  * @param {string} id - workspace id or Session id.
256
294
  * @param {boolean} muted - desired state.
295
+ * @param {'push'|'browser'} [channel] - notification channel.
257
296
  * @returns {Promise<object>} snapshot after the write.
258
297
  */
259
- async setMuted(kind, id, muted) {
260
- const target = kind === 'workspace' ? this.mutedWorkspaces : this.mutedSessions;
298
+ async setMuted(kind, id, muted, channel = 'push') {
299
+ const target = this.#muteSet(kind, channel);
261
300
  if (muted) target.add(id);
262
301
  else target.delete(id);
263
302
  await this.save({ config: this.data.config });
@@ -268,10 +307,11 @@ export class StateStore {
268
307
  * Toggle one mute flag.
269
308
  * @param {'workspace'|'session'} kind - mute level.
270
309
  * @param {string} id - workspace id or Session id.
310
+ * @param {'push'|'browser'} [channel] - notification channel.
271
311
  * @returns {Promise<{ muted: boolean, state: object }>} new state.
272
312
  */
273
- async toggleMuted(kind, id) {
274
- const target = kind === 'workspace' ? this.mutedWorkspaces : this.mutedSessions;
313
+ async toggleMuted(kind, id, channel = 'push') {
314
+ const target = this.#muteSet(kind, channel);
275
315
  const next = !target.has(id);
276
316
  if (next) target.add(id);
277
317
  else target.delete(id);
@@ -320,6 +360,8 @@ export class StateStore {
320
360
  if (patch.config !== undefined) this.data.config = { ...patch.config };
321
361
  this.data.mutedWorkspaces = [...this.mutedWorkspaces];
322
362
  this.data.mutedSessions = [...this.mutedSessions];
363
+ this.data.browserMutedWorkspaces = [...this.browserMutedWorkspaces];
364
+ this.data.browserMutedSessions = [...this.browserMutedSessions];
323
365
  this.data.version = STATE_VERSION;
324
366
  this.data.updatedAt = Date.now();
325
367
  const run = () => this.#write();
@@ -338,6 +380,8 @@ export class StateStore {
338
380
  config: this.data.config,
339
381
  mutedWorkspaces: [...this.mutedWorkspaces],
340
382
  mutedSessions: [...this.mutedSessions],
383
+ browserMutedWorkspaces: [...this.browserMutedWorkspaces],
384
+ browserMutedSessions: [...this.browserMutedSessions],
341
385
  lastTest: this.data.lastTest,
342
386
  updatedAt: Date.now(),
343
387
  },