You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/common/sframe-chainpad-netflux-inn...

101 lines
3.8 KiB
JavaScript

7 years ago
/*
* Copyright 2014 XWiki SAS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define([
7 years ago
'/bower_components/chainpad/chainpad.dist.js'
], function () {
7 years ago
var ChainPad = window.ChainPad;
var module = { exports: {} };
var verbose = function (x) { console.log(x); };
verbose = function () {}; // comment out to enable verbose logging
7 years ago
module.exports.start = function (config) {
var onConnectionChange = config.onConnectionChange || function () { };
var onRemote = config.onRemote || function () { };
var onInit = config.onInit || function () { };
var onLocal = config.onLocal || function () { };
var setMyID = config.setMyID || function () { };
var onReady = config.onReady || function () { };
var userName = config.userName;
var initialState = config.initialState;
var transformFunction = config.transformFunction;
var validateContent = config.validateContent;
var avgSyncMilliseconds = config.avgSyncMilliseconds;
var logLevel = typeof(config.logLevel) !== 'undefined'? config.logLevel : 1;
var readOnly = config.readOnly || false;
7 years ago
var sframeChan = config.sframeChan;
var metadataMgr = config.metadataMgr;
7 years ago
config = undefined;
var chainpad;
var myID;
var isReady = false;
7 years ago
sframeChan.on('EV_RT_DISCONNECT', function () {
7 years ago
isReady = false;
onConnectionChange({ state: false });
});
7 years ago
sframeChan.on('EV_RT_CONNECT', function (content) {
//content.members.forEach(userList.onJoin);
7 years ago
myID = content.myID;
isReady = false;
if (chainpad) {
// it's a reconnect
onConnectionChange({ state: true, myId: myID });
return;
}
chainpad = ChainPad.create({
7 years ago
userName: userName,
7 years ago
initialState: initialState,
transformFunction: transformFunction,
validateContent: validateContent,
avgSyncMilliseconds: avgSyncMilliseconds,
logLevel: logLevel
7 years ago
});
7 years ago
chainpad.onMessage(function(message, cb) {
sframeChan.query('Q_RT_MESSAGE', message, cb);
7 years ago
});
7 years ago
chainpad.onPatch(function () {
onRemote({ realtime: chainpad });
7 years ago
});
7 years ago
onInit({
7 years ago
myID: myID,
7 years ago
realtime: chainpad,
readOnly: readOnly
7 years ago
});
7 years ago
});
7 years ago
sframeChan.on('Q_RT_MESSAGE', function (content, cb) {
7 years ago
if (isReady) {
onLocal(); // should be onBeforeMessage
7 years ago
}
7 years ago
chainpad.message(content);
cb('OK');
});
7 years ago
sframeChan.on('EV_RT_READY', function () {
7 years ago
if (isReady) { return; }
isReady = true;
chainpad.start();
setMyID({ myID: myID });
onReady({ realtime: chainpad });
});
7 years ago
return Object.freeze({
getMyID: function () { return myID; },
metadataMgr: metadataMgr
});
7 years ago
};
7 years ago
return Object.freeze(module.exports);
7 years ago
});