-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathy-utils.js
More file actions
37 lines (34 loc) · 1.05 KB
/
y-utils.js
File metadata and controls
37 lines (34 loc) · 1.05 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
import * as env from 'lib0/environment'
import * as Y from '@y/y'
import { applyUpdates } from '@y-crdt/yn'
import { logger } from './logger.js'
const useYNative = env.hasConf('use-y-native')
if (useYNative) {
logger.warn('using experimental y-native')
}
/**
* Merge a set of v1-encoded updates into a single v1 update.
*
* When `gc` is `true`, deleted content is garbage-collected; when `false` it is
* retained. With the `use-y-native` conf set, the merge is delegated to the
* native yrs (Rust) binding (`@y-crdt/yn`); otherwise it runs on `@y/y`.
*
* @param {boolean} gc
* @param {Array<Uint8Array<ArrayBuffer>>} updates
* @returns {Uint8Array<ArrayBuffer>}
*/
export const mergeUpdates = (gc, updates) => {
if (useYNative) {
return /** @type {Uint8Array<ArrayBuffer>} */ (applyUpdates(gc, updates))
}
if (!gc) {
return Y.mergeUpdates(updates)
}
const ydoc = new Y.Doc()
ydoc.transact(() => {
updates.forEach(update => Y.applyUpdate(ydoc, update))
})
const result = Y.encodeStateAsUpdate(ydoc)
ydoc.destroy()
return result
}