Skip to content

Commit ff7085f

Browse files
committed
Add a delete function
1 parent 6df8149 commit ff7085f

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@ observer.observe(document.documentElement, {
1313
subtree: true
1414
})
1515

16-
module.exports = function onload (node, onload, offload) {
16+
module.exports = onload
17+
function onload (node, onload, offload) {
1718
off = false
1819
node.classList.add(clz)
1920
const set = upsert(node)
2021
set.add([ onload || noop, offload || noop, 2 ])
2122
return node
2223
}
2324

25+
onload.delete = function (node, onload = noop, offload = noop) {
26+
const set = tracking.get(node)
27+
if (!set) return false
28+
for (const ls of set) {
29+
if (ls[0] === onload && ls[1] === offload) {
30+
set.delete(ls)
31+
return true
32+
}
33+
}
34+
return false
35+
}
36+
2437
function upsert (node) {
2538
const set = tracking.get(node)
2639
if (set) return set
@@ -40,6 +53,10 @@ function callAll (nodes, idx, target) {
4053
}
4154
}
4255

56+
// State Enum
57+
// 0: mounted
58+
// 1: unmounted
59+
// 2: undefined
4360
function call (node, state, target) {
4461
for (const ls of tracking.get(node)) {
4562
if (ls[2] === state) continue

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ test('operates with memoized elements', function (t) {
222222
}, 100)
223223
})
224224

225+
test('onload.delete works', function (t) {
226+
var el = document.createElement('div')
227+
el.textContent = 'test'
228+
function loadFunction () {
229+
t.pass('Load function should be called')
230+
onload.delete(el, loadFunction, unloadFunction)
231+
setTimeout(() => {
232+
t.end()
233+
}, 200)
234+
document.body.removeChild(el)
235+
}
236+
function unloadFunction () {
237+
t.fail('Unload function should not be called')
238+
}
239+
onload(el, loadFunction, unloadFunction)
240+
document.body.appendChild(el)
241+
})
242+
225243
function runops (ops, done) {
226244
function loop () {
227245
var next = ops.shift()

0 commit comments

Comments
 (0)