source: trip-planner-front/node_modules/websocket-extensions/lib/pipeline/cell.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use strict';
2
3var Functor = require('./functor'),
4 Pledge = require('./pledge');
5
6var Cell = function(tuple) {
7 this._ext = tuple[0];
8 this._session = tuple[1];
9
10 this._functors = {
11 incoming: new Functor(this._session, 'processIncomingMessage'),
12 outgoing: new Functor(this._session, 'processOutgoingMessage')
13 };
14};
15
16Cell.prototype.pending = function(direction) {
17 var functor = this._functors[direction];
18 if (!functor._stopped) functor.pending += 1;
19};
20
21Cell.prototype.incoming = function(error, message, callback, context) {
22 this._exec('incoming', error, message, callback, context);
23};
24
25Cell.prototype.outgoing = function(error, message, callback, context) {
26 this._exec('outgoing', error, message, callback, context);
27};
28
29Cell.prototype.close = function() {
30 this._closed = this._closed || new Pledge();
31 this._doClose();
32 return this._closed;
33};
34
35Cell.prototype._exec = function(direction, error, message, callback, context) {
36 this._functors[direction].call(error, message, function(err, msg) {
37 if (err) err.message = this._ext.name + ': ' + err.message;
38 callback.call(context, err, msg);
39 this._doClose();
40 }, this);
41};
42
43Cell.prototype._doClose = function() {
44 var fin = this._functors.incoming,
45 fout = this._functors.outgoing;
46
47 if (!this._closed || fin.pending + fout.pending !== 0) return;
48 if (this._session) this._session.close();
49 this._session = null;
50 this._closed.done();
51};
52
53module.exports = Cell;
Note: See TracBrowser for help on using the repository browser.