nmtjs 0.16.1 → 0.17.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +41 -1
  3. package/dist/index.d.ts +22 -161
  4. package/dist/index.js +24 -53
  5. package/dist/index.js.map +1 -1
  6. package/package.json +11 -63
  7. package/src/index.ts +71 -91
  8. package/bin/cli.js +0 -2
  9. package/dist/cli.d.ts +0 -1
  10. package/dist/cli.js +0 -137
  11. package/dist/cli.js.map +0 -1
  12. package/dist/codegen.d.ts +0 -5
  13. package/dist/codegen.js +0 -18
  14. package/dist/codegen.js.map +0 -1
  15. package/dist/config.d.ts +0 -47
  16. package/dist/config.js +0 -16
  17. package/dist/config.js.map +0 -1
  18. package/dist/entrypoints/cli.d.ts +0 -1
  19. package/dist/entrypoints/cli.js +0 -12
  20. package/dist/entrypoints/cli.js.map +0 -1
  21. package/dist/entrypoints/main.d.ts +0 -5
  22. package/dist/entrypoints/main.js +0 -248
  23. package/dist/entrypoints/main.js.map +0 -1
  24. package/dist/entrypoints/thread.d.ts +0 -16
  25. package/dist/entrypoints/thread.js +0 -182
  26. package/dist/entrypoints/thread.js.map +0 -1
  27. package/dist/entrypoints/worker.d.ts +0 -9
  28. package/dist/entrypoints/worker.js +0 -143
  29. package/dist/entrypoints/worker.js.map +0 -1
  30. package/dist/resolver.d.ts +0 -2
  31. package/dist/resolver.js +0 -7
  32. package/dist/resolver.js.map +0 -1
  33. package/dist/runtime/application/index.d.ts +0 -1
  34. package/dist/runtime/application/index.js +0 -2
  35. package/dist/runtime/application/index.js.map +0 -1
  36. package/dist/runtime/constants.d.ts +0 -8
  37. package/dist/runtime/constants.js +0 -5
  38. package/dist/runtime/constants.js.map +0 -1
  39. package/dist/runtime/enums.d.ts +0 -10
  40. package/dist/runtime/enums.js +0 -13
  41. package/dist/runtime/enums.js.map +0 -1
  42. package/dist/runtime/hooks.d.ts +0 -1
  43. package/dist/runtime/hooks.js +0 -2
  44. package/dist/runtime/hooks.js.map +0 -1
  45. package/dist/runtime/index.d.ts +0 -28
  46. package/dist/runtime/index.js +0 -29
  47. package/dist/runtime/index.js.map +0 -1
  48. package/dist/runtime/injectables.d.ts +0 -69
  49. package/dist/runtime/injectables.js +0 -40
  50. package/dist/runtime/injectables.js.map +0 -1
  51. package/dist/runtime/jobs/job.d.ts +0 -146
  52. package/dist/runtime/jobs/job.js +0 -89
  53. package/dist/runtime/jobs/job.js.map +0 -1
  54. package/dist/runtime/jobs/manager.d.ts +0 -131
  55. package/dist/runtime/jobs/manager.js +0 -309
  56. package/dist/runtime/jobs/manager.js.map +0 -1
  57. package/dist/runtime/jobs/router.d.ts +0 -296
  58. package/dist/runtime/jobs/router.js +0 -458
  59. package/dist/runtime/jobs/router.js.map +0 -1
  60. package/dist/runtime/jobs/runner.d.ts +0 -103
  61. package/dist/runtime/jobs/runner.js +0 -425
  62. package/dist/runtime/jobs/runner.js.map +0 -1
  63. package/dist/runtime/jobs/step.d.ts +0 -26
  64. package/dist/runtime/jobs/step.js +0 -18
  65. package/dist/runtime/jobs/step.js.map +0 -1
  66. package/dist/runtime/jobs/types.d.ts +0 -86
  67. package/dist/runtime/jobs/types.js +0 -5
  68. package/dist/runtime/jobs/types.js.map +0 -1
  69. package/dist/runtime/metrics/metric.d.ts +0 -8
  70. package/dist/runtime/metrics/metric.js +0 -11
  71. package/dist/runtime/metrics/metric.js.map +0 -1
  72. package/dist/runtime/metrics/registry.d.ts +0 -3
  73. package/dist/runtime/metrics/registry.js +0 -5
  74. package/dist/runtime/metrics/registry.js.map +0 -1
  75. package/dist/runtime/metrics/server.d.ts +0 -7
  76. package/dist/runtime/metrics/server.js +0 -53
  77. package/dist/runtime/metrics/server.js.map +0 -1
  78. package/dist/runtime/plugin.d.ts +0 -2
  79. package/dist/runtime/plugin.js +0 -2
  80. package/dist/runtime/plugin.js.map +0 -1
  81. package/dist/runtime/runtime.d.ts +0 -27
  82. package/dist/runtime/runtime.js +0 -77
  83. package/dist/runtime/runtime.js.map +0 -1
  84. package/dist/runtime/scheduler/index.d.ts +0 -22
  85. package/dist/runtime/scheduler/index.js +0 -20
  86. package/dist/runtime/scheduler/index.js.map +0 -1
  87. package/dist/runtime/server/applications.d.ts +0 -91
  88. package/dist/runtime/server/applications.js +0 -171
  89. package/dist/runtime/server/applications.js.map +0 -1
  90. package/dist/runtime/server/config.d.ts +0 -148
  91. package/dist/runtime/server/config.js +0 -33
  92. package/dist/runtime/server/config.js.map +0 -1
  93. package/dist/runtime/server/environment.d.ts +0 -43
  94. package/dist/runtime/server/environment.js +0 -33
  95. package/dist/runtime/server/environment.js.map +0 -1
  96. package/dist/runtime/server/error-policy.d.ts +0 -62
  97. package/dist/runtime/server/error-policy.js +0 -73
  98. package/dist/runtime/server/error-policy.js.map +0 -1
  99. package/dist/runtime/server/hmr-coordinator.d.ts +0 -37
  100. package/dist/runtime/server/hmr-coordinator.js +0 -94
  101. package/dist/runtime/server/hmr-coordinator.js.map +0 -1
  102. package/dist/runtime/server/jobs.d.ts +0 -54
  103. package/dist/runtime/server/jobs.js +0 -176
  104. package/dist/runtime/server/jobs.js.map +0 -1
  105. package/dist/runtime/server/lifecycle.d.ts +0 -85
  106. package/dist/runtime/server/lifecycle.js +0 -177
  107. package/dist/runtime/server/lifecycle.js.map +0 -1
  108. package/dist/runtime/server/managed-worker.d.ts +0 -158
  109. package/dist/runtime/server/managed-worker.js +0 -420
  110. package/dist/runtime/server/managed-worker.js.map +0 -1
  111. package/dist/runtime/server/pool-manager.d.ts +0 -85
  112. package/dist/runtime/server/pool-manager.js +0 -129
  113. package/dist/runtime/server/pool-manager.js.map +0 -1
  114. package/dist/runtime/server/proxy.d.ts +0 -21
  115. package/dist/runtime/server/proxy.js +0 -76
  116. package/dist/runtime/server/proxy.js.map +0 -1
  117. package/dist/runtime/server/server.d.ts +0 -60
  118. package/dist/runtime/server/server.js +0 -163
  119. package/dist/runtime/server/server.js.map +0 -1
  120. package/dist/runtime/server/types.d.ts +0 -31
  121. package/dist/runtime/server/types.js +0 -2
  122. package/dist/runtime/server/types.js.map +0 -1
  123. package/dist/runtime/server/worker-pool.d.ts +0 -121
  124. package/dist/runtime/server/worker-pool.js +0 -171
  125. package/dist/runtime/server/worker-pool.js.map +0 -1
  126. package/dist/runtime/store/index.d.ts +0 -3
  127. package/dist/runtime/store/index.js +0 -23
  128. package/dist/runtime/store/index.js.map +0 -1
  129. package/dist/runtime/subscription/manager.d.ts +0 -49
  130. package/dist/runtime/subscription/manager.js +0 -170
  131. package/dist/runtime/subscription/manager.js.map +0 -1
  132. package/dist/runtime/subscription/redis.d.ts +0 -18
  133. package/dist/runtime/subscription/redis.js +0 -119
  134. package/dist/runtime/subscription/redis.js.map +0 -1
  135. package/dist/runtime/types.d.ts +0 -95
  136. package/dist/runtime/types.js +0 -2
  137. package/dist/runtime/types.js.map +0 -1
  138. package/dist/runtime/workers/application.d.ts +0 -29
  139. package/dist/runtime/workers/application.js +0 -77
  140. package/dist/runtime/workers/application.js.map +0 -1
  141. package/dist/runtime/workers/base.d.ts +0 -14
  142. package/dist/runtime/workers/base.js +0 -41
  143. package/dist/runtime/workers/base.js.map +0 -1
  144. package/dist/runtime/workers/cli.d.ts +0 -1
  145. package/dist/runtime/workers/cli.js +0 -2
  146. package/dist/runtime/workers/cli.js.map +0 -1
  147. package/dist/runtime/workers/job.d.ts +0 -20
  148. package/dist/runtime/workers/job.js +0 -170
  149. package/dist/runtime/workers/job.js.map +0 -1
  150. package/dist/vite/builder.d.ts +0 -5
  151. package/dist/vite/builder.js +0 -79
  152. package/dist/vite/builder.js.map +0 -1
  153. package/dist/vite/config.d.ts +0 -28
  154. package/dist/vite/config.js +0 -22
  155. package/dist/vite/config.js.map +0 -1
  156. package/dist/vite/plugins.d.ts +0 -2
  157. package/dist/vite/plugins.js +0 -43
  158. package/dist/vite/plugins.js.map +0 -1
  159. package/dist/vite/runners/worker.d.ts +0 -3
  160. package/dist/vite/runners/worker.js +0 -34
  161. package/dist/vite/runners/worker.js.map +0 -1
  162. package/dist/vite/server.d.ts +0 -3
  163. package/dist/vite/server.js +0 -36
  164. package/dist/vite/server.js.map +0 -1
  165. package/dist/vite/servers/main.d.ts +0 -8
  166. package/dist/vite/servers/main.js +0 -24
  167. package/dist/vite/servers/main.js.map +0 -1
  168. package/dist/vite/servers/worker.d.ts +0 -13
  169. package/dist/vite/servers/worker.js +0 -124
  170. package/dist/vite/servers/worker.js.map +0 -1
  171. package/src/cli.ts +0 -162
  172. package/src/codegen.ts +0 -30
  173. package/src/config.ts +0 -69
  174. package/src/entrypoints/cli.ts +0 -13
  175. package/src/entrypoints/main.ts +0 -317
  176. package/src/entrypoints/thread.ts +0 -223
  177. package/src/entrypoints/worker.ts +0 -174
  178. package/src/resolver.ts +0 -7
  179. package/src/runtime/application/index.ts +0 -1
  180. package/src/runtime/constants.ts +0 -13
  181. package/src/runtime/enums.ts +0 -12
  182. package/src/runtime/hooks.ts +0 -1
  183. package/src/runtime/index.ts +0 -29
  184. package/src/runtime/injectables.ts +0 -101
  185. package/src/runtime/jobs/job.ts +0 -467
  186. package/src/runtime/jobs/manager.ts +0 -454
  187. package/src/runtime/jobs/router.ts +0 -967
  188. package/src/runtime/jobs/runner.ts +0 -583
  189. package/src/runtime/jobs/step.ts +0 -66
  190. package/src/runtime/jobs/types.ts +0 -116
  191. package/src/runtime/metrics/metric.ts +0 -18
  192. package/src/runtime/metrics/registry.ts +0 -11
  193. package/src/runtime/metrics/server.ts +0 -69
  194. package/src/runtime/plugin.ts +0 -2
  195. package/src/runtime/runtime.ts +0 -103
  196. package/src/runtime/scheduler/index.ts +0 -39
  197. package/src/runtime/server/applications.ts +0 -264
  198. package/src/runtime/server/config.ts +0 -199
  199. package/src/runtime/server/environment.ts +0 -71
  200. package/src/runtime/server/error-policy.ts +0 -132
  201. package/src/runtime/server/hmr-coordinator.ts +0 -116
  202. package/src/runtime/server/jobs.ts +0 -266
  203. package/src/runtime/server/lifecycle.ts +0 -234
  204. package/src/runtime/server/managed-worker.ts +0 -546
  205. package/src/runtime/server/pool-manager.ts +0 -187
  206. package/src/runtime/server/proxy.ts +0 -119
  207. package/src/runtime/server/server.ts +0 -218
  208. package/src/runtime/server/types.ts +0 -32
  209. package/src/runtime/server/worker-pool.ts +0 -275
  210. package/src/runtime/store/index.ts +0 -30
  211. package/src/runtime/subscription/manager.ts +0 -307
  212. package/src/runtime/subscription/redis.ts +0 -159
  213. package/src/runtime/types.ts +0 -84
  214. package/src/runtime/workers/application.ts +0 -133
  215. package/src/runtime/workers/base.ts +0 -65
  216. package/src/runtime/workers/cli.ts +0 -0
  217. package/src/runtime/workers/job.ts +0 -142
  218. package/src/vite/builder.ts +0 -94
  219. package/src/vite/config.ts +0 -45
  220. package/src/vite/plugins.ts +0 -53
  221. package/src/vite/runners/worker.ts +0 -55
  222. package/src/vite/server.ts +0 -55
  223. package/src/vite/servers/main.ts +0 -41
  224. package/src/vite/servers/worker.ts +0 -177
