1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.createDispatcher = void 0;
|
11 | const api_1 = require("./api");
|
12 | const exception_1 = require("./exception");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function 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 |
|
48 | });
|
49 | }
|
50 | exports.createDispatcher = createDispatcher;
|