@yysports/tools 2.0.0 → 2.0.1
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/dist/Track/index.d.ts +9 -21
- package/dist/Track/index.js +69 -141
- package/lib/Track/index.d.ts +9 -21
- package/lib/Track/index.js +28 -66
- package/package.json +1 -1
package/dist/Track/index.d.ts
CHANGED
|
@@ -1,32 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* 提供统一
|
|
2
|
+
* 简化版埋点入口(单实例)
|
|
3
|
+
* 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
|
|
4
4
|
*/
|
|
5
|
-
import { TrackConfig } from '../interfaces/track.interface';
|
|
5
|
+
import { AuthCenterUserProfile, TrackConfig } from '../interfaces/track.interface';
|
|
6
6
|
import { TrackerInstance } from './core/instance';
|
|
7
7
|
export type { TrackerInstance };
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* 创建埋点实例
|
|
13
|
-
* @param config TrackConfig
|
|
14
|
-
* @param options { lazy } 是否延迟 init(默认 false)
|
|
15
|
-
* - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
|
|
16
|
-
* - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
|
|
17
|
-
*/
|
|
18
|
-
export declare function createTracker(config: TrackConfig, options?: CreateTrackerOptions): Promise<TrackerInstance> | TrackerInstance;
|
|
19
|
-
declare const _default: {
|
|
20
|
-
init: (config: TrackConfig) => Promise<TrackerInstance>;
|
|
21
|
-
isReady: () => boolean;
|
|
22
|
-
getInstance: () => TrackerInstance;
|
|
8
|
+
type TrackAPI = {
|
|
9
|
+
init: (config: TrackConfig) => Promise<AuthCenterUserProfile>;
|
|
23
10
|
eventLog: TrackerInstance['eventLog'];
|
|
24
11
|
visitLog: TrackerInstance['visitLog'];
|
|
25
|
-
errorLog: TrackerInstance['errorLog']
|
|
12
|
+
errorLog: (...args: Parameters<TrackerInstance['errorLog']>) => Promise<void>;
|
|
26
13
|
apiLog: TrackerInstance['apiLog'];
|
|
27
14
|
flush: () => void;
|
|
28
15
|
destroy: () => void;
|
|
29
16
|
setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
|
|
30
|
-
getConfig: () => TrackConfig
|
|
17
|
+
getConfig: () => TrackConfig;
|
|
31
18
|
};
|
|
32
|
-
|
|
19
|
+
declare const Track: TrackAPI;
|
|
20
|
+
export default Track;
|
package/dist/Track/index.js
CHANGED
|
@@ -1,47 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
2
3
|
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
3
4
|
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
4
5
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
5
|
-
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
6
|
-
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
7
6
|
/**
|
|
8
|
-
*
|
|
9
|
-
* 提供统一
|
|
7
|
+
* 简化版埋点入口(单实例)
|
|
8
|
+
* 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
|
|
10
9
|
*/
|
|
11
10
|
|
|
12
11
|
import { TrackerInstance } from "./core/instance";
|
|
13
|
-
|
|
14
|
-
// ---------------- 多实例工厂 ----------------
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 创建埋点实例
|
|
18
|
-
* @param config TrackConfig
|
|
19
|
-
* @param options { lazy } 是否延迟 init(默认 false)
|
|
20
|
-
* - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
|
|
21
|
-
* - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
|
|
22
|
-
*/
|
|
23
|
-
export function createTracker(config, options) {
|
|
24
|
-
var _ref = options || {},
|
|
25
|
-
lazy = _ref.lazy;
|
|
26
|
-
var ins = new TrackerInstance(config);
|
|
27
|
-
if (lazy) return ins;
|
|
28
|
-
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
29
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
30
|
-
while (1) switch (_context.prev = _context.next) {
|
|
31
|
-
case 0:
|
|
32
|
-
_context.next = 2;
|
|
33
|
-
return ins.init();
|
|
34
|
-
case 2:
|
|
35
|
-
return _context.abrupt("return", ins);
|
|
36
|
-
case 3:
|
|
37
|
-
case "end":
|
|
38
|
-
return _context.stop();
|
|
39
|
-
}
|
|
40
|
-
}, _callee);
|
|
41
|
-
}))();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// ---------------- 静态外观(兼容方便使用) ----------------
|
|
45
12
|
var TrackFacade = /*#__PURE__*/function () {
|
|
46
13
|
function TrackFacade() {
|
|
47
14
|
_classCallCheck(this, TrackFacade);
|
|
@@ -49,99 +16,58 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
49
16
|
_createClass(TrackFacade, null, [{
|
|
50
17
|
key: "init",
|
|
51
18
|
value: ( /** 初始化(必须先调用一次)。重复调用直接返回已存在实例 */function () {
|
|
52
|
-
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
19
|
+
var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
|
|
53
20
|
var _this = this;
|
|
54
|
-
return _regeneratorRuntime().wrap(function
|
|
55
|
-
while (1) switch (
|
|
21
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
22
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
56
23
|
case 0:
|
|
57
24
|
if (!this._instance) {
|
|
58
|
-
|
|
25
|
+
_context2.next = 2;
|
|
59
26
|
break;
|
|
60
27
|
}
|
|
61
|
-
return
|
|
28
|
+
return _context2.abrupt("return", Promise.resolve(this._profile));
|
|
62
29
|
case 2:
|
|
63
30
|
if (!this._initPromise) {
|
|
64
|
-
this._initPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function
|
|
65
|
-
var ins;
|
|
66
|
-
return _regeneratorRuntime().wrap(function
|
|
67
|
-
while (1) switch (
|
|
31
|
+
this._initPromise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
32
|
+
var ins, profile;
|
|
33
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
34
|
+
while (1) switch (_context.prev = _context.next) {
|
|
68
35
|
case 0:
|
|
69
36
|
ins = new TrackerInstance(config);
|
|
70
|
-
|
|
37
|
+
_context.next = 3;
|
|
71
38
|
return ins.init();
|
|
72
39
|
case 3:
|
|
40
|
+
profile = _context.sent;
|
|
41
|
+
// AuthCenterUserProfile
|
|
73
42
|
_this._instance = ins;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
var _ref5;
|
|
77
|
-
var method = _ref4.method,
|
|
78
|
-
args = _ref4.args;
|
|
79
|
-
return (_ref5 = ins)[method].apply(_ref5, _toConsumableArray(args));
|
|
80
|
-
});
|
|
81
|
-
_this._queue.length = 0;
|
|
82
|
-
return _context2.abrupt("return", ins);
|
|
43
|
+
_this._profile = profile;
|
|
44
|
+
return _context.abrupt("return", profile);
|
|
83
45
|
case 7:
|
|
84
46
|
case "end":
|
|
85
|
-
return
|
|
47
|
+
return _context.stop();
|
|
86
48
|
}
|
|
87
|
-
},
|
|
49
|
+
}, _callee);
|
|
88
50
|
}))();
|
|
89
51
|
}
|
|
90
|
-
return
|
|
52
|
+
return _context2.abrupt("return", this._initPromise);
|
|
91
53
|
case 4:
|
|
92
54
|
case "end":
|
|
93
|
-
return
|
|
55
|
+
return _context2.stop();
|
|
94
56
|
}
|
|
95
|
-
},
|
|
57
|
+
}, _callee2, this);
|
|
96
58
|
}));
|
|
97
59
|
function init(_x) {
|
|
98
60
|
return _init.apply(this, arguments);
|
|
99
61
|
}
|
|
100
62
|
return init;
|
|
101
|
-
}() /**
|
|
63
|
+
}() /** 工具:确保已初始化,否则抛错 */)
|
|
102
64
|
}, {
|
|
103
|
-
key: "
|
|
104
|
-
value: function
|
|
105
|
-
return !!this._instance;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** 获取内部实例(未初始化时抛错) */
|
|
109
|
-
}, {
|
|
110
|
-
key: "getInstance",
|
|
111
|
-
value: function getInstance() {
|
|
65
|
+
key: "ensure",
|
|
66
|
+
value: function ensure() {
|
|
112
67
|
if (!this._instance) throw new Error('Track 未初始化,请先调用 Track.init(config)');
|
|
113
68
|
return this._instance;
|
|
114
69
|
}
|
|
115
70
|
|
|
116
|
-
/** 延迟调用帮助:未 init 时先入队 */
|
|
117
|
-
}, {
|
|
118
|
-
key: "invoke",
|
|
119
|
-
value: function invoke(method) {
|
|
120
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
121
|
-
args[_key - 1] = arguments[_key];
|
|
122
|
-
}
|
|
123
|
-
if (this._instance) {
|
|
124
|
-
var _ref6;
|
|
125
|
-
(_ref6 = this._instance)[method].apply(_ref6, args);
|
|
126
|
-
} else if (this._initPromise) {
|
|
127
|
-
// 初始化进行中,进入队列
|
|
128
|
-
this._queue.push({
|
|
129
|
-
method: method,
|
|
130
|
-
args: args
|
|
131
|
-
});
|
|
132
|
-
} else {
|
|
133
|
-
// 尚未开始 init,仍入队,提示一次
|
|
134
|
-
this._queue.push({
|
|
135
|
-
method: method,
|
|
136
|
-
args: args
|
|
137
|
-
});
|
|
138
|
-
if (!this._warned) {
|
|
139
|
-
console.warn('[track] 在 Track.init 前调用了日志方法,已暂存至初始化后回放');
|
|
140
|
-
this._warned = true;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
71
|
/**
|
|
146
72
|
* 发送事件日志(通用事件)
|
|
147
73
|
* - 若未初始化:自动排队等待 init 完成后回放
|
|
@@ -149,10 +75,8 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
149
75
|
}, {
|
|
150
76
|
key: "eventLog",
|
|
151
77
|
value: function eventLog() {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
this.invoke.apply(this, ['eventLog'].concat(args));
|
|
78
|
+
var _this$ensure;
|
|
79
|
+
(_this$ensure = this.ensure()).eventLog.apply(_this$ensure, arguments);
|
|
156
80
|
}
|
|
157
81
|
/**
|
|
158
82
|
* 发送访问/页面曝光日志
|
|
@@ -161,10 +85,8 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
161
85
|
}, {
|
|
162
86
|
key: "visitLog",
|
|
163
87
|
value: function visitLog() {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
this.invoke.apply(this, ['visitLog'].concat(args));
|
|
88
|
+
var _this$ensure2;
|
|
89
|
+
(_this$ensure2 = this.ensure()).visitLog.apply(_this$ensure2, arguments);
|
|
168
90
|
}
|
|
169
91
|
/**
|
|
170
92
|
* 发送错误日志
|
|
@@ -172,23 +94,35 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
172
94
|
*/
|
|
173
95
|
}, {
|
|
174
96
|
key: "errorLog",
|
|
175
|
-
value: function
|
|
176
|
-
|
|
177
|
-
|
|
97
|
+
value: (function () {
|
|
98
|
+
var _errorLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
99
|
+
var _this$ensure3;
|
|
100
|
+
var _args3 = arguments;
|
|
101
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
102
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
103
|
+
case 0:
|
|
104
|
+
return _context3.abrupt("return", (_this$ensure3 = this.ensure()).errorLog.apply(_this$ensure3, _args3));
|
|
105
|
+
case 1:
|
|
106
|
+
case "end":
|
|
107
|
+
return _context3.stop();
|
|
108
|
+
}
|
|
109
|
+
}, _callee3, this);
|
|
110
|
+
}));
|
|
111
|
+
function errorLog() {
|
|
112
|
+
return _errorLog.apply(this, arguments);
|
|
178
113
|
}
|
|
179
|
-
|
|
180
|
-
}
|
|
114
|
+
return errorLog;
|
|
115
|
+
}()
|
|
181
116
|
/**
|
|
182
117
|
* 发送接口调用相关日志
|
|
183
118
|
* - 记录接口耗时 / 结果状态
|
|
184
119
|
*/
|
|
120
|
+
)
|
|
185
121
|
}, {
|
|
186
122
|
key: "apiLog",
|
|
187
123
|
value: function apiLog() {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
this.invoke.apply(this, ['apiLog'].concat(args));
|
|
124
|
+
var _this$ensure4;
|
|
125
|
+
(_this$ensure4 = this.ensure()).apiLog.apply(_this$ensure4, arguments);
|
|
192
126
|
}
|
|
193
127
|
/**
|
|
194
128
|
* 强制立即 flush 已缓冲的日志(若实例策略存在缓冲)
|
|
@@ -196,8 +130,7 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
196
130
|
}, {
|
|
197
131
|
key: "flush",
|
|
198
132
|
value: function flush() {
|
|
199
|
-
|
|
200
|
-
this._instance.flush();
|
|
133
|
+
this.ensure().flush();
|
|
201
134
|
}
|
|
202
135
|
/**
|
|
203
136
|
* 销毁当前单例实例
|
|
@@ -211,6 +144,7 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
211
144
|
this._instance.destroy();
|
|
212
145
|
this._instance = null;
|
|
213
146
|
this._initPromise = null;
|
|
147
|
+
this._profile = null;
|
|
214
148
|
}
|
|
215
149
|
/**
|
|
216
150
|
* 设置用户画像 / 用户基础属性
|
|
@@ -219,35 +153,29 @@ var TrackFacade = /*#__PURE__*/function () {
|
|
|
219
153
|
}, {
|
|
220
154
|
key: "setUserProfile",
|
|
221
155
|
value: function setUserProfile(profile) {
|
|
222
|
-
|
|
223
|
-
// 允许提前设置,将其转换为 eventLog 之前的一个补丁:加入队列尾部一个函数式调用
|
|
224
|
-
this._queue.push({
|
|
225
|
-
method: 'eventLog',
|
|
226
|
-
args: []
|
|
227
|
-
}); // 占位避免丢失顺序
|
|
228
|
-
// 等 init 后再真正 set(用回放后 hook)
|
|
229
|
-
var prevInit = this._initPromise;
|
|
230
|
-
if (prevInit) {
|
|
231
|
-
prevInit.then(function (ins) {
|
|
232
|
-
return ins.setUserProfile(profile);
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
this._instance.setUserProfile(profile);
|
|
156
|
+
this.ensure().setUserProfile(profile);
|
|
238
157
|
}
|
|
239
158
|
/** 获取当前配置(未初始化时返回 undefined) */
|
|
240
159
|
}, {
|
|
241
160
|
key: "getConfig",
|
|
242
161
|
value: function getConfig() {
|
|
243
|
-
|
|
244
|
-
return (_this$_instance = this._instance) === null || _this$_instance === void 0 ? void 0 : _this$_instance.getConfig();
|
|
162
|
+
return this.ensure().getConfig();
|
|
245
163
|
}
|
|
246
164
|
}]);
|
|
247
165
|
return TrackFacade;
|
|
248
|
-
}(); //
|
|
166
|
+
}(); // 更安全的导出:显式 API 形状 + 绑定 this
|
|
249
167
|
_defineProperty(TrackFacade, "_instance", null);
|
|
250
168
|
_defineProperty(TrackFacade, "_initPromise", null);
|
|
251
|
-
_defineProperty(TrackFacade, "
|
|
252
|
-
|
|
253
|
-
|
|
169
|
+
_defineProperty(TrackFacade, "_profile", null);
|
|
170
|
+
var Track = {
|
|
171
|
+
init: TrackFacade.init.bind(TrackFacade),
|
|
172
|
+
eventLog: TrackFacade.eventLog.bind(TrackFacade),
|
|
173
|
+
visitLog: TrackFacade.visitLog.bind(TrackFacade),
|
|
174
|
+
errorLog: TrackFacade.errorLog.bind(TrackFacade),
|
|
175
|
+
apiLog: TrackFacade.apiLog.bind(TrackFacade),
|
|
176
|
+
flush: TrackFacade.flush.bind(TrackFacade),
|
|
177
|
+
destroy: TrackFacade.destroy.bind(TrackFacade),
|
|
178
|
+
setUserProfile: TrackFacade.setUserProfile.bind(TrackFacade),
|
|
179
|
+
getConfig: TrackFacade.getConfig.bind(TrackFacade)
|
|
180
|
+
};
|
|
181
|
+
export default Track;
|
package/lib/Track/index.d.ts
CHANGED
|
@@ -1,32 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* 提供统一
|
|
2
|
+
* 简化版埋点入口(单实例)
|
|
3
|
+
* 提供统一 Track.init(config) 及若干日志方法,未初始化即抛错
|
|
4
4
|
*/
|
|
5
|
-
import { TrackConfig } from '../interfaces/track.interface';
|
|
5
|
+
import { AuthCenterUserProfile, TrackConfig } from '../interfaces/track.interface';
|
|
6
6
|
import { TrackerInstance } from './core/instance';
|
|
7
7
|
export type { TrackerInstance };
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* 创建埋点实例
|
|
13
|
-
* @param config TrackConfig
|
|
14
|
-
* @param options { lazy } 是否延迟 init(默认 false)
|
|
15
|
-
* - lazy=false: 返回 Promise<TrackerInstance>(已完成 init)
|
|
16
|
-
* - lazy=true: 返回未初始化实例 (TrackerInstance),需外部手动调用 init()
|
|
17
|
-
*/
|
|
18
|
-
export declare function createTracker(config: TrackConfig, options?: CreateTrackerOptions): Promise<TrackerInstance> | TrackerInstance;
|
|
19
|
-
declare const _default: {
|
|
20
|
-
init: (config: TrackConfig) => Promise<TrackerInstance>;
|
|
21
|
-
isReady: () => boolean;
|
|
22
|
-
getInstance: () => TrackerInstance;
|
|
8
|
+
type TrackAPI = {
|
|
9
|
+
init: (config: TrackConfig) => Promise<AuthCenterUserProfile>;
|
|
23
10
|
eventLog: TrackerInstance['eventLog'];
|
|
24
11
|
visitLog: TrackerInstance['visitLog'];
|
|
25
|
-
errorLog: TrackerInstance['errorLog']
|
|
12
|
+
errorLog: (...args: Parameters<TrackerInstance['errorLog']>) => Promise<void>;
|
|
26
13
|
apiLog: TrackerInstance['apiLog'];
|
|
27
14
|
flush: () => void;
|
|
28
15
|
destroy: () => void;
|
|
29
16
|
setUserProfile: (profile: Parameters<TrackerInstance['setUserProfile']>[0]) => void;
|
|
30
|
-
getConfig: () => TrackConfig
|
|
17
|
+
getConfig: () => TrackConfig;
|
|
31
18
|
};
|
|
32
|
-
|
|
19
|
+
declare const Track: TrackAPI;
|
|
20
|
+
export default Track;
|
package/lib/Track/index.js
CHANGED
|
@@ -19,101 +19,65 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/Track/index.ts
|
|
20
20
|
var Track_exports = {};
|
|
21
21
|
__export(Track_exports, {
|
|
22
|
-
createTracker: () => createTracker,
|
|
23
22
|
default: () => Track_default
|
|
24
23
|
});
|
|
25
24
|
module.exports = __toCommonJS(Track_exports);
|
|
26
25
|
var import_instance = require("./core/instance");
|
|
27
|
-
function createTracker(config, options) {
|
|
28
|
-
const { lazy } = options || {};
|
|
29
|
-
const ins = new import_instance.TrackerInstance(config);
|
|
30
|
-
if (lazy)
|
|
31
|
-
return ins;
|
|
32
|
-
return (async () => {
|
|
33
|
-
await ins.init();
|
|
34
|
-
return ins;
|
|
35
|
-
})();
|
|
36
|
-
}
|
|
37
26
|
var TrackFacade = class {
|
|
38
27
|
/** 初始化(必须先调用一次)。重复调用直接返回已存在实例 */
|
|
39
28
|
static async init(config) {
|
|
40
29
|
if (this._instance)
|
|
41
|
-
return this.
|
|
30
|
+
return Promise.resolve(this._profile);
|
|
42
31
|
if (!this._initPromise) {
|
|
43
32
|
this._initPromise = (async () => {
|
|
44
33
|
const ins = new import_instance.TrackerInstance(config);
|
|
45
|
-
await ins.init();
|
|
34
|
+
const profile = await ins.init();
|
|
46
35
|
this._instance = ins;
|
|
47
|
-
this.
|
|
48
|
-
|
|
49
|
-
);
|
|
50
|
-
this._queue.length = 0;
|
|
51
|
-
return ins;
|
|
36
|
+
this._profile = profile;
|
|
37
|
+
return profile;
|
|
52
38
|
})();
|
|
53
39
|
}
|
|
54
40
|
return this._initPromise;
|
|
55
41
|
}
|
|
56
|
-
/**
|
|
57
|
-
static
|
|
58
|
-
return !!this._instance;
|
|
59
|
-
}
|
|
60
|
-
/** 获取内部实例(未初始化时抛错) */
|
|
61
|
-
static getInstance() {
|
|
42
|
+
/** 工具:确保已初始化,否则抛错 */
|
|
43
|
+
static ensure() {
|
|
62
44
|
if (!this._instance)
|
|
63
45
|
throw new Error("Track 未初始化,请先调用 Track.init(config)");
|
|
64
46
|
return this._instance;
|
|
65
47
|
}
|
|
66
|
-
/** 延迟调用帮助:未 init 时先入队 */
|
|
67
|
-
static invoke(method, ...args) {
|
|
68
|
-
if (this._instance) {
|
|
69
|
-
this._instance[method](...args);
|
|
70
|
-
} else if (this._initPromise) {
|
|
71
|
-
this._queue.push({ method, args });
|
|
72
|
-
} else {
|
|
73
|
-
this._queue.push({ method, args });
|
|
74
|
-
if (!this._warned) {
|
|
75
|
-
console.warn(
|
|
76
|
-
"[track] 在 Track.init 前调用了日志方法,已暂存至初始化后回放"
|
|
77
|
-
);
|
|
78
|
-
this._warned = true;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
48
|
/**
|
|
83
49
|
* 发送事件日志(通用事件)
|
|
84
50
|
* - 若未初始化:自动排队等待 init 完成后回放
|
|
85
51
|
*/
|
|
86
52
|
static eventLog(...args) {
|
|
87
|
-
this.
|
|
53
|
+
this.ensure().eventLog(...args);
|
|
88
54
|
}
|
|
89
55
|
/**
|
|
90
56
|
* 发送访问/页面曝光日志
|
|
91
57
|
* - 用于记录页面 / 场景进入等行为
|
|
92
58
|
*/
|
|
93
59
|
static visitLog(...args) {
|
|
94
|
-
this.
|
|
60
|
+
this.ensure().visitLog(...args);
|
|
95
61
|
}
|
|
96
62
|
/**
|
|
97
63
|
* 发送错误日志
|
|
98
64
|
* - 适用于运行时异常、资源加载失败、接口异常包装等
|
|
99
65
|
*/
|
|
100
|
-
static errorLog(...args) {
|
|
101
|
-
this.
|
|
66
|
+
static async errorLog(...args) {
|
|
67
|
+
return this.ensure().errorLog(...args);
|
|
102
68
|
}
|
|
103
69
|
/**
|
|
104
70
|
* 发送接口调用相关日志
|
|
105
71
|
* - 记录接口耗时 / 结果状态
|
|
106
72
|
*/
|
|
107
73
|
static apiLog(...args) {
|
|
108
|
-
this.
|
|
74
|
+
this.ensure().apiLog(...args);
|
|
109
75
|
}
|
|
110
76
|
/**
|
|
111
77
|
* 强制立即 flush 已缓冲的日志(若实例策略存在缓冲)
|
|
112
78
|
*/
|
|
113
79
|
static flush() {
|
|
114
|
-
|
|
115
|
-
return;
|
|
116
|
-
this._instance.flush();
|
|
80
|
+
this.ensure().flush();
|
|
117
81
|
}
|
|
118
82
|
/**
|
|
119
83
|
* 销毁当前单例实例
|
|
@@ -126,34 +90,32 @@ var TrackFacade = class {
|
|
|
126
90
|
this._instance.destroy();
|
|
127
91
|
this._instance = null;
|
|
128
92
|
this._initPromise = null;
|
|
93
|
+
this._profile = null;
|
|
129
94
|
}
|
|
130
95
|
/**
|
|
131
96
|
* 设置用户画像 / 用户基础属性
|
|
132
97
|
* - 若在 init 前调用:会延迟到实例 ready 后再真正执行
|
|
133
98
|
*/
|
|
134
99
|
static setUserProfile(profile) {
|
|
135
|
-
|
|
136
|
-
this._queue.push({ method: "eventLog", args: [] });
|
|
137
|
-
const prevInit = this._initPromise;
|
|
138
|
-
if (prevInit) {
|
|
139
|
-
prevInit.then((ins) => ins.setUserProfile(profile));
|
|
140
|
-
}
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
this._instance.setUserProfile(profile);
|
|
100
|
+
this.ensure().setUserProfile(profile);
|
|
144
101
|
}
|
|
145
102
|
/** 获取当前配置(未初始化时返回 undefined) */
|
|
146
103
|
static getConfig() {
|
|
147
|
-
|
|
148
|
-
return (_a = this._instance) == null ? void 0 : _a.getConfig();
|
|
104
|
+
return this.ensure().getConfig();
|
|
149
105
|
}
|
|
150
106
|
};
|
|
151
107
|
TrackFacade._instance = null;
|
|
152
108
|
TrackFacade._initPromise = null;
|
|
153
|
-
TrackFacade.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
109
|
+
TrackFacade._profile = null;
|
|
110
|
+
var Track = {
|
|
111
|
+
init: TrackFacade.init.bind(TrackFacade),
|
|
112
|
+
eventLog: TrackFacade.eventLog.bind(TrackFacade),
|
|
113
|
+
visitLog: TrackFacade.visitLog.bind(TrackFacade),
|
|
114
|
+
errorLog: TrackFacade.errorLog.bind(TrackFacade),
|
|
115
|
+
apiLog: TrackFacade.apiLog.bind(TrackFacade),
|
|
116
|
+
flush: TrackFacade.flush.bind(TrackFacade),
|
|
117
|
+
destroy: TrackFacade.destroy.bind(TrackFacade),
|
|
118
|
+
setUserProfile: TrackFacade.setUserProfile.bind(TrackFacade),
|
|
119
|
+
getConfig: TrackFacade.getConfig.bind(TrackFacade)
|
|
120
|
+
};
|
|
121
|
+
var Track_default = Track;
|