Skip to content

Commit 18022e5

Browse files
committed
#52: first phase of imporvement, skipped three iteration of the rFA
1 parent 498efb3 commit 18022e5

10 files changed

+47
-38
lines changed

src/metal/engine.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
import { EngineInterface } from './interfaces';
1313
import W from './window-proxy';
1414

15+
const NUM_SKIPPED_FRAMES = 3;
16+
1517
export class Engine implements EngineInterface {
1618
private reads: Array<Function> = [];
1719
private work: Array<Function> = [];
1820
private running: Boolean = false;
21+
private skipCounter: number = NUM_SKIPPED_FRAMES + 1;
1922
scheduleRead(callback: Function) {
2023
this.reads.unshift(callback);
2124
this.run();
@@ -28,14 +31,18 @@ export class Engine implements EngineInterface {
2831
if (!this.running) {
2932
this.running = true;
3033
W.rAF(() => {
31-
this.running = false;
32-
for (let i = 0, rlen = this.reads.length; i < rlen; i++) {
33-
this.reads.pop()();
34-
}
35-
for (let i = 0, wlen = this.work.length; i < wlen; i++) {
36-
this.work.pop()();
34+
if (this.skipCounter > NUM_SKIPPED_FRAMES) {
35+
this.skipCounter = 0;
36+
for (let i = 0, rlen = this.reads.length; i < rlen; i++) {
37+
this.reads.pop()();
38+
}
39+
for (let i = 0, wlen = this.work.length; i < wlen; i++) {
40+
this.work.pop()();
41+
}
3742
}
43+
this.skipCounter++;
3844
if (this.work.length > 0 || this.reads.length > 0) {
45+
this.running = false;
3946
this.run();
4047
}
4148
});

test/constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const constants = {
44
RAF_THRESHOLD : 16,
55
SMALL: 5
66
},
7-
ITEM_TO_OBSERVE: 5
7+
ITEM_TO_OBSERVE: 5,
8+
NUM_SKIPPED_FRAMES: 3
89
};
910

1011

test/headless/specs/intersection-observer.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ testModule('IntersectionObserver', class extends TestClass {
2424
});
2525
observer.observe(target);
2626
})
27-
.wait(50)
27+
.wait(100)
2828
.getExecution()
2929
.evaluate(function() {
3030
return window.STATE.impressions;
@@ -44,7 +44,7 @@ testModule('IntersectionObserver', class extends TestClass {
4444
});
4545
observer.observe(target);
4646
})
47-
.wait(50)
47+
.wait(100)
4848
.getExecution()
4949
.evaluate(function() {
5050
return window.STATE.impressions;
@@ -154,7 +154,7 @@ testModule('IntersectionObserver', class extends TestClass {
154154
});
155155
window.observer.observe(window.target);
156156
})
157-
.wait(50)
157+
.wait(100)
158158
.evaluate(function() {
159159
window.observer.unobserve(window.target);
160160
})
@@ -184,7 +184,7 @@ testModule('IntersectionObserver', class extends TestClass {
184184
window.observer.observe(target2);
185185
window.observer.observe(target3);
186186
})
187-
.wait(50)
187+
.wait(100)
188188
.evaluate(function() {
189189
window.observer.disconnect();
190190
})
@@ -214,15 +214,15 @@ testModule('IntersectionObserver', class extends TestClass {
214214
window.observer.observe(target2);
215215
window.observer.observe(target3);
216216
})
217-
.wait(50)
217+
.wait(100)
218218
.evaluate(function() {
219219
window.observer.disconnect();
220220
})
221-
.wait(50)
221+
.wait(100)
222222
.evaluate(function() {
223223
window.observer.observe(document.querySelector('.tracked-item[data-id="1"]'));
224224
})
225-
.wait(50)
225+
.wait(100)
226226
.scrollTo(500)
227227
.wait(50)
228228
.scrollTo(0)

test/headless/specs/spaniel-observer.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ testModule('SpanielObserver', class extends SpanielObserverTestClass {
5050
}
5151
['@test observing a visible element should fire after threshold time has passed']() {
5252
return this.setupTest()
53-
.wait(250)
53+
.wait(300)
5454
.getExecution()
5555
.evaluate(function() {
5656
return window.STATE.impressions;
@@ -64,7 +64,7 @@ testModule('SpanielObserver', class extends SpanielObserverTestClass {
6464
.evaluate(function() {
6565
window.observer.unobserve(window.target);
6666
})
67-
.wait(20)
67+
.wait(100)
6868
.getExecution()
6969
.evaluate(function() {
7070
return window.STATE.impressions === 1 && window.STATE.completes === 1;

test/headless/specs/utilities.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
import constants from './../../constants.js';
1414

15-
const { time: { RAF_THRESHOLD }, ITEM_TO_OBSERVE } = constants;
15+
const { time: { RAF_THRESHOLD }, ITEM_TO_OBSERVE, NUM_SKIPPED_FRAMES } = constants;
1616

1717
testModule('elementSatisfiesRatio', class extends TestClass {
1818
['@test passes true into callback when ratio satisfied']() {
@@ -79,11 +79,11 @@ testModule('Eventing', class extends TestClass {
7979
});
8080
})
8181
.scrollTo(10)
82-
.wait(RAF_THRESHOLD * 3)
82+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
8383
.scrollTo(20)
84-
.wait(RAF_THRESHOLD * 3)
84+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
8585
.getExecution()
86-
.wait(RAF_THRESHOLD)
86+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
8787
.evaluate(function() {
8888
return window.STATE.scrollEvents;
8989
}).then(function(result) {

test/headless/specs/watcher/exposed-event.spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
import constants from './../../../constants.js';
1414

15-
const { time: { RAF_THRESHOLD }, ITEM_TO_OBSERVE } = constants;
15+
const { time: { RAF_THRESHOLD }, ITEM_TO_OBSERVE, NUM_SKIPPED_FRAMES } = constants;
1616

1717
testModule('Watcher Exposed Event', class extends WatcherTestClass {
1818
['@test should not fire if item is not exposed']() {
@@ -40,13 +40,13 @@ testModule('Watcher Exposed Event', class extends WatcherTestClass {
4040
['@test should fire twice if moved in, out, and then back in viewport']() {
4141
return this.context.scrollTo(100)
4242
.scrollTo(140)
43-
.wait(RAF_THRESHOLD)
43+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
4444
.scrollTo(120)
45-
.wait(RAF_THRESHOLD)
45+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
4646
.scrollTo(0)
47-
.wait(RAF_THRESHOLD)
47+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
4848
.scrollTo(50)
49-
.wait(RAF_THRESHOLD)
49+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
5050
.assertEvent(ITEM_TO_OBSERVE, 'exposed', 2)
5151
.done();
5252
}

test/headless/specs/watcher/impression-complete-event.spec.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
import constants from './../../../constants.js';
1414

15-
const { time: { IMPRESSION_THRESHOLD, RAF_THRESHOLD }, ITEM_TO_OBSERVE } = constants;
15+
const { time: { IMPRESSION_THRESHOLD, RAF_THRESHOLD }, ITEM_TO_OBSERVE, NUM_SKIPPED_FRAMES } = constants;
1616

1717
testModule('Impression Complete event', class extends WatcherTestClass {
1818
['@test should not fire if item is not exposed']() {
@@ -47,6 +47,7 @@ testModule('Impression Complete event', class extends WatcherTestClass {
4747
return this.context.scrollTo(200)
4848
.wait(IMPRESSION_THRESHOLD + RAF_THRESHOLD * 4)
4949
.scrollTo(0)
50+
.wait(RAF_THRESHOLD * 3)
5051
.assertOnce(ITEM_TO_OBSERVE, 'impression-complete')
5152
.done();
5253
}
@@ -55,7 +56,7 @@ testModule('Impression Complete event', class extends WatcherTestClass {
5556
return this.context.scrollTo(200)
5657
.wait(IMPRESSION_THRESHOLD + RAF_THRESHOLD * 4)
5758
.unwatch(ITEM_TO_OBSERVE)
58-
.wait(RAF_THRESHOLD * 2)
59+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
5960
.assertOnce(ITEM_TO_OBSERVE, 'impression-complete')
6061
.done();
6162
}
@@ -64,9 +65,9 @@ testModule('Impression Complete event', class extends WatcherTestClass {
6465
return this.context.scrollTo(150)
6566
.wait(IMPRESSION_THRESHOLD * 5)
6667
.scrollTo(0)
67-
.wait(RAF_THRESHOLD)
68+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
6869
.assert(function(e) {
69-
return e.meta.duration >= 495 && e.meta.duration <= 545 && e.id === 5 && e.e === 'impression-complete';
70+
return e.meta.duration >= ((IMPRESSION_THRESHOLD * 5) - (RAF_THRESHOLD * NUM_SKIPPED_FRAMES)) && e.meta.duration <= ((IMPRESSION_THRESHOLD * 5) + (RAF_THRESHOLD * NUM_SKIPPED_FRAMES)) && e.id === 5 && e.e === 'impression-complete';
7071
}, 1)
7172
.done();
7273
}
@@ -90,7 +91,7 @@ testModule('Impression Complete event', class extends WatcherTestClass {
9091
.scrollTo(250)
9192
.wait(IMPRESSION_THRESHOLD + RAF_THRESHOLD * 4)
9293
.scrollTo(0)
93-
.wait(RAF_THRESHOLD)
94+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
9495
.assertEvent(ITEM_TO_OBSERVE, 'impression-complete', 2)
9596
.done();
9697
}

test/headless/specs/watcher/impression-event.spec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212

1313
import constants from './../../../constants.js';
1414

15-
const { time: { IMPRESSION_THRESHOLD, RAF_THRESHOLD, SMALL }, ITEM_TO_OBSERVE } = constants;
15+
const { time: { IMPRESSION_THRESHOLD, RAF_THRESHOLD, SMALL }, ITEM_TO_OBSERVE, NUM_SKIPPED_FRAMES } = constants;
1616

1717
testModule('Impression event', class extends WatcherTestClass {
1818
['@test should not fire if item is exposed but not impressed']() {
1919
return this.context.scrollTo(50)
20-
.wait(RAF_THRESHOLD * 2)
20+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
2121
.assertOnce(ITEM_TO_OBSERVE, 'exposed')
2222
.assertNever(ITEM_TO_OBSERVE, 'impressed')
2323
.assertOnce(ITEM_TO_OBSERVE, 'exposed')
@@ -34,7 +34,6 @@ testModule('Impression event', class extends WatcherTestClass {
3434
return this.context.scrollTo(150)
3535
.wait(RAF_THRESHOLD)
3636
.scrollTo(250)
37-
.wait(RAF_THRESHOLD)
3837
.scrollTo(0)
3938
.assertNever(ITEM_TO_OBSERVE, 'impressed').done();
4039
}
@@ -66,7 +65,7 @@ testModule('Impression event', class extends WatcherTestClass {
6665
.wait(RAF_THRESHOLD)
6766
.assertNever(ITEM_TO_OBSERVE, 'impressed')
6867
.scrollTo(200)
69-
.wait(IMPRESSION_THRESHOLD + RAF_THRESHOLD)
68+
.wait(IMPRESSION_THRESHOLD + RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
7069
.assertOnce(ITEM_TO_OBSERVE, 'impressed')
7170
.done();
7271
}

test/headless/specs/watcher/visible-event.spec.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212

1313
import constants from './../../../constants.js';
1414

15-
const { time: { RAF_THRESHOLD }, ITEM_TO_OBSERVE } = constants;
15+
const { time: { RAF_THRESHOLD }, ITEM_TO_OBSERVE, NUM_SKIPPED_FRAMES } = constants;
1616

1717
testModule('Visible event', class extends WatcherTestClass {
1818
['@test should not fire if item is exposed but not visible']() {
1919
return this.context.scrollTo(50)
20-
.wait(RAF_THRESHOLD * 2)
20+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
2121
.assertOnce(ITEM_TO_OBSERVE, 'exposed')
2222
.assertNever(ITEM_TO_OBSERVE, 'visible')
2323
.done();
@@ -36,6 +36,7 @@ testModule('Visible event', class extends WatcherTestClass {
3636
.scrollTo(300)
3737
.wait(RAF_THRESHOLD * 5)
3838
.scrollTo(250)
39+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
3940
.assertOnce(ITEM_TO_OBSERVE, 'visible')
4041
.done();
4142
}
@@ -50,7 +51,7 @@ testModule('Visible event', class extends WatcherTestClass {
5051
.scrollTo(10)
5152
.wait(RAF_THRESHOLD * 5)
5253
.scrollTo(200)
53-
.wait(RAF_THRESHOLD)
54+
.wait(RAF_THRESHOLD * NUM_SKIPPED_FRAMES)
5455
.assertEvent(ITEM_TO_OBSERVE, 'visible', 2)
5556
.done();
5657
}

test/setup/environment.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var runTest = function(threshold, options) {
1010
}
1111
options = options || {};
1212
var thresholds = [threshold];
13-
var timeout = 50 + (options.timeout || 0);
13+
var timeout = 100 + (options.timeout || 0);
1414
var entries = [];
1515
var target = options.target || document.createElement('div');
1616
target.style.height = '10px';

0 commit comments

Comments
 (0)