auto-point 0.0.22 → 0.0.23

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.
Files changed (2) hide show
  1. package/AP/auto_point.js +27 -12
  2. package/package.json +1 -1
package/AP/auto_point.js CHANGED
@@ -13,6 +13,16 @@
13
13
  set server(url) {
14
14
  this.#SERVER = url
15
15
  }
16
+ #SEND_FUNC = function(data){
17
+ return fetch(this.#SERVER, {
18
+ method: 'POST',
19
+ headers: {
20
+ 'Content-Type': 'application/json',
21
+ },
22
+ body: JSON.stringify(data),
23
+ keepalive: true
24
+ });
25
+ }
16
26
  // 事件列表
17
27
  #EVENTS = ['click']
18
28
  // 发送数据间隔 单位毫秒 默认10秒
@@ -93,6 +103,9 @@
93
103
  if (params.events && this.#isArray(params.events, "events must be an Array")) {
94
104
  this.#EVENTS = params.events
95
105
  }
106
+ if(params.send_func && this.#isFunction(params.send_func, "send_func must be a function")){
107
+ this.#SEND_FUNC = params.send_func
108
+ }
96
109
  this.#POINT_SYS_INIT_TIME = new Date().getTime()
97
110
  this.#mount_dom_event()
98
111
  this.#timer_send()
@@ -167,13 +180,11 @@
167
180
  })
168
181
  // 挂载页面离开事件监听
169
182
  window.addEventListener('visibilitychange', () => {
170
- if (navigator && navigator.sendBeacon) {
171
- let type = "visibilitychange"
172
- let value = document.visibilityState
173
- let describe = "Webpage visibility state is changed to: " + document.visibilityState
174
- this.#add_point(this.#create_active_point_data(type, value, describe))
175
- this.#send()
176
- }
183
+ let type = "visibilitychange"
184
+ let value = document.visibilityState
185
+ let describe = "Webpage visibility state is changed to: " + document.visibilityState
186
+ this.#add_point(this.#create_active_point_data(type, value, describe))
187
+ this.#send()
177
188
  })
178
189
  } else {
179
190
  throw "<body></body> not found in DOM"
@@ -189,7 +200,7 @@
189
200
  }
190
201
 
191
202
  // 发送埋点数据
192
- #send() {
203
+ async #send() {
193
204
  if (this.#POINT_LIST.length) {
194
205
  this.#MIDDLEWARE.map((item) => {
195
206
  if (this.#isFunction(item)) {
@@ -198,16 +209,20 @@
198
209
  })
199
210
  this.#POINT_LAST_SEND_TIME = new Date().getTime() // 更新最后发送时间
200
211
  navigator.sendBeacon(this.#SERVER, JSON.stringify(this.#POINT_LIST));
212
+ let res = await this.#SEND_FUNC(this.#POINT_LIST)
201
213
  // 发送后清空埋点列表数据
202
214
  this.#POINT_LIST = new Array()
215
+ return res
203
216
  }
204
217
  }
205
218
 
206
- // 定时发送
219
+ // 根据间隔时间,定时发送埋点数据
207
220
  #timer_send() {
208
221
  setInterval(() => {
209
- this.#send()
210
- }, this.#INTERVAL_TIME)
222
+ if(!this.#POINT_LAST_SEND_TIME || (new Date().getTime() - this.#POINT_LAST_SEND_TIME >= this.#INTERVAL_TIME)){
223
+ this.#send()
224
+ }
225
+ }, 200)
211
226
  }
212
227
 
213
228
  /**
@@ -228,7 +243,7 @@
228
243
 
229
244
  // 手动发送数据
230
245
  send() {
231
- this.#send()
246
+ return this.#send()
232
247
  }
233
248
 
234
249
  /**
package/package.json CHANGED
@@ -13,5 +13,5 @@
13
13
  "scripts": {
14
14
  "test": "echo \"Error: no test specified\" && exit 1"
15
15
  },
16
- "version": "0.0.22"
16
+ "version": "0.0.23"
17
17
  }