PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.11.1
Independent Analytics – WordPress Analytics Plugin v2.11.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / javascript-source / modules / module-markup.js
module-markup.js
68 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Stores ids for modules to request
2 const requestedModules = new Set()
3
4 // Store the id of the setTimeout currently running
5 let pollingTimeout = null
6
7 let isFirstAttempt = true
8
9 function isPolling() {
10 return Number.isInteger(pollingTimeout) && pollingTimeout > 0
11 }
12
13 function startPolling() {
14 if(isPolling() || requestedModules.size === 0) {
15 return
16 }
17
18 pollingTimeout = setTimeout(() => {
19 isFirstAttempt = false
20 pollingTimeout = null
21 fetchMarkup()
22 }, isFirstAttempt ? 0 : 500)
23 }
24
25 function dispatchEvent(id, moduleHtml) {
26 document.dispatchEvent(
27 new CustomEvent('iawp:module-markup:' + id, {
28 detail: {
29 id,
30 moduleHtml,
31 }
32 })
33 )
34 }
35
36 function fetchMarkup() {
37 const data = {
38 ...iawpActions.get_markup_for_modules,
39 ids: Array.from(requestedModules),
40 }
41
42 jQuery.post(ajaxurl, data, (response) => {
43 if(!Array.isArray(response.data)) {
44 return
45 }
46
47 response.data.forEach(module => {
48 if(module.hasDataset) {
49 dispatchEvent(module.id, module.moduleHtml)
50 requestedModules.delete(module.id)
51 }
52 })
53
54 startPolling()
55 }).fail((error) => {
56 startPolling()
57 })
58 }
59
60 module.exports = {
61 /**
62 * @return Markup for the module or null if markup is still being fetched.
63 */
64 getModuleMarkup(moduleId) {
65 requestedModules.add(moduleId)
66 startPolling()
67 }
68 }