Skip to content

Commit d80a2fb

Browse files
committed
feat: 当第一次全同步时提供一个额外的标识,帮助识别是第一次同步
1 parent bee649b commit d80a2fb

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

docs/zh/guide/intro/history.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
---
2-
outline: 2
3-
---
41
# 更新历史
52

63
`Autostore`共包括以下包:
7-
- `autostore`: 核心库
8-
- `@autostorejs/react`: 用于`React`的状态管理库
9-
- `@autostorejs/devtools`: 调试工具库
10-
114

5+
- `autostore`: 核心库
6+
- `@autostorejs/react`: 用于`React`的状态管理库
7+
- `@autostorejs/syncer`: 用于`AutoStore`之间的同步
8+
- `@autostorejs/devtools`: 调试工具库
129

1310
## @autostorejs/react
1411

1512
<!--@include: ../../../packages/react/CHANGELOG.md{3,}-->
1613

14+
## @autostorejs/syncer
15+
16+
<!--@include: ../../../packages/syncer/CHANGELOG.md{3,}-->
17+
1718
## autostore
18-
19+
1920
<!--@include: ../../../packages/core/CHANGELOG.md{3,}-->

packages/syncer/src/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function createSyncerPlugin() {
4343
const localSyncer = new AutoStoreSyncer(store, Object.assign({ immediate: true }, options, {
4444
transport: localTransport
4545
}))
46+
localSyncer.peer = remoteSyncer
4647
return localSyncer
4748
}
4849

packages/syncer/src/syncer.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ type NormalizeAutoStoreSyncerOptions = Required<Omit<AutoStoreSyncerOptions, 'fr
2424
to: string[]
2525
}>
2626

27+
28+
export const SYNC_INIT_FLAG = -1
29+
2730
export class AutoStoreSyncer {
28-
static seq = 0
31+
static seq = 9
2932
private _options: NormalizeAutoStoreSyncerOptions
3033
syncing: boolean = false
34+
peer?: AutoStoreSyncer
3135
private _watcher: Watcher | undefined
3236
private _operateCache: StateRemoteOperate[] = [] // 本地操作缓存,
3337
private seq: number = 0 // 实例标识
@@ -46,7 +50,7 @@ export class AutoStoreSyncer {
4650
if (typeof (this._options.to) === 'string') this._options.to = (this._options.to as string).split(PATH_DELIMITER)
4751
this.seq = ++AutoStoreSyncer.seq
4852
this._options.autostart && this.start()
49-
if (this.options.immediate) this.push()
53+
if (this.options.immediate) this.push(true)
5054
}
5155
get id() { return this._options.id }
5256
get options() { return this._options }
@@ -78,7 +82,9 @@ export class AutoStoreSyncer {
7882
if (isFunction(this._options.filter)) {
7983
if (this._options.filter(operate) === false) return
8084
}
81-
if (operate.flags === this.seq) return
85+
// 为什么要Math.abs?
86+
// 在初始化进行第一次同步时传送seq时使用了负数,这样就可以让接收方区分是否是第一次同步
87+
if (Math.abs(operate.flags || 0) === this.seq) return
8288
if (this.options.direction === "backward") return
8389
this._sendToRemote(operate)
8490
}, {
@@ -165,7 +171,7 @@ export class AutoStoreSyncer {
165171

166172
const toPath = [...this.entry, ...operate.path.slice(this.options.to.length)]
167173
const updateOpts = {
168-
flags: this.seq
174+
flags: operate.flags === SYNC_INIT_FLAG ? -this.seq : this.seq
169175
}
170176
if (type === 'set' || type === 'update') {
171177
this.store.update(state => {
@@ -251,8 +257,11 @@ export class AutoStoreSyncer {
251257
}
252258
/**
253259
* 将本地store推送到远程
260+
*
261+
* @param initial 是否是第一次同步
262+
*
254263
*/
255-
push() {
264+
push(initial: boolean = false) {
256265
const localSnap = this._getLocalSnap()
257266
if (typeof (this._options.pathMap.toRemote) === 'function') {
258267
forEachObject(localSnap, ({ value, path }) => {
@@ -264,6 +273,7 @@ export class AutoStoreSyncer {
264273
path: [... this.options.to, ...toPath],
265274
value: toValue
266275
} as StateRemoteOperate
276+
if (initial) operate.flags = SYNC_INIT_FLAG
267277
this._sendOperate(operate)
268278
}
269279
})
@@ -273,7 +283,7 @@ export class AutoStoreSyncer {
273283
type: '$push',
274284
path: this.options.to,
275285
value: localSnap,
276-
flags: 0
286+
flags: initial ? SYNC_INIT_FLAG : 0
277287
} as StateRemoteOperate)
278288
}
279289
}

0 commit comments

Comments
 (0)