@@ -1 +0,0 @@
1
- export * from '@nmtjs/application'
@@ -1,13 +0,0 @@
1
- export const kServerConfig: unique symbol = Symbol.for(
2
- 'neemata:ServerConfigKey',
3
- )
4
- export type kServerConfig = typeof kServerConfig
5
-
6
- export const kCommand: unique symbol = Symbol.for('neemata:CommandKey')
7
- export type kCommand = typeof kCommand
8
-
9
- export const kJobKey: unique symbol = Symbol.for('neemata:JobKey')
10
- export type kJobKey = typeof kJobKey
11
-
12
- export const kJobStepKey: unique symbol = Symbol.for('neemata:JobStepKey')
13
- export type kJobStepKey = typeof kJobStepKey
@@ -1,12 +0,0 @@
1
- export enum StoreType {
2
- Redis = 'Redis',
3
- Valkey = 'Valkey',
4
- }
5
-
6
- export enum WorkerType {
7
- Application = 'Application',
8
- Job = 'Job',
9
- Command = 'Command',
10
- }
11
-
12
- export { LifecycleHook } from '@nmtjs/application'
@@ -1 +0,0 @@
1
- export { LifecycleHooks } from '@nmtjs/application'
@@ -1,29 +0,0 @@
1
- export * from '@nmtjs/application'
2
-
3
- export * from './constants.ts'
4
- export * from './enums.ts'
5
- export * from './injectables.ts'
6
- export * from './jobs/job.ts'
7
- export * from './jobs/manager.ts'
8
- export * from './jobs/router.ts'
9
- export * from './jobs/step.ts'
10
- export * from './jobs/types.ts'
11
- export * from './runtime.ts'
12
- export * from './scheduler/index.ts'
13
- export * from './server/config.ts'
14
- export * from './server/environment.ts'
15
- export * from './server/error-policy.ts'
16
- export * from './server/hmr-coordinator.ts'
17
- export * from './server/lifecycle.ts'
18
- export * from './server/managed-worker.ts'
19
- export * from './server/pool-manager.ts'
20
- export * from './server/server.ts'
21
- export * from './server/types.ts'
22
- export * from './server/worker-pool.ts'
23
- export * from './store/index.ts'
24
- export * from './subscription/manager.ts'
25
- export * from './subscription/redis.ts'
26
- export * from './types.ts'
27
- export * from './workers/application.ts'
28
- export * from './workers/base.ts'
29
- export * from './workers/job.ts'
@@ -1,101 +0,0 @@
1
- import {
2
- CoreInjectables,
3
- createFactoryInjectable,
4
- createLazyInjectable,
5
- Scope,
6
- } from '@nmtjs/core'
7
-
8
- import type { WorkerType } from './enums.ts'
9
- import type { JobManagerInstance } from './jobs/manager.ts'
10
- import type { JobExecutionContext, SaveJobProgress } from './jobs/types.ts'
11
- import type { ServerStoreConfig } from './server/config.ts'
12
- import type {
13
- PublishFn,
14
- SubscribeFn,
15
- SubscriptionAdapterType,
16
- } from './subscription/manager.ts'
17
- import { SubscriptionManager } from './subscription/manager.ts'
18
-
19
- export const subscriptionAdapter =
20
- createLazyInjectable<SubscriptionAdapterType>(
21
- Scope.Global,
22
- 'SubscriptionAdapter',
23
- )
24
-
25
- export const subscriptionManager = createFactoryInjectable(
26
- {
27
- dependencies: {
28
- adapter: subscriptionAdapter,
29
- logger: CoreInjectables.logger,
30
- },
31
- factory: ({ adapter, logger }) =>
32
- new SubscriptionManager({ logger, adapter }),
33
- dispose: (manager) => manager.dispose(),
34
- },
35
- 'SubscriptionManager',
36
- )
37
-
38
- export const publish = createFactoryInjectable(
39
- {
40
- dependencies: { manager: subscriptionManager },
41
- factory: ({ manager }): PublishFn => manager.publish.bind(manager),
42
- },
43
- 'Publish',
44
- )
45
-
46
- export const subscribe = createFactoryInjectable(
47
- {
48
- dependencies: { manager: subscriptionManager },
49
- factory: ({ manager }): SubscribeFn => manager.subscribe.bind(manager),
50
- },
51
- 'Subscribe',
52
- )
53
-
54
- export const jobManager = createLazyInjectable<JobManagerInstance>(
55
- Scope.Global,
56
- 'JobManager',
57
- )
58
-
59
- export const storeConfig = createLazyInjectable<ServerStoreConfig>(
60
- Scope.Global,
61
- 'StoreConfig',
62
- )
63
-
64
- export const workerType = createLazyInjectable<WorkerType>(
65
- Scope.Global,
66
- 'WorkerType',
67
- )
68
-
69
- export const jobWorkerPool = createLazyInjectable<string>(
70
- Scope.Global,
71
- 'JobWorkerPool',
72
- )
73
-
74
- export const jobAbortSignal = createLazyInjectable<AbortSignal>(
75
- Scope.Global,
76
- 'JobAbortSignal',
77
- )
78
-
79
- export const saveJobProgress = createLazyInjectable<SaveJobProgress>(
80
- Scope.Global,
81
- 'SaveJobProgress',
82
- )
83
-
84
- export const currentJobInfo = createLazyInjectable<JobExecutionContext>(
85
- Scope.Global,
86
- 'CurrentJobInfo',
87
- )
88
-
89
- export const RuntimeInjectables = {
90
- subscriptionAdapter,
91
- subscriptionManager,
92
- publish,
93
- subscribe,
94
- jobManager,
95
- storeConfig,
96
- workerType,
97
- jobWorkerPool,
98
- jobAbortSignal,
99
- saveJobProgress,
100
- currentJobInfo,
101
- }
@@ -1,467 +0,0 @@
1
- import type { MaybePromise, TSError } from '@nmtjs/common'
2
- import type { Dependencies, DependencyContext } from '@nmtjs/core'
3
- import type { t } from '@nmtjs/type'
4
- import type { AnyObjectLikeType, ObjectType } from '@nmtjs/type/object'
5
- import { tryCaptureStackTrace } from '@nmtjs/common'
6
-
7
- import type { AnyJobStep, JobStep } from './step.ts'
8
- import { kJobKey } from '../constants.ts'
9
- import { isJobStep } from './step.ts'
10
-
11
- type DefaultObjectType = ObjectType<{}>
12
-
13
- type DefaultResultType = Record<string, any>
14
-
15
- type UnionToIntersection<T> = (
16
- T extends unknown
17
- ? (value: T) => void
18
- : never
19
- ) extends (value: infer R) => void
20
- ? R
21
- : never
22
-
23
- type StepOutput<Step extends AnyJobStep> =
24
- Step extends JobStep<any, infer Output, any, any, any>
25
- ? t.infer.decode.output<
26
- Output extends undefined ? DefaultObjectType : Output
27
- >
28
- : {}
29
-
30
- type GroupOutput<Steps extends readonly AnyJobStep[]> = UnionToIntersection<
31
- StepOutput<Steps[number]>
32
- >
33
-
34
- type EnsureStepInputSatisfied<
35
- CurrentResult,
36
- StepInput extends AnyObjectLikeType,
37
- > =
38
- CurrentResult extends t.infer.decode.output<StepInput>
39
- ? unknown
40
- : TSError<
41
- 'Accumulated job result does not satisfy current step input:',
42
- CurrentResult
43
- >
44
-
45
- type EnsureStepDataSatisfied<CurrentData, StepData> =
46
- CurrentData extends StepData
47
- ? unknown
48
- : TSError<'Job data does not satisfy step data type:', CurrentData>
49
-
50
- type ValidatedParallelStep<
51
- Step extends AnyJobStep,
52
- CurrentResult,
53
- CurrentData,
54
- > =
55
- Step extends JobStep<infer StepInput, any, any, any, infer StepData>
56
- ? Step &
57
- EnsureStepInputSatisfied<CurrentResult, StepInput> &
58
- EnsureStepDataSatisfied<CurrentData, StepData>
59
- : never
60
-
61
- type ValidatedParallelSteps<
62
- Steps extends readonly AnyJobStep[],
63
- CurrentResult,
64
- CurrentData,
65
- > = {
66
- [K in keyof Steps]: ValidatedParallelStep<
67
- Steps[K],
68
- CurrentResult,
69
- CurrentData
70
- >
71
- }
72
-
73
- export type AnyJobOptions = JobOptions<string, any, any, any, any, any>
74
-
75
- export interface AnyJob {
76
- _: { data: any; result: any; input: any; output: any; progress: any }
77
- [kJobKey]: true
78
- options: AnyJobOptions
79
- jobSteps: readonly AnyJobStep[]
80
- parallelGroups: Map<number, number>
81
- parallelGroupByStepIndex: Map<number, number>
82
- conditions: Map<number, JobCondition<any, any, any, any>>
83
- name: string
84
- dependencies: Dependencies
85
- input: AnyObjectLikeType
86
- output: AnyObjectLikeType
87
- progress: AnyObjectLikeType
88
- afterEachHandler?: JobAfterEachHandler<any, any, any, any, any>
89
- beforeEachHandler?: JobBeforeEachHandler<any, any, any, any, any>
90
- onErrorHandler?: JobOnErrorHandler<any, any, any, any, any>
91
- returnHandler?: JobReturnHandler<any, any, any, any, any, any>
92
- }
93
-
94
- export type JobBackoffOptions = {
95
- type: 'fixed' | 'exponential'
96
- delay: number
97
- jitter?: number
98
- }
99
-
100
- export type JobCondition<
101
- Deps extends Dependencies = {},
102
- Result extends DefaultResultType = {},
103
- Data = any,
104
- Input extends AnyObjectLikeType = AnyObjectLikeType,
105
- Progress extends AnyObjectLikeType = AnyObjectLikeType,
106
- > = (params: {
107
- context: DependencyContext<Deps>
108
- data: Data
109
- input: t.infer.decode.output<Input>
110
- result: Result
111
- progress: t.infer.decode.output<Progress>
112
- }) => MaybePromise<boolean>
113
-
114
- export type JobReturnHandler<
115
- Deps extends Dependencies,
116
- Result extends DefaultResultType,
117
- Input extends AnyObjectLikeType,
118
- Output extends AnyObjectLikeType,
119
- Data,
120
- Progress extends AnyObjectLikeType = AnyObjectLikeType,
121
- > = (params: {
122
- context: DependencyContext<Deps>
123
- data: Data
124
- input: t.infer.decode.output<Input>
125
- result: Result
126
- progress: t.infer.decode.output<Progress>
127
- }) => MaybePromise<t.infer.encode.input<Output>>
128
-
129
- export type JobDataHandler<
130
- Deps extends Dependencies,
131
- Input extends AnyObjectLikeType,
132
- Data,
133
- Progress extends AnyObjectLikeType = AnyObjectLikeType,
134
- > = (
135
- ctx: DependencyContext<Deps>,
136
- input: t.infer.decode.output<Input>,
137
- progress: t.infer.decode.output<Progress>,
138
- ) => MaybePromise<Data>
139
-
140
- export type JobAfterEachHandler<
141
- Deps extends Dependencies,
142
- Result extends DefaultResultType,
143
- Input extends AnyObjectLikeType,
144
- Data,
145
- Progress extends AnyObjectLikeType = AnyObjectLikeType,
146
- > = (params: {
147
- context: DependencyContext<Deps>
148
- data: Data
149
- input: t.infer.decode.output<Input>
150
- result: Result
151
- progress: t.infer.decode.output<Progress>
152
- step: AnyJobStep
153
- stepIndex: number
154
- }) => MaybePromise<void>
155
-
156
- export type JobBeforeEachHandler<
157
- Deps extends Dependencies,
158
- Result extends DefaultResultType,
159
- Input extends AnyObjectLikeType,
160
- Data,
161
- Progress extends AnyObjectLikeType = AnyObjectLikeType,
162
- > = (params: {
163
- context: DependencyContext<Deps>
164
- data: Data
165
- input: t.infer.decode.output<Input>
166
- result: Result
167
- progress: t.infer.decode.output<Progress>
168
- step: AnyJobStep
169
- stepIndex: number
170
- }) => MaybePromise<void>
171
-
172
- export type JobOnErrorHandler<
173
- Deps extends Dependencies,
174
- Result extends DefaultResultType,
175
- Input extends AnyObjectLikeType,
176
- Data,
177
- Progress extends AnyObjectLikeType = AnyObjectLikeType,
178
- > = (params: {
179
- context: DependencyContext<Deps>
180
- data: Data
181
- input: t.infer.decode.output<Input>
182
- result: Result
183
- progress: t.infer.decode.output<Progress>
184
- step: AnyJobStep
185
- stepIndex: number
186
- error: unknown
187
- }) => MaybePromise<
188
- | boolean
189
- // biome-ignore lint/suspicious/noConfusingVoidType: its ok
190
- | void
191
- >
192
-
193
- export interface JobOptions<
194
- Name extends string = string,
195
- Input extends AnyObjectLikeType = AnyObjectLikeType,
196
- Output extends AnyObjectLikeType = AnyObjectLikeType,
197
- Progress extends AnyObjectLikeType = DefaultObjectType,
198
- Deps extends Dependencies = {},
199
- Data = any,
200
- > {
201
- name: Name
202
- pool: string
203
- input: Input
204
- output: Output
205
- progress?: Progress
206
- concurrency?: number
207
- timeout?: number
208
- dependencies?: Deps
209
- data?: JobDataHandler<Deps, Input, Data, Progress>
210
- attempts?: number
211
- backoff?: JobBackoffOptions
212
- oneoff?: boolean
213
- }
214
-
215
- export class Job<
216
- in out Name extends string = string,
217
- in out Deps extends Dependencies = {},
218
- in out Data = any,
219
- in out Input extends AnyObjectLikeType = DefaultObjectType,
220
- in out Output extends AnyObjectLikeType = DefaultObjectType,
221
- in out Progress extends AnyObjectLikeType = DefaultObjectType,
222
- out JobSteps extends [...AnyJobStep[]] = [],
223
- in out Result extends DefaultResultType = {},
224
- out Return extends boolean = false,
225
- > implements AnyJob
226
- {
227
- _!: {
228
- data: Data
229
- result: Result & t.infer.decode.output<Input>
230
- input: t.infer.decode.output<Input>
231
- output: t.infer.decode.output<Output>
232
- progress: t.infer.decode.output<Progress>
233
- };
234
- [kJobKey] = true as const
235
- protected _jobSteps: JobSteps = [] as unknown as JobSteps
236
- parallelGroups = new Map<number, number>()
237
- parallelGroupByStepIndex = new Map<number, number>()
238
- conditions: Map<number, JobCondition<any, any, any, any, any>> = new Map()
239
- returnHandler?: JobReturnHandler<
240
- Deps,
241
- this['_']['result'],
242
- Input,
243
- Output,
244
- Data,
245
- Progress
246
- >
247
- afterEachHandler?: JobAfterEachHandler<
248
- Deps,
249
- this['_']['result'],
250
- Input,
251
- Data,
252
- Progress
253
- >
254
- beforeEachHandler?: JobBeforeEachHandler<
255
- Deps,
256
- this['_']['result'],
257
- Input,
258
- Data,
259
- Progress
260
- >
261
- onErrorHandler?: JobOnErrorHandler<
262
- Deps,
263
- this['_']['result'],
264
- Input,
265
- Data,
266
- Progress
267
- >
268
-
269
- constructor(
270
- public options: JobOptions<Name, Input, Output, Progress, Deps, Data>,
271
- public stack?: string,
272
- ) {}
273
-
274
- get name(): Name {
275
- return this.options.name
276
- }
277
-
278
- get jobSteps(): JobSteps {
279
- return this._jobSteps
280
- }
281
-
282
- get dependencies(): Deps {
283
- return this.options.dependencies ?? ({} as Deps)
284
- }
285
-
286
- get input(): Input {
287
- return this.options.input as Input
288
- }
289
-
290
- get output(): Output {
291
- return this.options.output as Output
292
- }
293
-
294
- get progress(): Progress {
295
- return this.options.progress as Progress
296
- }
297
-
298
- step<
299
- StepInput extends AnyObjectLikeType,
300
- StepOutput extends AnyObjectLikeType,
301
- StepDeps extends Dependencies,
302
- Condition extends
303
- | JobCondition<Deps, Result, this['_']['data'], Input>
304
- | undefined,
305
- >(
306
- ...[step, condition]: Return extends false
307
- ? [
308
- step: JobStep<
309
- StepInput,
310
- StepOutput,
311
- StepDeps,
312
- any,
313
- this['_']['data']
314
- > &
315
- EnsureStepInputSatisfied<this['_']['result'], StepInput>,
316
- condition?: Condition,
317
- ]
318
- : [TSError<'Job has already has return statement'>]
319
- ) {
320
- if (!isJobStep(step)) throw new Error('Invalid job step object')
321
-
322
- const length = this._jobSteps.push(step)
323
- if (condition) this.conditions.set(length - 1, condition)
324
-
325
- return this as unknown as Job<
326
- Name,
327
- Deps,
328
- Data,
329
- Input,
330
- Output,
331
- Progress,
332
- [...JobSteps, JobStep<StepInput, StepOutput, StepDeps, any, any>],
333
- Result &
334
- (undefined extends Condition
335
- ? t.infer.decode.output<
336
- StepOutput extends undefined ? DefaultObjectType : StepOutput
337
- >
338
- : Partial<
339
- t.infer.decode.output<
340
- StepOutput extends undefined ? DefaultObjectType : StepOutput
341
- >
342
- >)
343
- >
344
- }
345
-
346
- steps<const GroupSteps extends [AnyJobStep, AnyJobStep, ...AnyJobStep[]]>(
347
- ...steps: Return extends false
348
- ? GroupSteps &
349
- ValidatedParallelSteps<
350
- GroupSteps,
351
- this['_']['result'],
352
- this['_']['data']
353
- >
354
- : [TSError<'Job has already has return statement'>]
355
- ) {
356
- const validated: AnyJobStep[] = []
357
- for (const step of steps) {
358
- if (!isJobStep(step)) throw new Error('Invalid job step object')
359
- validated.push(step)
360
- }
361
-
362
- const start = this._jobSteps.length
363
- this._jobSteps.push(...validated)
364
- const end = this._jobSteps.length
365
-
366
- this.parallelGroups.set(start, end)
367
- for (let index = start; index < end; index++) {
368
- this.parallelGroupByStepIndex.set(index, start)
369
- }
370
-
371
- return this as unknown as Job<
372
- Name,
373
- Deps,
374
- Data,
375
- Input,
376
- Output,
377
- Progress,
378
- [...JobSteps, ...GroupSteps],
379
- Result & GroupOutput<GroupSteps>
380
- >
381
- }
382
-
383
- return(
384
- ...[handler]: Return extends false
385
- ? Result extends t.infer.encode.input<Output>
386
- ? [
387
- JobReturnHandler<
388
- Deps,
389
- this['_']['result'],
390
- Input,
391
- Output,
392
- Data,
393
- Progress
394
- >?,
395
- ]
396
- : [
397
- JobReturnHandler<
398
- Deps,
399
- this['_']['result'],
400
- Input,
401
- Output,
402
- Data,
403
- Progress
404
- >,
405
- ]
406
- : [TSError<'Job already has a return statement'>]
407
- ) {
408
- if (typeof handler === 'function') {
409
- this.returnHandler = handler
410
- } else {
411
- this.returnHandler = (result) => result as any
412
- }
413
- return this as unknown as Job<
414
- Name,
415
- Deps,
416
- Data,
417
- Input,
418
- Output,
419
- Progress,
420
- JobSteps,
421
- Result,
422
- true
423
- >
424
- }
425
-
426
- afterEach(
427
- handler: Return extends true
428
- ? JobAfterEachHandler<Deps, this['_']['result'], Input, Data, Progress>
429
- : TSError<'Job must have a return statement to use afterEach'>,
430
- ) {
431
- this.afterEachHandler = handler as any
432
- return this
433
- }
434
-
435
- beforeEach(
436
- handler: Return extends true
437
- ? JobBeforeEachHandler<Deps, this['_']['result'], Input, Data, Progress>
438
- : TSError<'Job must have a return statement to use beforeEach'>,
439
- ) {
440
- this.beforeEachHandler = handler as any
441
- return this
442
- }
443
-
444
- onError(
445
- handler: Return extends true
446
- ? JobOnErrorHandler<Deps, this['_']['result'], Input, Data, Progress>
447
- : TSError<'Job must have a return statement to use onError'>,
448
- ) {
449
- this.onErrorHandler = handler as any
450
- return this
451
- }
452
- }
453
-
454
- export function createJob<
455
- Name extends string,
456
- Input extends AnyObjectLikeType,
457
- Output extends AnyObjectLikeType,
458
- Deps extends Dependencies = {},
459
- Data = any,
460
- Progress extends AnyObjectLikeType = DefaultObjectType,
461
- >(options: JobOptions<Name, Input, Output, Progress, Deps, Data>) {
462
- const stack = tryCaptureStackTrace()
463
- return new Job<Name, Deps, Data, Input, Output, Progress>(
464
- { dependencies: {} as Deps, ...options },
465
- stack,
466
- )
467
- }