dsh-bark-push 0.2.2 → 0.3.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.
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
  },