UNPKG

5.27 kBJavaScriptView Raw
1(function() {
2 var Tuple, TupleSpace, path;
3
4 path = require('path');
5
6 Tuple = require(path.join(__dirname, 'tuple'));
7
8 module.exports = TupleSpace = (function() {
9 function TupleSpace(name) {
10 this.name = name != null ? name : 'noname';
11 this.tuples = [];
12 this.callbacks = [];
13 this.__defineGetter__('size', function() {
14 return this.tuples.length;
15 });
16 }
17
18 TupleSpace.prototype.write = function(tuple, options) {
19 var c, called, i, taked, _i, _j, _ref;
20 if (options == null) {
21 options = {
22 expire: Tuple.DEFAULT.expire
23 };
24 }
25 if (!Tuple.isHash(tuple) && !(tuple instanceof Tuple)) {
26 return;
27 }
28 if (!(tuple instanceof Tuple)) {
29 tuple = new Tuple(tuple);
30 }
31 tuple.expire = typeof options.expire === 'number' && options.expire > 0 ? options.expire : Tuple.DEFAULT.expire;
32 tuple.from = options.from;
33 called = [];
34 taked = false;
35 for (i = _i = 0, _ref = this.callbacks.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
36 c = this.callbacks[i];
37 if (c.tuple.match(tuple)) {
38 if (c.type === 'take' || c.type === 'read') {
39 called.push(i);
40 }
41 (function(c) {
42 return setImmediate(function() {
43 return c.callback(null, tuple);
44 });
45 })(c);
46 if (c.type === 'take') {
47 taked = true;
48 break;
49 }
50 }
51 }
52 for (_j = called.length - 1; _j >= 0; _j += -1) {
53 i = called[_j];
54 this.callbacks.splice(i, 1);
55 }
56 if (!taked) {
57 return this.tuples.push(tuple);
58 }
59 };
60
61 TupleSpace.prototype.create_callback_id = function() {
62 return Date.now() - Math.random();
63 };
64
65 TupleSpace.prototype.read = function(tuple, callback) {
66 var i, id, t, _i, _ref;
67 if (typeof callback !== 'function') {
68 return;
69 }
70 if (!Tuple.isHash(tuple) && !(tuple instanceof Tuple)) {
71 setImmediate(function() {
72 return callback('argument_error');
73 });
74 return null;
75 }
76 if (!(tuple instanceof Tuple)) {
77 tuple = new Tuple(tuple);
78 }
79 for (i = _i = _ref = this.size - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
80 t = this.tuples[i];
81 if (tuple.match(t)) {
82 setImmediate(function() {
83 return callback(null, t);
84 });
85 return;
86 }
87 }
88 id = this.create_callback_id();
89 this.callbacks.push({
90 type: 'read',
91 callback: callback,
92 tuple: tuple,
93 id: id
94 });
95 return id;
96 };
97
98 TupleSpace.prototype.take = function(tuple, callback) {
99 var i, id, t, _i, _ref;
100 if (typeof callback !== 'function') {
101 return;
102 }
103 if (!Tuple.isHash(tuple) && !(tuple instanceof Tuple)) {
104 setImmediate(function() {
105 return callback('argument_error');
106 });
107 return null;
108 }
109 if (!(tuple instanceof Tuple)) {
110 tuple = new Tuple(tuple);
111 }
112 for (i = _i = _ref = this.size - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
113 t = this.tuples[i];
114 if (tuple.match(t)) {
115 setImmediate(function() {
116 return callback(null, t);
117 });
118 this.tuples.splice(i, 1);
119 return;
120 }
121 }
122 id = this.create_callback_id();
123 this.callbacks.push({
124 type: 'take',
125 callback: callback,
126 tuple: tuple,
127 id: id
128 });
129 return id;
130 };
131
132 TupleSpace.prototype.watch = function(tuple, callback) {
133 var id;
134 if (typeof callback !== 'function') {
135 return;
136 }
137 if (!Tuple.isHash(tuple) && !(tuple(instance(Tuple)))) {
138 setImmediate(function() {
139 return callback('argument_error');
140 });
141 return;
142 }
143 if (!(tuple instanceof Tuple)) {
144 tuple = new Tuple(tuple);
145 }
146 id = this.create_callback_id();
147 this.callbacks.unshift({
148 id: id,
149 type: 'watch',
150 tuple: tuple,
151 callback: callback
152 });
153 return id;
154 };
155
156 TupleSpace.prototype.cancel = function(id) {
157 var c, i, _i, _ref;
158 if (id == null) {
159 return;
160 }
161 for (i = _i = 0, _ref = this.callbacks.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
162 c = this.callbacks[i];
163 if (id === c.id) {
164 setImmediate(function() {
165 return c.callback('cancel', null);
166 });
167 this.callbacks.splice(i, 1);
168 return;
169 }
170 }
171 };
172
173 TupleSpace.prototype.check_expire = function() {
174 var expires, i, _i, _j, _ref;
175 expires = [];
176 for (i = _i = 0, _ref = this.tuples.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
177 if (this.tuples[i].expire_at < Date.now() / 1000) {
178 expires.push(i);
179 }
180 }
181 for (_j = expires.length - 1; _j >= 0; _j += -1) {
182 i = expires[_j];
183 this.tuples.splice(i, 1);
184 }
185 return expires.length;
186 };
187
188 return TupleSpace;
189
190 })();
191
192}).call(this);