1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Exercise the real input/coast handlers with controlled frame timing.
import assert from 'node:assert/strict';
import { runInNewContext } from 'node:vm';
const source = await Bun.file('src/app.js').text();
const start = source.indexOf('(() => {', source.indexOf('// The feed scrolls like a phone'));
const end = source.indexOf("\nwindow.addEventListener('load'", start);
assert(start > 0 && end > start);
function fixture(inside = '', { feedOnPage = true } = {}) {
let now = 100, id = 0;
const handlers = new Map(), frames = new Map(), steps = [];
const window = { scrollY: 5000,
addEventListener: (name, fn) => handlers.set(name, fn),
scrollBy: ({ top }) => { steps.push(top); window.scrollY += top; },
};
// the page shows a feed (or, for a thread case, a thread) unless told otherwise
const document = { body: {}, addEventListener() {}, removeEventListener() {},
querySelector: (selector) => (feedOnPage && selector.split(/,\s*/).some((p) => p === (inside === '.thread-page' ? '.thread-page' : '.feed-page')) ? {} : null) };
// the target sits inside the given element (over the posts unless it names something else)
const target = { closest: (selector) => selector.split(/,\s*/).some((p) => p === inside) ? target : null,
scrollHeight: 100, clientHeight: 100, parentElement: document.body };
runInNewContext(source.slice(start, end), { window, document,
matchMedia: () => ({ matches: false }), performance: { now: () => now },
requestAnimationFrame: (fn) => { frames.set(++id, fn); return id; },
cancelAnimationFrame: (id) => frames.delete(id), setTimeout, clearTimeout,
});
return {
get y() { return window.scrollY; }, steps,
wheel(dir) { handlers.get('wheel')({ deltaY: dir * 100, wheelDeltaY: -dir * 120,
deltaMode: 0, deltaX: 0, target, timeStamp: now, preventDefault() {} }); },
advance(ms) { now += ms; },
frame(stamp = now) { const callbacks = [...frames.values()]; frames.clear(); for (const fn of callbacks) fn(stamp); },
};
}
const settle = (f, n = 70) => { for (let i = 0; i < n; i++) { f.advance(17); f.frame(); } };
for (const dir of [1, -1]) {
const f = fixture();
f.wheel(dir); f.advance(17); f.frame();
f.wheel(dir); f.advance(17); f.frame();
// A busy main thread has missed several frames when the turn back arrives.
f.advance(120);
const before = f.y;
// [flying: the first turn back is a brake]
f.wheel(-dir);
assert.equal(f.y, before, 'the brake notch moves nothing by itself');
f.frame(0); // a queued animation frame can carry an older timestamp
assert.equal(f.y, before, 'old frame time moves nothing');
settle(f, 30);
const over = (f.y - before) * dir;
assert(over >= 0 && over < 120, 'the brake eases the flight out within a short way, never the other way: ' + over);
// [the next notch back: the gentle start]
const stopped = f.y;
f.wheel(-dir);
assert((stopped - f.y) * dir > 0 && (stopped - f.y) * dir < 20, 'the gentle start responds at once with a small step');
settle(f);
const back = (stopped - f.y) * dir;
assert(back > 130 && back < 170, 'the notch after the brake creeps back half a notch: ' + back);
// [then the wind-up]
{ const y = f.y; f.wheel(-dir); f.advance(60); f.frame(); f.wheel(-dir); settle(f);
const on = (y - f.y) * dir; assert(on > 600, 'the notches after wind up: ' + on); }
// The gentle start waits a while for its notch: a second and a half after the brake, still gentle.
{ f.advance(2500); f.wheel(dir); f.advance(17); f.frame(); f.wheel(dir); f.advance(120); f.frame();
f.wheel(-dir); settle(f, 30); f.advance(1500); const y = f.y; f.wheel(-dir); settle(f);
const s = (y - f.y) * dir; assert(s > 130 && s < 170, 'a notch back a while after the brake is still the gentle start: ' + s); }
// ...but long after, the brake forgotten, a notch back is a plain push from rest.
{ f.advance(2500); f.wheel(dir); f.advance(17); f.frame(); f.wheel(dir); f.advance(120); f.frame();
f.wheel(-dir); settle(f, 30); f.advance(3000); const y = f.y; f.wheel(-dir); settle(f);
const s = (y - f.y) * dir; assert(s > 280 && s < 330, 'long after the brake, a notch back is a full push: ' + s); }
// A stroll (one notch) turned back has nothing worth braking: the gentle start at once.
{ f.advance(2500); f.wheel(dir); f.advance(100); f.frame(); const y = f.y; f.wheel(-dir);
assert((y - f.y) * dir > 0 && (y - f.y) * dir < 20, 'against a stroll the turn back starts at once, with a small step');
settle(f); const s = (y - f.y) * dir; assert(s > 130 && s < 170, 'against a stroll the turn back is the gentle start: ' + s); }
// From rest a notch either way is a full push.
{ f.advance(2500); const y = f.y; f.wheel(dir); settle(f); const s = (f.y - y) * dir; assert(s > 280 && s < 330, 'from rest a notch either way is a full push: ' + s); }
// A turn back against a coast that only just died is the gentle start too.
{ f.wheel(dir); settle(f, 40); f.advance(100); const y = f.y; f.wheel(-dir); settle(f); const s = (y - f.y) * dir; assert(s > 130 && s < 170, 'reversing right after the coast dies is the gentle start: ' + s); }
// Well after the coast died it is a plain notch from rest, whichever way.
{ f.advance(2500); const y = f.y; f.wheel(-dir); settle(f); const s = (y - f.y) * dir; assert(s > 280 && s < 330, 'a notch the other way from rest is a full push: ' + s); }
}
// A notch over a video (or an audio player) in the feed coasts like any
// other — the wheel does nothing to a player itself — and so does one on a
// thread page, and one with the pointer anywhere else on the page while a
// feed is up (the rail, the wallet column, the header).
for (const sel of ['video', 'audio', '.thread-page', '.wallet-column', '']) {
const f = fixture(sel);
const y = f.y; f.wheel(1); settle(f);
assert(f.y - y > 280 && f.y - y < 330, 'a notch over ' + sel + ' coasts a full push: ' + (f.y - y));
}
// ...while a notch in a text box, or in the media pager, is left alone — and
// so is every notch on a page with no feed on it (the wallet).
for (const sel of ['textarea', '.media-pager']) {
const f = fixture(sel);
const y = f.y; f.wheel(1); settle(f);
assert.equal(f.y, y, 'a notch in ' + sel + ' is not ours');
}
{ const f = fixture('', { feedOnPage: false }); const y = f.y; f.wheel(1); settle(f); assert.equal(f.y, y, 'no feed on the page: the wheel is the browser\'s'); }
console.log('✓ a turn back against a flight brakes, the next notch is the gentle start, then the wind-up; against a stroll or a dead coast the gentle start at once; stale frames; videos and threads coast');
|