dsh-go-workbuddy 1.1.0 → 1.1.2

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/cordis.patch.yml CHANGED
@@ -4,5 +4,5 @@
4
4
  - insert:
5
5
  - id: dsh-connect-workbuddy
6
6
  name: dsh-connect-workbuddy
7
- - id: dsh-go-workbuddy
8
- name: dsh-go-workbuddy
7
+ - id: local-workbuddy
8
+ name: local-workbuddy
package/lib/index.js CHANGED
@@ -142,6 +142,7 @@ function saveAccount(id) {
142
142
  }
143
143
  let stripEmit = null
144
144
  let stripLocale = 'zh'
145
+ let stripGen = 0 // strip 代次:dispose 后旧请求不能往新 strip 推,避免渲染错乱
145
146
  let stripTimer = null
146
147
 
147
148
  function isZh(caps) {
@@ -171,24 +172,33 @@ function accountQuery() {
171
172
  return accountId === '' ? '' : `&account=${encodeURIComponent(accountId)}`
172
173
  }
173
174
 
175
+ let stripFails = 0
174
176
  async function pushStrip() {
175
177
  const emit = stripEmit
178
+ const gen = stripGen
176
179
  if (typeof emit !== 'function') return
177
180
  const zh = !stripLocale.startsWith('en')
181
+ // 异步取数期间 strip 可能已被销毁/重建;代次变了就丢弃这次结果
182
+ const fresh = () => stripEmit === emit && stripGen === gen && typeof stripEmit === 'function'
178
183
  try {
179
184
  const credits = await api(`/api/credits?region=${region}${accountQuery()}`)
185
+ stripFails = 0
180
186
  const total = Number(credits?.total ?? 0)
181
- emit({ text: clip(zh ? `WB 余 ${fmt(total)} 积分` : `WB ${fmt(total)} left`, 24), tone: total <= 0 ? 'warn' : 'good' })
187
+ if (fresh()) emit({ text: clip(zh ? `WB 余 ${fmt(total)} 积分` : `WB ${fmt(total)} left`, 24), tone: total <= 0 ? 'warn' : 'good' })
182
188
  } catch (error) {
189
+ // 桥刚重启 / 网络抖动 / 限流都会让这次读取失败。偶发失败静默保留上一次的
190
+ // 显示,连续 3 次都失败才提示异常:避免胶囊在「正常文字 ↔ 错误文字」之间反复闪。
191
+ stripFails += 1
192
+ if (stripFails < 3) return
183
193
  if (error?.code === 'not_logged_in') {
184
- emit({ text: zh ? 'WB 未登录' : 'WB signed out', tone: 'warn' })
194
+ if (fresh()) emit({ text: zh ? 'WB 未登录' : 'WB signed out', tone: 'warn' })
185
195
  return
186
196
  }
187
197
  if (error?.code === 'bridge_down') {
188
- emit({ text: zh ? 'WB 桥未启动' : 'WB bridge off', tone: 'warn' })
198
+ if (fresh()) emit({ text: zh ? 'WB 桥未启动' : 'WB bridge off', tone: 'warn' })
189
199
  return
190
200
  }
191
- emit({ text: clip(zh ? `WB 读取失败:${error?.code ?? '网络'}` : 'WB read failed', 24), tone: 'warn' })
201
+ if (fresh()) emit({ text: clip(zh ? `WB 读取失败:${error?.code ?? '网络'}` : 'WB read failed', 24), tone: 'warn' })
192
202
  }
193
203
  }
194
204
 
@@ -216,7 +226,7 @@ function pollLogin(loginId) {
216
226
  pendingLogins.delete(loginId)
217
227
  if (answer.error === undefined) {
218
228
  void pushStrip()
219
- try { dshGoRef?.refresh?.('main', {}) } catch {}
229
+ try { dshGoRef?.refresh?.('wb-main', {}) } catch {}
220
230
  }
221
231
  } catch {}
222
232
  }, 3000)
@@ -228,7 +238,7 @@ async function startLogin(zh, force = false) {
228
238
  try {
229
239
  const snapshot = await api(`/api/accounts?region=${region}`)
230
240
  const n = Array.isArray(snapshot?.accounts) ? snapshot.accounts.length : 0
231
- if (n > 0) return { open: 'panel:main', toast: clip(zh ? `已登录 ${n} 个账号` : `${n} accounts`, 40) }
241
+ if (n > 0) return { open: 'panel:wb-main', toast: clip(zh ? `已登录 ${n} 个账号` : `${n} accounts`, 40) }
232
242
  } catch {}
233
243
  }
