1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import { Injectable } from '@angular/core';
|
7 | import { Platform } from './platform';
|
8 | import { removeArrayItem } from '../util/util';
|
9 |
|
10 |
|
11 |
|
12 | var DomDebouncer = (function () {
|
13 | |
14 |
|
15 |
|
16 | function DomDebouncer(dom) {
|
17 | this.dom = dom;
|
18 | this.writeTask = null;
|
19 | this.readTask = null;
|
20 | }
|
21 | |
22 |
|
23 |
|
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 |
|
37 |
|
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 |
|
51 |
|
52 | DomDebouncer.prototype.cancel = function () {
|
53 | var writeTask = this.writeTask;
|
54 | writeTask && this.dom.cancel(writeTask);
|
55 | var readTask = this.readTask;
|
56 | readTask && this.dom.cancel(readTask);
|
57 | this.readTask = this.writeTask = null;
|
58 | };
|
59 | return DomDebouncer;
|
60 | }());
|
61 | export { DomDebouncer };
|
62 | function DomDebouncer_tsickle_Closure_declarations() {
|
63 |
|
64 | DomDebouncer.prototype.writeTask;
|
65 |
|
66 | DomDebouncer.prototype.readTask;
|
67 |
|
68 | DomDebouncer.prototype.dom;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | var DomController = (function () {
|
74 | |
75 |
|
76 |
|
77 | function DomController(plt) {
|
78 | this.plt = plt;
|
79 | this.r = [];
|
80 | this.w = [];
|
81 | }
|
82 | |
83 |
|
84 |
|
85 | DomController.prototype.debouncer = function () {
|
86 | return new DomDebouncer(this);
|
87 | };
|
88 | |
89 |
|
90 |
|
91 |
|
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 |
|
109 |
|
110 |
|
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 |
|
128 |
|
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 |
|
140 |
|
141 | DomController.prototype._queue = function () {
|
142 | var 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 |
|
152 |
|
153 |
|
154 | DomController.prototype._flush = function (timeStamp) {
|
155 | var 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 | }());
|
172 | export { DomController };
|
173 | DomController.decorators = [
|
174 | { type: Injectable },
|
175 | ];
|
176 |
|
177 |
|
178 |
|
179 | DomController.ctorParameters = function () { return [
|
180 | { type: Platform, },
|
181 | ]; };
|
182 | function DomController_tsickle_Closure_declarations() {
|
183 |
|
184 | DomController.decorators;
|
185 | |
186 |
|
187 |
|
188 |
|
189 | DomController.ctorParameters;
|
190 |
|
191 | DomController.prototype.r;
|
192 |
|
193 | DomController.prototype.w;
|
194 |
|
195 | DomController.prototype.q;
|
196 |
|
197 | DomController.prototype.plt;
|
198 | }
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | function dispatch(timeStamp, r, w) {
|
206 | var fn;
|
207 |
|
208 | while (fn = r.shift()) {
|
209 | fn(timeStamp);
|
210 | }
|
211 |
|
212 | while (fn = w.shift()) {
|
213 | fn(timeStamp);
|
214 | }
|
215 | }
|
216 |
|
\ | No newline at end of file |