working on better abstractions. still held together with duct tape

This commit is contained in:
ansuz
2016-05-26 11:55:33 +02:00
parent c1bca09cce
commit 014dce272b
3 changed files with 176 additions and 148 deletions

View File

@@ -13,170 +13,92 @@ define([
api.ListMap = ListMap;
var key;
var channel = '';
var hash = false;
if (!/#/.test(window.location.href)) {
key = Crypto.genKey();
} else {
hash = window.location.hash.slice(1);
channel = hash.slice(0,32);
key = hash.slice(32);
}
var module = window.APP = {
TextPatcher: TextPatcher,
Sortify: Sortify,
};
var $repl = $('[name="repl"]');
var Map = module.Map = {};
var initializing = true;
var config = module.config = {
initialState: Sortify(Map) || '{}',
websocketURL: Config.websocketURL,
userName: Crypto.rand64(8),
channel: channel,
cryptKey: key,
crypto: Crypto,
transformFunction: JsonOT.validate
};
var setEditable = module.setEditable = function (bool) {
/* (dis)allow editing */
[$repl].forEach(function ($el) {
$el.attr('disabled', !bool);
});
};
setEditable(false);
var onInit = config.onInit = function (info) {
var realtime = module.realtime = info.realtime;
window.location.hash = info.channel + key;
// create your patcher
module.patchText = TextPatcher.create({
realtime: realtime,
logging: true,
});
};
/* we still need to pass in the function that bumps to ListMap.
this is no good. FIXME */
var onLocal = config.onLocal = ListMap.onLocal = module.bump = function () {
if (initializing) { return; }
var strung = Sortify(Map);
console.log(strung);
/* serialize local changes */
module.patchText(strung);
if (module.realtime.getUserDoc !== strung) {
module.patchText(strung);
}
};
var onRemote = config.onRemote = function (info) {
if (initializing) { return; }
/* integrate remote changes */
var proxy = module.proxy;
var userDoc = module.realtime.getUserDoc();
var parsed = JSON.parse(userDoc);
ListMap.update(proxy, parsed);
};
var onReady = config.onReady = function (info) {
console.log("READY");
var userDoc = module.realtime.getUserDoc();
var parsed = JSON.parse(userDoc);
Object.keys(parsed).forEach(function (key) {
module.proxy[key] = ListMap.recursiveProxies(parsed[key]);
});
setEditable(true);
initializing = false;
};
var onAbort = config.onAbort = function (info) {
window.alert("Network Connection Lost");
};
var rt = Realtime.start(config);
var proxy = module.proxy = ListMap.makeProxy(Map);
$repl.on('keyup', function (e) {
if (e.which === 13) {
var value = $repl.val();
if (!value.trim()) { return; }
console.log("evaluating `%s`", value);
var x = proxy;
console.log('> ', eval(value)); // jshint ignore:line
console.log();
$repl.val('');
}
});
var create = api.create = function (config) {
var create = api.create = function (cfg) {
/* validate your inputs before proceeding */
if (['object', 'array'].indexOf(ListMap.type(config.data))) {
if (['object', 'array'].indexOf(ListMap.type(cfg.data))) {
throw new Error('unsupported datatype');
}
var Config = {
initialState: Sortify(config.data),
var config = {
initialState: Sortify(cfg.data),
transformFunction: JsonOT.validate,
userName: userName,
channel: channel,
cryptKey: cryptKey,
crypto: crypto,
userName: Crypto.rand64(8),
channel: cfg.channel,
cryptKey: cfg.cryptKey,
crypto: Crypto,
websocketURL: Config.websocketURL,
};
var rt;
var proxy = ListMap.makeProxy(cfg.data);
var realtime;
var onInit = config.onInit = function (info) {
realtime = info.realtime;
// create your patcher
realtime.patchText = TextPatcher.create({
realtime: realtime,
logging: config.logging || false,
});
var onInit = Config.onInit = function (info) {
// onInit
config.onInit(info);
cfg.onInit(info);
};
var onReady = Config.onReady = function (info) {
var onReady = config.onReady = function (info) {
var userDoc = realtime.getUserDoc();
var parsed = JSON.parse(userDoc);
// update your proxy to the state of the userDoc
Object.keys(parsed).forEach(function (key) {
proxy[key] = ListMap.recursiveProxies(parsed[key]);
});
// onReady
config.onReady(info);
cfg.onReady(info);
};
var onLocal = Config.onLocal = function () {
// FIXME
var onLocal = config.onLocal = ListMap.onLocal = function () {
var strung = Sortify(proxy);
realtime.patchText(strung);
// try harder
if (realtime.getUserDoc() !== strung) {
realtime.patchText(strung);
}
// onLocal
config.onLocal();
if (cfg.onLocal) {
cfg.onLocal();
}
// TODO actually emit 'change' events, or something like them
};
var onRemote = Config.onRemote = function (info) {
var onRemote = config.onRemote = function (info) {
var userDoc = realtime.getUserDoc();
var parsed = JSON.parse(userDoc);
ListMap.update(proxy, parsed);
// onRemote
config.onRemote(info);
if (cfg.onRemote) {
cfg.onRemote(info);
}
};
var onAbort = Config.onAbort = function (info) {
var onAbort = config.onAbort = function (info) {
// onAbort
config.onAbort(info);
cfg.onAbort(info);
};
rt =Realtime.start(Config);
var proxy = rt.proxy = ListMap.makeProxy(data);
rt = Realtime.start(config);
rt.proxy = proxy;
rt.realtime = realtime;
return rt;
};