dsh-token-gauge 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-token-gauge",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "DSH 机车风格 Token 用量/费用仪表盘侧边栏插件(稳定版 0.1.1-rc.x 兼容)",
5
5
  "type": "module",
6
6
  "main": "lib/index.mjs",
package/src/client.ts CHANGED
@@ -204,16 +204,38 @@ function GaugeTab(): React.ReactElement {
204
204
  }
205
205
 
206
206
  export const name = PLUGIN_ID
207
- export const inject = ['betterSidebar']
207
+ // 注意:不要声明 `inject: ['betterSidebar']`。
208
+ // 在本机这个 DSH cordis 启动里,兄弟 bundle 通过 ctx.provide 提供的服务在单趟
209
+ // boot 的激活检查点(assertEntriesActivated)之前不会被注入,导致插件被永久 defer
210
+ // 在 pending("waiting for service: betterSidebar")。改为在 apply 内直接
211
+ // ctx.get('betterSidebar') 取服务——bundles 顺序保证 better-sidebar 在本插件之前
212
+ // apply,服务此时已在全局注册表中。再加短轮询兜底防顺序抖动。
208
213
  export function apply(ctx: any): void {
209
- if (!ctx.betterSidebar || typeof ctx.betterSidebar.registerTab !== 'function') {
210
- console.warn(`[${PLUGIN_ID}] ctx.betterSidebar 不可用,无法注册侧边栏 Tab(请确认 dsh-better-sidebar 已安装并在 web profile 中启用)`)
211
- return
214
+ let disposed = false
215
+ const getSidebar = () =>
216
+ (typeof ctx.get === 'function' ? ctx.get('betterSidebar') : void 0) || ctx.betterSidebar
217
+ const tryRegister = (): boolean => {
218
+ if (disposed) return true
219
+ const sidebar = getSidebar()
220
+ if (!sidebar || typeof sidebar.registerTab !== 'function') return false
221
+ sidebar.registerTab({
222
+ id: PLUGIN_ID,
223
+ title: 'Token 仪表盘',
224
+ single: true,
225
+ component: GaugeTab,
226
+ })
227
+ return true
228
+ }
229
+ if (!tryRegister()) {
230
+ const timer = setInterval(() => {
231
+ if (tryRegister()) clearInterval(timer)
232
+ }, 200)
233
+ setTimeout(() => clearInterval(timer), 15000)
234
+ if (typeof ctx.on === 'function') {
235
+ ctx.on('dispose', () => {
236
+ disposed = true
237
+ clearInterval(timer)
238
+ })
239
+ }
212
240
  }
213
- ctx.betterSidebar.registerTab({
214
- id: PLUGIN_ID,
215
- title: 'Token 仪表盘',
216
- single: true,
217
- component: GaugeTab,
218
- })
219
241
  }