-
Notifications
You must be signed in to change notification settings - Fork 0
/
petitions-monitor.ts
250 lines (227 loc) · 8.45 KB
/
petitions-monitor.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
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
/// <reference path="../node.d.ts" />
import events = require('events');
import util = require('util');
import queries = require('./petition-queries');
import petitionPager = require('./petition-pager');
import equal = require('deep-equal');
function logDebug () {
console.log.apply(null, arguments);
}
function acceptRaw(summary, petitions) {
if (summary.attributes.state !== 'rejected' && summary.attributes.state !== 'closed') {
var currentInfo = petitions[summary.id];
if (currentInfo) {
return currentInfo.attributes.signature_count !== summary.attributes.signature_count ||
!equal(currentInfo.attributes.government_response, summary.attributes.government_response) ||
!equal(currentInfo.attributes.debate, summary.attributes.debate);
}
else {
return true;
}
}
return false;
}
function acceptEnriched (summary, petitions) {
if (summary.state !== 'rejected' && summary.state !== 'closed') {
var currentInfo = petitions[summary.id];
if (currentInfo) {
return currentInfo.signature_count !== summary.signature_count ||
!equal(currentInfo.government_response, summary.government_response) ||
!equal(currentInfo.debate, summary.debate);
}
else {
return true;
}
}
return false;
}
function standardAccepter (config, summary, petitions) {
if (config.raw) {
return acceptRaw(summary, petitions);
}
return acceptEnriched(summary, petitions);
}
function removeRaw (summary, petitions) {
return summary.attributes.state === 'rejected' || summary.attributes.state === 'closed';
}
function removeEnriched (summary, petitions) {
return summary.state === 'rejected' || summary.state === 'closed';
}
function standardRemover (config, summary, petitions) {
if (config.raw) {
return removeRaw(summary, petitions);
}
return removeEnriched(summary, petitions);
}
/**
* Configuration for PetitionsMonitor.
*/
class PetitionsMonitorConfig {
/**
* The initial interval.
*/
initialInterval = 200;
/**
* The interval.
*/
interval = 2000;
/**
* If debug should be passed to the loaders.
*/
passDebug: boolean = false;
/**
* If debug logging should be enabled.
*/
debug = function(message: string, ...objects: any[]) {};
/**
* If detailed petition information should be loaded.
*/
loadDetail = false;
/**
* The accepting function.
*/
accepter = standardAccepter.bind(null, this);
/**
* The remover function.
*/
remover = standardRemover.bind(null, this);
/**
* If the petitions loaded should be raw.
*/
raw: boolean = false;
constructor(config?: any) {
if (config) {
if (config.initialInterval) {
this.initialInterval = config.initialInterval;
}
if (config.interval) {
this.interval = config.interval;
}
if (config.debug) {
this.passDebug = true;
this.debug = logDebug;
}
if (config.loadDetail !== undefined) {
this.loadDetail = config.loadDetail;
}
if (config.accepter !== undefined) {
this.accepter = config.accepter;
}
if (config.remover !== undefined) {
this.remover = config.remover;
}
if (config.raw) {
this.raw = true;
}
}
}
}
export module UkPetitions {
/**
* Monitors the petitions data for changes and generates a notification event for changes.
*/
export class PetitionsMonitor extends events.EventEmitter {
private events = [];
private deltaEvents = [];
private conf: PetitionsMonitorConfig;
/**
* Constructor for petition pager.
* @param config Configuration for PetitionsMonitor.
*/
constructor(config: any) {
super();
this.conf = new PetitionsMonitorConfig(config);
this.conf.debug('Debug enabled');
}
/**
* Add an event to emit when the check function returns true. The check function is passed the the new data. Returns the monitor.
* @param event The name of the event to add.
* @param check The predicate required to emit the event.
*/
addMonitorEvent(event, check): PetitionsMonitor {
this.events.push({event : event, check : check});
return this;
}
/**
* Add an event to emit when the check function returns true. The check function is passed the the new and old data. Returns the monitor.
* @param event The name of the event to add.
* @param check The check required to emit the event.
*/
addMonitorDeltaEvent(event, check): PetitionsMonitor {
this.deltaEvents.push({event : event, check : check});
return this;
}
on(eventName: string, listener: {(...eventObjects: any[]): void;}): PetitionsMonitor;
/**
* @event A new petition has been added
*/
on(eventName: 'new-petition', listener: {(newPetition: any): void;}): PetitionsMonitor;
/**
* @event An existing petition has been updated
*/
on(eventName: 'updated-petition', listener: {(newPetitionData: any, oldPetitionData: any): void;}): PetitionsMonitor;
/**
* @event An existing petition has been removed
*/
on(eventName: 'removed-petition', listener: {(removedPetiton: any): void;}): PetitionsMonitor;
on(eventName: string, listener: {(petition: any): void;}): PetitionsMonitor {
super.on(eventName, listener);
return this;
}
/**
* Start the monitor. Returns the monitor.
*/
start(): PetitionsMonitor {
this.conf.debug('Starting monitor');
var pager: petitionPager.UkPetitions.PetitionPager = new petitionPager.UkPetitions.PetitionPager({
loadInterval : this.conf.initialInterval,
debug : this.conf.passDebug,
loadDetail : this.conf.loadDetail,
transformer : this.conf.raw ? function (petiton) { return petiton; } : null
});
var self: PetitionsMonitor = this;
pager
.on('petition', function(newData, oldData) {
// Emit new or update event
if (!oldData) {
self.emit('new-petition', newData);
}
else {
self.emit('updated-petition', newData, oldData);
}
// Check for events to emit based on the current value
self.events.forEach(function(registeredEvent) {
if (registeredEvent.check(newData)) {
self.emit(registeredEvent.event, newData);
}
});
if (oldData) {
// Check for events to emit based on the current and previous values
self.deltaEvents.forEach(function(registeredEvent) {
if (registeredEvent.check(newData, oldData)) {
self.emit(registeredEvent.event, newData);
}
});
}
})
.on('removed-petition', function (newData, oldData) {
self.emit('removed-petition', newData, oldData);
})
.on('loaded', function() {
self.conf.debug('Initial petitions polled, found %d petitions, going again', pager.petitions.length);
pager
.setPageLoadInterval(this.conf.interval)
.removeAllListeners('loaded')
.on('loaded', function() {
self.conf.debug('All petitions polled, found %d petitions, going again', pager.petitions.length);
self.emit('loaded', pager.petitions);
pager.populate(self.conf.accepter, self.conf.remover);
});
self.emit('initial-load', pager.petitions);
pager.populate(self.conf.accepter, self.conf.remover);
});
pager.populate(self.conf.accepter, self.conf.remover);
return self;
}
}
}