1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var SideralObject_1 = require("./../SideralObject");
|
4 |
|
5 |
|
6 |
|
7 | var Signal = (function () {
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 | function Signal(parent, onBind, onUnbind) {
|
14 | |
15 |
|
16 |
|
17 |
|
18 | this._listeners = [];
|
19 | if (!(parent instanceof SideralObject_1.SideralObject)) {
|
20 | throw new Error("Signal.constructor: an instance of SideralObject must be passed on the constructor (the owner of the signal).");
|
21 | }
|
22 | this.parent = parent;
|
23 | this.onBind = onBind;
|
24 | this.onUnbind = onUnbind;
|
25 | }
|
26 | |
27 |
|
28 |
|
29 |
|
30 | Signal.prototype.dispatch = function () {
|
31 | var params = [];
|
32 | for (var _i = 0; _i < arguments.length; _i++) {
|
33 | params[_i] = arguments[_i];
|
34 | }
|
35 | if (typeof this._listeners === "undefined") {
|
36 | console.log([this, this.parent], this._listeners);
|
37 | }
|
38 | this._listeners.forEach(function (listener) { return listener.apply(void 0, params); });
|
39 | };
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | Signal.prototype.add = function (listener) {
|
46 | this._listeners.push(listener);
|
47 | if (this.onBind) {
|
48 | this.onBind(listener);
|
49 | }
|
50 | return listener;
|
51 | };
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | Signal.prototype.remove = function (listener) {
|
58 | var nextListeners = this._listeners.filter(function (x) { return x !== listener; }), hasRemoved = nextListeners.length !== this._listeners.length;
|
59 | this._listeners = nextListeners;
|
60 | if (this.onUnbind && hasRemoved) {
|
61 | this.onUnbind(listener);
|
62 | }
|
63 | return hasRemoved;
|
64 | };
|
65 | |
66 |
|
67 |
|
68 | Signal.prototype.removeAll = function () {
|
69 | this._listeners.forEach(this.remove.bind(this));
|
70 | this._listeners = [];
|
71 | };
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | Signal.prototype.bind = function (param, listener) {
|
79 | var params = [].concat(param);
|
80 | return this.add(function (key) {
|
81 | if (typeof key === "string" && params.find(function (x) { return x === key; })) {
|
82 | listener.apply(void 0, [].slice.call(arguments, 1));
|
83 | }
|
84 | });
|
85 | };
|
86 | return Signal;
|
87 | }());
|
88 | exports.Signal = Signal;
|