dsh-ui-smooth 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -173
- package/lib/client.js +1665 -1483
- package/lib/index.js +169 -248
- package/package.json +68 -68
- package/src/config.js +129 -140
- package/src/follow.js +68 -0
- package/src/keepawake.js +96 -70
- package/src/main.js +142 -136
- package/src/recycle.js +334 -262
- package/src/settings.js +201 -183
package/src/config.js
CHANGED
|
@@ -1,140 +1,129 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The host half owns the file at `$DSH_HOME/dsh-ui-smooth.json` and serves it over
|
|
3
|
-
* `/dsh-ui-smooth/config`; the page keeps a copy, publishes it to every listener,
|
|
4
|
-
* and writes back through the same route.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const CONFIG_URL = '/dsh-ui-smooth/config'
|
|
8
|
-
const DEBUG_KEY = 'dsh-ui-smooth:debug'
|
|
9
|
-
|
|
10
|
-
/** Shown in the settings header and reported by the debug handle. */
|
|
11
|
-
const VERSION = '0.1.
|
|
12
|
-
|
|
13
|
-
/* ------------------------------------------------------------------ *
|
|
14
|
-
* Config
|
|
15
|
-
* ------------------------------------------------------------------ */
|
|
16
|
-
|
|
17
|
-
const DEFAULTS = {
|
|
18
|
-
animations: true,
|
|
19
|
-
backgroundThrottle: true,
|
|
20
|
-
duringStream: true,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let
|
|
28
|
-
const configListeners = new Set()
|
|
29
|
-
|
|
30
|
-
/** Read one tier of the thresholds; anything missing falls back to the default. */
|
|
31
|
-
function readTier(source, key) {
|
|
32
|
-
const tier = source?.[key] ?? {}
|
|
33
|
-
return {
|
|
34
|
-
enabled: tier.enabled !== false,
|
|
35
|
-
turns: numberOr(tier.turns, DEFAULTS[key].turns),
|
|
36
|
-
minutes: numberOr(tier.minutes, DEFAULTS[key].minutes)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** The aggressive tier keeps a tail and drops what has been quiet long enough. */
|
|
41
|
-
function readRelease(source) {
|
|
42
|
-
const tier = source?.release ?? {}
|
|
43
|
-
return {
|
|
44
|
-
enabled: tier.enabled === true,
|
|
45
|
-
turns: Math.max(2, numberOr(tier.turns, DEFAULTS.release.turns)),
|
|
46
|
-
minutes: numberOr(tier.minutes, DEFAULTS.release.minutes)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function cloneConfig(source) {
|
|
51
|
-
const base = source !== null && typeof source === 'object' ? source : {}
|
|
52
|
-
return {
|
|
53
|
-
animations: base.animations !== false,
|
|
54
|
-
backgroundThrottle: base.backgroundThrottle !== false,
|
|
55
|
-
duringStream: base.duringStream !== false,
|
|
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
|
-
if (body?.config !== undefined) publishConfig(body.config)
|
|
86
|
-
} catch (reason) {
|
|
87
|
-
/* a page without the host route keeps the defaults */
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async function saveConfig(patch) {
|
|
92
|
-
publishConfig({ ...config, ...patch })
|
|
93
|
-
try {
|
|
94
|
-
await fetch(CONFIG_URL, {
|
|
95
|
-
method: 'POST',
|
|
96
|
-
headers: { 'content-type': 'application/json' },
|
|
97
|
-
body: JSON.stringify(config)
|
|
98
|
-
})
|
|
99
|
-
} catch (reason) {
|
|
100
|
-
/* the in-memory value still applies; the next page reads the stored one */
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** Whether the page asked for the console diagnostics. */
|
|
105
|
-
function debugEnabled() {
|
|
106
|
-
try {
|
|
107
|
-
return window.localStorage.getItem(DEBUG_KEY) === '1'
|
|
108
|
-
} catch (reason) {
|
|
109
|
-
return false
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
function
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
VERSION,
|
|
131
|
-
cloneConfig,
|
|
132
|
-
debugEnabled,
|
|
133
|
-
desktop,
|
|
134
|
-
getConfig: () => config,
|
|
135
|
-
hostElectron: () => hostElectron,
|
|
136
|
-
loadConfig,
|
|
137
|
-
onConfig,
|
|
138
|
-
publishConfig,
|
|
139
|
-
saveConfig
|
|
140
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The host half owns the file at `$DSH_HOME/dsh-ui-smooth.json` and serves it over
|
|
3
|
+
* `/dsh-ui-smooth/config`; the page keeps a copy, publishes it to every listener,
|
|
4
|
+
* and writes back through the same route.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const CONFIG_URL = '/dsh-ui-smooth/config'
|
|
8
|
+
const DEBUG_KEY = 'dsh-ui-smooth:debug'
|
|
9
|
+
|
|
10
|
+
/** Shown in the settings header and reported by the debug handle. */
|
|
11
|
+
const VERSION = '0.1.1'
|
|
12
|
+
|
|
13
|
+
/* ------------------------------------------------------------------ *
|
|
14
|
+
* Config
|
|
15
|
+
* ------------------------------------------------------------------ */
|
|
16
|
+
|
|
17
|
+
const DEFAULTS = {
|
|
18
|
+
animations: true,
|
|
19
|
+
backgroundThrottle: true,
|
|
20
|
+
duringStream: true,
|
|
21
|
+
follow: true,
|
|
22
|
+
skip: { enabled: true, turns: 3, minutes: 5 },
|
|
23
|
+
unmount: { enabled: true, turns: 5, minutes: 16 },
|
|
24
|
+
release: { enabled: false, turns: 8, minutes: 20 }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let config = cloneConfig(DEFAULTS)
|
|
28
|
+
const configListeners = new Set()
|
|
29
|
+
|
|
30
|
+
/** Read one tier of the thresholds; anything missing falls back to the default. */
|
|
31
|
+
function readTier(source, key) {
|
|
32
|
+
const tier = source?.[key] ?? {}
|
|
33
|
+
return {
|
|
34
|
+
enabled: tier.enabled !== false,
|
|
35
|
+
turns: numberOr(tier.turns, DEFAULTS[key].turns),
|
|
36
|
+
minutes: numberOr(tier.minutes, DEFAULTS[key].minutes)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The aggressive tier keeps a tail and drops what has been quiet long enough. */
|
|
41
|
+
function readRelease(source) {
|
|
42
|
+
const tier = source?.release ?? {}
|
|
43
|
+
return {
|
|
44
|
+
enabled: tier.enabled === true,
|
|
45
|
+
turns: Math.max(2, numberOr(tier.turns, DEFAULTS.release.turns)),
|
|
46
|
+
minutes: numberOr(tier.minutes, DEFAULTS.release.minutes)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function cloneConfig(source) {
|
|
51
|
+
const base = source !== null && typeof source === 'object' ? source : {}
|
|
52
|
+
return {
|
|
53
|
+
animations: base.animations !== false,
|
|
54
|
+
backgroundThrottle: base.backgroundThrottle !== false,
|
|
55
|
+
duringStream: base.duringStream !== false,
|
|
56
|
+
follow: base.follow !== false,
|
|
57
|
+
skip: readTier(base, 'skip'),
|
|
58
|
+
unmount: readTier(base, 'unmount'),
|
|
59
|
+
release: readRelease(base)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function numberOr(value, fallback) {
|
|
64
|
+
const number = Number(value)
|
|
65
|
+
if (!Number.isFinite(number) || number < 0) return fallback
|
|
66
|
+
return Math.min(10000, Math.floor(number))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function publishConfig(next) {
|
|
70
|
+
config = cloneConfig(next)
|
|
71
|
+
for (const listener of configListeners) {
|
|
72
|
+
try {
|
|
73
|
+
listener(config)
|
|
74
|
+
} catch (reason) {
|
|
75
|
+
/* a settings panel that threw must not stop the rest */
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function loadConfig() {
|
|
81
|
+
try {
|
|
82
|
+
const response = await fetch(CONFIG_URL, { headers: { accept: 'application/json' } })
|
|
83
|
+
if (!response.ok) return
|
|
84
|
+
const body = await response.json()
|
|
85
|
+
if (body?.config !== undefined) publishConfig(body.config)
|
|
86
|
+
} catch (reason) {
|
|
87
|
+
/* a page without the host route keeps the defaults */
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function saveConfig(patch) {
|
|
92
|
+
publishConfig({ ...config, ...patch })
|
|
93
|
+
try {
|
|
94
|
+
await fetch(CONFIG_URL, {
|
|
95
|
+
method: 'POST',
|
|
96
|
+
headers: { 'content-type': 'application/json' },
|
|
97
|
+
body: JSON.stringify(config)
|
|
98
|
+
})
|
|
99
|
+
} catch (reason) {
|
|
100
|
+
/* the in-memory value still applies; the next page reads the stored one */
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Whether the page asked for the console diagnostics. */
|
|
105
|
+
function debugEnabled() {
|
|
106
|
+
try {
|
|
107
|
+
return window.localStorage.getItem(DEBUG_KEY) === '1'
|
|
108
|
+
} catch (reason) {
|
|
109
|
+
return false
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Subscribe to config changes; returns the unsubscribe. */
|
|
114
|
+
function onConfig(listener) {
|
|
115
|
+
configListeners.add(listener)
|
|
116
|
+
return () => configListeners.delete(listener)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = {
|
|
120
|
+
DEFAULTS,
|
|
121
|
+
VERSION,
|
|
122
|
+
cloneConfig,
|
|
123
|
+
debugEnabled,
|
|
124
|
+
getConfig: () => config,
|
|
125
|
+
loadConfig,
|
|
126
|
+
onConfig,
|
|
127
|
+
publishConfig,
|
|
128
|
+
saveConfig
|
|
129
|
+
}
|
package/src/follow.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Come back to the tail.
|
|
3
|
+
*
|
|
4
|
+
* A hidden window produces no frames, so the transcript keeps growing unseen
|
|
5
|
+
* while the scroll offset stays where the reader left it: the burst that lands
|
|
6
|
+
* on return is added above the viewport, and the newest answer sits below it.
|
|
7
|
+
*
|
|
8
|
+
* A reader who was at the bottom before the window left wanted to see that
|
|
9
|
+
* answer, so returning puts them back on it — one scroll past the frames the
|
|
10
|
+
* renderer queued while hidden, which is after the app's own catch-up. A reader
|
|
11
|
+
* who was somewhere in the history keeps their place: the offset they were
|
|
12
|
+
* reading at is the one thing that must not move.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { getConfig } = require('./config.js')
|
|
16
|
+
|
|
17
|
+
const SCROLL_SELECTOR = '[data-conversation-scroll]'
|
|
18
|
+
|
|
19
|
+
/** Slack allowed when deciding the reader was at the bottom. */
|
|
20
|
+
const TAIL_SLACK = 24
|
|
21
|
+
|
|
22
|
+
function createFollow() {
|
|
23
|
+
/** Whether the reader was at the bottom when the window left; null until it does. */
|
|
24
|
+
let pinned = null
|
|
25
|
+
|
|
26
|
+
function remember() {
|
|
27
|
+
if (getConfig().follow === false) {
|
|
28
|
+
pinned = null
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
const scroller = document.querySelector(SCROLL_SELECTOR)
|
|
32
|
+
if (scroller === null) {
|
|
33
|
+
pinned = null
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
pinned = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight <= TAIL_SLACK
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function land() {
|
|
40
|
+
const scroller = document.querySelector(SCROLL_SELECTOR)
|
|
41
|
+
if (scroller === null) return
|
|
42
|
+
scroller.scrollTop = scroller.scrollHeight
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function onVisibility() {
|
|
46
|
+
if (document.visibilityState === 'hidden') {
|
|
47
|
+
remember()
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
const wasPinned = pinned
|
|
51
|
+
pinned = null
|
|
52
|
+
if (wasPinned !== true) return
|
|
53
|
+
// Two frames: the first lets the app finish the paint it owed the reader,
|
|
54
|
+
// the second lands on the tail that paint produced.
|
|
55
|
+
requestAnimationFrame(() => {
|
|
56
|
+
land()
|
|
57
|
+
requestAnimationFrame(land)
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
document.addEventListener('visibilitychange', onVisibility)
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
dispose: () => document.removeEventListener('visibilitychange', onVisibility)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = { createFollow }
|
package/src/keepawake.js
CHANGED
|
@@ -1,70 +1,96 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Keep the
|
|
3
|
-
*
|
|
4
|
-
* Chromium
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
function createKeepAwake() {
|
|
18
|
-
let context = null
|
|
19
|
-
let source = null
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (typeof AudioContext !== 'function') return false
|
|
25
|
-
try {
|
|
26
|
-
const next = new AudioContext()
|
|
27
|
-
const gain = next.createGain()
|
|
28
|
-
gain.gain.value = INAUDIBLE_GAIN
|
|
29
|
-
const oscillator = next.createOscillator()
|
|
30
|
-
oscillator.frequency.value = INAUDIBLE_HZ
|
|
31
|
-
oscillator.connect(gain)
|
|
32
|
-
gain.connect(next.destination)
|
|
33
|
-
oscillator.start()
|
|
34
|
-
context = next
|
|
35
|
-
source = oscillator
|
|
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
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Keep the page running while it is not in front.
|
|
3
|
+
*
|
|
4
|
+
* Every Chromium browser, and the Electron shell around DSH, throttles a
|
|
5
|
+
* renderer whose window is hidden or covered: timers slow to a crawl and frames
|
|
6
|
+
* stop, so a streamed answer piles up and lands in one burst when the reader
|
|
7
|
+
* comes back. A page that is playing audio is exempt from that throttling,
|
|
8
|
+
* wherever it runs, so the switch holds an audio stream open — a 40 Hz
|
|
9
|
+
* oscillator at a gain far below audibility, which adds no sound.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const INAUDIBLE_GAIN = 0.0001
|
|
13
|
+
const INAUDIBLE_HZ = 40
|
|
14
|
+
|
|
15
|
+
const GESTURES = ['pointerdown', 'keydown', 'wheel', 'touchstart']
|
|
16
|
+
|
|
17
|
+
function createKeepAwake() {
|
|
18
|
+
let context = null
|
|
19
|
+
let source = null
|
|
20
|
+
let waiting = false
|
|
21
|
+
|
|
22
|
+
const start = () => {
|
|
23
|
+
if (context !== null) return false
|
|
24
|
+
if (typeof window.AudioContext !== 'function') return false
|
|
25
|
+
try {
|
|
26
|
+
const next = new window.AudioContext()
|
|
27
|
+
const gain = next.createGain()
|
|
28
|
+
gain.gain.value = INAUDIBLE_GAIN
|
|
29
|
+
const oscillator = next.createOscillator()
|
|
30
|
+
oscillator.frequency.value = INAUDIBLE_HZ
|
|
31
|
+
oscillator.connect(gain)
|
|
32
|
+
gain.connect(next.destination)
|
|
33
|
+
oscillator.start()
|
|
34
|
+
context = next
|
|
35
|
+
source = oscillator
|
|
36
|
+
wake()
|
|
37
|
+
return true
|
|
38
|
+
} catch (reason) {
|
|
39
|
+
context = null
|
|
40
|
+
source = null
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A context built before the page saw a gesture starts suspended, and a
|
|
47
|
+
* suspended context buys nothing: Chromium only exempts a renderer that is
|
|
48
|
+
* actually playing. Hold the first gesture and the return to the front as
|
|
49
|
+
* the moments to ask again.
|
|
50
|
+
*/
|
|
51
|
+
function wake() {
|
|
52
|
+
if (context === null || context.state !== 'suspended') {
|
|
53
|
+
waiting = false
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
context.resume().then(() => {
|
|
57
|
+
if (context !== null && context.state === 'running') waiting = false
|
|
58
|
+
}).catch(() => {})
|
|
59
|
+
if (waiting) return
|
|
60
|
+
waiting = true
|
|
61
|
+
for (const name of GESTURES) window.addEventListener(name, wake, { capture: true, once: true, passive: true })
|
|
62
|
+
document.addEventListener('visibilitychange', wake)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function unwind() {
|
|
66
|
+
for (const name of GESTURES) window.removeEventListener(name, wake, { capture: true })
|
|
67
|
+
document.removeEventListener('visibilitychange', wake)
|
|
68
|
+
waiting = false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const stop = () => {
|
|
72
|
+
if (context === null) return false
|
|
73
|
+
try {
|
|
74
|
+
source?.stop()
|
|
75
|
+
source?.disconnect()
|
|
76
|
+
context.close()
|
|
77
|
+
} catch (reason) {
|
|
78
|
+
/* a closed context has nothing left to release */
|
|
79
|
+
}
|
|
80
|
+
context = null
|
|
81
|
+
source = null
|
|
82
|
+
unwind()
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
start,
|
|
88
|
+
stop,
|
|
89
|
+
status: () => ({
|
|
90
|
+
active: context !== null,
|
|
91
|
+
state: context === null ? 'idle' : context.state
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = { createKeepAwake }
|