-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner-shimmer-methods.ts
More file actions
197 lines (179 loc) · 5.87 KB
/
spinner-shimmer-methods.ts
File metadata and controls
197 lines (179 loc) · 5.87 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
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @file Shimmer-configuration methods for the Socket `Spinner` class, split out
* of `create-spinner-class.ts` to keep that module under the file-size cap.
* The methods read and write the spinner's shimmer state through well-known
* symbols (`getShimmerSymbol`, `setShimmerSymbol`, `getSavedShimmerSymbol`,
* `setSavedShimmerSymbol`, `updateTextSymbol`) the class exposes, so they
* share the same private state without crossing the private-field boundary.
* `installShimmerMethods()` defines them on the prototype after the class is
* built.
*/
import type { ColorInherit, ColorValue } from '../colors/types'
import type { Palette, ShimmerConfig } from '../effects/shimmer'
import { COLOR_INHERIT } from './format'
import type { ShimmerInfo, SpinnerInstance } from './types'
/**
* Well-known symbol the spinner class exposes to read its current shimmer
* state.
*/
export const getShimmerSymbol: unique symbol = Symbol.for(
'socket.spinner.getShimmer',
)
/**
* Well-known symbol the spinner class exposes to replace its current shimmer
* state.
*/
export const setShimmerSymbol: unique symbol = Symbol.for(
'socket.spinner.setShimmer',
)
/**
* Well-known symbol the spinner class exposes to read the saved shimmer config
* that `enableShimmer()` restores from.
*/
export const getSavedShimmerSymbol: unique symbol = Symbol.for(
'socket.spinner.getSavedShimmer',
)
/**
* Well-known symbol the spinner class exposes to replace the saved shimmer
* config.
*/
export const setSavedShimmerSymbol: unique symbol = Symbol.for(
'socket.spinner.setSavedShimmer',
)
/**
* Well-known symbol the spinner class exposes for its `#updateSpinnerText`
* helper so the shimmer methods can trigger a re-render after a state change.
*/
export const updateTextSymbol: unique symbol = Symbol.for(
'socket.spinner.updateText',
)
/**
* Runtime shape of the spinner instance the shimmer methods operate on.
*/
export type ShimmerHost = SpinnerInstance & {
[getShimmerSymbol]: () => ShimmerInfo | undefined
[setShimmerSymbol]: (value: ShimmerInfo | undefined) => void
[getSavedShimmerSymbol]: () => ShimmerInfo | undefined
[setSavedShimmerSymbol]: (value: ShimmerInfo | undefined) => void
[updateTextSymbol]: () => void
}
/**
* Install the shimmer-configuration methods + `shimmerState` getter onto the
* spinner prototype. The methods reach the spinner's private shimmer state
* through the well-known symbols the class exposes.
*
* @param proto - The spinner class prototype to augment.
*/
export function installShimmerMethods(proto: object): void {
const target = proto as Record<PropertyKey, unknown>
function shimmerState(this: ShimmerHost): ShimmerInfo | undefined {
const shimmer = this[getShimmerSymbol]()
if (!shimmer) {
return undefined
}
return {
__proto__: null,
color: shimmer.color,
direction: shimmer.direction,
speed: shimmer.speed,
frame: shimmer.frame,
} as ShimmerInfo
}
function disableShimmer(this: ShimmerHost): SpinnerInstance {
// Clear the active shimmer; the saved config is left intact so
// enableShimmer() can bring it back.
this[setShimmerSymbol](undefined)
this[updateTextSymbol]()
return this
}
function enableShimmer(this: ShimmerHost): SpinnerInstance {
const saved = this[getSavedShimmerSymbol]()
if (saved) {
// Restore the saved config with a fresh frame counter.
this[setShimmerSymbol]({ ...saved, frame: 0 } as ShimmerInfo)
} else {
const next = {
__proto__: null,
color: COLOR_INHERIT,
direction: 'ltr',
speed: 1 / 3,
frame: 0,
} as ShimmerInfo
this[setShimmerSymbol](next)
this[setSavedShimmerSymbol](next)
}
this[updateTextSymbol]()
return this
}
function setShimmer(
this: ShimmerHost,
config: ShimmerConfig,
): SpinnerInstance {
const next = {
__proto__: null,
color:
(config.color as ColorInherit | ColorValue | Palette | undefined) ??
COLOR_INHERIT,
direction: config.dir ?? 'ltr',
speed: config.speed ?? 1 / 3,
frame: 0,
} as ShimmerInfo
this[setShimmerSymbol](next)
this[setSavedShimmerSymbol](next)
this[updateTextSymbol]()
return this
}
function updateShimmer(
this: ShimmerHost,
config: Partial<ShimmerConfig>,
): SpinnerInstance {
/* c8 ignore start - each partial-config field branch fires only when caller updates that field; shimmer-state cascade covers three init paths */
const partialConfig = {
__proto__: null,
...config,
} as Partial<ShimmerConfig>
const update: Partial<ShimmerInfo> = {
__proto__: null,
} as Partial<ShimmerInfo>
if (partialConfig.color !== undefined) {
update.color = partialConfig.color as ColorInherit | ColorValue | Palette
}
if (partialConfig.dir !== undefined) {
update.direction = partialConfig.dir
}
if (partialConfig.speed !== undefined) {
update.speed = partialConfig.speed
}
const shimmer = this[getShimmerSymbol]()
const saved = this[getSavedShimmerSymbol]()
let next: ShimmerInfo
if (shimmer) {
next = { ...shimmer, ...update } as ShimmerInfo
} else if (saved) {
next = { ...saved, ...update, frame: 0 } as ShimmerInfo
} else {
next = {
__proto__: null,
color: COLOR_INHERIT,
direction: 'ltr',
speed: 1 / 3,
frame: 0,
...update,
} as ShimmerInfo
}
this[setShimmerSymbol](next)
this[setSavedShimmerSymbol](next)
this[updateTextSymbol]()
return this
/* c8 ignore stop */
}
Object.defineProperty(target, 'shimmerState', {
configurable: true,
enumerable: false,
get: shimmerState,
})
target['disableShimmer'] = disableShimmer
target['enableShimmer'] = enableShimmer
target['setShimmer'] = setShimmer
target['updateShimmer'] = updateShimmer
}