You can simplely view it as a period publish-subscription system with throttle and debounce support.
Laction js 是一个对分时任务进行节流和防抖控制的一个粗实现控制器。
简单封装了一个使用定时器维护的任务队列,以较好的性能提供给同时大量的任务。
一个可能的用法就是,你可以将一个实例保存到全局,对于分时任务(没有这么高的实时性要求)可以使用bubble
方法来进行全局调配。
## Direction:
[使用消息队列] : register -> addAction -> run
[支持的消息类型] : [String Message] 调用对应消息方法, 本周期执行,执行间隔小于周期时间 [Array Message] String消息和其附带参数构成的参数消息 [Function Message] 防抖消息,本周期不执行,下一周期执行.执行间隔大于周期时间
由周期切入点的不确定为钩子响应消息的invoke
时刻带来不确定性。
让应用程序的体验更加的随机和灵动。
速度几乎接近multi-setInterval/setTimeout, 性能优良。
可添加多种类型钩子和管理,并支持对不同类型的消息进行节流和防抖。
甚至可将节流和防抖同时应用(Precisely says that it is throttle and debounce in a period).
良好的拓展支持,很容易就能集成更多的模块。
option | 描述 | type | default |
---|---|---|---|
period | 基础轨道周期性执行的周期时间间隔 | Number(Int) | 500 |
orbitNumber | 总计线性拓展不同执行周期的轨道数 | Number(Int) | 10 |
fixedPeriod | 决定内部实现使用setTimeout 还是setInterval |
Boolean | true |
debug | 是否开启调试模式. 这将会在控制台看到消息处理的所有过程 | Boolean | false |
根据 options 生成一个Laction
实例。
// Instantiate 实例化
const ins = new Laction({
fixedPeriod: false, // Interval model with less calculations.(potential tricky)
period: 50, // ms
orbitNumber: 5,
debug: true,
});
// Run 启动Laction
ins.run();
如果需要停止Lactin运行
ins.stop();
接下来冒泡的消息将被滞留在内部队列中,你可以再次调用 run 启动队列处理消息.
注册消息钩子以供消息触发。
ins.registerHook({
name: "msgTest",
once: true, // set period throttle
debounce: true, // set period debounce
actions: ()=>{
console.log("Message Actions are Invoked");
},
});
通过发送消息来触发消息钩子。
ins.bubble("msgTest");
// a few millisecond later
// console >>> "Message Actions are Invoked"
根据配置的消息节流和防抖, 如果同时多次发送了消息, 第一个周期只会触发一次(节流),第二个周期会再触发一次(防抖):
ins.bubble("msgTest");
ins.bubble("msgTest");
ins.bubble("msgTest");
ins.bubble("msgTest");
// a few milliseconds later
// console >>> "Message Actions are Invoked"
// another few milliseconds later
// console >>> "Message Actions are Invoked"
// todo