2019-10-06 22:47:17 +03:00
|
|
|
// ==============
|
|
|
|
// === IxPool ===
|
|
|
|
// ==============
|
|
|
|
|
|
|
|
class IxPool {
|
|
|
|
constructor() {
|
|
|
|
this.next = 0
|
|
|
|
this.free = []
|
|
|
|
}
|
|
|
|
|
|
|
|
reserve() {
|
|
|
|
let ix
|
2021-11-05 14:51:43 +03:00
|
|
|
if (this.free.length == 0) {
|
2019-10-06 22:47:17 +03:00
|
|
|
ix = this.next
|
|
|
|
this.next += 1
|
|
|
|
} else {
|
|
|
|
ix = this.free.shift()
|
|
|
|
}
|
|
|
|
return ix
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(ix) {
|
|
|
|
this.free.unshift(ix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============
|
|
|
|
// === Pool ===
|
|
|
|
// ============
|
|
|
|
|
|
|
|
class Pool {
|
|
|
|
constructor(cons) {
|
2021-11-05 14:51:43 +03:00
|
|
|
this.cons = cons
|
|
|
|
this.ixs = new IxPool()
|
2019-10-06 22:47:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reserve(...args) {
|
2021-11-05 14:51:43 +03:00
|
|
|
let ix = this.ixs.reserve()
|
2019-10-06 22:47:17 +03:00
|
|
|
this[ix] = this.cons(...args)
|
|
|
|
return ix
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(ix) {
|
|
|
|
this.ixs.drop(ix)
|
|
|
|
this[ix] = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ======================
|
|
|
|
// === ResizeObserver ===
|
|
|
|
// ======================
|
|
|
|
|
|
|
|
let resizeObserverPool = new Pool((...args) => new ResizeObserver(...args))
|
|
|
|
|
|
|
|
export function resize_observe(target, f) {
|
|
|
|
let id = resizeObserverPool.reserve(resize_observer_update(f))
|
|
|
|
resizeObserverPool[id].observe(target)
|
2019-12-04 19:45:31 +03:00
|
|
|
return id
|
2019-10-06 22:47:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resize_unobserve(id) {
|
|
|
|
resizeObserverPool[id].disconnect()
|
|
|
|
resizeObserverPool.drop(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
function resize_observer_update(f) {
|
|
|
|
return entries => {
|
2021-11-05 14:51:43 +03:00
|
|
|
let rect = entries[0].contentRect
|
|
|
|
f(rect.width, rect.height)
|
2019-10-06 22:47:17 +03:00
|
|
|
}
|
|
|
|
}
|