-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathref.js
More file actions
27 lines (24 loc) · 739 Bytes
/
ref.js
File metadata and controls
27 lines (24 loc) · 739 Bytes
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
const refMap = new WeakMap()
function setup(attributes, element) {
const object = attributes.ref.object
const property = attributes.ref.property
if (typeof object[property] === 'function') {
setTimeout(() => {
object[property]({ ...attributes, element })
}, 0)
} else {
object[property] = element
}
const map = refMap.get(attributes.ref.object) || {}
map[attributes.ref.property] = true
refMap.set(attributes.ref.object, map)
}
export function ref(attributes, element) {
if (!attributes?.ref) return
setup(attributes, element)
}
export function reref(attributes, element) {
const map = refMap.get(attributes.ref.object)
if (map?.[attributes.ref.property]) return
setup(attributes, element)
}