1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
14 |
|
15 | module.exports = (() => {
|
16 | let receiver;
|
17 |
|
18 | function start(options, taskReceivedCallback, receiverStartedCallback) {
|
19 | if (!taskReceivedCallback || !receiverStartedCallback) {
|
20 | const missingArgumentsError = new Error('Missing Arguments');
|
21 | missingArgumentsError.description = 'Cannot start Task Receiver - missing arguments';
|
22 | missingArgumentsError.debug = 'Missing arguments - initializing a task receiver requires a taskReceived callback '
|
23 | + 'function and a receiver started callback function.';
|
24 | throw missingArgumentsError;
|
25 | }
|
26 |
|
27 | if (typeof taskReceivedCallback !== 'function' || typeof receiverStartedCallback !== 'function') {
|
28 | const invalidArgumentTypeError = new Error('Invalid Argument Type');
|
29 | invalidArgumentTypeError.description = 'Cannot start Task Receiver - invalid arguments';
|
30 | invalidArgumentTypeError.debug = 'Invalid arguments - taskReceivedCallback and receiverStartedCallback '
|
31 | + 'must be functions.';
|
32 | throw invalidArgumentTypeError;
|
33 | }
|
34 |
|
35 | if (options && options.type === 'http') {
|
36 | receiver = require('./receivers/httpReceiver');
|
37 | } else {
|
38 | receiver = require('./receivers/tcpReceiver');
|
39 | }
|
40 | receiver.startServer(taskReceivedCallback, receiverStartedCallback, options);
|
41 | }
|
42 |
|
43 | function stop() {
|
44 | receiver.stop();
|
45 | }
|
46 |
|
47 | function _getApp() {
|
48 | return receiver._app || null;
|
49 | }
|
50 |
|
51 | return {
|
52 | start,
|
53 | stop,
|
54 | _getApp
|
55 | };
|
56 | })();
|