mirror of
https://github.com/LalleSX/4chan-XZ.git
synced 2025-10-07 07:22:37 +02:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
// This file was created because these functions on $ were sometimes not initialized yet because of circular
|
|
// dependencies, so try to keep this file without dependencies, so these functions don't have to wait for something else
|
|
|
|
export const debounce = (wait: number, fn: Function) => {
|
|
let lastCall = 0
|
|
let timeout = null
|
|
const exec = function () {
|
|
lastCall = Date.now()
|
|
return fn.apply(this, arguments)
|
|
}
|
|
return function () {
|
|
if (lastCall < (Date.now() - wait)) {
|
|
return exec()
|
|
}
|
|
// stop current reset
|
|
clearTimeout(timeout)
|
|
// after wait, let next invocation execute immediately
|
|
return timeout = setTimeout(exec, wait)
|
|
}
|
|
}
|
|
|
|
export const dict = () => Object.create(null)
|
|
|
|
dict.clone = function (obj) {
|
|
if ((typeof obj !== 'object') || (obj === null)) {
|
|
return obj
|
|
} else if (obj instanceof Array) {
|
|
const arr = []
|
|
for (let i = 0, end = obj.length; i < end; i++) {
|
|
arr.push(dict.clone(obj[i]))
|
|
}
|
|
return arr
|
|
} else {
|
|
const map = Object.create(null)
|
|
for (const key in obj) {
|
|
const val = obj[key]
|
|
map[key] = dict.clone(val)
|
|
}
|
|
return map
|
|
}
|
|
}
|
|
|
|
dict.json = (str: string) => dict.clone(JSON.parse(str))
|
|
|
|
export const SECOND = 1000
|
|
export const MINUTE = SECOND * 60
|
|
export const HOUR = MINUTE * 60
|
|
export const DAY = HOUR * 24
|
|
|
|
export const platform = window.GM_xmlhttpRequest ? 'userscript' : 'crx'
|