UNPKG

4.06 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 Processor,
16 Promise,
17 _,
18 uuid,
19 boundMethodCheck = function boundMethodCheck(instance, Constructor) {
20 if (!(instance instanceof Constructor)) {
21 throw new Error('Bound instance method accessed before binding');
22 }
23 };
24
25 _ = require("lodash");
26
27 EventEmitter = require("events");
28
29 Promise = require("bluebird");
30
31 uuid = require("uuid/v4");
32
33 ENABLE_EVENTS = process.env.ENABLE_EVENTS !== "false";
34
35 module.exports = Processor = function (_EventEmitter) {
36 _inherits(Processor, _EventEmitter);
37
38 function Processor(_ref) {
39 var source = _ref.source,
40 runner = _ref.runner,
41 timeout = _ref.timeout;
42
43 _classCallCheck(this, Processor);
44
45 var _this = _possibleConstructorReturn(this, (Processor.__proto__ || Object.getPrototypeOf(Processor)).call(this));
46
47 _this.process = _this.process.bind(_this);
48 _this._emitEvent = _this._emitEvent.bind(_this);
49 _this.source = source;
50 _this.runner = runner;
51 _this.timeout = timeout;
52 return _this;
53 }
54
55 _createClass(Processor, [{
56 key: "process",
57 value: function process(context, raw) {
58 var _this2 = this;
59
60 var $promise, id, notification;
61 boundMethodCheck(this, Processor);
62 id = uuid();
63 notification = this.source.newNotification({
64 context: context,
65 id: id,
66 message: raw
67 });
68 this._emitEvent("started", { context: context, id: id, notification: notification });
69 $promise = Promise.method(this.runner)(notification, context);
70 if (this.timeout != null) {
71 $promise = $promise.timeout(this.timeout, "processor timeout");
72 }
73 $promise.tap(function () {
74 return _this2._emitEvent("successful", { context: context, id: id, notification: notification });
75 }).tapCatch(function (error) {
76 return _this2._emitEvent("unsuccessful", { context: context, id: id, notification: notification, error: error });
77 }).finally(function () {
78 return _this2._emitEvent("finished", { context: context, id: id, notification: notification });
79 }).asCallback(context.done);
80 }
81 }, {
82 key: "_emitEvent",
83 value: function _emitEvent(eventName, value) {
84 boundMethodCheck(this, Processor);
85 if (ENABLE_EVENTS) {
86 return this.emit(eventName, value);
87 }
88 }
89 }]);
90
91 return Processor;
92 }(EventEmitter);
93}).call(undefined);
\No newline at end of file