-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaggerBase.js
More file actions
136 lines (123 loc) · 3.03 KB
/
Copy pathDaggerBase.js
File metadata and controls
136 lines (123 loc) · 3.03 KB
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
'use strict'
/**
* Helper for UUID generation
*/
if (typeof window === 'object') {
// HTML javascript implementation of uuid-v4
uuid = {
v4: function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(cc) {
var rr = Math.random() * 16 | 0; return (cc === 'x' ? rr : (rr & 0x3 | 0x8)).toString(16);
})
}
}
} else {
var uuid = require('uuid');
}
/**
* Base class for all Dagger objects.
*/
class DaggerBase {
/**
* DaggerBase ctor
*/
constructor() {
this._instanceID = uuid.v4();
this._parent = null;
}
/**
* Get the unique id for this instance
* @return {string}
*/
get instanceID() {
return this._instanceID;
}
/**
* Get the parent DaggerBase object this object belongs to
* @returns {DaggerBase}
*/
get parent() {
return this._parent;
}
/**
* Set the parent DaggerBase object this object belongs to
* @param {DaggerBase}
*/
set parent(p) {
this._parent = p;
}
emitError(err) {
console.log(err);
}
purgeAll() {}
}
/**
* This class acts as a simple emulation of the Qt signal/slot pattern. Objects
* derived from DaggerBase expose these as properties that can be connected to with callbacks (slots)
* to respond to certain Dagger events.
*
* @example
* var nodeAddedSlot = function (nodeID) {
* console.log("node added to graph" + nodeID);
* // now disconnect the slot
* graph.nodeAdded.disconnect(nodeAddedSlot);
* }
* graph.nodeAdded.connect(nodeAddedSlot);
*
* @example
* var nodeAddedSlot = function (nodeID) {
* console.log("slot node added " + nodeID);
* }
*
* // calling connect on the signal will return a reference to
* // the signal that one can call disconnect on directly when no
* // longer needed:
* var mysignal = graph.nodeAdded.connect(nodeAddedSlot);
*
* // when not needed, call disconnect on the sig object
* mysignal.disconnect();
*
*/
class DaggerSignal {
/**
* DaggerSignal ctor
* @param {*} implementation
*/
constructor(implementation) {
this._cb = [];
if(typeof implementation === 'function') {
this._cb[0] = implementation;
}
}
/**
* Add a connection callback
* @param {Function} slot
*/
connect(slot) {
this._cb[this._cb.length] = slot;
return this;
}
/**
* Remove a connection callback
* @param {Function} slot
*/
disconnect(slot) {
var index = this._cb.indexOf(slot);
if (index > -1) this._cb.splice(index, 1);
}
/**
* Remova all connection callbacks
*/
disconnectAll() {
this._cb = [];
}
/**
* Signal all connected callbacks
*/
emit() {
for (var i = 0, len = this._cb.length; i < len; i++)
this._cb[i].apply(this._cb[i], arguments);
}
}
module.exports.DaggerBase = DaggerBase;
module.exports.DaggerSignal = DaggerSignal;