Created
April 17, 2013 15:20
-
-
Save stefano-bortolotti/5405181 to your computer and use it in GitHub Desktop.
JS - FIFO stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uMemory() { | |
var props = { | |
'blocksLimit' : 5 | |
}; | |
var queue = []; | |
var blocks = {}; | |
this.init = function (options) { | |
props = $.extend(props, options); | |
//console.dir(props); | |
}; | |
this.save = function (page, data) { | |
queue.unshift(page); | |
blocks['p' + page] = data; | |
return checkSizeLimit(page); | |
} | |
this.get = function (page) { | |
var p = blocks['p' + page]; | |
return p ? p : false; | |
} | |
this.status = function() { | |
/*console.log('Mem Queue ', queue); | |
console.log('Mem Blocks ', blocks);*/ | |
} | |
this.clear = function() { | |
queue = []; | |
blocks = {}; | |
} | |
var checkSizeLimit = function (page) { | |
/*console.log('q length: ' + queue.length + ', limit: ' + props.blocksLimit);*/ | |
if ( queue.length > props.blocksLimit ) { | |
var oldest = queue.pop(); | |
blocks['p' + oldest] = null; | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment