Skip to content

Commit 9a44e57

Browse files
committed
refactor: 令 lastEmit 相关的数组类型声明为 ReadonlyArray
...并调整 QueryToken 里的 lastEmit 字段初始值,避免使用不易与实际推出 结果区分的 `[]`,使用 undefined;traces 接口在遇到 lastEmit 尚未设值的 情况时,返回意为全新结果集的 TraceResult。
1 parent 8156526 commit 9a44e57

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/storage/modules/QueryToken.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ import { Selector } from './Selector'
44
import { ProxySelector } from './ProxySelector'
55
import { assert } from '../../utils/assert'
66
import { TokenConsumed } from '../../exception/token'
7-
import { diff, Ops, OpsType } from '../../utils/diff'
7+
import { diff, Ops, OpsType, OpType } from '../../utils/diff'
88

99
export type TraceResult<T> = Ops & {
10-
result: T[]
10+
result: ReadonlyArray<T>
11+
}
12+
13+
function initialTraceResult<T>(list: ReadonlyArray<T>): TraceResult<T> {
14+
return {
15+
type: OpsType.Success,
16+
ops: list.map((_value, index) => ({ type: OpType.New, index })),
17+
result: list
18+
}
1119
}
1220

1321
export type SelectorMeta<T> = Selector<T> | ProxySelector<T>
@@ -20,14 +28,16 @@ export class QueryToken<T> {
2028
selector$: Observable<SelectorMeta<T>>
2129

2230
private consumed = false
23-
private lastEmit: T[] = []
2431

25-
constructor(selector$: Observable<SelectorMeta<T>>, lastEmit?: T[]) {
32+
constructor(
33+
selector$: Observable<SelectorMeta<T>>,
34+
private lastEmit?: ReadonlyArray<T>
35+
) {
2636
this.selector$ = selector$.pipe(
2737
publishReplay(1),
2838
refCount(),
2939
)
30-
this.lastEmit = lastEmit || []
40+
this.lastEmit = lastEmit
3141
}
3242

3343
setLastEmit(data: T[]) {
@@ -59,6 +69,9 @@ export class QueryToken<T> {
5969
traces(pk?: string): Observable<TraceResult<T>> {
6070
return this.changes().pipe(
6171
map((result: T[]) => {
72+
if (!this.lastEmit) {
73+
return initialTraceResult(result)
74+
}
6275
const ops = diff(this.lastEmit, result, pk)
6376
return { result, ...ops }
6477
}),

src/utils/diff.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type Ops = {
2626
}
2727

2828
// as an example, use diff to patch data
29-
export const patch = <T>(ops: Op[], oldList: T[], newList: T[]) => {
29+
export const patch = <T>(ops: ReadonlyArray<Op>, oldList: ReadonlyArray<T>, newList: ReadonlyArray<T>) => {
3030
if (!oldList.length) {
3131
return newList
3232
}
@@ -42,7 +42,7 @@ export const patch = <T>(ops: Op[], oldList: T[], newList: T[]) => {
4242
})
4343
}
4444

45-
export const getPatchResult = <T>(oldList: T[], newList: T[], ops: Ops): T[] => {
45+
export const getPatchResult = <T>(oldList: ReadonlyArray<T>, newList: ReadonlyArray<T>, ops: Ops): ReadonlyArray<T> => {
4646
switch (ops.type) {
4747
case OpsType.Error:
4848
return newList
@@ -120,7 +120,7 @@ function fastEqual(left: object, right: object) {
120120
return left !== left && right !== right
121121
}
122122

123-
export function diff<T>(oldList: T[], newList: T[], pk = '_id'): Ops {
123+
export function diff<T>(oldList: ReadonlyArray<T>, newList: ReadonlyArray<T>, pk = '_id'): Ops {
124124
const prev = oldList
125125
const curr = newList
126126

0 commit comments

Comments
 (0)