create a helper for cancellable Gun.chain.on callbacks

This commit is contained in:
Jesse D. McDonald 2020-03-29 00:29:47 -05:00
parent c7c1905f9d
commit b5c830ab68
1 changed files with 65 additions and 56 deletions

View File

@ -44,6 +44,31 @@ $(function (){
let cancelGameCallback = function() {};
let cancelMetaCallback = function() {};
Gun.chain.onWithCancel = (function() {
function cancelCallback(data,key,msg,ev) {
if (ev && typeof ev.off === 'function') {
ev.off();
}
}
return function(tag, arg, eas, as) {
if (typeof tag === 'function') {
let callback = tag;
const cancelEv = function() {
callback = cancelCallback;
};
const wrapper = function() {
return callback.apply(this, arguments);
};
this.on(wrapper, arg, eas, as);
return cancelEv;
} else {
this.on(tag, arg, eas, as);
return null;
}
};
})();
function pieceTypeCode(type) {
if (type === PS.KING) {
return 'k';
@ -451,66 +476,50 @@ $(function (){
$('#cb_light_name').val('');
$('#cb_dark_name').val('');
(function(){
let callback = function(d) {
if (d && d.board) {
try {
const moves = JSON.parse(d.board);
const oldState = { past: currentGame.moves, future: currentGame.redoMoves };
cancelGameCallback = gun.get(PacoSakoUUID).get('games').get(newId).onWithCancel(function(d) {
if (d && d.board) {
try {
const moves = JSON.parse(d.board);
const oldState = { past: currentGame.moves, future: currentGame.redoMoves };
if (deepEqual(moves, oldState)) {
/* we already have this */
return;
}
debug('got board', moves);
const newGame = new PS.Game();
for (const move of moves.past) {
newGame.replayMove(move);
}
let n = 0;
for (const move of moves.future.slice().reverse()) {
newGame.replayMove(move);
n += 1;
}
for (let i = 0; i < n; ++i) {
newGame.undo();
}
setCurrentGame(newGame, moves.past.length > currentGame.moves.length);
} catch (err) {
debug('Error replaying board state', err);
if (deepEqual(moves, oldState)) {
/* we already have this */
return;
}
debug('got board', moves);
const newGame = new PS.Game();
for (const move of moves.past) {
newGame.replayMove(move);
}
let n = 0;
for (const move of moves.future.slice().reverse()) {
newGame.replayMove(move);
n += 1;
}
for (let i = 0; i < n; ++i) {
newGame.undo();
}
setCurrentGame(newGame, moves.past.length > currentGame.moves.length);
} catch (err) {
debug('Error replaying board state', err);
}
};
}
});
cancelGameCallback = function() { callback = function() {}; };
gun.get(PacoSakoUUID).get('games').get(newId).on(function(d){
callback(d);
});
})();
(function(){
let callback = function(d) {
d = d || {};
debug('got meta', d);
$('#cb_board').data('lightName', shortenName(String(d.lightName || 'Light')));
$('#cb_board').data('darkName', shortenName(String(d.darkName || 'Dark')));
$('#cb_light_name').val(String(d.lightName || ''));
$('#cb_dark_name').val(String(d.darkName || ''));
};
cancelMetaCallback = function() { callback = function() {}; };
gun.get(PacoSakoUUID).get('meta').get(newId).on(function(d){
callback(d);
});
})();
cancelMetaCallback = gun.get(PacoSakoUUID).get('meta').get(newId).onWithCancel(function(d) {
d = d || {};
debug('got meta', d);
$('#cb_board').data('lightName', shortenName(String(d.lightName || 'Light')));
$('#cb_board').data('darkName', shortenName(String(d.darkName || 'Dark')));
$('#cb_light_name').val(String(d.lightName || ''));
$('#cb_dark_name').val(String(d.darkName || ''));
});
let selOpt = $('#cb_game_' + newId);
if (selOpt.length === 1) {