Skip to content

Commit cac7164

Browse files
committed
feat(DomController): organize dom reads/writes
1 parent 0af494e commit cac7164

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

src/module.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import { HttpModule } from '@angular/http';
1010
import { ActionSheetController } from './components/action-sheet/action-sheet';
1111
import { AlertController } from './components/alert/alert';
1212
import { App } from './components/app/app';
13+
import { AppRootToken } from './components/app/app-root';
14+
import { ClickBlock } from './util/click-block';
1315
import { Config, ConfigToken, setupConfig } from './config/config';
1416
import { DeepLinker, setupDeepLinker } from './navigation/deep-linker';
17+
import { DomController } from './util/dom-controller';
1518
import { Events, setupProvideEvents } from './util/events';
1619
import { Form } from './util/form';
1720
import { GestureController } from './gestures/gesture-controller';
@@ -31,9 +34,7 @@ import { ToastController } from './components/toast/toast';
3134
import { registerModeConfigs } from './config/mode-registry';
3235
import { registerTransitions } from './transitions/transition-registry';
3336
import { TransitionController } from './transitions/transition-controller';
34-
import { AppRootToken } from './components/app/app-root';
3537
import { UrlSerializer, setupUrlSerializer, DeepLinkConfigToken } from './navigation/url-serializer';
36-
import { ClickBlock } from './util/click-block';
3738
/**
3839
* Import Overlay Entry Components
3940
*/
@@ -51,6 +52,7 @@ import { ToastCmp } from './components/toast/toast-component';
5152
* Export Providers
5253
*/
5354
export { Config, setupConfig, ConfigToken } from './config/config';
55+
export { DomController } from './util/dom-controller';
5456
export { Platform, setupPlatform, UserAgentToken, DocumentDirToken, DocLangToken, NavigatorPlatformToken } from './platform/platform';
5557
export { Haptic } from './util/haptic';
5658
export { QueryParams, setupQueryParams, UrlToken } from './platform/query-params';
@@ -102,13 +104,13 @@ export { ViewController } from './navigation/view-controller';
102104
declarations: [
103105
ActionSheetCmp,
104106
AlertCmp,
107+
ClickBlock,
105108
IONIC_DIRECTIVES,
106109
LoadingCmp,
107110
ModalCmp,
108111
PickerCmp,
109112
PopoverCmp,
110-
ToastCmp,
111-
ClickBlock
113+
ToastCmp
112114
],
113115
entryComponents: [
114116
ActionSheetCmp,
@@ -154,7 +156,7 @@ export class IonicModule {
154156
// useFactory: ionic app initializers
155157
{ provide: APP_INITIALIZER, useFactory: registerModeConfigs, deps: [ Config ], multi: true },
156158
{ provide: APP_INITIALIZER, useFactory: registerTransitions, deps: [ Config ], multi: true },
157-
{ provide: APP_INITIALIZER, useFactory: setupProvideEvents, deps: [ Platform ], multi: true },
159+
{ provide: APP_INITIALIZER, useFactory: setupProvideEvents, deps: [ Platform, DomController ], multi: true },
158160
{ provide: APP_INITIALIZER, useFactory: setupTapClick, deps: [ Config, App, NgZone, GestureController ], multi: true },
159161

160162
// useClass
@@ -167,10 +169,11 @@ export class IonicModule {
167169
ActionSheetController,
168170
AlertController,
169171
App,
172+
DomController,
170173
Events,
171174
Form,
172-
Haptic,
173175
GestureController,
176+
Haptic,
174177
Keyboard,
175178
LoadingController,
176179
Location,

src/util/dom-controller.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Adopted from FastDom
3+
* https://github.com/wilsonpage/fastdom
4+
* MIT License
5+
*/
6+
import { nativeRaf } from './dom';
7+
import { removeArrayItem } from './util';
8+
9+
10+
export class DomController {
11+
private r: Function[] = [];
12+
private w: Function[] = [];
13+
private q: boolean;
14+
15+
read(fn: Function, ctx?: any): Function {
16+
const task = !ctx ? fn : fn.bind(ctx);
17+
this.r.push(task);
18+
this.queue();
19+
return task;
20+
}
21+
22+
write(fn: Function, ctx?: any): Function {
23+
const task = !ctx ? fn : fn.bind(ctx);
24+
this.w.push(task);
25+
this.queue();
26+
return task;
27+
}
28+
29+
cancel(task: any) {
30+
return removeArrayItem(this.r, task) || removeArrayItem(this.w, task);
31+
}
32+
33+
private queue() {
34+
if (!this.q) {
35+
this.q = true;
36+
nativeRaf(timestamp => {
37+
this.flush(timestamp);
38+
});
39+
}
40+
}
41+
42+
private flush(timestamp: number) {
43+
let err;
44+
let task;
45+
46+
try {
47+
// ******** DOM READS ****************
48+
while (task = this.r.shift()) {
49+
task(timestamp);
50+
}
51+
52+
// ******** DOM WRITES ****************
53+
while (task = this.w.shift()) {
54+
task(timestamp);
55+
}
56+
} catch (e) {
57+
err = e;
58+
}
59+
60+
this.q = false;
61+
62+
if (this.r.length || this.w.length) {
63+
this.queue();
64+
}
65+
66+
if (err) {
67+
throw err;
68+
}
69+
}
70+
71+
}

src/util/events.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DomController } from '../util/dom-controller';
12
import { nativeTimeout, nativeRaf } from '../util/dom';
23
import { Platform } from '../platform/platform';
34
import { ScrollView } from '../util/scroll-view';
@@ -109,7 +110,7 @@ export class Events {
109110
/**
110111
* @private
111112
*/
112-
export function setupEvents(platform: Platform): Events {
113+
export function setupEvents(platform: Platform, dom: DomController): Events {
113114
const events = new Events();
114115

115116
// start listening for resizes XXms after the app starts
@@ -134,7 +135,7 @@ export function setupEvents(platform: Platform): Events {
134135

135136
let content = <HTMLElement>el.closest('.scroll-content');
136137
if (content) {
137-
var scroll = new ScrollView(content);
138+
var scroll = new ScrollView(content, dom);
138139
// We need to stop scrolling if it's happening and scroll up
139140

140141
content.style['WebkitBackfaceVisibility'] = 'hidden';
@@ -173,8 +174,8 @@ export function setupEvents(platform: Platform): Events {
173174
/**
174175
* @private
175176
*/
176-
export function setupProvideEvents(platform: Platform) {
177+
export function setupProvideEvents(platform: Platform, dom: DomController) {
177178
return function() {
178-
return setupEvents(platform);
179+
return setupEvents(platform, dom);
179180
};
180181
}

0 commit comments

Comments
 (0)