UNPKG

2.87 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.SimpleJobRegistry = void 0;
11const rxjs_1 = require("rxjs");
12const json_1 = require("../../json");
13const api_1 = require("./api");
14const exception_1 = require("./exception");
15/**
16 * A simple job registry that keep a map of JobName => JobHandler internally.
17 */
18class 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 // Switch on the arguments.
27 if (typeof nameOrHandler == 'string') {
28 if (!api_1.isJobHandler(handlerOrOptions)) {
29 // This is an error.
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 // This is an error.
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 // We shouldn't allow conflicts.
52 throw new exception_1.JobNameAlreadyRegisteredException(name);
53 }
54 // Merge all fields with the ones in the handler (to make sure we respect the handler).
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 // Create the job description.
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 * Returns the job names of all jobs.
72 */
73 getJobNames() {
74 return [...this._jobNames.keys()];
75 }
76}
77exports.SimpleJobRegistry = SimpleJobRegistry;