-
Notifications
You must be signed in to change notification settings - Fork 1
/
dispatcher.js
31 lines (28 loc) · 929 Bytes
/
dispatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(() => {
function dispatchItem(item) {
var event = new CustomEvent('post-added', {
'detail': item
})
document.dispatchEvent(event)
}
function dispatchChildren(node) {
var posts = node.querySelectorAll('.post')
for(var post of Array.from(posts)) {
dispatchItem(post)
}
}
var observer = new MutationObserver((changeSets) => {
changeSets.forEach((changeSet) => {
Array.from(changeSet.addedNodes).forEach(dispatchChildren)
})
})
// Promise is used here to create a deferred call so all events are dispatched after the synchronous code is called
new Promise((resolve) => {
var batchList = document.querySelector('#more_history')
if(batchList) {
dispatchChildren(batchList)
observer.observe(batchList, {childList: true})
}
resolve()
})
})()