Skip to content

Commit

Permalink
Update: setp to onupdate handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Bacra committed Jun 27, 2018
1 parent e081b06 commit 3dc6c49
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"time-stamp": "^2.0.0"
},
"devDependencies": {
"bluebird": "^3.5.1",
"cross-env": "^2.0.0",
"eslint": "^5.0.0",
"eslint-config-brcjs": "^0.1.0",
Expand Down
31 changes: 28 additions & 3 deletions test/test_base.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
var expect = require('expect.js');
var timekey = require('../');
var timestamp = require('time-stamp');
var Promise = require('bluebird');

describe('#base', function()
{
it('#base', function()
{
var format = 'YYYY/MM/DD HH';
var now = new Date;
var key1 = timekey(format);
expect(key1.key()).to.be(timestamp(format, now));
var key = timekey(format);
expect(key.key()).to.be(timestamp(format, now));
now.setHours(now.getHours()+1);
expect(key1.ttl).to.be(+new Date(timestamp(format, now)+':00:00:00'));
expect(key.ttl).to.be(+new Date(timestamp(format, now)+':00:00:00'));
});

it('#step', function()
{
var format = 'YYYY/MM/DD HH';
var now = new Date;
var key = timekey(format, 2);
expect(key.key()).to.be(timestamp(format, now));
now.setHours(now.getHours()+2);
expect(key.ttl).to.be(+new Date(timestamp(format, now)+':00:00:00'));
});

it('#onupdate', function()
{
var format = 'YYYY/MM/DD HH:mm:ss:ms';
var hasUpdate = false;
var key = timekey(format, function(){hasUpdate = true});

return Promise.delay(100)
.then(function()
{
key.key();
expect(hasUpdate).to.be(true);
});
});
});
8 changes: 7 additions & 1 deletion time-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = TimeKey;
* 时间结果缓存
*
* @param {String/Function} format 日期格式化
* @param {Number} step ttl计算时,需要追加的区间
* @param {Number/Function} step ttl计算时,需要追加的区间 / onupdate handler
* @constructor
*/
function TimeKey(format, step)
Expand All @@ -17,6 +17,12 @@ function TimeKey(format, step)
return new TimeKey(format, step);
}

if (typeof step == 'function')
{
this.onupdate = step;
step = null;
}

// 日期格式化
this.format = format;
// 过期后追加的进度
Expand Down

0 comments on commit 3dc6c49

Please sign in to comment.