UNPKG

1.79 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.createDispatcher = void 0;
11const api_1 = require("./api");
12const exception_1 = require("./exception");
13/**
14 * OnReady a dispatcher that can dispatch to a sub job, depending on conditions.
15 * @param options
16 */
17function createDispatcher(options = {}) {
18 let defaultDelegate = null;
19 const conditionalDelegateList = [];
20 const job = Object.assign((argument, context) => {
21 const maybeDelegate = conditionalDelegateList.find(([predicate]) => predicate(argument));
22 let delegate = null;
23 if (maybeDelegate) {
24 delegate = context.scheduler.schedule(maybeDelegate[1], argument);
25 }
26 else if (defaultDelegate) {
27 delegate = context.scheduler.schedule(defaultDelegate, argument);
28 }
29 else {
30 throw new exception_1.JobDoesNotExistException('<null>');
31 }
32 context.inboundBus.subscribe(delegate.inboundBus);
33 return delegate.outboundBus;
34 }, {
35 jobDescription: options,
36 });
37 return Object.assign(job, {
38 setDefaultJob(name) {
39 if ((0, api_1.isJobHandler)(name)) {
40 name = name.jobDescription.name === undefined ? null : name.jobDescription.name;
41 }
42 defaultDelegate = name;
43 },
44 addConditionalJob(predicate, name) {
45 conditionalDelegateList.push([predicate, name]);
46 },
47 // TODO: Remove return-only generic from createDispatcher() API.
48 });
49}
50exports.createDispatcher = createDispatcher;