create a helper for cancellable Gun.chain.on callbacks
This commit is contained in:
parent
c7c1905f9d
commit
b5c830ab68
|
|
@ -44,6 +44,31 @@ $(function (){
|
||||||
let cancelGameCallback = function() {};
|
let cancelGameCallback = function() {};
|
||||||
let cancelMetaCallback = 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) {
|
function pieceTypeCode(type) {
|
||||||
if (type === PS.KING) {
|
if (type === PS.KING) {
|
||||||
return 'k';
|
return 'k';
|
||||||
|
|
@ -451,66 +476,50 @@ $(function (){
|
||||||
$('#cb_light_name').val('');
|
$('#cb_light_name').val('');
|
||||||
$('#cb_dark_name').val('');
|
$('#cb_dark_name').val('');
|
||||||
|
|
||||||
(function(){
|
cancelGameCallback = gun.get(PacoSakoUUID).get('games').get(newId).onWithCancel(function(d) {
|
||||||
let callback = function(d) {
|
if (d && d.board) {
|
||||||
if (d && d.board) {
|
try {
|
||||||
try {
|
const moves = JSON.parse(d.board);
|
||||||
const moves = JSON.parse(d.board);
|
const oldState = { past: currentGame.moves, future: currentGame.redoMoves };
|
||||||
const oldState = { past: currentGame.moves, future: currentGame.redoMoves };
|
|
||||||
|
|
||||||
if (deepEqual(moves, oldState)) {
|
if (deepEqual(moves, oldState)) {
|
||||||
/* we already have this */
|
/* we already have this */
|
||||||
return;
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {}; };
|
cancelMetaCallback = gun.get(PacoSakoUUID).get('meta').get(newId).onWithCancel(function(d) {
|
||||||
|
d = d || {};
|
||||||
gun.get(PacoSakoUUID).get('games').get(newId).on(function(d){
|
debug('got meta', d);
|
||||||
callback(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 || ''));
|
||||||
(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);
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
let selOpt = $('#cb_game_' + newId);
|
let selOpt = $('#cb_game_' + newId);
|
||||||
if (selOpt.length === 1) {
|
if (selOpt.length === 1) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue