Skip to content

Commit a7edca6

Browse files
#1073, change interval to timeout + fix core-serv
1 parent 6ae6ba5 commit a7edca6

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

arduino-ide-extension/src/node/core-service-impl.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
5757
.compile(request)
5858
.on('data', handler.onData)
5959
.on('error', (error) => {
60+
handler.dispose();
6061
if (!ServiceError.is(error)) {
6162
console.error(
6263
'Unexpected error occurred while compiling the sketch.',
@@ -83,8 +84,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
8384
reject(CoreError.VerifyFailed(message, compilerErrors));
8485
}
8586
})
86-
.on('end', resolve);
87-
}).finally(() => handler.dispose());
87+
.on('end', () => {
88+
handler.dispose();
89+
resolve();
90+
});
91+
});
8892
}
8993

9094
private compileRequest(
@@ -171,6 +175,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
171175
responseHandler(client, request)
172176
.on('data', handler.onData)
173177
.on('error', (error) => {
178+
handler.dispose();
174179
if (!ServiceError.is(error)) {
175180
console.error(`Unexpected error occurred while ${task}.`, error);
176181
reject(error);
@@ -193,9 +198,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
193198
);
194199
}
195200
})
196-
.on('end', resolve);
201+
.on('end', () => {
202+
handler.dispose();
203+
resolve();
204+
});
197205
}).finally(async () => {
198-
handler.dispose();
199206
await this.notifyUploadDidFinish(options);
200207
})
201208
);
@@ -238,6 +245,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
238245
.burnBootloader(request)
239246
.on('data', handler.onData)
240247
.on('error', (error) => {
248+
handler.dispose();
241249
if (!ServiceError.is(error)) {
242250
console.error(
243251
'Unexpected error occurred while burning the bootloader.',
@@ -258,9 +266,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
258266
);
259267
}
260268
})
261-
.on('end', resolve);
269+
.on('end', () => {
270+
handler.dispose();
271+
resolve();
272+
});
262273
}).finally(async () => {
263-
handler.dispose();
264274
await this.notifyUploadDidFinish(options);
265275
})
266276
);

arduino-ide-extension/src/node/utils/simple-buffer.ts

+37-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const DEFAULT_FLUS_TIMEOUT_MS = 32;
66
export class SimpleBuffer implements Disposable {
77
private readonly chunks = Chunks.create();
88
private readonly flush: () => void;
9-
private flushInterval?: NodeJS.Timeout;
9+
private flushTimeout?: NodeJS.Timeout;
10+
private disposed = false;
1011

1112
constructor(
1213
onFlush: (chunks: Map<OutputMessage.Severity, string | undefined>) => void,
@@ -19,7 +20,8 @@ export class SimpleBuffer implements Disposable {
1920
onFlush(chunks);
2021
}
2122
};
22-
this.flushInterval = setInterval(this.flush, flushTimeout);
23+
24+
this.setTimeoutVariable(flushTimeout);
2325
}
2426

2527
addChunk(
@@ -33,11 +35,40 @@ export class SimpleBuffer implements Disposable {
3335
Chunks.clear(this.chunks);
3436
}
3537

38+
private setTimeoutVariable(flushTimeout: number): void {
39+
let isDisposed = this.disposed;
40+
if (isDisposed) {
41+
// once "isDisposed" is true we stop
42+
// creating timeouts and do one more
43+
// flush AFTER any setTimeout
44+
// callback that may be in progress
45+
this.flush();
46+
isDisposed = false;
47+
return;
48+
}
49+
50+
if (!this.flushTimeout) {
51+
const onTimeout = () => {
52+
this.flush();
53+
this.clearTimeoutVariable();
54+
};
55+
56+
this.flushTimeout = setTimeout(() => {
57+
onTimeout();
58+
this.setTimeoutVariable(flushTimeout);
59+
}, flushTimeout);
60+
}
61+
}
62+
63+
private clearTimeoutVariable(): void {
64+
if (this.flushTimeout) {
65+
clearTimeout(this.flushTimeout);
66+
this.flushTimeout = undefined;
67+
}
68+
}
69+
3670
dispose(): void {
37-
this.flush();
38-
clearInterval(this.flushInterval);
39-
this.clearChunks();
40-
this.flushInterval = undefined;
71+
this.disposed = true;
4172
}
4273
}
4374

0 commit comments

Comments
 (0)