-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.ts
More file actions
185 lines (156 loc) · 4.35 KB
/
Copy pathelements.ts
File metadata and controls
185 lines (156 loc) · 4.35 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { EventController } from './events.js'
interface ElementEventControllers {
consumed: EventController<void>
consumptionStateChanged: EventController<void>
}
export enum ElementArchetype {
audio = 'audio',
image = 'image',
text = 'text',
video = 'video',
}
export interface IArticleElement {
achieved: number
archetype: ElementArchetype
consumable: boolean
consumed: boolean
consuming: boolean
consumptionStartedAt?: number
consumptionTimeTotal: number
displayed: boolean
estimateFastestTime(): number
estimateSlowestTime(): number
events: ElementEventControllers
getRootElement(): HTMLElement
markDisplayed(): void
markInViewport(): void
markNotInViewport(): void
type: string
wordCount?: number
}
export abstract class ArticleElement implements IArticleElement {
static selector: string
abstract archetype: ElementArchetype
abstract type: string
consuming = false
consumptionStartedAt?: number
consumptionTimeTracked = 0
inViewport = false
displayed = false
el: HTMLElement
events: ElementEventControllers
constructor(el: HTMLElement) {
this.el = el
this.events = this.createEventControllers()
}
static getAll(articleEl: HTMLElement): HTMLElement[] {
return Array.from(
articleEl.querySelectorAll(this.selector),
) as HTMLElement[]
}
/** Get the absolute minimum expected time, the user could take, to consume
* the entire content of this element in miliseconds */
abstract estimateFastestTime(): number
/** Get the absolute maximum expected time, the user could take, to consume
* the entire content of this element in miliseconds */
abstract estimateSlowestTime(): number
abstract get consumptionTimeTotal(): number
get consumable(): boolean {
// ArticleElement is consumable by default
return true
}
get consumed(): boolean {
if (this.estimateFastestTime() <= 0) {
return false
}
return this.consumptionTimeTotal >= this.estimateFastestTime()
}
get achieved(): number {
const fastest = this.estimateFastestTime()
if (fastest === 0) {
return 0
}
return Math.min(1, this.consumptionTimeTotal / fastest)
}
createEventControllers(): ElementEventControllers {
const props = {}
return {
consumed: new EventController<void>(props),
consumptionStateChanged: new EventController<void>(props),
}
}
getRootElement(): HTMLElement {
return this.el
}
markDisplayed(): void {
// Ignore zero sized elements
if (this.el.clientHeight > 0 && this.el.clientWidth > 0) {
this.displayed = true
}
}
markInViewport(): void {
this.markDisplayed()
this.inViewport = true
}
markNotInViewport(): void {
this.inViewport = false
}
getLastConsumptionTime(): number {
if (this.consumptionStartedAt) {
return Date.now() - this.consumptionStartedAt
}
return 0
}
updateConsumptionMetrics(): void {
this.consumptionTimeTracked += this.getLastConsumptionTime()
this.consumptionStartedAt = undefined
}
recordConsumptionTime(): void {
if (this.consumable) {
this.updateConsumptionMetrics()
this.consumptionStartedAt = Date.now()
if (!this.consuming) {
this.consuming = true
this.events.consumptionStateChanged.debounce()
}
}
}
stopConsumption(): void {
if (this.consumable) {
this.updateConsumptionMetrics()
if (this.consuming) {
this.consuming = false
this.events.consumptionStateChanged.debounce()
}
}
}
}
export abstract class VisualArticleElement extends ArticleElement {
consumptionTimer?: ReturnType<typeof setTimeout>
get consumptionTimeTotal(): number {
return this.consumptionTimeTracked + this.getLastConsumptionTime()
}
markInViewport(): void {
super.markInViewport()
this.recordConsumptionTime()
}
markNotInViewport(): void {
super.markNotInViewport()
this.stopConsumption()
}
updateConsumptionMetrics(): void {
if (this.consumptionTimer) {
clearTimeout(this.consumptionTimer)
this.consumptionTimer = undefined
}
super.updateConsumptionMetrics()
}
recordConsumptionTime(): void {
super.recordConsumptionTime()
if (this.consumable && !this.consumed) {
this.consumptionTimer = setTimeout(() => {
this.events.consumed.now()
}, this.estimateFastestTime())
}
}
}