diff options
author | Chocobozzz <me@florianbigard.com> | 2023-04-21 14:55:10 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2023-05-09 08:57:34 +0200 |
commit | 0c9668f77901e7540e2c7045eb0f2974a4842a69 (patch) | |
tree | 226d3dd1565b0bb56588897af3b8530e6216e96b /shared/core-utils | |
parent | 6bcb854cdea8688a32240bc5719c7d139806e00b (diff) | |
download | PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.gz PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.tar.zst PeerTube-0c9668f77901e7540e2c7045eb0f2974a4842a69.zip |
Implement remote runner jobs in server
Move ffmpeg functions to @shared
Diffstat (limited to 'shared/core-utils')
-rw-r--r-- | shared/core-utils/common/number.ts | 12 | ||||
-rw-r--r-- | shared/core-utils/common/promises.ts | 47 |
2 files changed, 49 insertions, 10 deletions
diff --git a/shared/core-utils/common/number.ts b/shared/core-utils/common/number.ts index 9a96dcf5c..ce5a6041a 100644 --- a/shared/core-utils/common/number.ts +++ b/shared/core-utils/common/number.ts | |||
@@ -1,7 +1,13 @@ | |||
1 | function forceNumber (value: any) { | 1 | export function forceNumber (value: any) { |
2 | return parseInt(value + '') | 2 | return parseInt(value + '') |
3 | } | 3 | } |
4 | 4 | ||
5 | export { | 5 | export function isOdd (num: number) { |
6 | forceNumber | 6 | return (num % 2) !== 0 |
7 | } | ||
8 | |||
9 | export function toEven (num: number) { | ||
10 | if (isOdd(num)) return num + 1 | ||
11 | |||
12 | return num | ||
7 | } | 13 | } |
diff --git a/shared/core-utils/common/promises.ts b/shared/core-utils/common/promises.ts index f17221b97..e3792d12e 100644 --- a/shared/core-utils/common/promises.ts +++ b/shared/core-utils/common/promises.ts | |||
@@ -1,12 +1,12 @@ | |||
1 | function isPromise <T = unknown> (value: T | Promise<T>): value is Promise<T> { | 1 | export function isPromise <T = unknown> (value: T | Promise<T>): value is Promise<T> { |
2 | return value && typeof (value as Promise<T>).then === 'function' | 2 | return value && typeof (value as Promise<T>).then === 'function' |
3 | } | 3 | } |
4 | 4 | ||
5 | function isCatchable (value: any) { | 5 | export function isCatchable (value: any) { |
6 | return value && typeof value.catch === 'function' | 6 | return value && typeof value.catch === 'function' |
7 | } | 7 | } |
8 | 8 | ||
9 | function timeoutPromise <T> (promise: Promise<T>, timeoutMs: number) { | 9 | export function timeoutPromise <T> (promise: Promise<T>, timeoutMs: number) { |
10 | let timer: ReturnType<typeof setTimeout> | 10 | let timer: ReturnType<typeof setTimeout> |
11 | 11 | ||
12 | return Promise.race([ | 12 | return Promise.race([ |
@@ -18,8 +18,41 @@ function timeoutPromise <T> (promise: Promise<T>, timeoutMs: number) { | |||
18 | ]).finally(() => clearTimeout(timer)) | 18 | ]).finally(() => clearTimeout(timer)) |
19 | } | 19 | } |
20 | 20 | ||
21 | export { | 21 | export function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
22 | isPromise, | 22 | return function promisified (): Promise<A> { |
23 | isCatchable, | 23 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { |
24 | timeoutPromise | 24 | // eslint-disable-next-line no-useless-call |
25 | func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ]) | ||
26 | }) | ||
27 | } | ||
28 | } | ||
29 | |||
30 | // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2 | ||
31 | export function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> { | ||
32 | return function promisified (arg: T): Promise<A> { | ||
33 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | ||
34 | // eslint-disable-next-line no-useless-call | ||
35 | func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | ||
36 | }) | ||
37 | } | ||
38 | } | ||
39 | |||
40 | // eslint-disable-next-line max-len | ||
41 | export function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> { | ||
42 | return function promisified (arg1: T, arg2: U): Promise<A> { | ||
43 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | ||
44 | // eslint-disable-next-line no-useless-call | ||
45 | func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | ||
46 | }) | ||
47 | } | ||
48 | } | ||
49 | |||
50 | // eslint-disable-next-line max-len | ||
51 | export function promisify3<T, U, V, A> (func: (arg1: T, arg2: U, arg3: V, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U, arg3: V) => Promise<A> { | ||
52 | return function promisified (arg1: T, arg2: U, arg3: V): Promise<A> { | ||
53 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | ||
54 | // eslint-disable-next-line no-useless-call | ||
55 | func.apply(null, [ arg1, arg2, arg3, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | ||
56 | }) | ||
57 | } | ||
25 | } | 58 | } |