gio-miniprogram-sdk 4.4.2 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -11
- package/dist/alipay.js +1 -1
- package/dist/baidu.js +1 -1
- package/dist/bytedance.js +1 -1
- package/dist/jingdong.js +1 -1
- package/dist/kuaishou.js +1 -1
- package/dist/mpx.js +1 -0
- package/dist/plugins/gioABTest.js +1 -1
- package/dist/plugins/gioCompress.js +1 -1
- package/dist/plugins/gioEventAutoTracking.js +1 -1
- package/dist/plugins/gioImpressionTracking.js +1 -1
- package/dist/plugins/gioMultipleInstances.js +1 -1
- package/dist/plugins/gioPerformance.js +1 -1
- package/dist/qq.js +1 -1
- package/dist/quickapp.js +1 -1
- package/dist/taobao.js +1 -1
- package/dist/taro.js +1 -1
- package/dist/uniapp.js +1 -1
- package/dist/wechat.js +1 -1
- package/dist/xiaohongshu.js +1 -1
- package/package.json +13 -2
- package/.eslintignore +0 -11
- package/.eslintrc.js +0 -120
- package/.github/workflows/greetings.yml +0 -13
- package/.github/workflows/publish.yml +0 -45
- package/.prettierignore +0 -19
- package/.prettierrc.js +0 -9
- package/babel.config.js +0 -6
- package/rollup.config.js +0 -100
- package/rollup.plugins.config.js +0 -84
- package/src/adapters/gioTaobaoAdapter.ts +0 -202
- package/src/adapters/gioTaroAdapter.ts +0 -467
- package/src/adapters/gioUniAppAdapter.ts +0 -431
- package/src/constants/config.ts +0 -170
- package/src/constants/emitMsg.ts +0 -24
- package/src/constants/regex.ts +0 -18
- package/src/core/dataStore/eventContextBuilder.ts +0 -95
- package/src/core/dataStore/eventHooks/appEffects.ts +0 -218
- package/src/core/dataStore/eventHooks/index.ts +0 -468
- package/src/core/dataStore/eventHooks/minipPage.ts +0 -221
- package/src/core/dataStore/eventHooks/pageEffects.ts +0 -189
- package/src/core/dataStore/index.ts +0 -661
- package/src/core/growingIO.ts +0 -637
- package/src/core/pluginsInstance/base.ts +0 -157
- package/src/core/pluginsInstance/taro.ts +0 -15
- package/src/core/pluginsInstance/tb.ts +0 -15
- package/src/core/pluginsInstance/uniapp.ts +0 -15
- package/src/core/uploader.ts +0 -272
- package/src/core/userStore.ts +0 -267
- package/src/index.ts +0 -77
- package/src/minipInstance/base.ts +0 -352
- package/src/minipInstance/framework.ts +0 -24
- package/src/minipInstance/jd.ts +0 -11
- package/src/minipInstance/ks.ts +0 -12
- package/src/minipInstance/my.ts +0 -131
- package/src/minipInstance/qq.ts +0 -12
- package/src/minipInstance/quickapp.ts +0 -233
- package/src/minipInstance/swan.ts +0 -20
- package/src/minipInstance/tb.ts +0 -58
- package/src/minipInstance/tt.ts +0 -12
- package/src/minipInstance/wx.ts +0 -27
- package/src/minipInstance/xhs.ts +0 -20
- package/src/platformConfig/base.ts +0 -82
- package/src/platformConfig/framework.ts +0 -31
- package/src/platformConfig/jd.ts +0 -11
- package/src/platformConfig/ks.ts +0 -11
- package/src/platformConfig/my.ts +0 -16
- package/src/platformConfig/qq.ts +0 -13
- package/src/platformConfig/quickapp.ts +0 -48
- package/src/platformConfig/swan.ts +0 -11
- package/src/platformConfig/tb.ts +0 -29
- package/src/platformConfig/tt.ts +0 -11
- package/src/platformConfig/wx.ts +0 -20
- package/src/platformConfig/xhs.ts +0 -27
- package/src/plugins/gioABTest.ts +0 -348
- package/src/plugins/gioCompress.ts +0 -253
- package/src/plugins/gioEventAutoTracking.ts +0 -310
- package/src/plugins/gioImpressionTracking.ts +0 -386
- package/src/plugins/gioMultipleInstances.ts +0 -189
- package/src/plugins/gioPerformance/exception.ts +0 -152
- package/src/plugins/gioPerformance/monitor.ts +0 -284
- package/src/plugins/gioPerformance/network.ts +0 -197
- package/src/plugins/gioPerformance.ts +0 -131
- package/src/types/base.ts +0 -167
- package/src/types/dataStore.ts +0 -62
- package/src/types/eventHooks.ts +0 -146
- package/src/types/gioCustomTracking.ts +0 -9
- package/src/types/gioEventTracking.ts +0 -10
- package/src/types/gioPerformance.ts +0 -8
- package/src/types/growingIO.ts +0 -137
- package/src/types/minipInstance.ts +0 -133
- package/src/types/platforms.ts +0 -64
- package/src/types/plugins.ts +0 -24
- package/src/types/uploader.ts +0 -30
- package/src/types/userStore.ts +0 -28
- package/src/utils/glodash.ts +0 -430
- package/src/utils/tools.ts +0 -511
- package/src/utils/verifiers.ts +0 -52
- package/tsconfig.json +0 -32
- package/typings/global.d.ts +0 -105
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
// 原生环境下的plugin实例,无任何内置插件
|
|
2
|
-
import { GrowingIOType } from '@@/types/growingIO';
|
|
3
|
-
import { PluginItem, PluginsType } from '@@/types/plugins';
|
|
4
|
-
import {
|
|
5
|
-
find,
|
|
6
|
-
isEmpty,
|
|
7
|
-
isFunction,
|
|
8
|
-
keys,
|
|
9
|
-
lowerFirst,
|
|
10
|
-
unset
|
|
11
|
-
} from '@@/utils/glodash';
|
|
12
|
-
import { consoleText } from '@@/utils/tools';
|
|
13
|
-
import EMIT_MSG from '@@/constants/emitMsg';
|
|
14
|
-
|
|
15
|
-
// 会被强制剔除出内置的插件(即必须通过手动注册的方式加载的内置插件)
|
|
16
|
-
const EXTRA_PLUGINS = ['gioPerformance'];
|
|
17
|
-
// @ts-nocheck
|
|
18
|
-
class BasePlugins implements PluginsType {
|
|
19
|
-
public pluginItems: PluginItem[];
|
|
20
|
-
public pluginsContext: any;
|
|
21
|
-
constructor(public growingIO: GrowingIOType) {
|
|
22
|
-
this.pluginsContext = { plugins: {} };
|
|
23
|
-
this.pluginItems = [];
|
|
24
|
-
this.growingIO.emitter?.on(EMIT_MSG.ON_INSTALL, (args) =>
|
|
25
|
-
this.callLifeCycle('onInstall', args)
|
|
26
|
-
);
|
|
27
|
-
this.growingIO.emitter?.on(EMIT_MSG.ON_ERROR, (args) =>
|
|
28
|
-
this.callLifeCycle('onError', args)
|
|
29
|
-
);
|
|
30
|
-
this.growingIO.emitter?.on(EMIT_MSG.ON_COMPOSE_BEFORE, (args) =>
|
|
31
|
-
this.callLifeCycle('onComposeBefore', args)
|
|
32
|
-
);
|
|
33
|
-
this.growingIO.emitter?.on(EMIT_MSG.ON_COMPOSE_AFTER, (args) =>
|
|
34
|
-
this.callLifeCycle('onComposeAfter', args)
|
|
35
|
-
);
|
|
36
|
-
this.growingIO.emitter?.on(EMIT_MSG.ON_SEND_BEFORE, (args) =>
|
|
37
|
-
this.callLifeCycle('onSendBefore', args)
|
|
38
|
-
);
|
|
39
|
-
this.growingIO.emitter?.on(EMIT_MSG.ON_SEND_AFTER, (args) =>
|
|
40
|
-
this.callLifeCycle('onSendAfter', args)
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// 初始化内置插件,不允许客户自己热插拔的插件,内置打包完成
|
|
45
|
-
innerPluginInit = () => {
|
|
46
|
-
EXTRA_PLUGINS.forEach((o) => unset(this.pluginsContext?.plugins, o));
|
|
47
|
-
keys(this.pluginsContext?.plugins).forEach((key) => {
|
|
48
|
-
const { name, method }: PluginItem =
|
|
49
|
-
this.pluginsContext?.plugins[key] ?? {};
|
|
50
|
-
// 若列表中出现同名插件,以第一个扫描出来的插件为准
|
|
51
|
-
const isExist = find(this.pluginItems, (o) => o.name === name);
|
|
52
|
-
if (!isExist) {
|
|
53
|
-
this.pluginItems.push({
|
|
54
|
-
// 优先使用指定插件名
|
|
55
|
-
name: lowerFirst(name || key),
|
|
56
|
-
method: method ? method : (growingIO) => {} // eslint-disable-line
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
if (!isEmpty(this.pluginItems)) {
|
|
61
|
-
this.installAll();
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// 挂载插件
|
|
66
|
-
install = (pluginName: string, pluginItem?: any, options?: any) => {
|
|
67
|
-
const { emitter } = this.growingIO;
|
|
68
|
-
const pluginTarget: PluginItem =
|
|
69
|
-
pluginItem ||
|
|
70
|
-
find(this.pluginItems, (o: PluginItem) => o.name === pluginName);
|
|
71
|
-
// 插件已加载或与已加载的插件重名
|
|
72
|
-
if ((this.growingIO?.plugins as any)[pluginName]) {
|
|
73
|
-
consoleText(`重复加载插件 ${pluginName} 或插件重名,已跳过加载!`, 'warn');
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
// 插件列表中不存在要加载的插件
|
|
77
|
-
if (!pluginTarget) {
|
|
78
|
-
consoleText(`插件加载失败!不存在名为 ${pluginName} 的插件!`, 'error');
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
// 将插件实例挂载到growingIO实例上使得其他模块可以调用
|
|
82
|
-
try {
|
|
83
|
-
(this.growingIO?.plugins as any)[pluginName] = new (
|
|
84
|
-
pluginTarget as any
|
|
85
|
-
).method(this.growingIO, options);
|
|
86
|
-
if (pluginItem) {
|
|
87
|
-
emitter.emit(EMIT_MSG.ON_INSTALL, pluginName);
|
|
88
|
-
consoleText(`加载插件 ${pluginName}`, 'info');
|
|
89
|
-
}
|
|
90
|
-
return true;
|
|
91
|
-
} catch (error) {
|
|
92
|
-
this.lifeError(pluginItem, error);
|
|
93
|
-
consoleText(`插件【${pluginName}】加载异常 ${error}`, 'error');
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// 挂载所有插件
|
|
99
|
-
installAll = (plugins?: PluginItem[]) => {
|
|
100
|
-
(plugins || this.pluginItems).forEach((o: PluginItem) => {
|
|
101
|
-
const suc = this.install(
|
|
102
|
-
o.name,
|
|
103
|
-
plugins ? o : undefined,
|
|
104
|
-
plugins ? o?.options : undefined
|
|
105
|
-
);
|
|
106
|
-
if (suc && !find(this.pluginItems, (p) => p.name === o.name)) {
|
|
107
|
-
this.pluginItems.push({
|
|
108
|
-
// 优先使用指定插件名
|
|
109
|
-
name: lowerFirst(o.name),
|
|
110
|
-
// @ts-ignore
|
|
111
|
-
method: o.method ? o.method : () => {}
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
// 移除插件
|
|
118
|
-
uninstall = (pluginName: string) => {
|
|
119
|
-
unset(this.pluginItems, pluginName);
|
|
120
|
-
const u = unset(this.growingIO?.plugins, pluginName);
|
|
121
|
-
if (!u) {
|
|
122
|
-
consoleText(`卸载插件 ${pluginName} 失败!`, 'error');
|
|
123
|
-
}
|
|
124
|
-
return u as boolean;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// 移除所有插件
|
|
128
|
-
uninstallAll = () => {
|
|
129
|
-
this.pluginItems.forEach((o: PluginItem) => this.uninstall(o.name));
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
lifeError = (p: PluginItem, e: any) => {
|
|
133
|
-
this.growingIO.emitter.emit(EMIT_MSG.ON_ERROR, {
|
|
134
|
-
plugin: p,
|
|
135
|
-
error: e
|
|
136
|
-
});
|
|
137
|
-
consoleText(`插件执行错误 ${p.name} ${e}`, 'error');
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
// 触发插件的生命周期事件
|
|
141
|
-
callLifeCycle = (lifetime: string, args: any) => {
|
|
142
|
-
this.pluginItems.forEach((o: PluginItem) => {
|
|
143
|
-
const method = (this.growingIO.plugins as any)[o.name]
|
|
144
|
-
? (this.growingIO.plugins as any)[o.name][lifetime]
|
|
145
|
-
: undefined;
|
|
146
|
-
if (method && isFunction(method)) {
|
|
147
|
-
try {
|
|
148
|
-
method(args);
|
|
149
|
-
} catch (e) {
|
|
150
|
-
this.lifeError(o, e);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export default BasePlugins;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// taro环境下的plugins实例,添加taro适配插件
|
|
2
|
-
import { GrowingIOType } from '@@/types/growingIO';
|
|
3
|
-
import BasePlugins from './base';
|
|
4
|
-
import gioTaroAdapter from '@@/adapters/gioTaroAdapter';
|
|
5
|
-
|
|
6
|
-
class Plugins extends BasePlugins {
|
|
7
|
-
constructor(public growingIO: GrowingIOType) {
|
|
8
|
-
super(growingIO);
|
|
9
|
-
this.pluginsContext = {
|
|
10
|
-
plugins: { gioTaroAdapter }
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default Plugins;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// taobao环境下的plugins实例,添加taobao适配插件
|
|
2
|
-
import { GrowingIOType } from '@@/types/growingIO';
|
|
3
|
-
import BasePlugins from './base';
|
|
4
|
-
import gioTaobaoAdapter from '@@/adapters/gioTaobaoAdapter';
|
|
5
|
-
|
|
6
|
-
class Plugins extends BasePlugins {
|
|
7
|
-
constructor(public growingIO: GrowingIOType) {
|
|
8
|
-
super(growingIO);
|
|
9
|
-
this.pluginsContext = {
|
|
10
|
-
plugins: { gioTaobaoAdapter }
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default Plugins;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// uniapp环境下的plugins实例,添加uniapp适配插件
|
|
2
|
-
import { GrowingIOType } from '@@/types/growingIO';
|
|
3
|
-
import BasePlugins from './base';
|
|
4
|
-
import gioUniAppAdapter from '@@/adapters/gioUniAppAdapter';
|
|
5
|
-
|
|
6
|
-
class Plugins extends BasePlugins {
|
|
7
|
-
constructor(public growingIO: GrowingIOType) {
|
|
8
|
-
super(growingIO);
|
|
9
|
-
this.pluginsContext = {
|
|
10
|
-
plugins: { gioUniAppAdapter }
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default Plugins;
|
package/src/core/uploader.ts
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
head,
|
|
3
|
-
isEmpty,
|
|
4
|
-
isNil,
|
|
5
|
-
startsWith,
|
|
6
|
-
throttle,
|
|
7
|
-
unset
|
|
8
|
-
} from '@@/utils/glodash';
|
|
9
|
-
import { EVENT, EXTEND_EVENT } from '@@/types/base';
|
|
10
|
-
import { consoleText } from '@@/utils/tools';
|
|
11
|
-
import { GrowingIOType } from '@@/types/growingIO';
|
|
12
|
-
import { UploaderType } from '@@/types/uploader';
|
|
13
|
-
import EMIT_MSG from '@@/constants/emitMsg';
|
|
14
|
-
|
|
15
|
-
class Uploader implements UploaderType {
|
|
16
|
-
// 因forceLogin配置积压的事件请求队列
|
|
17
|
-
public hoardingQueue: any;
|
|
18
|
-
// 请求队列
|
|
19
|
-
public requestQueue: any;
|
|
20
|
-
// 请求限制最大数
|
|
21
|
-
public requestLimit: number;
|
|
22
|
-
// 请求重试限制最大数
|
|
23
|
-
public retryLimit: number;
|
|
24
|
-
// 请求重试的原请求id
|
|
25
|
-
public retryIds: any;
|
|
26
|
-
// 请求中的请求数
|
|
27
|
-
public requestingNum: number;
|
|
28
|
-
|
|
29
|
-
constructor(public growingIO: GrowingIOType) {
|
|
30
|
-
this.hoardingQueue = {};
|
|
31
|
-
this.requestQueue = {};
|
|
32
|
-
this.requestLimit = 3;
|
|
33
|
-
this.retryLimit = 2;
|
|
34
|
-
this.retryIds = {};
|
|
35
|
-
this.requestingNum = 0;
|
|
36
|
-
this.growingIO.emitter.on(
|
|
37
|
-
EMIT_MSG.ON_SEND_BEFORE,
|
|
38
|
-
({ eventsQueue, requestData, trackingId }) => {
|
|
39
|
-
const { vdsConfig, plugins } = this.growingIO;
|
|
40
|
-
// 核心逻辑中只发送主实例的事件
|
|
41
|
-
if (trackingId === this.growingIO.trackingId) {
|
|
42
|
-
if (vdsConfig?.tbConfig?.cloudAppId) {
|
|
43
|
-
// 通过淘宝云应用转发
|
|
44
|
-
plugins?.gioTaobaoAdapter?.tbCloudAppInvoke(
|
|
45
|
-
eventsQueue,
|
|
46
|
-
requestData
|
|
47
|
-
);
|
|
48
|
-
} else {
|
|
49
|
-
this.sendEvent(eventsQueue, requestData);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 生成上报地址
|
|
57
|
-
generateURL = (trackingId: string) => {
|
|
58
|
-
const { serverUrl, projectId } =
|
|
59
|
-
this.growingIO.dataStore.getTrackerVds(trackingId);
|
|
60
|
-
if (!startsWith(serverUrl, 'http')) {
|
|
61
|
-
return `https://${serverUrl}/v3/projects/${projectId}/collect`;
|
|
62
|
-
} else {
|
|
63
|
-
return `${serverUrl}/v3/projects/${projectId}/collect`;
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// 获取积压队列
|
|
68
|
-
getHoardingQueue = (trackingId: string) =>
|
|
69
|
-
this.hoardingQueue[trackingId] ?? [];
|
|
70
|
-
|
|
71
|
-
// 获取请求队列
|
|
72
|
-
getRequestQueue = (trackingId: string) => this.requestQueue[trackingId] ?? [];
|
|
73
|
-
|
|
74
|
-
// 提交请求(将请求按队列进行管理)
|
|
75
|
-
commitRequest = (commitData: EXTEND_EVENT) => {
|
|
76
|
-
const data: EXTEND_EVENT = { ...commitData };
|
|
77
|
-
const { vdsConfig, emitter } = this.growingIO;
|
|
78
|
-
emitter.emit(EMIT_MSG.ON_COMMIT_REQUEST, {
|
|
79
|
-
eventData: commitData,
|
|
80
|
-
trackingId: commitData.trackingId
|
|
81
|
-
});
|
|
82
|
-
// 如果开启forceLogin 则将请求推入积压队列,反之则进入请求队列
|
|
83
|
-
if (vdsConfig.forceLogin) {
|
|
84
|
-
if (isNil(this.hoardingQueue[commitData.trackingId])) {
|
|
85
|
-
this.hoardingQueue[commitData.trackingId] = [];
|
|
86
|
-
}
|
|
87
|
-
this.hoardingQueue[commitData.trackingId]?.push(data);
|
|
88
|
-
} else {
|
|
89
|
-
if (isNil(this.requestQueue[commitData.trackingId])) {
|
|
90
|
-
this.requestQueue[commitData.trackingId] = [];
|
|
91
|
-
}
|
|
92
|
-
this.requestQueue[commitData.trackingId]?.push(data);
|
|
93
|
-
this.initiateRequest(commitData.trackingId);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// 发起请求
|
|
98
|
-
initiateRequest = (trackingId: string, forceSend?: boolean) => {
|
|
99
|
-
const { plugins, vdsConfig, minipInstance, dataStore } = this.growingIO;
|
|
100
|
-
const hoardingQueue = this.getHoardingQueue(trackingId);
|
|
101
|
-
const requestQueue = this.getRequestQueue(trackingId);
|
|
102
|
-
// 同时请求数的限制,防止挤占小程序本身的业务请求
|
|
103
|
-
const queues = [...hoardingQueue, ...requestQueue];
|
|
104
|
-
const requestProcess = (tid: string) => {
|
|
105
|
-
// 淘宝云函数直接发送要单发
|
|
106
|
-
if (vdsConfig?.tbConfig?.cloudFuncSend) {
|
|
107
|
-
plugins?.gioTaobaoAdapter?.singleInvoke(tid);
|
|
108
|
-
} else if (forceSend) {
|
|
109
|
-
// 强制即时发送
|
|
110
|
-
this.batchInvoke(tid);
|
|
111
|
-
} else {
|
|
112
|
-
// 有节流的批量发送(1秒钟之内产生的请求只会组团发送一次)
|
|
113
|
-
const { uploadInterval } = this.growingIO.dataStore.getTrackerVds(tid);
|
|
114
|
-
throttle(this.batchInvoke, uploadInterval, false, false)(tid);
|
|
115
|
-
}
|
|
116
|
-
// 发起visit请求时把存储里的originalSource删掉
|
|
117
|
-
const hasVisit = queues.some((e) => e.eventType === 'VISIT');
|
|
118
|
-
if (hasVisit) {
|
|
119
|
-
minipInstance.removeStorageSync(
|
|
120
|
-
dataStore.getStorageKey(tid, 'originalSource')
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
if (queues.length > 0) {
|
|
125
|
-
// 同时请求数在3个以下直接发起请求
|
|
126
|
-
if (this.requestingNum < this.requestLimit) {
|
|
127
|
-
requestProcess(trackingId);
|
|
128
|
-
} else {
|
|
129
|
-
// 同时有3个请求在发送时,设轮询延时直到请求数在3个以下
|
|
130
|
-
let t = setInterval(() => {
|
|
131
|
-
if (this.requestingNum < this.requestLimit) {
|
|
132
|
-
requestProcess(trackingId);
|
|
133
|
-
clearInterval(t);
|
|
134
|
-
t = undefined;
|
|
135
|
-
}
|
|
136
|
-
}, 500);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// 批量发送无延时(强制发送)
|
|
142
|
-
batchInvoke = (trackingId: string) => {
|
|
143
|
-
const { vdsConfig, emitter } = this.growingIO;
|
|
144
|
-
const hoardingQueue = this.getHoardingQueue(trackingId);
|
|
145
|
-
const requestQueue = this.getRequestQueue(trackingId);
|
|
146
|
-
let queues = [...hoardingQueue, ...requestQueue];
|
|
147
|
-
// 如果事件数大于50,剩下的切断放到下次请求
|
|
148
|
-
if (queues.length > 50) {
|
|
149
|
-
queues = queues.slice(0, 50);
|
|
150
|
-
// 如果积压队列超过50,只截取积压队列,剩下的下一次发
|
|
151
|
-
if (hoardingQueue.length > 50) {
|
|
152
|
-
this.hoardingQueue[trackingId] = hoardingQueue.slice(50);
|
|
153
|
-
} else {
|
|
154
|
-
// 如果积压队列小于50,正常的请求队列就只截取补满50后剩下的事件
|
|
155
|
-
this.requestQueue[trackingId] = requestQueue.slice(
|
|
156
|
-
50 - hoardingQueue.length
|
|
157
|
-
);
|
|
158
|
-
this.hoardingQueue[trackingId] = [];
|
|
159
|
-
}
|
|
160
|
-
} else {
|
|
161
|
-
this.hoardingQueue[trackingId] = [];
|
|
162
|
-
this.requestQueue[trackingId] = [];
|
|
163
|
-
}
|
|
164
|
-
// 过滤掉重试超过3次的请求(直接丢弃)
|
|
165
|
-
let eventsQueue: EXTEND_EVENT[] = queues.filter(
|
|
166
|
-
(o) => (this.retryIds[o.requestId] || 0) <= this.retryLimit
|
|
167
|
-
);
|
|
168
|
-
if (isEmpty(eventsQueue)) {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
// 生成新的请求事件(过滤事件中不能被上报的字段)
|
|
172
|
-
const requestData: EVENT[] = eventsQueue.map((eData) => {
|
|
173
|
-
const rdata = { ...eData };
|
|
174
|
-
unset(rdata, ['requestId', 'trackingId']);
|
|
175
|
-
return rdata;
|
|
176
|
-
});
|
|
177
|
-
// 开启debug模式时,打印事件日志
|
|
178
|
-
if (vdsConfig.debug) {
|
|
179
|
-
console.log('[GrowingIO Debug]:', JSON.stringify(requestData, null, 2));
|
|
180
|
-
}
|
|
181
|
-
// 广播事件
|
|
182
|
-
emitter.emit(EMIT_MSG.ON_SEND_BEFORE, {
|
|
183
|
-
eventsQueue,
|
|
184
|
-
requestData,
|
|
185
|
-
trackingId
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// 发送事件
|
|
190
|
-
sendEvent = (eventsQueue: EXTEND_EVENT[], requestData: EVENT[]) => {
|
|
191
|
-
const trackingId = head(eventsQueue).trackingId;
|
|
192
|
-
const { minipInstance, dataStore, emitter, plugins } = this.growingIO;
|
|
193
|
-
const { compress, requestTimeout } = dataStore.getTrackerVds(trackingId);
|
|
194
|
-
this.requestingNum += 1;
|
|
195
|
-
// 根据设置拼装最后请求数据
|
|
196
|
-
const header: any = {
|
|
197
|
-
// !!!京东小程序在7月4号的发版中,ios全匹配了application/json;导致我们的请求没法发出去,这里做一下兼容,等他们修复上线以后改回已注释的部分
|
|
198
|
-
// 'content-type': 'application/json;charset=UTF-8'
|
|
199
|
-
'content-type':
|
|
200
|
-
minipInstance.platform === 'jdp'
|
|
201
|
-
? 'application/json'
|
|
202
|
-
: 'application/json;charset=UTF-8'
|
|
203
|
-
};
|
|
204
|
-
let compressData = [...requestData];
|
|
205
|
-
// 数据加密
|
|
206
|
-
const isCompress = !!compress && plugins?.gioCompress?.compressToUTF16;
|
|
207
|
-
if (isCompress) {
|
|
208
|
-
compressData = plugins.gioCompress.compressToUTF16(
|
|
209
|
-
JSON.stringify(requestData)
|
|
210
|
-
);
|
|
211
|
-
header['X-Compress-Codec'] = '1';
|
|
212
|
-
}
|
|
213
|
-
const sendURL = this.generateURL(trackingId);
|
|
214
|
-
minipInstance.request({
|
|
215
|
-
url: `${sendURL}?stm=${Date.now()}&compress=${isCompress ? '1' : '0'}`,
|
|
216
|
-
header,
|
|
217
|
-
method: 'POST',
|
|
218
|
-
data: compressData,
|
|
219
|
-
timeout: requestTimeout,
|
|
220
|
-
fail: (result: any) => {
|
|
221
|
-
if (![200, 204].includes(result.code)) {
|
|
222
|
-
eventsQueue.forEach((o: any) => {
|
|
223
|
-
this.requestFailFn(o);
|
|
224
|
-
});
|
|
225
|
-
consoleText(`请求失败!${JSON.stringify(result)}`, 'error');
|
|
226
|
-
emitter.emit(EMIT_MSG.ON_SEND_ERROR, {
|
|
227
|
-
eventsQueue,
|
|
228
|
-
requestData,
|
|
229
|
-
trackingId
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
},
|
|
233
|
-
complete: (args: any) => {
|
|
234
|
-
this.requestingNum -= 1;
|
|
235
|
-
emitter.emit(EMIT_MSG.ON_SEND_AFTER, {
|
|
236
|
-
result: args,
|
|
237
|
-
requestData,
|
|
238
|
-
trackingId
|
|
239
|
-
});
|
|
240
|
-
this.initiateRequest(trackingId);
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
// 请求失败的回调
|
|
246
|
-
requestFailFn = (event: EXTEND_EVENT) => {
|
|
247
|
-
// 把重试的请求进行计数,超过重试上限的会被丢弃
|
|
248
|
-
if (!this.retryIds[event.requestId]) {
|
|
249
|
-
this.retryIds[event.requestId] = 0;
|
|
250
|
-
}
|
|
251
|
-
this.retryIds[event.requestId] += 1;
|
|
252
|
-
// 发送失败的事件会重新推入请求队列
|
|
253
|
-
const eventExist = this.requestQueue[event.trackingId].some(
|
|
254
|
-
(o) => o.requestId === event.requestId
|
|
255
|
-
);
|
|
256
|
-
if (!eventExist) {
|
|
257
|
-
// 延迟半秒后再推入请求队列,给网络一点恢复时间
|
|
258
|
-
let t = setTimeout(() => {
|
|
259
|
-
if (!isEmpty(this.requestQueue[event.trackingId])) {
|
|
260
|
-
this.requestQueue[event.trackingId].push(event);
|
|
261
|
-
} else {
|
|
262
|
-
this.requestQueue[event.trackingId].push(event);
|
|
263
|
-
this.initiateRequest(event.trackingId);
|
|
264
|
-
}
|
|
265
|
-
clearTimeout(t);
|
|
266
|
-
t = null;
|
|
267
|
-
}, 800);
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
export default Uploader;
|