UNPKG

5.07 kBJavaScriptView Raw
1/**
2 * Adopted from FastDom
3 * https://github.com/wilsonpage/fastdom
4 * MIT License
5 */
6import { Injectable } from '@angular/core';
7import { Platform } from './platform';
8import { removeArrayItem } from '../util/util';
9/**
10 * @hidden
11 */
12var DomDebouncer = (function () {
13 /**
14 * @param {?} dom
15 */
16 function DomDebouncer(dom) {
17 this.dom = dom;
18 this.writeTask = null;
19 this.readTask = null;
20 }
21 /**
22 * @param {?} fn
23 * @return {?}
24 */
25 DomDebouncer.prototype.read = function (fn) {
26 var _this = this;
27 if (this.readTask) {
28 return;
29 }
30 return this.readTask = this.dom.read(function (t) {
31 _this.readTask = null;
32 fn(t);
33 });
34 };
35 /**
36 * @param {?} fn
37 * @return {?}
38 */
39 DomDebouncer.prototype.write = function (fn) {
40 var _this = this;
41 if (this.writeTask) {
42 return;
43 }
44 return this.writeTask = this.dom.write(function (t) {
45 _this.writeTask = null;
46 fn(t);
47 });
48 };
49 /**
50 * @return {?}
51 */
52 DomDebouncer.prototype.cancel = function () {
53 var /** @type {?} */ writeTask = this.writeTask;
54 writeTask && this.dom.cancel(writeTask);
55 var /** @type {?} */ readTask = this.readTask;
56 readTask && this.dom.cancel(readTask);
57 this.readTask = this.writeTask = null;
58 };
59 return DomDebouncer;
60}());
61export { DomDebouncer };
62function DomDebouncer_tsickle_Closure_declarations() {
63 /** @type {?} */
64 DomDebouncer.prototype.writeTask;
65 /** @type {?} */
66 DomDebouncer.prototype.readTask;
67 /** @type {?} */
68 DomDebouncer.prototype.dom;
69}
70/**
71 * @hidden
72 */
73var DomController = (function () {
74 /**
75 * @param {?} plt
76 */
77 function DomController(plt) {
78 this.plt = plt;
79 this.r = [];
80 this.w = [];
81 }
82 /**
83 * @return {?}
84 */
85 DomController.prototype.debouncer = function () {
86 return new DomDebouncer(this);
87 };
88 /**
89 * @param {?} fn
90 * @param {?=} timeout
91 * @return {?}
92 */
93 DomController.prototype.read = function (fn, timeout) {
94 var _this = this;
95 if (timeout) {
96 ((fn)).timeoutId = this.plt.timeout(function () {
97 _this.r.push(fn);
98 _this._queue();
99 }, timeout);
100 }
101 else {
102 this.r.push(fn);
103 this._queue();
104 }
105 return fn;
106 };
107 /**
108 * @param {?} fn
109 * @param {?=} timeout
110 * @return {?}
111 */
112 DomController.prototype.write = function (fn, timeout) {
113 var _this = this;
114 if (timeout) {
115 ((fn)).timeoutId = this.plt.timeout(function () {
116 _this.w.push(fn);
117 _this._queue();
118 }, timeout);
119 }
120 else {
121 this.w.push(fn);
122 this._queue();
123 }
124 return fn;
125 };
126 /**
127 * @param {?} fn
128 * @return {?}
129 */
130 DomController.prototype.cancel = function (fn) {
131 if (fn) {
132 if (fn.timeoutId) {
133 this.plt.cancelTimeout(fn.timeoutId);
134 }
135 removeArrayItem(this.r, fn) || removeArrayItem(this.w, fn);
136 }
137 };
138 /**
139 * @return {?}
140 */
141 DomController.prototype._queue = function () {
142 var /** @type {?} */ self = this;
143 if (!self.q) {
144 self.q = true;
145 self.plt.raf(function rafCallback(timeStamp) {
146 self._flush(timeStamp);
147 });
148 }
149 };
150 /**
151 * @param {?} timeStamp
152 * @return {?}
153 */
154 DomController.prototype._flush = function (timeStamp) {
155 var /** @type {?} */ err;
156 try {
157 dispatch(timeStamp, this.r, this.w);
158 }
159 catch (e) {
160 err = e;
161 }
162 this.q = false;
163 if (this.r.length || this.w.length) {
164 this._queue();
165 }
166 if (err) {
167 throw err;
168 }
169 };
170 return DomController;
171}());
172export { DomController };
173DomController.decorators = [
174 { type: Injectable },
175];
176/**
177 * @nocollapse
178 */
179DomController.ctorParameters = function () { return [
180 { type: Platform, },
181]; };
182function DomController_tsickle_Closure_declarations() {
183 /** @type {?} */
184 DomController.decorators;
185 /**
186 * @nocollapse
187 * @type {?}
188 */
189 DomController.ctorParameters;
190 /** @type {?} */
191 DomController.prototype.r;
192 /** @type {?} */
193 DomController.prototype.w;
194 /** @type {?} */
195 DomController.prototype.q;
196 /** @type {?} */
197 DomController.prototype.plt;
198}
199/**
200 * @param {?} timeStamp
201 * @param {?} r
202 * @param {?} w
203 * @return {?}
204 */
205function dispatch(timeStamp, r, w) {
206 var /** @type {?} */ fn;
207 // ******** DOM READS ****************
208 while (fn = r.shift()) {
209 fn(timeStamp);
210 }
211 // ******** DOM WRITES ****************
212 while (fn = w.shift()) {
213 fn(timeStamp);
214 }
215}
216//# sourceMappingURL=dom-controller.js.map
\No newline at end of file