-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.hh
203 lines (175 loc) · 3.81 KB
/
Event.hh
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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Event;
/**
* An object representing the current event being dispatched.
* The event can be stopped at any time during the cycle.
*
* @package Titon\Event
*/
class Event {
/**
* Data to persist between observers.
*
* @var \Titon\Event\DataMap
*/
protected DataMap $data = Map {};
/**
* The event key.
*
* @var string
*/
protected string $key;
/**
* The current count of how many observers have been notified.
*
* @var int
*/
protected int $index = 0;
/**
* Has the event stopped? This will cancel upcoming observers.
*
* @var bool
*/
protected bool $stopped = false;
/**
* The call stack in order of priority.
*
* @var \Titon\Event\CallStackList
*/
protected CallStackList $stack = Vector {};
/**
* The last state before the object was stopped.
* This value is automatically set by the emitter using the callback response.
*
* @var mixed
*/
protected mixed $state = true;
/**
* The timestamp of when the event started.
*
* @var int
*/
protected int $time;
/**
* Initialize the event.
*
* @param string $key
*/
public function __construct(string $key) {
$this->key = $key;
$this->time = time();
}
/**
* Return the call stack in order of priority.
*
* @return \Titon\Event\CallStackList
*/
public function getCallStack(): CallStackList {
return $this->stack;
}
/**
* Return the data defined by key, or return all data.
*
* @param string $key
* @return mixed
*/
public function getData(string $key): mixed {
return $this->data->get($key);
}
/**
* Return the current notify count.
*
* @return int
*/
public function getIndex(): int {
return $this->index;
}
/**
* Return the event key.
*
* @return string
*/
public function getKey(): string {
return $this->key;
}
/**
* Return the response state.
*
* @return mixed
*/
public function getState(): mixed {
return $this->state;
}
/**
* Return the event timestamp.
*
* @return int
*/
public function getTime(): int {
return $this->time;
}
/**
* Check if the event has stopped.
*
* @return bool
*/
public function isStopped(): bool {
return $this->stopped;
}
/**
* Increase the notify counter if the event has not stopped.
*
* @return $this
*/
public function next(): this {
if (!$this->isStopped()) {
$this->index++;
}
return $this;
}
/**
* Set the call stack for the current event.
*
* @param \Titon\Event\CallStackList $stack
* @return $this
*/
public function setCallStack(CallStackList $stack): this {
$this->stack = $stack;
return $this;
}
/**
* Set data to pass to the next observer.
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function setData(string $key, mixed $value): this {
$this->data[$key] = $value;
return $this;
}
/**
* Set the response state.
*
* @param mixed $state
* @return $this
*/
public function setState(mixed $state): this {
$this->state = $state;
return $this;
}
/**
* Stop the event.
*
* @return $this
*/
public function stop(): this {
$this->stopped = true;
return $this;
}
}