UNPKG

16.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Broadcaster provides a helper methods to broadcast events to the subscribers.
5 */
6var Broadcaster = /** @class */ (function () {
7 // -------------------------------------------------------------------------
8 // Constructor
9 // -------------------------------------------------------------------------
10 function Broadcaster(queryRunner) {
11 this.queryRunner = queryRunner;
12 }
13 // -------------------------------------------------------------------------
14 // Public Methods
15 // -------------------------------------------------------------------------
16 /**
17 * Broadcasts "BEFORE_INSERT" event.
18 * Before insert event is executed before entity is being inserted to the database for the first time.
19 * All subscribers and entity listeners who listened to this event will be executed at this point.
20 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
21 *
22 * Note: this method has a performance-optimized code organization, do not change code structure.
23 */
24 Broadcaster.prototype.broadcastBeforeInsertEvent = function (result, metadata, entity) {
25 var _this = this;
26 if (entity && metadata.beforeInsertListeners.length) {
27 metadata.beforeInsertListeners.forEach(function (listener) {
28 if (listener.isAllowed(entity)) {
29 var executionResult = listener.execute(entity);
30 if (executionResult instanceof Promise)
31 result.promises.push(executionResult);
32 result.count++;
33 }
34 });
35 }
36 if (this.queryRunner.connection.subscribers.length) {
37 this.queryRunner.connection.subscribers.forEach(function (subscriber) {
38 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.beforeInsert) {
39 var executionResult = subscriber.beforeInsert({
40 connection: _this.queryRunner.connection,
41 queryRunner: _this.queryRunner,
42 manager: _this.queryRunner.manager,
43 entity: entity,
44 metadata: metadata
45 });
46 if (executionResult instanceof Promise)
47 result.promises.push(executionResult);
48 result.count++;
49 }
50 });
51 }
52 };
53 /**
54 * Broadcasts "BEFORE_UPDATE" event.
55 * Before update event is executed before entity is being updated in the database.
56 * All subscribers and entity listeners who listened to this event will be executed at this point.
57 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
58 *
59 * Note: this method has a performance-optimized code organization, do not change code structure.
60 */
61 Broadcaster.prototype.broadcastBeforeUpdateEvent = function (result, metadata, entity, databaseEntity, updatedColumns, updatedRelations) {
62 var _this = this;
63 if (entity && metadata.beforeUpdateListeners.length) {
64 metadata.beforeUpdateListeners.forEach(function (listener) {
65 if (listener.isAllowed(entity)) {
66 var executionResult = listener.execute(entity);
67 if (executionResult instanceof Promise)
68 result.promises.push(executionResult);
69 result.count++;
70 }
71 });
72 }
73 if (this.queryRunner.connection.subscribers.length) {
74 this.queryRunner.connection.subscribers.forEach(function (subscriber) {
75 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.beforeUpdate) {
76 var executionResult = subscriber.beforeUpdate({
77 connection: _this.queryRunner.connection,
78 queryRunner: _this.queryRunner,
79 manager: _this.queryRunner.manager,
80 entity: entity,
81 metadata: metadata,
82 databaseEntity: databaseEntity,
83 updatedColumns: updatedColumns || [],
84 updatedRelations: updatedRelations || []
85 });
86 if (executionResult instanceof Promise)
87 result.promises.push(executionResult);
88 result.count++;
89 }
90 });
91 }
92 };
93 /**
94 * Broadcasts "BEFORE_REMOVE" event.
95 * Before remove event is executed before entity is being removed from the database.
96 * All subscribers and entity listeners who listened to this event will be executed at this point.
97 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
98 *
99 * Note: this method has a performance-optimized code organization, do not change code structure.
100 */
101 Broadcaster.prototype.broadcastBeforeRemoveEvent = function (result, metadata, entity, databaseEntity) {
102 var _this = this;
103 if (entity && metadata.beforeRemoveListeners.length) {
104 metadata.beforeRemoveListeners.forEach(function (listener) {
105 if (listener.isAllowed(entity)) {
106 var executionResult = listener.execute(entity);
107 if (executionResult instanceof Promise)
108 result.promises.push(executionResult);
109 result.count++;
110 }
111 });
112 }
113 if (this.queryRunner.connection.subscribers.length) {
114 this.queryRunner.connection.subscribers.forEach(function (subscriber) {
115 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.beforeRemove) {
116 var executionResult = subscriber.beforeRemove({
117 connection: _this.queryRunner.connection,
118 queryRunner: _this.queryRunner,
119 manager: _this.queryRunner.manager,
120 entity: entity,
121 metadata: metadata,
122 databaseEntity: databaseEntity,
123 entityId: metadata.getEntityIdMixedMap(databaseEntity)
124 });
125 if (executionResult instanceof Promise)
126 result.promises.push(executionResult);
127 result.count++;
128 }
129 });
130 }
131 };
132 /**
133 * Broadcasts "AFTER_INSERT" event.
134 * After insert event is executed after entity is being persisted to the database for the first time.
135 * All subscribers and entity listeners who listened to this event will be executed at this point.
136 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
137 *
138 * Note: this method has a performance-optimized code organization, do not change code structure.
139 */
140 Broadcaster.prototype.broadcastAfterInsertEvent = function (result, metadata, entity) {
141 var _this = this;
142 if (entity && metadata.afterInsertListeners.length) {
143 metadata.afterInsertListeners.forEach(function (listener) {
144 if (listener.isAllowed(entity)) {
145 var executionResult = listener.execute(entity);
146 if (executionResult instanceof Promise)
147 result.promises.push(executionResult);
148 result.count++;
149 }
150 });
151 }
152 if (this.queryRunner.connection.subscribers.length) {
153 this.queryRunner.connection.subscribers.forEach(function (subscriber) {
154 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterInsert) {
155 var executionResult = subscriber.afterInsert({
156 connection: _this.queryRunner.connection,
157 queryRunner: _this.queryRunner,
158 manager: _this.queryRunner.manager,
159 entity: entity,
160 metadata: metadata
161 });
162 if (executionResult instanceof Promise)
163 result.promises.push(executionResult);
164 result.count++;
165 }
166 });
167 }
168 };
169 /**
170 * Broadcasts "AFTER_UPDATE" event.
171 * After update event is executed after entity is being updated in the database.
172 * All subscribers and entity listeners who listened to this event will be executed at this point.
173 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
174 *
175 * Note: this method has a performance-optimized code organization, do not change code structure.
176 */
177 Broadcaster.prototype.broadcastAfterUpdateEvent = function (result, metadata, entity, databaseEntity, updatedColumns, updatedRelations) {
178 var _this = this;
179 if (entity && metadata.afterUpdateListeners.length) {
180 metadata.afterUpdateListeners.forEach(function (listener) {
181 if (listener.isAllowed(entity)) {
182 var executionResult = listener.execute(entity);
183 if (executionResult instanceof Promise)
184 result.promises.push(executionResult);
185 result.count++;
186 }
187 });
188 }
189 if (this.queryRunner.connection.subscribers.length) {
190 this.queryRunner.connection.subscribers.forEach(function (subscriber) {
191 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterUpdate) {
192 var executionResult = subscriber.afterUpdate({
193 connection: _this.queryRunner.connection,
194 queryRunner: _this.queryRunner,
195 manager: _this.queryRunner.manager,
196 entity: entity,
197 metadata: metadata,
198 databaseEntity: databaseEntity,
199 updatedColumns: updatedColumns || [],
200 updatedRelations: updatedRelations || []
201 });
202 if (executionResult instanceof Promise)
203 result.promises.push(executionResult);
204 result.count++;
205 }
206 });
207 }
208 };
209 /**
210 * Broadcasts "AFTER_REMOVE" event.
211 * After remove event is executed after entity is being removed from the database.
212 * All subscribers and entity listeners who listened to this event will be executed at this point.
213 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
214 *
215 * Note: this method has a performance-optimized code organization, do not change code structure.
216 */
217 Broadcaster.prototype.broadcastAfterRemoveEvent = function (result, metadata, entity, databaseEntity) {
218 var _this = this;
219 if (entity && metadata.afterRemoveListeners.length) {
220 metadata.afterRemoveListeners.forEach(function (listener) {
221 if (listener.isAllowed(entity)) {
222 var executionResult = listener.execute(entity);
223 if (executionResult instanceof Promise)
224 result.promises.push(executionResult);
225 result.count++;
226 }
227 });
228 }
229 if (this.queryRunner.connection.subscribers.length) {
230 this.queryRunner.connection.subscribers.forEach(function (subscriber) {
231 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterRemove) {
232 var executionResult = subscriber.afterRemove({
233 connection: _this.queryRunner.connection,
234 queryRunner: _this.queryRunner,
235 manager: _this.queryRunner.manager,
236 entity: entity,
237 metadata: metadata,
238 databaseEntity: databaseEntity,
239 entityId: metadata.getEntityIdMixedMap(databaseEntity)
240 });
241 if (executionResult instanceof Promise)
242 result.promises.push(executionResult);
243 result.count++;
244 }
245 });
246 }
247 };
248 /**
249 * Broadcasts "AFTER_LOAD" event for all given entities, and their sub-entities.
250 * After load event is executed after entity has been loaded from the database.
251 * All subscribers and entity listeners who listened to this event will be executed at this point.
252 * Subscribers and entity listeners can return promises, it will wait until they are resolved.
253 *
254 * Note: this method has a performance-optimized code organization, do not change code structure.
255 */
256 Broadcaster.prototype.broadcastLoadEventsForAll = function (result, metadata, entities) {
257 var _this = this;
258 entities.forEach(function (entity) {
259 if (entity instanceof Promise) // todo: check why need this?
260 return;
261 // collect load events for all children entities that were loaded with the main entity
262 if (metadata.relations.length) {
263 metadata.relations.forEach(function (relation) {
264 // in lazy relations we cannot simply access to entity property because it will cause a getter and a database query
265 if (relation.isLazy && !entity.hasOwnProperty(relation.propertyName))
266 return;
267 var value = relation.getEntityValue(entity);
268 if (value instanceof Object)
269 _this.broadcastLoadEventsForAll(result, relation.inverseEntityMetadata, value instanceof Array ? value : [value]);
270 });
271 }
272 if (metadata.afterLoadListeners.length) {
273 metadata.afterLoadListeners.forEach(function (listener) {
274 if (listener.isAllowed(entity)) {
275 var executionResult = listener.execute(entity);
276 if (executionResult instanceof Promise)
277 result.promises.push(executionResult);
278 result.count++;
279 }
280 });
281 }
282 if (_this.queryRunner.connection.subscribers.length) {
283 _this.queryRunner.connection.subscribers.forEach(function (subscriber) {
284 if (_this.isAllowedSubscriber(subscriber, metadata.target) && subscriber.afterLoad) {
285 var executionResult = subscriber.afterLoad(entity, {
286 connection: _this.queryRunner.connection,
287 queryRunner: _this.queryRunner,
288 manager: _this.queryRunner.manager,
289 entity: entity,
290 metadata: metadata
291 });
292 if (executionResult instanceof Promise)
293 result.promises.push(executionResult);
294 result.count++;
295 }
296 });
297 }
298 });
299 };
300 // -------------------------------------------------------------------------
301 // Protected Methods
302 // -------------------------------------------------------------------------
303 /**
304 * Checks if subscriber's methods can be executed by checking if its don't listen to the particular entity,
305 * or listens our entity.
306 */
307 Broadcaster.prototype.isAllowedSubscriber = function (subscriber, target) {
308 return !subscriber.listenTo ||
309 !subscriber.listenTo() ||
310 subscriber.listenTo() === Object ||
311 subscriber.listenTo() === target ||
312 subscriber.listenTo().isPrototypeOf(target);
313 };
314 return Broadcaster;
315}());
316exports.Broadcaster = Broadcaster;
317
318//# sourceMappingURL=Broadcaster.js.map