-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.ts
188 lines (176 loc) · 6.01 KB
/
index.ts
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
import {versionTipDialog} from './components/versionTipDialog'
import {
createWorker,
createWorkerFunc,
cancelUpdateFunc,
checkVersionTypeFunc,
} from './utils/index'
/**
* Polling monitoring version update (No longer maintain)
*
* @param {string} localPackageVersion the current version of the page in the browser
* @param {string} originVersionFileUrl remote server version file address
* @param {number} [pollingTime = 5000] polling interval, in ms (Optional)
* @param {function} onVersionUpdate callback when updating version, used when customizing UI (Optional)
* @return {object} { refreshPageVersion } new version number
*/
export const pollingCompareVersion = (
localPackageVersion: string,
originVersionFileUrl: string,
pollingTime: number,
onVersionUpdate?: (event: any) => void
) => {
const worker = createWorker(createWorkerFunc)
worker.postMessage({
'version-key': localPackageVersion,
'polling-time': pollingTime,
'origin-version-file-url': originVersionFileUrl,
})
worker.onmessage = (event: any) => {
// custom version tip UI
if (typeof onVersionUpdate === 'function') {
onVersionUpdate(event.data)
} else {
// default version tip ui
versionTipDialog({newVersion: event.data.refreshPageVersion})
}
}
}
/**
* Polling monitoring version update v2 (Recommended)
*
* @param {object} config Polling the configuration parameters of the monitoring version
* @param {string} config.originVersionFileUrl remote server version file address (Required)
* @param {string} config.localPackageVersion the current version of the page in the browser
* @param {number} [config.pollingTime = 5000] polling interval, in ms (Optional)
* @param {function} config.onVersionUpdate callback when updating version, used when customizing UI (Optional)
*
* @param {object} options Customize version update popup copy and themes
* @param {string} [options.title = 'Update'] popup title (Optional)
* @param {string} [options.description = 'V xxx is available'] popup description (Optional)
* @param {string} [options.buttonText = 'Refresh'] popup button text (Optional)
* @param {string} [options.cancelButtonText] close popup button text (Optional)
* @param {string} [options.cancelMode = 'ignore-current-version'] close popup button mode
* @param {string} [options.cancelUpdateAndStopWorker = false] close popup and stop worker
* @param {string} options.imageUrl custom popup image address (Optional)
* @param {string} options.rocketColor custom popup rocket color in the picture (Optional)
* @param {string} options.primaryColor custom popup primary color, act on image background color and button background color (Optional)
* @param {string} options.buttonStyle custom popup button style (Optional)
*
* @return {object} { refreshPageVersion } new version number
*/
let worker: Worker | undefined = undefined
export const checkVersion = (
config: {
checkOriginSpecifiedFilesUrl?: string[]
checkOriginSpecifiedFilesUrlMode?: 'one' | 'all'
originVersionFileUrl?: string
localPackageVersion?: string
pollingTime?: number
immediate?: boolean
enable?: boolean
clearIntervalOnDialog?: boolean
onVersionUpdate?: (event: any) => void
onRefresh?: (event: any) => void
onCancel?: (event: any) => void
},
options?: {
title?: string
description?: string
buttonText?: string
cancelButtonText?: string
cancelMode?: string
cancelUpdateAndStopWorker?: boolean
imageUrl?: string
rocketColor?: string
primaryColor?: string
buttonStyle?: string
}
) => {
if (config.enable === false) return
if (!worker) {
worker = createWorker(createWorkerFunc, [checkVersionTypeFunc])
}
const processUserInput = (input: any) => {
if (!input) {
return
}
if (!Array.isArray(input)) {
console.warn('Invalid input: Expected an array.')
return []
}
return [...new Set(input)].filter((item) => item != null)
}
worker.postMessage({
'version-key': config.localPackageVersion || '',
'polling-time': config.pollingTime || 5000,
immediate: config.immediate || false,
'origin-version-file-url': config.originVersionFileUrl || '',
'check-origin-specified-files-url': processUserInput(
config.checkOriginSpecifiedFilesUrl
),
'check-origin-specified-files-url-mode':
config.checkOriginSpecifiedFilesUrlMode || 'one',
'clear-interval-on-dialog': config.clearIntervalOnDialog || false,
})
worker.onmessage = (event: any) => {
const cancelUpdateLock = cancelUpdateFunc(
options?.cancelMode,
event.data?.refreshPageVersion,
options?.cancelUpdateAndStopWorker,
worker
)
if (cancelUpdateLock) return
localStorage.removeItem('version-rocket:cancelled')
sessionStorage.removeItem('version-rocket:cancelled')
// custom version tip UI
if (typeof config.onVersionUpdate === 'function') {
config.onVersionUpdate(event.data)
} else {
// default version tip ui
const {
title,
description,
buttonText,
cancelButtonText,
cancelMode,
imageUrl,
rocketColor,
primaryColor,
buttonStyle,
} = options || {}
const {onRefresh, onCancel} = config || {}
versionTipDialog({
title,
description,
buttonText,
cancelButtonText,
cancelMode,
imageUrl,
rocketColor,
primaryColor,
buttonStyle,
newVersion: event.data.refreshPageVersion,
needRefresh: event.data.refreshPageVersion,
onRefresh,
onCancel,
})
}
}
}
/**
* destroy checkVersion
*/
export const unCheckVersion = ({closeDialog = false, closeWorker = true}) => {
if (closeWorker) {
worker?.terminate()
worker = undefined
}
if (closeDialog) {
const dialogElement = document.querySelector('#version-rocket')
const dialogElementParent = dialogElement?.parentElement
if (dialogElement && dialogElementParent) {
dialogElementParent.removeChild(dialogElement)
}
}
}