y-protocols
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -20,3 +20,3 @@ 'use strict'; | ||
*/ | ||
class YWithAwareness extends Y.Y { | ||
class YWithAwareness extends Y.Doc { | ||
constructor () { | ||
@@ -23,0 +23,0 @@ super(); |
@@ -18,3 +18,3 @@ 'use strict'; | ||
* @param {encoding.Encoder} encoder | ||
* @param {Y.Y} y | ||
* @param {Y.Doc} y | ||
* @param {Map<number, string>} userMap | ||
@@ -21,0 +21,0 @@ */ |
@@ -47,27 +47,11 @@ 'use strict'; | ||
/** | ||
* Read SyncStep1 and return it as a readable string. | ||
* | ||
* @param {decoding.Decoder} decoder | ||
* @return {string} | ||
*/ | ||
const stringifySyncStep1 = (decoder) => { | ||
let s = 'SyncStep1: '; | ||
const len = decoding.readUint32(decoder); | ||
for (let i = 0; i < len; i++) { | ||
const user = decoding.readVarUint(decoder); | ||
const clock = decoding.readVarUint(decoder); | ||
s += `(${user}:${clock})`; | ||
} | ||
return s | ||
}; | ||
/** | ||
* Create a sync step 1 message based on the state of the current shared document. | ||
* | ||
* @param {encoding.Encoder} encoder | ||
* @param {Y.StructStore} store | ||
* @param {Y.Doc} doc | ||
*/ | ||
const writeSyncStep1 = (encoder, store) => { | ||
const writeSyncStep1 = (encoder, doc) => { | ||
encoding.writeVarUint(encoder, messageYjsSyncStep1); | ||
Y.writeStates(encoder, store); | ||
const sv = Y.encodeDocumentStateVector(doc); | ||
encoding.writeVarUint8Array(encoder, sv); | ||
}; | ||
@@ -77,8 +61,8 @@ | ||
* @param {encoding.Encoder} encoder | ||
* @param {Y.StructStore} store | ||
* @param {Map<number, number>} sm | ||
* @param {Y.Doc} doc | ||
* @param {Uint8Array} encodedStateVector | ||
*/ | ||
const writeSyncStep2 = (encoder, store, sm) => { | ||
const writeSyncStep2 = (encoder, doc, encodedStateVector) => { | ||
encoding.writeVarUint(encoder, messageYjsSyncStep2); | ||
Y.writeModel(encoder, store, sm); | ||
encoding.writeVarUint8Array(encoder, Y.encodeStateAsUpdate(doc, encodedStateVector)); | ||
}; | ||
@@ -91,6 +75,6 @@ | ||
* @param {encoding.Encoder} encoder The received message | ||
* @param {Y.StructStore} store | ||
* @param {Y.Doc} doc | ||
*/ | ||
const readSyncStep1 = (decoder, encoder, store) => | ||
writeSyncStep2(encoder, store, Y.readStatesAsMap(decoder)); | ||
const readSyncStep1 = (decoder, encoder, doc) => | ||
writeSyncStep2(encoder, doc, decoding.readVarUint8Array(decoder)); | ||
@@ -101,17 +85,26 @@ /** | ||
* @param {decoding.Decoder} decoder | ||
* @param {Y.Transaction} transaction | ||
* @param {Y.StructStore} store | ||
* @param {Y.Doc} doc | ||
* @param {any} transactionOrigin | ||
*/ | ||
const readSyncStep2 = Y.readModel; | ||
const readSyncStep2 = (decoder, doc, transactionOrigin) => { | ||
Y.applyUpdate(doc, decoding.readVarUint8Array(decoder), transactionOrigin); | ||
}; | ||
/** | ||
* @param {encoding.Encoder} encoder | ||
* @param {encoding.Encoder} update | ||
* @param {Uint8Array} update | ||
*/ | ||
const writeUpdate = (encoder, update) => { | ||
encoding.writeVarUint(encoder, messageYjsUpdate); | ||
encoding.writeBinaryEncoder(encoder, update); | ||
encoding.writeVarUint8Array(encoder, update); | ||
}; | ||
const readUpdate = Y.readModel; | ||
/** | ||
* Read and apply Structs and then DeleteStore to a y instance. | ||
* | ||
* @param {decoding.Decoder} decoder | ||
* @param {Y.Doc} doc | ||
* @param {any} transactionOrigin | ||
*/ | ||
const readUpdate = readSyncStep2; | ||
@@ -121,18 +114,16 @@ /** | ||
* @param {encoding.Encoder} encoder The reply message. Will not be sent if empty. | ||
* @param {Y.Y} y | ||
* @param {any} origin | ||
* @param {Y.Doc} doc | ||
* @param {any} transactionOrigin | ||
*/ | ||
const readSyncMessage = (decoder, encoder, y, origin) => { | ||
const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => { | ||
const messageType = decoding.readVarUint(decoder); | ||
switch (messageType) { | ||
case messageYjsSyncStep1: | ||
readSyncStep1(decoder, encoder, y.store); | ||
readSyncStep1(decoder, encoder, doc); | ||
break | ||
case messageYjsSyncStep2: | ||
// @ts-ignore | ||
y.transact(transaction => readSyncStep2(decoder, transaction, y.store), origin); | ||
readSyncStep2(decoder, doc, transactionOrigin); | ||
break | ||
case messageYjsUpdate: | ||
// @ts-ignore | ||
y.transact(transaction => readUpdate(decoder, transaction, y.store), origin); | ||
readUpdate(decoder, doc, transactionOrigin); | ||
break | ||
@@ -148,3 +139,2 @@ default: | ||
exports.messageYjsUpdate = messageYjsUpdate; | ||
exports.stringifySyncStep1 = stringifySyncStep1; | ||
exports.writeSyncStep1 = writeSyncStep1; | ||
@@ -151,0 +141,0 @@ exports.writeSyncStep2 = writeSyncStep2; |
@@ -15,3 +15,3 @@ | ||
* @param {encoding.Encoder} encoder | ||
* @param {Y.Y} y | ||
* @param {Y.Doc} y | ||
* @param {Map<number, string>} userMap | ||
@@ -18,0 +18,0 @@ */ |
{ | ||
"name": "y-protocols", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Yjs encoding protocols", | ||
@@ -15,3 +15,3 @@ "files": [ | ||
"lint": "standard", | ||
"preversion": "npm run test", | ||
"preversion": "npm run dist && npm run test", | ||
"version": "npm run dist" | ||
@@ -32,3 +32,5 @@ }, | ||
"homepage": "https://github.com/y-js/y-protocols#readme", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"lib0": "0.0.4" | ||
}, | ||
"devDependencies": { | ||
@@ -40,5 +42,4 @@ "rollup": "^1.1.2", | ||
"peerDependenies": { | ||
"lib0": "*", | ||
"yjs": "*" | ||
} | ||
} |
73
sync.js
@@ -43,27 +43,11 @@ /** | ||
/** | ||
* Read SyncStep1 and return it as a readable string. | ||
* | ||
* @param {decoding.Decoder} decoder | ||
* @return {string} | ||
*/ | ||
export const stringifySyncStep1 = (decoder) => { | ||
let s = 'SyncStep1: ' | ||
const len = decoding.readUint32(decoder) | ||
for (let i = 0; i < len; i++) { | ||
const user = decoding.readVarUint(decoder) | ||
const clock = decoding.readVarUint(decoder) | ||
s += `(${user}:${clock})` | ||
} | ||
return s | ||
} | ||
/** | ||
* Create a sync step 1 message based on the state of the current shared document. | ||
* | ||
* @param {encoding.Encoder} encoder | ||
* @param {Y.StructStore} store | ||
* @param {Y.Doc} doc | ||
*/ | ||
export const writeSyncStep1 = (encoder, store) => { | ||
export const writeSyncStep1 = (encoder, doc) => { | ||
encoding.writeVarUint(encoder, messageYjsSyncStep1) | ||
Y.writeStates(encoder, store) | ||
const sv = Y.encodeDocumentStateVector(doc) | ||
encoding.writeVarUint8Array(encoder, sv) | ||
} | ||
@@ -73,8 +57,8 @@ | ||
* @param {encoding.Encoder} encoder | ||
* @param {Y.StructStore} store | ||
* @param {Map<number, number>} sm | ||
* @param {Y.Doc} doc | ||
* @param {Uint8Array} encodedStateVector | ||
*/ | ||
export const writeSyncStep2 = (encoder, store, sm) => { | ||
export const writeSyncStep2 = (encoder, doc, encodedStateVector) => { | ||
encoding.writeVarUint(encoder, messageYjsSyncStep2) | ||
Y.writeModel(encoder, store, sm) | ||
encoding.writeVarUint8Array(encoder, Y.encodeStateAsUpdate(doc, encodedStateVector)) | ||
} | ||
@@ -87,6 +71,6 @@ | ||
* @param {encoding.Encoder} encoder The received message | ||
* @param {Y.StructStore} store | ||
* @param {Y.Doc} doc | ||
*/ | ||
export const readSyncStep1 = (decoder, encoder, store) => | ||
writeSyncStep2(encoder, store, Y.readStatesAsMap(decoder)) | ||
export const readSyncStep1 = (decoder, encoder, doc) => | ||
writeSyncStep2(encoder, doc, decoding.readVarUint8Array(decoder)) | ||
@@ -97,17 +81,26 @@ /** | ||
* @param {decoding.Decoder} decoder | ||
* @param {Y.Transaction} transaction | ||
* @param {Y.StructStore} store | ||
* @param {Y.Doc} doc | ||
* @param {any} transactionOrigin | ||
*/ | ||
export const readSyncStep2 = Y.readModel | ||
export const readSyncStep2 = (decoder, doc, transactionOrigin) => { | ||
Y.applyUpdate(doc, decoding.readVarUint8Array(decoder), transactionOrigin) | ||
} | ||
/** | ||
* @param {encoding.Encoder} encoder | ||
* @param {encoding.Encoder} update | ||
* @param {Uint8Array} update | ||
*/ | ||
export const writeUpdate = (encoder, update) => { | ||
encoding.writeVarUint(encoder, messageYjsUpdate) | ||
encoding.writeBinaryEncoder(encoder, update) | ||
encoding.writeVarUint8Array(encoder, update) | ||
} | ||
export const readUpdate = Y.readModel | ||
/** | ||
* Read and apply Structs and then DeleteStore to a y instance. | ||
* | ||
* @param {decoding.Decoder} decoder | ||
* @param {Y.Doc} doc | ||
* @param {any} transactionOrigin | ||
*/ | ||
export const readUpdate = readSyncStep2 | ||
@@ -117,18 +110,16 @@ /** | ||
* @param {encoding.Encoder} encoder The reply message. Will not be sent if empty. | ||
* @param {Y.Y} y | ||
* @param {any} origin | ||
* @param {Y.Doc} doc | ||
* @param {any} transactionOrigin | ||
*/ | ||
export const readSyncMessage = (decoder, encoder, y, origin) => { | ||
export const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => { | ||
const messageType = decoding.readVarUint(decoder) | ||
switch (messageType) { | ||
case messageYjsSyncStep1: | ||
readSyncStep1(decoder, encoder, y.store) | ||
readSyncStep1(decoder, encoder, doc) | ||
break | ||
case messageYjsSyncStep2: | ||
// @ts-ignore | ||
y.transact(transaction => readSyncStep2(decoder, transaction, y.store), origin) | ||
readSyncStep2(decoder, doc, transactionOrigin) | ||
break | ||
case messageYjsUpdate: | ||
// @ts-ignore | ||
y.transact(transaction => readUpdate(decoder, transaction, y.store), origin) | ||
readUpdate(decoder, doc, transactionOrigin) | ||
break | ||
@@ -135,0 +126,0 @@ default: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22011
1
625
+ Addedlib0@0.0.4
+ Addedlib0@0.0.4(transitive)