UNPKG

4.76 kBJavaScriptView Raw
1(function() {
2 var LindaClient, TupleSpace;
3
4 LindaClient = (function() {
5 function LindaClient() {}
6
7 LindaClient.prototype.connect = function(io) {
8 this.io = io;
9 return this;
10 };
11
12 LindaClient.prototype.tuplespace = function(name) {
13 return new TupleSpace(this, name);
14 };
15
16 LindaClient.prototype.requestKeepalive = function(url) {
17 return this.tuplespace('__linda').write({
18 type: 'keepalive',
19 to: url
20 });
21 };
22
23 return LindaClient;
24
25 })();
26
27 TupleSpace = (function() {
28 function TupleSpace(linda, name) {
29 this.linda = linda;
30 this.name = name;
31 this.watch_callback_ids = {};
32 this.io_callbacks = [];
33 this.linda.io.on('disconnect', (function(_this) {
34 return function() {
35 return _this.remove_io_callbacks();
36 };
37 })(this));
38 }
39
40 TupleSpace.prototype.create_callback_id = function() {
41 return Date.now() - Math.random();
42 };
43
44 TupleSpace.prototype.create_watch_callback_id = function(tuple) {
45 var key;
46 key = JSON.stringify(tuple);
47 return this.watch_callback_ids[key] || (this.watch_callback_ids[key] = this.create_callback_id());
48 };
49
50 TupleSpace.prototype.remove_io_callbacks = function() {
51 var c, _i, _len, _ref;
52 _ref = this.io_callbacks;
53 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
54 c = _ref[_i];
55 this.linda.io.removeListener(c.name, c.listener);
56 }
57 return this.io_callbacks = [];
58 };
59
60 TupleSpace.prototype.write = function(tuple, options) {
61 var data;
62 if (options == null) {
63 options = {
64 expire: null
65 };
66 }
67 data = {
68 tuplespace: this.name,
69 tuple: tuple,
70 options: options
71 };
72 return this.linda.io.emit('__linda_write', data);
73 };
74
75 TupleSpace.prototype.take = function(tuple, callback) {
76 var id, listener, name;
77 if (typeof callback !== 'function') {
78 return;
79 }
80 id = this.create_callback_id();
81 name = "__linda_take_" + id;
82 listener = function(err, tuple) {
83 return callback(err, tuple);
84 };
85 this.io_callbacks.push({
86 name: name,
87 listener: listener
88 });
89 this.linda.io.once(name, listener);
90 this.linda.io.emit('__linda_take', {
91 tuplespace: this.name,
92 tuple: tuple,
93 id: id
94 });
95 return id;
96 };
97
98 TupleSpace.prototype.read = function(tuple, callback) {
99 var id, listener, name;
100 if (typeof callback !== 'function') {
101 return;
102 }
103 id = this.create_callback_id();
104 name = "__linda_read_" + id;
105 listener = function(err, tuple) {
106 return callback(err, tuple);
107 };
108 this.io_callbacks.push({
109 name: name,
110 listener: listener
111 });
112 this.linda.io.once(name, listener);
113 this.linda.io.emit('__linda_read', {
114 tuplespace: this.name,
115 tuple: tuple,
116 id: id
117 });
118 return id;
119 };
120
121 TupleSpace.prototype.watch = function(tuple, callback) {
122 var id, listener, name;
123 if (typeof callback !== 'function') {
124 return;
125 }
126 id = this.create_watch_callback_id(tuple);
127 name = "__linda_watch_" + id;
128 listener = function(err, tuple) {
129 return callback(err, tuple);
130 };
131 this.io_callbacks.push({
132 name: name,
133 listener: listener
134 });
135 this.linda.io.on(name, listener);
136 this.linda.io.emit('__linda_watch', {
137 tuplespace: this.name,
138 tuple: tuple,
139 id: id
140 });
141 return id;
142 };
143
144 TupleSpace.prototype.cancel = function(id) {
145 if (this.linda.io.connected) {
146 this.linda.io.emit('__linda_cancel', {
147 tuplespace: this.name,
148 id: id
149 });
150 }
151 return setTimeout((function(_this) {
152 return function() {
153 var c, i, _i, _ref, _results;
154 _results = [];
155 for (i = _i = _ref = _this.io_callbacks.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
156 c = _this.io_callbacks[i];
157 if (c.name.match(new RegExp("_" + id + "$"))) {
158 _this.linda.io.removeListener(c.name, c.listener);
159 _results.push(_this.io_callbacks.splice(i, 1));
160 } else {
161 _results.push(void 0);
162 }
163 }
164 return _results;
165 };
166 })(this), 100);
167 };
168
169 return TupleSpace;
170
171 })();
172
173 if (typeof window !== "undefined" && window !== null) {
174 window.Linda = LindaClient;
175 } else if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
176 module.exports = LindaClient;
177 }
178
179}).call(this);