Comparing version 1.1.1 to 1.1.2
39
index.js
@@ -6,9 +6,10 @@ "use strict" | ||
* | ||
* @param {Number} timeout 默认超时 | ||
* @param {Number} delay 默认超时 | ||
*/ | ||
constructor(options = {}) { | ||
let { timeout = 0 } = options | ||
let { delay = 0 } = options | ||
this.timeout = timeout | ||
this.delay = delay | ||
this.timeout = { _called: true } | ||
this.tasks = new Map() | ||
@@ -24,11 +25,14 @@ | ||
// 仅在定时队列为空时允许启动 | ||
if (this.tasks.size === 0) { | ||
// 仅在定时器called后激活 | ||
if (this.timeout._called === true) { | ||
this.run(this.delay) | ||
} | ||
this.run(this.timeout) | ||
// 仅在定时队列为空时激活 | ||
// if (this.tasks.size === 0) { | ||
// this.run(this.delay) | ||
// } | ||
} | ||
if (!timestamp) { | ||
timestamp = Date.now() + this.timeout | ||
timestamp = Date.now() + this.delay | ||
} | ||
@@ -63,9 +67,10 @@ | ||
/** | ||
* 递归循环触发时间队列,,直至队列为空 | ||
* @param {Number} timeout 等待时间,单位ms | ||
* 递归循环触发时间队列,直至队列为空 | ||
* @param {Number} delay 等待时间,单位ms | ||
*/ | ||
run(timeout) { | ||
run(delay) { | ||
setTimeout(() => { | ||
this.timeout = setTimeout(() => { | ||
let now = Date.now() | ||
let keys = this.tasks.keys() | ||
@@ -75,5 +80,5 @@ | ||
let { value, timestamp } = this.tasks.get(key) | ||
let timeout = timestamp - Date.now() | ||
if (timeout > 0) { | ||
this.run(timeout) | ||
let delay = timestamp - now | ||
if (delay > 0) { | ||
this.run(delay) | ||
return | ||
@@ -88,3 +93,3 @@ } else { | ||
}, timeout); | ||
}, delay); | ||
@@ -91,0 +96,0 @@ } |
{ | ||
"name": "timechain", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "时间队列链式触发器", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jtf -w" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} | ||
"license": "ISC", | ||
"devDependencies": { | ||
"jtf": "^1.1.3", | ||
"zpromise": "^1.2.0" | ||
}, | ||
"dependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/xiangle/timechain.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/xiangle/timechain/issues" | ||
}, | ||
"homepage": "https://github.com/xiangle/timechain#readme" | ||
} |
@@ -42,5 +42,5 @@ 基于链式事件触发策略,使用单个setTimeout处理过期时间队列 | ||
timechain目前采用的是定时策略。计划同时支持多种过期策略,根据不同的数据类型采用最优策略组合。 | ||
每种过期策略都有其优势和劣势,timechain目前采用的是定时删除策略。计划同时支持多种过期策略,针对不同的数据类型选用最佳的过期策略。 | ||
对于函数类型,依然只能使用定时策略,非函数类型则使用惰性策略 + 定期策略(借鉴于Redis)。 | ||
对于函数类型,只能使用定时策略,非函数类型则使用惰性策略 + 定期策略(借鉴于Redis)。 | ||
@@ -50,5 +50,10 @@ | ||
在设置变量时激活定时器,仅在队列为空时触发,避免创建多个setTimeout。 | ||
为每个资源创建定时器,在设置变量时激活定时器,仅在队列为空时触发,避免创建多个setTimeout。 | ||
定时删除可以通过一些优化策略来控制定时器的使用频率,如基于链式策略使同一个时间点只存在一个定时器,或基于公差进一步减少对定时器的使用。 | ||
* 优点:高时效,主动触发,可及时释放超时的资源,减少内存占用,支持超时回调函数。 | ||
* 缺点:会创建多个定时器,但通过链式策略,可以减少多个定时器同时存在的数量。 | ||
#### 惰性删除 | ||
@@ -60,8 +65,10 @@ | ||
* 缺点:如果key没有被获取则会产生积压,资源始终得不到释放,导致内存泄漏或溢出的悲剧 | ||
* 缺点:被动执行,如果key没有被获取则会产生积压,资源始终得不到释放,导致内存泄漏或溢出的悲剧 | ||
* 应用场景:适用于key/value的存取,必须搭配定期删除,否则产生现内存泄漏。 | ||
#### 定期删除 | ||
每隔一段时间批量删除已过期的key | ||
使用固定时间周期,每隔一段时间批量删除已过期的key | ||
@@ -68,0 +75,0 @@ * 优点:简单,效率折中,可以灵活控制删除周期。 |
@@ -0,21 +1,34 @@ | ||
let test = require('jtf') | ||
let timeChain = require('..') | ||
let timechain = new timeChain({ timeout: 3000 }) | ||
test('回调函数', async t => { | ||
timechain.set(function (value) { | ||
let { zPromise } = require('./helpers/') | ||
console.log(value) | ||
}, 111) | ||
let timechain = new timeChain({ delay: 3000 }) | ||
timechain.set(function (value) { | ||
let p1 = new zPromise() | ||
let p2 = new zPromise() | ||
let p3 = new zPromise() | ||
console.log(value) | ||
}, 222) | ||
timechain.set(function (value) { | ||
p1.resolve(value) | ||
}, 1) | ||
timechain.set(function (value) { | ||
t.deepEqual(1, await p1); | ||
console.log(value) | ||
}, 333) | ||
timechain.set(function (value) { | ||
p2.resolve(value) | ||
}, 2) | ||
t.deepEqual(2, await p2); | ||
timechain.set(function (value) { | ||
p3.resolve(value) | ||
}, { a: 1 }) | ||
t.deepEqual({ a: 1 }, await p3); | ||
}) |
@@ -0,7 +1,17 @@ | ||
let test = require('jtf') | ||
let timeChain = require('..') | ||
let { zPromise } = require('./helpers/') | ||
let timechain = new timeChain({ timeout: 1000 }) | ||
test('单项时间设定', async t => { | ||
timechain.set(function(value){ | ||
console.log(value) | ||
}, 999, Date.now() + 5000) | ||
let timechain = new timeChain({ delay: 1000 }) | ||
let p1 = new zPromise() | ||
timechain.set(function(value){ | ||
p1.resolve(value) | ||
}, 999, Date.now() + 5000) | ||
t.deepEqual(999, await p1); | ||
}) |
@@ -0,21 +1,22 @@ | ||
let test = require('jtf') | ||
let timeChain = require('..') | ||
let { zPromise } = require('./helpers/') | ||
let timechain = new timeChain({ timeout: 3000 }) | ||
test('Promise函数', async t => { | ||
let reject1 | ||
new Promise(function(resolve, reject) { | ||
reject1 = reject | ||
}).catch(function(error){ | ||
console.error(error) | ||
}) | ||
let timechain = new timeChain({ delay: 3000 }) | ||
let p1 = new zPromise() | ||
let reject2 | ||
new Promise(function(resolve, reject) { | ||
reject2 = reject | ||
}).catch(function(error){ | ||
console.error(error) | ||
}) | ||
timechain.set(p1.resolve, 111) | ||
timechain.set(reject1, 111) | ||
t.deepEqual(111, await p1); | ||
timechain.set(reject2, 222) | ||
let p2 = new zPromise() | ||
timechain.set(p2.resolve, 222) | ||
t.deepEqual(222, await p2); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
7402
9
138
2
0
111
0
2