plugin: api.setInterval, setTimeout
Massimo Melina committed
Feb 16, 2025 at 15:11 UTC
1578024f1415154db2af1098ae4e2e66334dfa7e
1 file changed
+12
src/plugins.ts
+12
@@ -115,6 +115,7 @@ export function getPluginConfigFields(id: string) {
115
116
async function initPlugin(pl: any, morePassedToInit?: { id: string } & Dict<any>) {
117
const undoEvents: any[] = []
118
+ const timeouts: NodeJS.Timer[] = []
119
const res = await pl.init?.({
120
Const,
121
require,
@@ -130,6 +131,16 @@ async function initPlugin(pl: any, morePassedToInit?: { id: string } & Dict<any>
131
log: console.log,
132
setError(msg: string) { setError(morePassedToInit?.id || 'server_code', msg) },
133
getHfsConfig: getConfig,
134
+ setInterval() { // @ts-ignore
135
+ const ret = setInterval(...arguments)
136
+ timeouts.push(ret) // intervals can be canceled by clearTimeout (source: MDN)
137
+ return ret
138
+ },
139
+ setTimeout() { // @ts-ignore
140
+ const ret = setTimeout(...arguments)
141
+ timeouts.push(ret)
142
+ return ret
143
+ },
144
customApiCall,
145
notifyClient,
146
addBlock,
@@ -142,6 +153,7 @@ async function initPlugin(pl: any, morePassedToInit?: { id: string } & Dict<any>
153
})
154
Object.assign(pl, typeof res === 'function' ? { unload: res } : res)
155
patchKey(pl, 'unload', was => () => {
156
+ for (const x of timeouts) clearTimeout(x)
157
for (const cb of undoEvents) cb()
158
if (typeof was === 'function')
159
return was(...arguments)