UNPKG

9.1 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
9 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
10 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
11 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
12 return c > 3 && r && Object.defineProperty(target, key, r), r;
13};
14var __metadata = (this && this.__metadata) || function (k, v) {
15 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
16};
17var __param = (this && this.__param) || function (paramIndex, decorator) {
18 return function (target, key) { decorator(target, key, paramIndex); }
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21var logger_1 = require("./logger");
22var utils_1 = require("./utils");
23var context_1 = require("./context/context");
24var context_2 = require("./context/context");
25var gridOptionsWrapper_1 = require("./gridOptionsWrapper");
26var EventService = (function () {
27 function EventService() {
28 this.allSyncListeners = {};
29 this.allAsyncListeners = {};
30 this.globalSyncListeners = [];
31 this.globalAsyncListeners = [];
32 this.asyncFunctionsQueue = [];
33 this.scheduled = false;
34 }
35 EventService_1 = EventService;
36 // because this class is used both inside the context and outside the context, we do not
37 // use autowired attributes, as that would be confusing, as sometimes the attributes
38 // would be wired, and sometimes not.
39 //
40 // the global event servers used by ag-Grid is autowired by the context once, and this
41 // setBeans method gets called once.
42 //
43 // the times when this class is used outside of the context (eg RowNode has an instance of this
44 // class) then it is not a bean, and this setBeans method is not called.
45 EventService.prototype.setBeans = function (loggerFactory, gridOptionsWrapper, globalEventListener) {
46 if (globalEventListener === void 0) { globalEventListener = null; }
47 this.logger = loggerFactory.create('EventService');
48 if (globalEventListener) {
49 var async = gridOptionsWrapper.useAsyncEvents();
50 this.addGlobalListener(globalEventListener, async);
51 }
52 };
53 EventService.prototype.getListenerList = function (eventType, async) {
54 var listenerMap = async ? this.allAsyncListeners : this.allSyncListeners;
55 var listenerList = listenerMap[eventType];
56 if (!listenerList) {
57 listenerList = [];
58 listenerMap[eventType] = listenerList;
59 }
60 return listenerList;
61 };
62 EventService.prototype.addEventListener = function (eventType, listener, async) {
63 if (async === void 0) { async = false; }
64 var listenerList = this.getListenerList(eventType, async);
65 if (listenerList.indexOf(listener) < 0) {
66 listenerList.push(listener);
67 }
68 };
69 // for some events, it's important that the model gets to hear about them before the view,
70 // as the model may need to update before the view works on the info. if you register
71 // via this method, you get notified before the view parts
72 EventService.prototype.addModalPriorityEventListener = function (eventType, listener, async) {
73 if (async === void 0) { async = false; }
74 this.addEventListener(eventType + EventService_1.PRIORITY, listener, async);
75 };
76 EventService.prototype.addGlobalListener = function (listener, async) {
77 if (async === void 0) { async = false; }
78 if (async) {
79 this.globalAsyncListeners.push(listener);
80 }
81 else {
82 this.globalSyncListeners.push(listener);
83 }
84 };
85 EventService.prototype.removeEventListener = function (eventType, listener, async) {
86 if (async === void 0) { async = false; }
87 var listenerList = this.getListenerList(eventType, async);
88 utils_1.Utils.removeFromArray(listenerList, listener);
89 };
90 EventService.prototype.removeGlobalListener = function (listener, async) {
91 if (async === void 0) { async = false; }
92 if (async) {
93 utils_1.Utils.removeFromArray(this.globalAsyncListeners, listener);
94 }
95 else {
96 utils_1.Utils.removeFromArray(this.globalSyncListeners, listener);
97 }
98 };
99 // why do we pass the type here? the type is in ColumnChangeEvent, so unless the
100 // type is not in other types of events???
101 EventService.prototype.dispatchEvent = function (event) {
102 // console.log(`dispatching ${eventType}: ${event}`);
103 this.dispatchToListeners(event, true);
104 this.dispatchToListeners(event, false);
105 };
106 EventService.prototype.dispatchToListeners = function (event, async) {
107 var _this = this;
108 var globalListeners = async ? this.globalAsyncListeners : this.globalSyncListeners;
109 var eventType = event.type;
110 // this allows the columnController to get events before anyone else
111 var p1ListenerList = this.getListenerList(eventType + EventService_1.PRIORITY, async);
112 utils_1.Utils.forEachSnapshotFirst(p1ListenerList, function (listener) {
113 if (async) {
114 _this.dispatchAsync(function () { return listener(event); });
115 }
116 else {
117 listener(event);
118 }
119 });
120 var listenerList = this.getListenerList(eventType, async);
121 utils_1.Utils.forEachSnapshotFirst(listenerList, function (listener) {
122 if (async) {
123 _this.dispatchAsync(function () { return listener(event); });
124 }
125 else {
126 listener(event);
127 }
128 });
129 utils_1.Utils.forEachSnapshotFirst(globalListeners, function (listener) {
130 if (async) {
131 _this.dispatchAsync(function () { return listener(eventType, event); });
132 }
133 else {
134 listener(eventType, event);
135 }
136 });
137 };
138 // this gets called inside the grid's thread, for each event that it
139 // wants to set async. the grid then batches the events into one setTimeout()
140 // because setTimeout() is an expensive operation. ideally we would have
141 // each event in it's own setTimeout(), but we batch for performance.
142 EventService.prototype.dispatchAsync = function (func) {
143 // add to the queue for executing later in the next VM turn
144 this.asyncFunctionsQueue.push(func);
145 // check if timeout is already scheduled. the first time the grid calls
146 // this within it's thread turn, this should be false, so it will schedule
147 // the 'flush queue' method the first time it comes here. then the flag is
148 // set to 'true' so it will know it's already scheduled for subsequent calls.
149 if (!this.scheduled) {
150 // if not scheduled, schedule one
151 setTimeout(this.flushAsyncQueue.bind(this), 0);
152 // mark that it is scheduled
153 this.scheduled = true;
154 }
155 };
156 // this happens in the next VM turn only, and empties the queue of events
157 EventService.prototype.flushAsyncQueue = function () {
158 this.scheduled = false;
159 // we take a copy, because the event listener could be using
160 // the grid, which would cause more events, which would be potentially
161 // added to the queue, so safe to take a copy, the new events will
162 // get executed in a later VM turn rather than risk updating the
163 // queue as we are flushing it.
164 var queueCopy = this.asyncFunctionsQueue.slice();
165 this.asyncFunctionsQueue = [];
166 // execute the queue
167 queueCopy.forEach(function (func) { return func(); });
168 };
169 // this is an old idea niall had, should really take it out, was to do with ordering who gets to process
170 // events first, to give model and service objects preference over the view
171 EventService.PRIORITY = '-P1';
172 __decorate([
173 __param(0, context_2.Qualifier('loggerFactory')),
174 __param(1, context_2.Qualifier('gridOptionsWrapper')),
175 __param(2, context_2.Qualifier('globalEventListener')),
176 __metadata("design:type", Function),
177 __metadata("design:paramtypes", [logger_1.LoggerFactory,
178 gridOptionsWrapper_1.GridOptionsWrapper,
179 Function]),
180 __metadata("design:returntype", void 0)
181 ], EventService.prototype, "setBeans", null);
182 EventService = EventService_1 = __decorate([
183 context_1.Bean('eventService')
184 ], EventService);
185 return EventService;
186 var EventService_1;
187}());
188exports.EventService = EventService;