-
Notifications
You must be signed in to change notification settings - Fork 0
/
withLogs.ts
47 lines (42 loc) · 1.12 KB
/
withLogs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { MetaData } from '@productive-codebases/toolbox'
import { AsyncProcess } from '..'
interface IWithLogsMetadata {
[identifier: string]: {
initialized: boolean
}
}
/**
* Log states of an async process.
*/
export function withLogs<TIdentifier extends string>(
logger: (message: string, ...args: any[]) => void,
identifier = 'withLogs'
) {
return (
asyncProcess: AsyncProcess<TIdentifier>
): AsyncProcess<TIdentifier> => {
const metadata = asyncProcess.metadata as MetaData<IWithLogsMetadata>
const composerMetadata = metadata.get(identifier)
// avoid register again if already done
if (composerMetadata?.initialized) {
return asyncProcess
}
metadata.set({
withLogs: {
initialized: true
}
})
return AsyncProcess.instance<TIdentifier>(asyncProcess.identifier, [
identifier
])
.onStart(() => {
logger('Start %s', asyncProcess.identifier)
})
.onSuccess(() => {
logger('Success %s', asyncProcess.identifier)
})
.onError(err => {
logger('Error %s: %o', asyncProcess.identifier, err)
})
}
}