simplify api
* refactor conditional with DeepProxy helpers * don't call 'onRemote' until fully initialized * use disconnect, ready, and create handlers on proxy object
This commit is contained in:
parent
954ee26f4d
commit
4222c0b516
@ -14,8 +14,8 @@ define([
|
|||||||
var create = api.create = function (cfg) {
|
var create = api.create = function (cfg) {
|
||||||
/* validate your inputs before proceeding */
|
/* validate your inputs before proceeding */
|
||||||
|
|
||||||
if (['object', 'array'].indexOf(DeepProxy.type(cfg.data))) {
|
if (!DeepProxy.isProxyable(cfg.data)) {
|
||||||
throw new Error('unsupported datatype');
|
throw new Error('unsupported datatype: '+ DeepProxy.type(cfg.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = {
|
var config = {
|
||||||
@ -48,8 +48,6 @@ define([
|
|||||||
if (cfg.onLocal) {
|
if (cfg.onLocal) {
|
||||||
cfg.onLocal();
|
cfg.onLocal();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO actually emit 'change' events, or something like them
|
|
||||||
};
|
};
|
||||||
|
|
||||||
proxy = DeepProxy.create(cfg.data, onLocal, true);
|
proxy = DeepProxy.create(cfg.data, onLocal, true);
|
||||||
@ -62,35 +60,38 @@ define([
|
|||||||
logging: config.logging || false,
|
logging: config.logging || false,
|
||||||
});
|
});
|
||||||
|
|
||||||
// onInit
|
proxy._events.create.forEach(function (handler) {
|
||||||
cfg.onInit(info);
|
handler.cb(info);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var initializing = true;
|
||||||
|
|
||||||
var onReady = config.onReady = function (info) {
|
var onReady = config.onReady = function (info) {
|
||||||
var userDoc = realtime.getUserDoc();
|
var userDoc = realtime.getUserDoc();
|
||||||
var parsed = JSON.parse(userDoc);
|
var parsed = JSON.parse(userDoc);
|
||||||
|
|
||||||
DeepProxy.update(proxy, parsed);
|
DeepProxy.update(proxy, parsed, onLocal);
|
||||||
|
|
||||||
// onReady
|
proxy._events.ready.forEach(function (handler) {
|
||||||
cfg.onReady(info);
|
handler.cb(info);
|
||||||
|
});
|
||||||
|
|
||||||
|
initializing = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
var onRemote = config.onRemote = function (info) {
|
var onRemote = config.onRemote = function (info) {
|
||||||
|
if (initializing) { return; }
|
||||||
var userDoc = realtime.getUserDoc();
|
var userDoc = realtime.getUserDoc();
|
||||||
var parsed = JSON.parse(userDoc);
|
var parsed = JSON.parse(userDoc);
|
||||||
|
|
||||||
DeepProxy.update(proxy, parsed, onLocal);
|
DeepProxy.update(proxy, parsed, onLocal);
|
||||||
|
|
||||||
// onRemote
|
|
||||||
if (cfg.onRemote) {
|
|
||||||
cfg.onRemote(info);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var onAbort = config.onAbort = function (info) {
|
var onAbort = config.onAbort = function (info) {
|
||||||
// onAbort
|
proxy._events.disconnect.forEach(function (handler) {
|
||||||
cfg.onAbort(info);
|
handler.cb(info);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
rt = Realtime.start(config);
|
rt = Realtime.start(config);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user