Files
image-workbench-desktop/src/runtime-lifecycle.js
T

111 lines
3.7 KiB
JavaScript

;(function (global) {
'use strict'
function createLifecycle(options) {
const opt = options || {}
const disposers = []
const objectUrls = []
let disposed = false
let added = 0
let removed = 0
let errors = 0
function runDisposer(disposer) {
try { disposer() } catch (error) { errors++ }
}
function addDisposer(disposer) {
if (typeof disposer !== 'function') return function () {}
if (disposed) { runDisposer(disposer); return function () {} }
let active = true
const wrapped = function () {
if (!active) return
active = false
const index = disposers.indexOf(wrapped)
if (index >= 0) disposers.splice(index, 1)
removed++
runDisposer(disposer)
}
disposers.push(wrapped)
added++
return wrapped
}
function addEvent(target, type, handler, eventOptions) {
if (!target || typeof target.addEventListener !== 'function') return function () {}
target.addEventListener(type, handler, eventOptions)
return addDisposer(function () { target.removeEventListener(type, handler, eventOptions) })
}
function addTimer(timer, cancel) {
if (timer === undefined || timer === null) return function () {}
const clear = typeof cancel === 'function' ? cancel : (typeof global.clearTimeout === 'function' ? global.clearTimeout : function () {})
return addDisposer(function () { clear(timer) })
}
function timeout(handler, delay) {
if (typeof global.setTimeout !== 'function') return null
const timer = global.setTimeout(function () {
const index = disposers.length
handler()
if (index === disposers.length) return
}, delay)
addTimer(timer, global.clearTimeout)
return timer
}
function interval(handler, delay) {
if (typeof global.setInterval !== 'function') return null
const timer = global.setInterval(handler, delay)
addTimer(timer, global.clearInterval)
return timer
}
function addAbortController(controller) {
if (!controller || typeof controller.abort !== 'function') return function () {}
return addDisposer(function () {
try { if (!controller.signal || !controller.signal.aborted) controller.abort() } catch (error) { errors++ }
})
}
function addObjectUrl(url, urlApi) {
if (!url) return url
const api = urlApi || global.URL
objectUrls.push({ url, api })
return url
}
function revokeObjectUrl(url, urlApi) {
const api = urlApi || global.URL
if (api && typeof api.revokeObjectURL === 'function') {
try { api.revokeObjectURL(url) } catch (error) { errors++ }
}
for (let i = objectUrls.length - 1; i >= 0; i--) {
if (objectUrls[i].url === url) objectUrls.splice(i, 1)
}
}
function dispose() {
if (disposed) return
disposed = true
while (disposers.length) runDisposer(disposers.pop())
while (objectUrls.length) {
const item = objectUrls.pop()
if (item.api && typeof item.api.revokeObjectURL === 'function') {
try { item.api.revokeObjectURL(item.url) } catch (error) { errors++ }
}
}
if (typeof opt.onDispose === 'function') { try { opt.onDispose() } catch (error) { errors++ } }
}
function snapshot() {
return { disposed, disposers: disposers.length, objectUrls: objectUrls.length, added, removed, errors }
}
return { addDisposer, addEvent, addTimer, timeout, interval, addAbortController, addObjectUrl, revokeObjectUrl, dispose, snapshot }
}
global.IWBRuntimeLifecycleFactory = createLifecycle
global.IWBRuntimeLifecycle = createLifecycle()
})(typeof window !== 'undefined' ? window : globalThis)