234
244
  try {
@@ -249,12 +259,12 @@ async function doCheckin(zh) {
249
259
  try {
250
260
  const before = await api(`/api/checkin?region=${region}${accountQuery()}`)
251
261
  if (before?.todayCheckedIn === true) {
252
- return { refresh: 'main', toast: clip(zh ? `今天已经签过了,连续 ${Number(before.streakDays ?? 0)} 天` : 'Already checked in today', 40) }
262
+ return { refresh: 'wb-main', toast: clip(zh ? `今天已经签过了,连续 ${Number(before.streakDays ?? 0)} 天` : 'Already checked in today', 40) }
253
263
  }
254
264
  const answer = await api(`/api/checkin?region=${region}${accountQuery()}`, { method: 'POST', body: '{}' })
255
265
  const credit = Number(answer?.claim?.credit ?? 0)
256
266
  await pushStrip()
257
- return { refresh: 'main', toast: clip(zh ? `签到成功,+${fmt(credit)} 积分` : `Checked in +${fmt(credit)}`, 40) }
267
+ return { refresh: 'wb-main', toast: clip(zh ? `签到成功,+${fmt(credit)} 积分` : `Checked in +${fmt(credit)}`, 40) }
258
268
  } catch (error) {
259
269
  if (error?.code === 'not_logged_in') return { toast: clip(zh ? '还没登录 WorkBuddy' : 'Not signed in', 40) }
260
270
  return { toast: clip(zh ? `签到失败:${error?.code ?? '网络'}` : 'Check-in failed', 40) }
@@ -373,7 +383,7 @@ async function buildPatch(zh) {
373
383
  }
374
384
 
375
385
  function mainPanel(ctx) {
376
- ctx.dshGo.panel('main', {
386
+ ctx.dshGo.panel('wb-main', {
377
387
  view: (scope, caps) => {
378
388
  const zh = isZh(caps)
379
389
  return {
@@ -428,7 +438,7 @@ function mainPanel(ctx) {
428
438
  const zh = isZh(event)
429
439
  await pushStrip()
430
440
  try { return { patch: await buildPatch(zh), toast: clip(zh ? '已刷新' : 'Refreshed', 40) } }
431
- catch { return { refresh: 'main' } }
441
+ catch { return { refresh: 'wb-main' } }
432
442
  },
433
443
  checkin: (scope, event) => doCheckin(isZh(event)),
434
444
  autoToggle: async (scope, event) => {
@@ -436,7 +446,7 @@ function mainPanel(ctx) {
436
446
  const raw = event?.value?.value
437
447
  const on = Array.isArray(raw) ? raw.includes('on') : raw === 'on'
438
448
  await setAutoEnabled(on)
439
- return { refresh: 'main', toast: clip(zh ? (on ? '已开启自动签到' : '已关闭自动签到') : (on ? 'Auto check-in on' : 'Auto check-in off'), 40) }
449
+ return { refresh: 'wb-main', toast: clip(zh ? (on ? '已开启自动签到' : '已关闭自动签到') : (on ? 'Auto check-in on' : 'Auto check-in off'), 40) }
440
450
  },
441
451
  'switch-account': async (scope, event) => {
442
452
  const zh = isZh(event)
@@ -448,7 +458,7 @@ function mainPanel(ctx) {
448
458
  try {
449
459
  return { patch: await buildPatch(zh), toast: clip(zh ? '已切换账号' : 'Switched', 40) }
450
460
  } catch {
451
- return { refresh: 'main', toast: clip(zh ? '切换中…' : 'Switching…', 40) }
461
+ return { refresh: 'wb-main', toast: clip(zh ? '切换中…' : 'Switching…', 40) }
452
462
  }
453
463
  },
454
464
  logout: async (scope, event) => {
@@ -487,7 +497,7 @@ function mainPanel(ctx) {
487
497
  saveAccount('')
488
498
  try {
489
499
  return { patch: await buildPatch(zh), toast: clip(zh ? `已切到${region === 'cn' ? '国内版' : '国际版'}` : `Switched to ${region}`, 40) }
490
- } catch { return { refresh: 'main' } }
500
+ } catch { return { refresh: 'wb-main' } }
491
501
  },
492
502
  },
493
503
  })
@@ -501,9 +511,11 @@ export function apply(ctx) {
501
511
  ctx.dshGo.on('login', (scope, event) => startLogin(isZh(event)))
502
512
 
503
513
 
504
- ctx.dshGo.strip('state', (scope, emit, caps) => {
514
+ ctx.dshGo.strip('wb-state', (scope, emit, caps) => {
515
+ stripGen += 1
505
516
  stripEmit = typeof emit === 'function' ? emit : null
506
517
  stripLocale = String(caps?.locale ?? 'zh')
518
+ stripFails = 0
507
519
  void pushStrip()
508
520
  void autoCheckin()
509
521
  restartStripTimer()
@@ -513,6 +525,7 @@ export function apply(ctx) {
513
525
  stripTimer = null
514
526
  clearInterval(autoTimer)
515
527
  stripEmit = null
528
+ stripGen += 1 // 让在途的 pushStrip 结果作废
516
529
  }
517
530
  })
518
531
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-go-workbuddy",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "dsh": {
@@ -34,14 +34,14 @@
34
34
  ],
35
35
  "composer.strip": [
36
36
  {
37
- "id": "state",
37
+ "id": "wb-state",
38
38
  "icon": "coin",
39
- "panel": "main"
39
+ "panel": "wb-main"
40
40
  }
41
41
  ],
42
42
  "panels": [
43
43
  {
44
- "id": "main",
44
+ "id": "wb-main",
45
45
  "title": {
46
46
  "zh": "WorkBuddy 积分",
47
47
  "en": "WB credits"