2017-09-04 21:21:47 +02:00
|
|
|
import * as WebTorrent from 'webtorrent'
|
|
|
|
|
|
|
|
let webtorrent = new WebTorrent()
|
|
|
|
|
2017-12-28 15:25:31 +01:00
|
|
|
function immutableAssign <T, U> (target: T, source: U) {
|
|
|
|
return Object.assign<{}, T, U>({}, target, source)
|
|
|
|
}
|
|
|
|
|
2017-12-29 11:51:55 +01:00
|
|
|
// Default interval -> 5 minutes
|
|
|
|
function dateIsValid (dateString: string, interval = 300000) {
|
2017-09-04 21:21:47 +02:00
|
|
|
const dateToCheck = new Date(dateString)
|
|
|
|
const now = new Date()
|
|
|
|
|
|
|
|
return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait (milliseconds: number) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds))
|
|
|
|
}
|
|
|
|
|
|
|
|
function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
|
|
|
|
if (refreshWebTorrent === true) webtorrent = new WebTorrent()
|
|
|
|
|
|
|
|
return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
dateIsValid,
|
|
|
|
wait,
|
2017-12-28 15:25:31 +01:00
|
|
|
webtorrentAdd,
|
|
|
|
immutableAssign
|
2017-09-04 21:21:47 +02:00
|
|
|
}
|