-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdefault-conflict-handler.ts
More file actions
43 lines (38 loc) · 1.2 KB
/
Copy pathdefault-conflict-handler.ts
File metadata and controls
43 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {
deepEqual,
flatClone
} from '../plugins/utils/index.ts';
import { stripAttachmentsDataFromDocument } from '../rx-storage-helper.ts';
import type { RxConflictHandler, RxDocumentData } from '../types';
export const defaultConflictHandler: RxConflictHandler<any> = {
isEqual(a, b, _ctx) {
a = addAttachmentsIfNotExists(a);
b = addAttachmentsIfNotExists(b);
/**
* If the documents are deep equal,
* we have no conflict.
* On your custom conflict handler you might only
* check some properties, like the updatedAt time,
* for better performance, because deepEqual is expensive.
*/
const ret = deepEqual(
stripAttachmentsDataFromDocument(a),
stripAttachmentsDataFromDocument(b)
);
return ret;
},
resolve(i) {
/**
* The default conflict handler will always
* drop the fork state and use the master state instead.
*/
return i.realMasterState;
}
};
function addAttachmentsIfNotExists<T>(d: RxDocumentData<T>): RxDocumentData<T> {
if (!d._attachments) {
d = flatClone(d);
d._attachments = {};
}
return d;
}