1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.SimpleJobRegistry = void 0;
|
11 | const rxjs_1 = require("rxjs");
|
12 | const json_1 = require("../../json");
|
13 | const api_1 = require("./api");
|
14 | const exception_1 = require("./exception");
|
15 |
|
16 |
|
17 |
|
18 | class SimpleJobRegistry {
|
19 | constructor() {
|
20 | this._jobNames = new Map();
|
21 | }
|
22 | get(name) {
|
23 | return rxjs_1.of(this._jobNames.get(name) || null);
|
24 | }
|
25 | register(nameOrHandler, handlerOrOptions = {}, options = {}) {
|
26 |
|
27 | if (typeof nameOrHandler == 'string') {
|
28 | if (!api_1.isJobHandler(handlerOrOptions)) {
|
29 |
|
30 | throw new TypeError('Expected a JobHandler as second argument.');
|
31 | }
|
32 | this._register(nameOrHandler, handlerOrOptions, options);
|
33 | }
|
34 | else if (api_1.isJobHandler(nameOrHandler)) {
|
35 | if (typeof handlerOrOptions !== 'object') {
|
36 |
|
37 | throw new TypeError('Expected an object options as second argument.');
|
38 | }
|
39 | const name = options.name || nameOrHandler.jobDescription.name || handlerOrOptions.name;
|
40 | if (name === undefined) {
|
41 | throw new TypeError('Expected name to be a string.');
|
42 | }
|
43 | this._register(name, nameOrHandler, options);
|
44 | }
|
45 | else {
|
46 | throw new TypeError('Unrecognized arguments.');
|
47 | }
|
48 | }
|
49 | _register(name, handler, options) {
|
50 | if (this._jobNames.has(name)) {
|
51 |
|
52 | throw new exception_1.JobNameAlreadyRegisteredException(name);
|
53 | }
|
54 |
|
55 | const argument = json_1.schema.mergeSchemas(handler.jobDescription.argument, options.argument);
|
56 | const input = json_1.schema.mergeSchemas(handler.jobDescription.input, options.input);
|
57 | const output = json_1.schema.mergeSchemas(handler.jobDescription.output, options.output);
|
58 |
|
59 | const jobDescription = {
|
60 | name,
|
61 | argument,
|
62 | output,
|
63 | input,
|
64 | };
|
65 | const jobHandler = Object.assign(handler.bind(undefined), {
|
66 | jobDescription,
|
67 | });
|
68 | this._jobNames.set(name, jobHandler);
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | getJobNames() {
|
74 | return [...this._jobNames.keys()];
|
75 | }
|
76 | }
|
77 | exports.SimpleJobRegistry = SimpleJobRegistry;
|