squirrel

小松鼠——适用于 JavaScript 平台的数据上报工具,可指定多种上报策略
使用场景
安装
npm i squirrel-report -S
使用
import { Squirrel, DataLevel, SwallowStrategyMode } from "squirrel-report";
const reporter = new Squirrel({
adapter: (list) => {
},
strategy: {
mode: SwallowStrategyMode.intervalCount,
value: 10,
},
});
reporter.stuff("data1...");
reporter.stuff("data2...", DataLevel.high);
.
.
.
reporter.stuff("data10...");
reporter.swallow();
上报策略
enum SwallowStrategyMode {
delayTime = "delayTime",
intervalTime = "intervalTime",
intervalCount = "intervalCount",
event = "event",
eventCount = "eventCount",
}
在使用构造函数new Squirrel(options)或者squirrel.setOptions(options)设置配置项时,可在options中传递strategy选项,用于指定上报策略;
strategy的值可以是单纯的对象,如:
{ mode: SwallowStrategyMode.intervalCount, value: 10}
也可以在strategy对象上按照优先级设置上报策略,如:
{
[DataLevel.high]: {
mode: SwallowStrategyMode.intervalCount,
value: 10
}
}
API
class Squirrel
-
constructor(options:SquirrelOptions)
- 构造函数,可传入
options,使用this.setOptions进行配置项解析
-
setOptions(options:SquirrelOptions)
-
stuff(data:any, level:DataLevel)
-
swallow(data?:any, level?:DataLevel)
- 强制执行上报操作,如果不传递任何参数或参数皆为空,则上报所有优先级的数据,如果传递的数据为空,但是存在优先级,则会上报该优先级下的所有数据
-
trigger(eventName)
- 触发事件,Squirrel 类本身会触发如下事件:
stuff: 插入数据时触发
swallow: 上报数据时触发
setOptions: 设置配置项时触发
storageError: 在配置项中指定了setStorage,并且在执行数据持久化调用setStorage后失败时触发
-
destory()
- 销毁实例,如果上报策略中指定了与实际相关的策略时,请务必执行此函数以销毁定时器
interface SquirrelOptions
{
adapter: Function;
strategy: SwallowStrategy | LevelSwallowStrategy;
failAction: FailAction | FailActionCustom;
getStorage?: Function;
setStorage?: Function;
}