Compare commits
1 Commits
v13.0.0-11
...
v13.0.0-9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9634da316 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "yjs",
|
||||
"version": "13.0.0-11",
|
||||
"version": "13.0.0-9",
|
||||
"description": "A framework for real-time p2p shared editing on any data",
|
||||
"main": "./y.node.js",
|
||||
"browser": "./y.js",
|
||||
|
||||
127
src/Connector.js
127
src/Connector.js
@@ -43,12 +43,10 @@ export default function extendConnector (Y/* :any */) {
|
||||
this.setUserId(Y.utils.generateUserId())
|
||||
}
|
||||
}
|
||||
|
||||
reconnect () {
|
||||
this.log('reconnecting..')
|
||||
return this.y.db.startGarbageCollector()
|
||||
}
|
||||
|
||||
disconnect () {
|
||||
this.log('discronnecting..')
|
||||
this.connections = new Map()
|
||||
@@ -58,16 +56,13 @@ export default function extendConnector (Y/* :any */) {
|
||||
this.y.db.stopGarbageCollector()
|
||||
return this.y.db.whenTransactionsFinished()
|
||||
}
|
||||
|
||||
repair () {
|
||||
this.log('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues')
|
||||
this.connections.forEach(user => { user.isSynced = false })
|
||||
this.isSynced = false
|
||||
this.connections.forEach((user, userId) => {
|
||||
user.isSynced = false
|
||||
this._syncWithUser(userId)
|
||||
})
|
||||
this.currentSyncTarget = null
|
||||
this.findNextSyncTarget()
|
||||
}
|
||||
|
||||
setUserId (userId) {
|
||||
if (this.userId == null) {
|
||||
if (!Number.isInteger(userId)) {
|
||||
@@ -82,21 +77,20 @@ export default function extendConnector (Y/* :any */) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
onUserEvent (f) {
|
||||
this.userEventListeners.push(f)
|
||||
}
|
||||
|
||||
removeUserEventListener (f) {
|
||||
this.userEventListeners = this.userEventListeners.filter(g => f !== g)
|
||||
}
|
||||
|
||||
userLeft (user) {
|
||||
if (this.connections.has(user)) {
|
||||
this.log('%s: User left %s', this.userId, user)
|
||||
this.connections.delete(user)
|
||||
// check if isSynced event can be sent now
|
||||
this._setSyncedWith(null)
|
||||
if (user === this.currentSyncTarget) {
|
||||
this.currentSyncTarget = null
|
||||
this.findNextSyncTarget()
|
||||
}
|
||||
for (var f of this.userEventListeners) {
|
||||
f({
|
||||
action: 'userLeft',
|
||||
@@ -105,7 +99,7 @@ export default function extendConnector (Y/* :any */) {
|
||||
}
|
||||
}
|
||||
}
|
||||
userJoined (user, role, auth) {
|
||||
userJoined (user, role) {
|
||||
if (role == null) {
|
||||
throw new Error('You must specify the role of the joined user!')
|
||||
}
|
||||
@@ -118,7 +112,7 @@ export default function extendConnector (Y/* :any */) {
|
||||
isSynced: false,
|
||||
role: role,
|
||||
processAfterAuth: [],
|
||||
auth: auth || null,
|
||||
auth: null,
|
||||
receivedSyncStep2: false
|
||||
})
|
||||
let defer = {}
|
||||
@@ -131,7 +125,9 @@ export default function extendConnector (Y/* :any */) {
|
||||
role: role
|
||||
})
|
||||
}
|
||||
this._syncWithUser(user)
|
||||
if (this.currentSyncTarget == null) {
|
||||
this.findNextSyncTarget()
|
||||
}
|
||||
}
|
||||
// Execute a function _when_ we are connected.
|
||||
// If not connected, wait until connected
|
||||
@@ -142,25 +138,39 @@ export default function extendConnector (Y/* :any */) {
|
||||
this.whenSyncedListeners.push(f)
|
||||
}
|
||||
}
|
||||
_syncWithUser (userid) {
|
||||
if (this.role === 'slave') {
|
||||
findNextSyncTarget () {
|
||||
if (this.currentSyncTarget != null || this.role === 'slave') {
|
||||
return // "The current sync has not finished or this is controlled by a master!"
|
||||
}
|
||||
sendSyncStep1(this, userid)
|
||||
}
|
||||
_fireIsSyncedListeners () {
|
||||
this.y.db.whenTransactionsFinished().then(() => {
|
||||
if (!this.isSynced) {
|
||||
this.isSynced = true
|
||||
// It is safer to remove this!
|
||||
// TODO: remove: yield * this.garbageCollectAfterSync()
|
||||
// call whensynced listeners
|
||||
for (var f of this.whenSyncedListeners) {
|
||||
f()
|
||||
}
|
||||
this.whenSyncedListeners = []
|
||||
|
||||
var syncUser = null
|
||||
for (var [uid, user] of this.connections) {
|
||||
if (!user.isSynced) {
|
||||
syncUser = uid
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
var conn = this
|
||||
if (syncUser != null) {
|
||||
this.currentSyncTarget = syncUser
|
||||
sendSyncStep1(this, syncUser)
|
||||
} else {
|
||||
if (!conn.isSynced) {
|
||||
this.y.db.requestTransaction(function * () {
|
||||
if (!conn.isSynced) {
|
||||
// it is crucial that isSynced is set at the time garbageCollectAfterSync is called
|
||||
conn.isSynced = true
|
||||
// It is safer to remove this!
|
||||
// TODO: remove: yield * this.garbageCollectAfterSync()
|
||||
// call whensynced listeners
|
||||
for (var f of conn.whenSyncedListeners) {
|
||||
f()
|
||||
}
|
||||
conn.whenSyncedListeners = []
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
send (uid, buffer) {
|
||||
if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) {
|
||||
@@ -212,10 +222,10 @@ export default function extendConnector (Y/* :any */) {
|
||||
*/
|
||||
receiveMessage (sender, buffer) {
|
||||
if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) {
|
||||
return Promise.reject(new Error('Expected Message to be an ArrayBuffer or Uint8Array!'))
|
||||
throw new Error('Expected Message to be an ArrayBuffer or Uint8Array!')
|
||||
}
|
||||
if (sender === this.userId) {
|
||||
return Promise.resolve()
|
||||
return
|
||||
}
|
||||
let decoder = new BinaryDecoder(buffer)
|
||||
let encoder = new BinaryEncoder()
|
||||
@@ -234,33 +244,29 @@ export default function extendConnector (Y/* :any */) {
|
||||
if (messageType === 'sync step 1' || messageType === 'sync step 2') {
|
||||
let auth = decoder.readVarUint()
|
||||
if (senderConn.auth == null) {
|
||||
senderConn.processAfterAuth.push([messageType, senderConn, decoder, encoder, sender])
|
||||
senderConn.processAfterAuth.push([sender, buffer])
|
||||
|
||||
// check auth
|
||||
return this.checkAuth(auth, this.y, sender).then(authPermissions => {
|
||||
if (senderConn.auth == null) {
|
||||
senderConn.auth = authPermissions
|
||||
this.y.emit('userAuthenticated', {
|
||||
user: senderConn.uid,
|
||||
auth: authPermissions
|
||||
})
|
||||
senderConn.auth = authPermissions
|
||||
this.y.emit('userAuthenticated', {
|
||||
user: senderConn.uid,
|
||||
auth: authPermissions
|
||||
})
|
||||
return senderConn.syncStep2.promise
|
||||
}).then(() => {
|
||||
if (senderConn.processAfterAuth == null) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
let messages = senderConn.processAfterAuth
|
||||
senderConn.processAfterAuth = []
|
||||
|
||||
return messages.reduce((p, m) =>
|
||||
p.then(() => this.computeMessage(m[0], m[1], m[2], m[3], m[4]))
|
||||
, Promise.resolve())
|
||||
senderConn.processAfterAuth = null
|
||||
return Promise.all(messages.map(m =>
|
||||
this.receiveMessage(m[0], m[1])
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
if (senderConn.auth != null) {
|
||||
return this.computeMessage(messageType, senderConn, decoder, encoder, sender)
|
||||
} else {
|
||||
senderConn.processAfterAuth.push([messageType, senderConn, decoder, encoder, sender])
|
||||
}
|
||||
}
|
||||
|
||||
computeMessage (messageType, senderConn, decoder, encoder, sender) {
|
||||
if (messageType === 'sync step 1' && (senderConn.auth === 'write' || senderConn.auth === 'read')) {
|
||||
// cannot wait for sync step 1 to finish, because we may wait for sync step 2 in sync step 1 (->lock)
|
||||
computeMessageSyncStep1(decoder, encoder, this, senderConn, sender)
|
||||
@@ -270,20 +276,19 @@ export default function extendConnector (Y/* :any */) {
|
||||
} else if (messageType === 'update' && senderConn.auth === 'write') {
|
||||
return computeMessageUpdate(decoder, encoder, this, senderConn, sender)
|
||||
} else {
|
||||
return Promise.reject(new Error('Unable to receive message'))
|
||||
Promise.reject(new Error('Unable to receive message'))
|
||||
}
|
||||
}
|
||||
|
||||
_setSyncedWith (user) {
|
||||
if (user != null) {
|
||||
this.connections.get(user).isSynced = true
|
||||
var conn = this.connections.get(user)
|
||||
if (conn != null) {
|
||||
conn.isSynced = true
|
||||
}
|
||||
let conns = Array.from(this.connections.values())
|
||||
if (conns.length > 0 && conns.every(u => u.isSynced)) {
|
||||
this._fireIsSyncedListeners()
|
||||
if (user === this.currentSyncTarget) {
|
||||
this.currentSyncTarget = null
|
||||
this.findNextSyncTarget()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Currently, the HB encodes operations as JSON. For the moment I want to keep it
|
||||
that way. Maybe we support encoding in the HB as XML in the future, but for now I don't want
|
||||
|
||||
@@ -141,7 +141,7 @@ export async function initArrays (t, opts) {
|
||||
let connOpts
|
||||
if (i === 0) {
|
||||
// Only one instance can gc!
|
||||
dbOpts = Object.assign({ gc: false }, opts.db)
|
||||
dbOpts = Object.assign({ gc: true }, opts.db)
|
||||
connOpts = Object.assign({ role: 'master' }, connector)
|
||||
} else {
|
||||
dbOpts = Object.assign({ gc: false }, opts.db)
|
||||
|
||||
129
y.node.js
129
y.node.js
@@ -1,7 +1,7 @@
|
||||
|
||||
/**
|
||||
* yjs - A framework for real-time p2p shared editing on any data
|
||||
* @version v13.0.0-11
|
||||
* @version v13.0.0-9
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
@@ -640,12 +640,10 @@ function extendConnector (Y/* :any */) {
|
||||
this.setUserId(Y.utils.generateUserId());
|
||||
}
|
||||
}
|
||||
|
||||
reconnect () {
|
||||
this.log('reconnecting..');
|
||||
return this.y.db.startGarbageCollector()
|
||||
}
|
||||
|
||||
disconnect () {
|
||||
this.log('discronnecting..');
|
||||
this.connections = new Map();
|
||||
@@ -655,16 +653,13 @@ function extendConnector (Y/* :any */) {
|
||||
this.y.db.stopGarbageCollector();
|
||||
return this.y.db.whenTransactionsFinished()
|
||||
}
|
||||
|
||||
repair () {
|
||||
this.log('Repairing the state of Yjs. This can happen if messages get lost, and Yjs detects that something is wrong. If this happens often, please report an issue here: https://github.com/y-js/yjs/issues');
|
||||
this.connections.forEach(user => { user.isSynced = false; });
|
||||
this.isSynced = false;
|
||||
this.connections.forEach((user, userId) => {
|
||||
user.isSynced = false;
|
||||
this._syncWithUser(userId);
|
||||
});
|
||||
this.currentSyncTarget = null;
|
||||
this.findNextSyncTarget();
|
||||
}
|
||||
|
||||
setUserId (userId) {
|
||||
if (this.userId == null) {
|
||||
if (!Number.isInteger(userId)) {
|
||||
@@ -679,21 +674,20 @@ function extendConnector (Y/* :any */) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
onUserEvent (f) {
|
||||
this.userEventListeners.push(f);
|
||||
}
|
||||
|
||||
removeUserEventListener (f) {
|
||||
this.userEventListeners = this.userEventListeners.filter(g => f !== g);
|
||||
}
|
||||
|
||||
userLeft (user) {
|
||||
if (this.connections.has(user)) {
|
||||
this.log('%s: User left %s', this.userId, user);
|
||||
this.connections.delete(user);
|
||||
// check if isSynced event can be sent now
|
||||
this._setSyncedWith(null);
|
||||
if (user === this.currentSyncTarget) {
|
||||
this.currentSyncTarget = null;
|
||||
this.findNextSyncTarget();
|
||||
}
|
||||
for (var f of this.userEventListeners) {
|
||||
f({
|
||||
action: 'userLeft',
|
||||
@@ -702,7 +696,7 @@ function extendConnector (Y/* :any */) {
|
||||
}
|
||||
}
|
||||
}
|
||||
userJoined (user, role, auth) {
|
||||
userJoined (user, role) {
|
||||
if (role == null) {
|
||||
throw new Error('You must specify the role of the joined user!')
|
||||
}
|
||||
@@ -715,7 +709,7 @@ function extendConnector (Y/* :any */) {
|
||||
isSynced: false,
|
||||
role: role,
|
||||
processAfterAuth: [],
|
||||
auth: auth || null,
|
||||
auth: null,
|
||||
receivedSyncStep2: false
|
||||
});
|
||||
let defer = {};
|
||||
@@ -728,7 +722,9 @@ function extendConnector (Y/* :any */) {
|
||||
role: role
|
||||
});
|
||||
}
|
||||
this._syncWithUser(user);
|
||||
if (this.currentSyncTarget == null) {
|
||||
this.findNextSyncTarget();
|
||||
}
|
||||
}
|
||||
// Execute a function _when_ we are connected.
|
||||
// If not connected, wait until connected
|
||||
@@ -739,25 +735,39 @@ function extendConnector (Y/* :any */) {
|
||||
this.whenSyncedListeners.push(f);
|
||||
}
|
||||
}
|
||||
_syncWithUser (userid) {
|
||||
if (this.role === 'slave') {
|
||||
findNextSyncTarget () {
|
||||
if (this.currentSyncTarget != null || this.role === 'slave') {
|
||||
return // "The current sync has not finished or this is controlled by a master!"
|
||||
}
|
||||
sendSyncStep1(this, userid);
|
||||
}
|
||||
_fireIsSyncedListeners () {
|
||||
this.y.db.whenTransactionsFinished().then(() => {
|
||||
if (!this.isSynced) {
|
||||
this.isSynced = true;
|
||||
// It is safer to remove this!
|
||||
// TODO: remove: yield * this.garbageCollectAfterSync()
|
||||
// call whensynced listeners
|
||||
for (var f of this.whenSyncedListeners) {
|
||||
f();
|
||||
}
|
||||
this.whenSyncedListeners = [];
|
||||
|
||||
var syncUser = null;
|
||||
for (var [uid, user] of this.connections) {
|
||||
if (!user.isSynced) {
|
||||
syncUser = uid;
|
||||
break
|
||||
}
|
||||
});
|
||||
}
|
||||
var conn = this;
|
||||
if (syncUser != null) {
|
||||
this.currentSyncTarget = syncUser;
|
||||
sendSyncStep1(this, syncUser);
|
||||
} else {
|
||||
if (!conn.isSynced) {
|
||||
this.y.db.requestTransaction(function * () {
|
||||
if (!conn.isSynced) {
|
||||
// it is crucial that isSynced is set at the time garbageCollectAfterSync is called
|
||||
conn.isSynced = true;
|
||||
// It is safer to remove this!
|
||||
// TODO: remove: yield * this.garbageCollectAfterSync()
|
||||
// call whensynced listeners
|
||||
for (var f of conn.whenSyncedListeners) {
|
||||
f();
|
||||
}
|
||||
conn.whenSyncedListeners = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
send (uid, buffer) {
|
||||
if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) {
|
||||
@@ -809,10 +819,10 @@ function extendConnector (Y/* :any */) {
|
||||
*/
|
||||
receiveMessage (sender, buffer) {
|
||||
if (!(buffer instanceof ArrayBuffer || buffer instanceof Uint8Array)) {
|
||||
return Promise.reject(new Error('Expected Message to be an ArrayBuffer or Uint8Array!'))
|
||||
throw new Error('Expected Message to be an ArrayBuffer or Uint8Array!')
|
||||
}
|
||||
if (sender === this.userId) {
|
||||
return Promise.resolve()
|
||||
return
|
||||
}
|
||||
let decoder = new BinaryDecoder(buffer);
|
||||
let encoder = new BinaryEncoder();
|
||||
@@ -831,33 +841,29 @@ function extendConnector (Y/* :any */) {
|
||||
if (messageType === 'sync step 1' || messageType === 'sync step 2') {
|
||||
let auth = decoder.readVarUint();
|
||||
if (senderConn.auth == null) {
|
||||
senderConn.processAfterAuth.push([messageType, senderConn, decoder, encoder, sender]);
|
||||
senderConn.processAfterAuth.push([sender, buffer]);
|
||||
|
||||
// check auth
|
||||
return this.checkAuth(auth, this.y, sender).then(authPermissions => {
|
||||
if (senderConn.auth == null) {
|
||||
senderConn.auth = authPermissions;
|
||||
this.y.emit('userAuthenticated', {
|
||||
user: senderConn.uid,
|
||||
auth: authPermissions
|
||||
});
|
||||
senderConn.auth = authPermissions;
|
||||
this.y.emit('userAuthenticated', {
|
||||
user: senderConn.uid,
|
||||
auth: authPermissions
|
||||
});
|
||||
return senderConn.syncStep2.promise
|
||||
}).then(() => {
|
||||
if (senderConn.processAfterAuth == null) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
let messages = senderConn.processAfterAuth;
|
||||
senderConn.processAfterAuth = [];
|
||||
|
||||
return messages.reduce((p, m) =>
|
||||
p.then(() => this.computeMessage(m[0], m[1], m[2], m[3], m[4]))
|
||||
, Promise.resolve())
|
||||
senderConn.processAfterAuth = null;
|
||||
return Promise.all(messages.map(m =>
|
||||
this.receiveMessage(m[0], m[1])
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
if (senderConn.auth != null) {
|
||||
return this.computeMessage(messageType, senderConn, decoder, encoder, sender)
|
||||
} else {
|
||||
senderConn.processAfterAuth.push([messageType, senderConn, decoder, encoder, sender]);
|
||||
}
|
||||
}
|
||||
|
||||
computeMessage (messageType, senderConn, decoder, encoder, sender) {
|
||||
if (messageType === 'sync step 1' && (senderConn.auth === 'write' || senderConn.auth === 'read')) {
|
||||
// cannot wait for sync step 1 to finish, because we may wait for sync step 2 in sync step 1 (->lock)
|
||||
computeMessageSyncStep1(decoder, encoder, this, senderConn, sender);
|
||||
@@ -867,20 +873,19 @@ function extendConnector (Y/* :any */) {
|
||||
} else if (messageType === 'update' && senderConn.auth === 'write') {
|
||||
return computeMessageUpdate(decoder, encoder, this, senderConn, sender)
|
||||
} else {
|
||||
return Promise.reject(new Error('Unable to receive message'))
|
||||
Promise.reject(new Error('Unable to receive message'));
|
||||
}
|
||||
}
|
||||
|
||||
_setSyncedWith (user) {
|
||||
if (user != null) {
|
||||
this.connections.get(user).isSynced = true;
|
||||
var conn = this.connections.get(user);
|
||||
if (conn != null) {
|
||||
conn.isSynced = true;
|
||||
}
|
||||
let conns = Array.from(this.connections.values());
|
||||
if (conns.length > 0 && conns.every(u => u.isSynced)) {
|
||||
this._fireIsSyncedListeners();
|
||||
if (user === this.currentSyncTarget) {
|
||||
this.currentSyncTarget = null;
|
||||
this.findNextSyncTarget();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Currently, the HB encodes operations as JSON. For the moment I want to keep it
|
||||
that way. Maybe we support encoding in the HB as XML in the future, but for now I don't want
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user