All files / main registry.js

97.96% Statements 48/49
91.67% Branches 11/12
100% Functions 7/7
97.96% Lines 48/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 502x 2x 2x 2x 2x 2x 2x 2x 11x 11x 11x 11x 11x 17x 17x 17x   17x 17x 17x 11x 5x 5x 3x 11x 2x 2x 2x 1x 1x 1x 11x 7x 7x 7x 11x 4x 4x 4x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x  
function createRegistry(boardP){
    let mementos = [];
    let index = 0;
    let board = boardP;
 
    function register() {
        for(let i = 0; i < index; i++){
            mementos.shift();
        }
        index = 0;
        mementos.splice(0, 0, board.createMemento());
    }
 
    function undo() {
        if(!isUndoable())
            return;
        index++;
        board.setMemento(mementos[index]);
    }
 
    function redo() {
        if(!isRedoable())
            return;
        index--;
        board.setMemento(mementos[index]);
    }
 
    function isUndoable() {
        return index < (mementos.length - 1);
    }
 
    function isRedoable() {
        return index > 0;
    }
 
    register();
 
    return {
        register,
        undo,
        redo,
        isUndoable,
        isRedoable,
    }
}
 
export {
    createRegistry
}