forked from electricessence/EventPublisher
/
EventPublisher.ts
196 lines (174 loc) · 5.73 KB
/
EventPublisher.ts
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
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT
*/
import DisposableBase from '@tsdotnet/disposable';
import Disposable from '@tsdotnet/disposable/dist/Disposable';
import {Lazy} from '@tsdotnet/lazy';
import {OrderedAutoRegistry} from '@tsdotnet/ordered-registry';
import {EventDispatcher} from './EventDispatcher';
import {EventPublisherOptions} from './EventPublisherOptions';
export default class EventPublisher<T>
extends DisposableBase
{
public readonly options: EventPublisherOptions;
protected readonly _pre = Lazy.create(() => new OrderedAutoRegistry<EventPublisher<T>>());
protected readonly _dispatcher = Lazy.create(() => new EventDispatcher<T>(this.options));
protected readonly _post = Lazy.create(() => new OrderedAutoRegistry<EventPublisher<T>>());
constructor (remaining: number, finalizer?: () => void);
constructor (options?: EventPublisherOptions | number | null, finalizer?: () => void);
constructor (options?: EventPublisherOptions | number | null, finalizer?: () => void)
{
super('EventPublisher', finalizer);
this.options = createOptions(options);
Object.freeze(this);
}
/**
* Sets the remaining number of publishes that will emit to listeners.
* A value of zero will clear all listeners.
* @param value
*/
set remaining (value: number)
{
if(isNaN(value)) return;
this.throwIfDisposed('Updating remaining for disposed publisher.');
this.options.remaining = value;
if(!value) this._dispatcher.valueReference?.clear();
}
/**
* Gets the remaining number of publishes that will emit to listeners.
* When this number is zero all listeners are cleared and none can be added.
*/
get remaining (): number
{
const rem = this.options.remaining;
return typeof rem=='number' ? rem : Number.POSITIVE_INFINITY;
}
/**
* The event dispatcher.
*/
get dispatcher (): EventDispatcher<T>
{
return this._dispatcher.value;
}
/**
* Adds an event publisher to be triggered before the event is published.
* Disposing the returned `EventPublisher<T>` removes it from it's parent (this).
* @param {number} remaining
* @return {EventPublisher<T>}
*/
addPre (remaining: number): EventPublisher<T>;
/**
* Adds an event publisher to be triggered before the event is published.
* Disposing the returned `EventPublisher<T>` removes it from it's parent (this).
* @param {EventPublisherOptions} options
* @return {EventPublisher<T>}
*/
addPre (options?: EventPublisherOptions): EventPublisher<T>;
/**
* Adds an event publisher to be triggered before the event is published.
* Disposing the returned `EventPublisher<T>` removes it from it's parent (this).
* @param {number | EventPublisherOptions} options
* @return {EventPublisher<T>}
*/
addPre (options?: number | EventPublisherOptions): EventPublisher<T>
{
this.throwIfDisposed();
return addPub(this._pre.value, options);
}
/**
* Adds an event publisher to be triggered after the event is published.
* Disposing the returned `EventPublisher<T>` removes it from it's parent (this).
* @param {number} remaining
* @return {EventPublisher<T>}
*/
addPost (remaining: number): EventPublisher<T>;
/**
* Adds an event publisher to be triggered after the event is published.
* Disposing the returned `EventPublisher<T>` removes it from it's parent (this).
* @param {EventPublisherOptions} options
* @return {EventPublisher<T>}
*/
addPost (options?: EventPublisherOptions): EventPublisher<T>;
/**
* Adds an event publisher to be triggered after the event is published.
* Disposing the returned `EventPublisher<T>` removes it from it's parent (this).
* @param {number | EventPublisherOptions} options
* @return {EventPublisher<T>}
*/
addPost (options?: number | EventPublisherOptions): EventPublisher<T>
{
this.throwIfDisposed();
return addPub(this._post.value, options);
}
/**
* Dispatches payload to listeners.
* @param payload
*/
publish (payload: T): void
{
this.throwIfDisposed();
const _ = this, o = _.options;
let r = o.remaining;
if(r===0) return;
if(!r || isNaN(r)) r = Number.POSITIVE_INFINITY;
if(r<0) return;
if(isFinite(r)) o.remaining = --r;
try
{
const
d = _._dispatcher.valueReference,
pre = _._pre.valueReference?.values.toArray(),
post = _._post.valueReference?.values.toArray();
publish(pre as EventPublisher<T>[], payload);
if(d) d.dispatch(payload);
publish(post as EventPublisher<T>[], payload);
}
catch(ex)
{
if(o.onError) o.onError(ex);
else throw ex;
}
finally
{
if(r==0 || o.clearListenersAfterPublish) _._dispatcher.valueReference?.clear();
}
}
protected _onDispose (): void
{
const d = this._dispatcher;
d.valueReference?.dispose();
d.dispose();
cleanReg(this._pre.valueReference)?.dispose();
cleanReg(this._post.valueReference)?.dispose();
}
}
function publish<T> (p: EventPublisher<T>[] | undefined, payload: T)
{
if(p) for(const e of p) (e as EventPublisher<T>).publish(payload);
}
function createOptions (options?: EventPublisherOptions | number | null): EventPublisherOptions
{
return typeof options=='number' ? {remaining: options} : !options ? {} : {
reversePublish: options.reversePublish,
onError: options.onError,
clearListenersAfterPublish: options.clearListenersAfterPublish,
remaining: options.remaining
};
}
function addPub<T> (
reg: OrderedAutoRegistry<EventPublisher<T>>,
options?: EventPublisherOptions | number): EventPublisher<T>
{
let id = 0;
const p = new EventPublisher<T>(options, () => reg.remove(id));
id = reg.add(p);
return p;
}
function cleanReg<TDisposable extends Disposable> (reg?: OrderedAutoRegistry<TDisposable>): OrderedAutoRegistry<TDisposable> | undefined
{
if(!reg) return reg;
for(const r of reg.toArray()) r.value.dispose();
reg.clear();
return reg;
}