dsh-go-workbuddy 1.1.0 → 1.1.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.
Files changed (2) hide show
  1. package/lib/index.js +17 -4
  2. package/package.json +1 -1
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
 
@@ -502,8 +512,10 @@ export function apply(ctx) {
502
512
 
503
513
 
504
514
  ctx.dshGo.strip('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.1",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "dsh": {