Repository navigation
Expand file tree
/
Copy pathTransactionLoadingState.ts
More file actions
141 lines (125 loc) · 3.73 KB
/
Copy pathTransactionLoadingState.ts
File metadata and controls
141 lines (125 loc) · 3.73 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
import {
BackwardCompatibleLoadingOptions,
BackwardCompatibleLoadingState,
} from '../internalRpcSchema'
import Logger from './Logger'
export interface TransactionLoadingStateConfig {
logger: Logger
send: (loadingState: BackwardCompatibleLoadingState) => Promise<void>
}
export default class TransactionLoadingState {
#logger: Logger
#sender: TransactionLoadingStateConfig['send']
#state: BackwardCompatibleLoadingState | undefined
#sendTimeout: NodeJS.Timeout | null = null
#sendTimeoutMs = 100
constructor(config: TransactionLoadingStateConfig) {
this.#sender = config.send
this.#logger = config.logger
}
async #sendState() {
if (!this.#sendTimeout) {
// Buffer send calls for 100ms to prevent accidental DoSing with
// many loading calls
this.#sendTimeout = setTimeout(() => {
this.#sender(this.#state ?? {}).catch(err => {
this.#logger.error('Failed sending loading state to Interval')
this.#logger.debug(err)
})
this.#sendTimeout = null
}, this.#sendTimeoutMs)
}
}
get state() {
return { ...this.#state }
}
/**
* Kicks off a loading spinner to provide context during any long-running action work. Can also be called with a single string argument as the label, or with no arguments to display only a spinner.
*
* **Usage:**
*
*```typescript
* await ctx.loading.start({
* label: "Reticulating splines...",
* });
*
* await ctx.loading.start("Label only shorthand");
*```
*/
async start(options?: string | BackwardCompatibleLoadingOptions) {
if (typeof options === 'string') {
options = { label: options }
} else if (options === undefined) {
options = {}
}
this.#state = { ...options }
if (this.#state.itemsInQueue) {
this.#state.itemsCompleted = 0
}
return this.#sendState()
}
/**
* Updates any existing loading spinner initated with `ctx.loading.start` to dynamically provide new loading information to the action runner.
*
* **Usage:**
*
*```typescript
* await ctx.loading.start({
* label: "Something is loading",
* description: "Mapping all the things",
* });
*
* await ctx.loading.update({
* label: "Something is loading",
* description: "Now reducing all the things",
* });
*```
*/
async update(options?: string | BackwardCompatibleLoadingOptions) {
if (!this.#state) {
this.#logger.warn('Please call `loading.start` before `loading.update`')
return this.start(options)
}
if (typeof options === 'string') {
options = { label: options }
} else if (options === undefined) {
options = {}
}
Object.assign(this.#state, options)
if (this.#state?.itemsInQueue && this.#state.itemsCompleted === undefined) {
this.#state.itemsCompleted = 0
}
return this.#sendState()
}
/**
* Marks a chunk of work as completed to dynamically provide granular loading progress. Can only be used after `ctx.loading.start` was called with `itemsInQueue`.
*
* **Usage:**
*
*```typescript
* await ctx.loading.start({
* label: "Migrating users",
* description: "Enabling edit button for selected users",
* itemsInQueue: 100,
* });
*
* for (const user of users) {
* migrateUser(user);
* await ctx.loading.completeOne();
* }
*```
*/
async completeOne() {
if (!this.#state || !this.#state.itemsInQueue) {
this.#logger.warn(
'Please call `loading.start` with `itemsInQueue` before `loading.completeOne`, nothing to complete.'
)
return
}
if (this.#state.itemsCompleted === undefined) {
this.#state.itemsCompleted = 0
}
this.#state.itemsCompleted++
return this.#sendState()
}
}