UNPKG

4.32 kBJavaScriptView Raw
1"use strict";
2
3var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8
9function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
10
11// Generated by CoffeeScript 2.4.1
12(function () {
13 var ENABLE_EVENTS,
14 EventEmitter,
15 NonRetryable,
16 Processor,
17 Promise,
18 _,
19 uuid,
20 boundMethodCheck = function boundMethodCheck(instance, Constructor) {
21 if (!(instance instanceof Constructor)) {
22 throw new Error('Bound instance method accessed before binding');
23 }
24 };
25
26 _ = require("lodash");
27
28 NonRetryable = require("./exceptions/non.retryable");
29
30 EventEmitter = require("events");
31
32 Promise = require("bluebird");
33
34 uuid = require("uuid/v4");
35
36 ENABLE_EVENTS = process.env.ENABLE_EVENTS !== "false";
37
38 module.exports = Processor = function (_EventEmitter) {
39 _inherits(Processor, _EventEmitter);
40
41 function Processor(_ref) {
42 var source = _ref.source,
43 runner = _ref.runner,
44 timeout = _ref.timeout;
45
46 _classCallCheck(this, Processor);
47
48 var _this = _possibleConstructorReturn(this, (Processor.__proto__ || Object.getPrototypeOf(Processor)).call(this));
49
50 _this.process = _this.process.bind(_this);
51 _this._emitEvent = _this._emitEvent.bind(_this);
52 _this.source = source;
53 _this.runner = runner;
54 _this.timeout = timeout;
55 return _this;
56 }
57
58 _createClass(Processor, [{
59 key: "process",
60 value: function process(context, raw) {
61 var _this2 = this;
62
63 var $promise, id, notification;
64 boundMethodCheck(this, Processor);
65 id = uuid();
66 notification = this.source.newNotification({
67 context: context,
68 id: id,
69 message: raw
70 });
71 this._emitEvent("started", { context: context, id: id, notification: notification });
72 $promise = Promise.method(this.runner)(notification, context);
73 if (this.timeout != null) {
74 $promise = $promise.timeout(this.timeout, "processor timeout");
75 }
76 $promise.tap(function () {
77 return _this2._emitEvent("successful", { context: context, id: id, notification: notification });
78 }).catch(NonRetryable, function (error) {
79 return _this2._emitEvent("unsuccessful_non_retryable", { context: context, id: id, notification: notification, error: error });
80 }).tapCatch(function (error) {
81 return _this2._emitEvent("unsuccessful", { context: context, id: id, notification: notification, error: error });
82 }).finally(function () {
83 return _this2._emitEvent("finished", { context: context, id: id, notification: notification });
84 }).asCallback(context.done);
85 }
86 }, {
87 key: "_emitEvent",
88 value: function _emitEvent(eventName, value) {
89 boundMethodCheck(this, Processor);
90 if (ENABLE_EVENTS) {
91 return this.emit(eventName, value);
92 }
93 }
94 }]);
95
96 return Processor;
97 }(EventEmitter);
98}).call(undefined);
\No newline at end of file