summaryrefslogtreecommitdiff
path: root/scripts/lib/signals.js
blob: b645a2dacedc80a310b5c99cc2ebac5065ad738a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
(function ()  
{
    var _sigCount = {};
    var _byName = {};
    var _byId = {};

    var _getByCallback = function(callback)
    {
        var id;
        for (id in _byId)
        {
            if (callback == _byId[id].callback)
            {
                return _byId[id];
            }
        }
        return null;
    };
    var _getBySelfOrCallback = function(selfOrCallback)
    {
        if (selfOrCallback instanceof Signal)
            return selfOrCallback;
        return _getByCallback(selfOrCallback);
    };
    /** 
     *
     * @name Signal
     * @class 
     *      A Signal can be used to connect to certain browser events. For a list
     *      of available signals see {@link signals}. Signals are directly
     *      emitted on {@link signals}, this class is just a convience
     *      class to handle signals. 
     * @description
     *      Constructs a new Signal
     * @example 
     * function navigationCallback(wv, frame, request, response) 
     * {
     *      ...
     * }
     *
     * // Create a new signal
     * var a = new Signal("navigation", navigationCallback);
     * var b = new Signal("navigation");
     *
     * // The following patterns are equivalent
     * a.connect();
     *
     * b.connect(navigationCallback);
     *
     * // using the static method
     * var c = Signal.connect("navigation", navigationCallback);
     *
     * // set the signal callback directly on {@link signals} 
     * // you won't get the constructed signal then
     * signals.onNavigation = navigationCallback;
     *
     * @param {String} name 
     *      The event to connect to
     * @param {Function} [callback] 
     *      Callback that will be called when the signal is emitted. If omitted
     *      a callback must be passed to {@link connect}.
     *
     * @returns {Signal}
     *      A new Signal
     * */
    Object.defineProperty(this, "Signal", {
            writable : true,
            value : (function() {
                var id = 0;
                return function(name, callback)
                {
                    if (!name)
                        throw new Error("new Signal() : missing signal name");
                    
                    id++;
                    return Object.create(Signal.prototype, 
                        {
                            /**
                             * The id of the signal
                             * @name id
                             * @memberOf Signal.prototype
                             * @readonly
                             *
                             * */
                            "id" : { value : id },
                            /** 
                             * The callback that will be called when the signal
                             * is emitted, the context of the signal will be the
                             * signal itself (i.e. <i>this</i> refers to the
                             * connected Signal).
                             *
                             * @name callback
                             * @memberOf Signal.prototype
                             *
                             * @example 
                             * function a() {
                             *      io.print("Calling from a");
                             *      this.callback = b;
                             * }
                             * function b() { 
                             *      io.print("Calling from b");
                             *      this.callback = a;
                             * }
                             * var s = new Signal("createTab", a).connect();
                             * */
                            "callback" : { value : callback, writable : true }, 
                            /**
                             * The name of the event
                             * @name name
                             * @memberOf Signal.prototype
                             * @readonly
                             * */
                            "name" : { value : name },
                            /**
                             * Disconnect this signal from the event, if disconnected the
                             * callback will no longer be called, to reconnect
                             * call signal.connect()
                             *
                             * @name disconnect
                             * @function 
                             * @memberOf Signal.prototype
                             *
                             * @returns {Signal}
                             *      self
                             *
                             * @example
                             * var i = 0;
                             * var signal = new Signal("navigation", function(wv, frame, request) {
                             *      i++;
                             *      if (i == 3)
                             *          this.disconnect();
                             * });
                             * */
                            "disconnect" : 
                            { 
                                value : function() 
                                { 
                                    if (!this.connected)
                                        return this;

                                    var name = this.name, id = this.id;
                                    if (_sigCount[name] > 0)
                                        _sigCount[name]--;

                                    _byName[name][id] = null;
                                    delete _byName[name][id];

                                    _byId[id] = null;
                                    delete _byId[id];

                                    if (_sigCount[name] == 0)
                                        signals[name] = null;

                                    return this;
                                }
                            },
                            /**
                             * Connect this signal to the event
                             *
                             * @name connect
                             * @function 
                             * @memberOf Signal.prototype
                             *
                             * @param {Function} [callback]
                             *      The callback function to call, if no
                             *      callback was passed to the constructor
                             *      callback is mandatory.
                             *
                             * @returns {Signal}
                             *      self
                             * @example 
                             * function a() {
                             *      ...
                             * }
                             * function b() {
                             *      ...
                             * }
                             * var signal = new Signal("navigation", a);
                             * // connect to a
                             * signal.connect();
                             * // connect to b
                             * signal.connect(b);
                             * */
                            "connect" : 
                            { 
                                value : function(callback) 
                                {
                                    if (callback)
                                        this.callback = callback;
                                    if (this.connected)
                                        return this;

                                    if (!this.callback)
                                        throw new Error("Signal.connect() : missing callback");

                                    var name = this.name, id = this.id;

                                    if (!_sigCount[name])
                                        _sigCount[name] = 0;

                                    if (!_byName[name])
                                        _byName[name] = {};

                                    if (_sigCount[name] == 0)
                                        signals[name] = function() { return Signal.emit(name, arguments); };

                                    _sigCount[name]++;
                                    _byName[name][id] = this;
                                    _byId[id] = this;

                                    return this;
                                }
                            }, 
                            /**
                             * Whether the signal is connected
                             *
                             * @name connected 
                             * @memberOf Signal.prototype
                             * @type Boolean
                             * @readonly
                             *
                             * */
                            "connected" : 
                            {
                                get : function() 
                                {
                                    return Boolean(_byId[this.id]);
                                }
                            },
                            /**
                             * Toggles a signal, if it is connected it will be
                             * disconnected and vice versa
                             *
                             * @name toggle 
                             * @memberOf Signal.prototype
                             * @function
                             *
                             * @returns {Boolean}
                             *      <i>true</i> if the signal was connected, <i>false</i>
                             *      otherwise
                             * */
                            "toggle" : 
                            {
                                value : function()
                                {
                                    var connected = this.connected;
                                    if (connected)
                                        this.disconnect();
                                    else 
                                        this.connect();
                                    return !connected;
                                }
                            }
                        }
                    );
                };
            })()
    });
    Object.defineProperties(Signal, {
            /**
             * Connects to an event
             *
             * @name connect
             * @memberOf Signal
             * @function
             * 
             * @param {String} name 
             *      The signal to connect to
             * @param {Function} callback 
             *      Callback that will be called when the signal is emitted.
             *
             * @returns {Signal}
             *      A new Signal
             *
             * @example
             * function onCloseTab()
             * {
             *      ...
             * }
             * var s = Signal.connect("closeTab", onCloseTab);
             * // equivalent to 
             * var s = new Signal("closeTab", onCloseTab);
             * s.connect();
             * */
            "connect" : 
            {
                value : function(name, callback)
                {
                    return new Signal(name, callback).connect();
                }
            },
            /**
             * Disconnects from an event.              
             * @name disconnect
             * @memberOf Signal
             * @function
             * 
             * @param {Signal|Callback} object 
             *      Either a Signal or the callback of a signal
             *      If a callback is passed to this function and the same
             *      callback is connected multiple times only the first matching
             *      callback will be disconnected, to disconnect all matching
             *      callbacks call use {@link Signal.disconnectAll}
             *
             * @returns {Signal}
             *      The disconnected Signal
             *
             * @example
             * function callback(wv) 
             * {
             *      ...
             * }
             * var s = new Signal("loadStatus").connect(callback);
             *
             * // Disconnect from the first matching callback
             * Signal.disconnect(callback);
             *
             * Signal.disconnect(s);
             * // or equivalently
             * s.disconnect();
             * */
            "disconnect" : 
            {
                value : function(selfOrCallback)
                {
                    var signal = _getBySelfOrCallback(selfOrCallback);
                    if (signal)
                        signal.disconnect();
                    return signal;
                }
            }, 
            /**
             * Connects all webviews to a GObject signal. 
             *
             * @name connectWebView
             * @memberOf Signal 
             * @function 
             *
             * @param {String} signal The signal name
             * @param {GObject~connectCallback} callback 
             *      A callback function the will be called when the signal is
             *      emitted, the arguments of the callback correspond to the GObject
             *      callback
             * @example 
             * Signal.connectWebView("hovering-over-link", function(title, uri) {
             *      io.write("/tmp/hovered_sites", "a", uri + " " + title);
             * }); 
             *
             * */
            "connectWebView" : 
            {
                value : function(name, callback)
                {
                    var wv;
                    for (var i=0; i<tabs.length; i++)
                    {
                        if (tabs.nth(i))
                            tabs.nth(i).connect(name, function() { callback.apply(wv, arguments); });
                    }
                    Signal.connect("createTab", function(wv) {
                        wv.connect(name, function() { callback.apply(wv, arguments);});
                    });
                }
            }, 
            /**
             * Emits a signal, can be used to implement custom signals.
             *
             * @name emit 
             * @memberOf Signal 
             * @function
             *
             * @param {String} signal The signal name 
             * @param {varargs} args  Arguments passed to the callback function of
             *                        {@link Signal.connect} 
             *
             * @returns {Boolean}
             *      The overall return value of all callback function, if one
             *      callback function returns <i>true</i> the overall return value
             *      will be <i>true</i>
             * */
            "emit" :
            {
                value : function(signal, args)
                {
                    var id, current;
                    var ret = false;
                    var connected = _byName[signal];
                    for (id in connected)
                    {
                        current = connected[id];
                        ret = current.callback.apply(current, args) || ret;
                    }
                    return ret;
                }
            }, 
            /**
             * Disconnect from all signals with matching callback function
             *
             * @name disconnectAll
             * @memberOf Signal 
             * @function 
             *
             * @param {Function} callback
             *      A callback function
             * 
             * @returns {Array}
             *      Array of signals that were disconnected
             *
             * @example
             * function onNavigation(wv, frame, request) 
             * {
             *      ...
             * }
             * var a = new Signal("navigation", onNavigation).connect();
             * var b = new Signal("navigation", onNavigation).connect();
             *
             * Signals.disconnectAll(onNavigation);
             * */
            "disconnectAll" : 
            {
                value : function(callback) 
                {
                    var signals = [];
                    var signal; 
                    while((signal = _getBySelfOrCallback(callback)))
                    {
                        if (signal.connected)
                        {
                            signals.push(signal);
                            signal.disconnect();
                        }
                    }
                    return signals;

                }
            }, 
            /**
             * Connect to more than one signal at once
             *
             * @name connectAll
             * @memberOf Signal 
             * @function 
             *
             * @param {Array}  signals
             *      Array of signals
             * @param {Function}  [callback]
             *      Callbackfunction to connect to
             *
             *
             * @example
             * function onNavigation(wv, frame, request) 
             * {
             *      ...
             * }
             * function onNavigation2(wv, frame, request) 
             * {
             *      ...
             * }
             * var a = new Signal("navigation", onNavigation).connect();
             * var b = new Signal("navigation", onNavigation).connect();
             *
             * // disconnect from all signals
             * var signals = Signal.disconnectAll(onNavigation);
             *
             * // reconnect to all signals
             * Signal.connectAll(signals);
             *
             * // Reconnect to all signals with a new callback
             * Signal.connectAll([a, b], onNavigation2);
             * */
            "connectAll" : 
            {
                value : function(signalOrArray, callback)
                {
                    var i, l;
                    if (signalOrArray instanceof Signal)
                        signalOrArray.connect(callback);
                    else 
                    {
                        for (i=signalOrArray.length-1; i>=0; i--)
                            signalOrArray[i].connect(callback);
                    }
                }
            }
    });
    Object.defineProperties(signals, {
        /**
         * @name emit 
         * @memberOf signals 
         * @function 
         * @deprecated use {@link Signal.emit} instead
         * */
        "emit" : 
        {
            value : function(sig, func, pre) 
            {
                return _deprecated("signals.emit", "Signal.emit", arguments);
            }
        },
        /**
         * @name connect 
         * @memberOf signals 
         * @function 
         * @deprecated use {@link Signal.connect} instead
         * */
        "connect" : 
        {
            value : function(sig, func, pre) 
            {
                return _deprecated("signals.connect", "Signal.connect", arguments);
            }
        },
        /**
         * Connects all webviews to a GObject signal. 
         *
         * @name connectWebView
         * @memberOf signals 
         * @function 
         * @deprecated use {@link Signal.connectWebView} instead
         *
         * */
        "connectWebView" :
        {
            value : function()
            {
                return _deprecated("signals.connectWebView", "Signal.connectWebView", arguments);
            }
        },
        /**
         * @name disconnect
         * @memberOf signals 
         * @function 
         * @deprecated use {@link Signal.disconnect} instead
         * */
        "disconnect" : 
        {
            value : function()
            {
                return _deprecated("signals.disconnect", "Signal.disconnect", arguments);
            }
        },
        /**
         * @name disconnectAll
         * @memberOf signals 
         * @function 
         * @deprecated use {@link Signal.disconnectAll}
         * */
        "disconnectAll" : 
        {
            value : function(callback) 
            {
                return _deprecated("signals.disconnectAll", "Signal.disconnectAll", arguments);
            }
        }, 
        /**
         * @name connectAll
         * @memberOf signals 
         * @function 
         * @deprecated use {@link Signal.connectAll} instead
         * */
        "connectAll" : 
        {
            value : function()
            {
                return _deprecated("signals.connectAll", "Signal.connectAll", arguments);
            }
        }
    });
})();