@anycon/sdk 0.3.1 → 0.4.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/README.md +86 -3
- package/README.zh-CN.md +87 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/node.d.ts +161 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +173 -0
- package/dist/node.js.map +1 -1
- package/dist/pair.d.ts +2 -1
- package/dist/pair.d.ts.map +1 -1
- package/dist/pair.js.map +1 -1
- package/dist/session.d.ts +17 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +90 -27
- package/dist/session.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +7 -7
- package/src/index.ts +36 -0
- package/src/node.ts +272 -0
- package/src/pair.ts +2 -1
- package/src/session.ts +90 -22
- package/src/version.ts +1 -1
package/src/session.ts
CHANGED
|
@@ -31,7 +31,7 @@ import type {
|
|
|
31
31
|
} from '@anycon/protocol'
|
|
32
32
|
|
|
33
33
|
import { AsyncQueue } from './async-queue.js'
|
|
34
|
-
import { AnyconError, NodeOffline } from './errors.js'
|
|
34
|
+
import { AnyconError, ConnectionError, NodeOffline } from './errors.js'
|
|
35
35
|
import type { Node } from './node.js'
|
|
36
36
|
import type { PermissionPolicy } from './permissions.js'
|
|
37
37
|
|
|
@@ -139,6 +139,46 @@ export interface ArchiveOptions {
|
|
|
139
139
|
worktree?: WorktreeDisposal
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
+
/**
|
|
143
|
+
* 补历史期间扣住实时事件的闸门(`events({ fromSeq })` 和重连后的 `Session.resync()` 共用)。
|
|
144
|
+
* 补历史时,实时事件到达的时机不受控:它们可能比还没补完的历史更新。如果直接放行,消费者先看到较新的 seq,
|
|
145
|
+
* 随后补来的较旧事件会被「seq 不大于已见过的最大值」的去重丢掉。所以补历史期间 `live()` 只扣住,
|
|
146
|
+
* 补完(`release()`)之后按 seq 接在历史后面;`emit` 负责去重和保序。
|
|
147
|
+
*/
|
|
148
|
+
class ReplayGate {
|
|
149
|
+
readonly #emit: (se: SessionEvent) => void
|
|
150
|
+
readonly #held: SessionEvent[] = []
|
|
151
|
+
#replaying: boolean
|
|
152
|
+
|
|
153
|
+
constructor(emit: (se: SessionEvent) => void, replaying: boolean) {
|
|
154
|
+
this.#emit = emit
|
|
155
|
+
this.#replaying = replaying
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get replaying(): boolean {
|
|
159
|
+
return this.#replaying
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** 实时事件:补历史期间扣住,之后直接放行。 */
|
|
163
|
+
live(se: SessionEvent): void {
|
|
164
|
+
if (this.#replaying) this.#held.push(se)
|
|
165
|
+
else this.#emit(se)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 补历史没有补完而放弃:扣住的实时事件丢掉(不放出),之后的实时事件直接放行。 */
|
|
169
|
+
discard(): void {
|
|
170
|
+
this.#replaying = false
|
|
171
|
+
this.#held.length = 0
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 补历史结束:按 seq 放出扣住的实时事件,之后的实时事件直接放行。 */
|
|
175
|
+
release(): void {
|
|
176
|
+
this.#replaying = false
|
|
177
|
+
for (const se of this.#held.toSorted((a, b) => a.seq - b.seq)) this.#emit(se)
|
|
178
|
+
this.#held.length = 0
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
142
182
|
/** `session.delete` 的选项。 */
|
|
143
183
|
export interface DeleteOptions extends ArchiveOptions {
|
|
144
184
|
/** 同时删掉 worktree 的分支(`git branch -D`),分支上没合并的提交会丢。只有 worktree 也被删掉时才生效。 */
|
|
@@ -176,6 +216,9 @@ export class Session {
|
|
|
176
216
|
readonly #subs = new Set<Subscriber>()
|
|
177
217
|
#watched = false
|
|
178
218
|
#watchChain: Promise<unknown> = Promise.resolve()
|
|
219
|
+
#resyncChain: Promise<unknown> = Promise.resolve()
|
|
220
|
+
/** `resync()` 补历史期间的闸门;有它时 `dispatch` 的实时事件先扣住。 */
|
|
221
|
+
#resyncGate: ReplayGate | undefined
|
|
179
222
|
|
|
180
223
|
/** @internal 由 `Node` 创建。 */
|
|
181
224
|
constructor(node: Node, summary: SessionSummary) {
|
|
@@ -329,17 +372,13 @@ export class Session {
|
|
|
329
372
|
const onAbort = () => queue.fail(options.signal?.reason ?? new Error('aborted'))
|
|
330
373
|
// 已经推给消费者的最大 seq;补历史和实时事件会重叠,按它去重、保序。
|
|
331
374
|
let high = options.fromSeq ?? 0
|
|
332
|
-
let replaying = options.fromSeq !== undefined
|
|
333
|
-
const held: SessionEvent[] = []
|
|
334
375
|
const emit = (se: SessionEvent) => {
|
|
335
376
|
if (se.seq <= high) return
|
|
336
377
|
high = se.seq
|
|
337
378
|
queue.push(this.#toStream(se))
|
|
338
379
|
}
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
else emit(se)
|
|
342
|
-
}
|
|
380
|
+
const gate = new ReplayGate(emit, options.fromSeq !== undefined)
|
|
381
|
+
const live = (se: SessionEvent) => gate.live(se)
|
|
343
382
|
release = this.#subscribe(live)
|
|
344
383
|
options.signal?.addEventListener('abort', onAbort, { once: true })
|
|
345
384
|
queue.onDispose(this.#guardQueue(queue, 0))
|
|
@@ -347,9 +386,7 @@ export class Session {
|
|
|
347
386
|
await this.#ensureWatching()
|
|
348
387
|
if (options.fromSeq !== undefined) {
|
|
349
388
|
await this.#replay(emit, options.fromSeq)
|
|
350
|
-
|
|
351
|
-
for (const se of held.toSorted((a, b) => a.seq - b.seq)) emit(se)
|
|
352
|
-
held.length = 0
|
|
389
|
+
gate.release()
|
|
353
390
|
}
|
|
354
391
|
})().catch((err: unknown) => queue.fail(err))
|
|
355
392
|
return queue
|
|
@@ -483,8 +520,13 @@ export class Session {
|
|
|
483
520
|
|
|
484
521
|
// ---------- 内部:事件分发 ----------
|
|
485
522
|
|
|
486
|
-
/** @internal Node 收到 `session.event` 时调用。按 seq
|
|
523
|
+
/** @internal Node 收到 `session.event` 时调用。按 seq 去重;`resync()` 补历史期间先扣住。 */
|
|
487
524
|
dispatch(se: SessionEvent): void {
|
|
525
|
+
if (this.#resyncGate) this.#resyncGate.live(se)
|
|
526
|
+
else this.#deliver(se)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
#deliver(se: SessionEvent): void {
|
|
488
530
|
if (se.seq <= this.#lastSeq) return
|
|
489
531
|
this.#lastSeq = se.seq
|
|
490
532
|
for (const sub of Array.from(this.#subs)) {
|
|
@@ -502,20 +544,46 @@ export class Session {
|
|
|
502
544
|
}
|
|
503
545
|
}
|
|
504
546
|
|
|
505
|
-
/**
|
|
506
|
-
|
|
547
|
+
/**
|
|
548
|
+
* @internal 重新握手 / 重连之后:重新 watch 并补齐错过的事件。
|
|
549
|
+
*
|
|
550
|
+
* 补历史(`session.history`,分页)期间到达的实时事件先扣住(和 `events({ fromSeq })` 同一个闸门),补完之后
|
|
551
|
+
* 按 seq 接在历史后面,所以订阅者看到的 seq 严格递增、每个只有一次,不会因为较新的实时事件先到而丢掉较旧的历史事件。
|
|
552
|
+
* `since` 在重新 watch 之前取,补的是「断线前最后一次看到的 seq」之后的全部。
|
|
553
|
+
* 补历史没补完(`watch` 或 `history` 抛错)时,看通道还在不在:
|
|
554
|
+
* - 通道没了(节点不在线,或错误是 `ConnectionError` / `NodeOffline`;最常见的原因是补历史期间又断了一次):
|
|
555
|
+
* 扣住的实时事件**丢掉**,`lastSeq` 停在已经交付的地方。daemon 持久化事件,下一次 `ready` 的 `resync()`
|
|
556
|
+
* 从这里补,补回的历史把丢掉的实时事件也带上,仍然按序、每个一次。如果放出它们,`lastSeq` 会越过还没补到的历史,
|
|
557
|
+
* 那段历史永远补不回来;
|
|
558
|
+
* - 通道还在、是别的原因失败(比如 `history` 答 `internal`):扣住的实时事件照样按序放出(不丢、不乱序、不重复),
|
|
559
|
+
* 没补上的历史留下一个空档,调用方之后用 `history({ afterSeq })` 取回。
|
|
560
|
+
* 错误向上抛(`Node` 吞掉它)。丢掉的 `permission` 事件会随下一次补历史重新交付并交给权限策略(`permission.request`
|
|
561
|
+
* 通知不经过会话的事件分发,`Node` 直接处理,不受影响)。同一个会话的多次 `resync()` 排队执行。
|
|
562
|
+
*/
|
|
563
|
+
resync(): Promise<void> {
|
|
564
|
+
const run = this.#resyncChain.then(() => this.#resyncOnce())
|
|
565
|
+
this.#resyncChain = run.catch(() => undefined)
|
|
566
|
+
return run
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
async #resyncOnce(): Promise<void> {
|
|
507
570
|
if (this.#subs.size === 0) return
|
|
508
|
-
this.#watched = false
|
|
509
|
-
await this.#ensureWatching()
|
|
510
571
|
const since = this.#lastSeq
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
572
|
+
const gate = new ReplayGate((se) => this.#deliver(se), true)
|
|
573
|
+
this.#resyncGate = gate
|
|
574
|
+
try {
|
|
575
|
+
this.#watched = false
|
|
576
|
+
await this.#ensureWatching()
|
|
577
|
+
await this.#replay((se) => this.#deliver(se), since)
|
|
578
|
+
} catch (err) {
|
|
579
|
+
this.#resyncGate = undefined
|
|
580
|
+
if (!this.node.online || err instanceof ConnectionError || err instanceof NodeOffline) {
|
|
581
|
+
gate.discard()
|
|
582
|
+
} else gate.release()
|
|
583
|
+
throw err
|
|
518
584
|
}
|
|
585
|
+
this.#resyncGate = undefined
|
|
586
|
+
gate.release()
|
|
519
587
|
}
|
|
520
588
|
|
|
521
589
|
#subscribe(sub: Subscriber): () => void {
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** SDK 版本。与 package.json 一致(`tests/version.test.ts` 守着)。 */
|
|
2
|
-
export const SDK_VERSION = '0.
|
|
2
|
+
export const SDK_VERSION = '0.4.2'
|