change cancelation in the async data loader to correctly cancel requests (#3516)

This commit is contained in:
Anthony Dresser
2018-12-10 16:29:02 -08:00
committed by GitHub
parent a92dd2d4e4
commit 8b447e361f

View File

@@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { IDisposableDataProvider } from 'sql/base/browser/ui/table/interfaces';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
export interface IObservableCollection<T> {
getLength(): number;
@@ -14,16 +15,12 @@ export interface IObservableCollection<T> {
dispose(): void;
}
class LoadCancellationToken {
isCancelled: boolean;
}
class DataWindow<T> {
private _data: T[];
private _length: number = 0;
private _offsetFromDataSource: number = -1;
private lastLoadCancellationToken: LoadCancellationToken;
private cancellationToken = new CancellationTokenSource();
constructor(
private loadFunction: (offset: number, count: number) => Thenable<T[]>,
@@ -36,9 +33,7 @@ class DataWindow<T> {
this.loadFunction = undefined;
this.placeholderItemGenerator = undefined;
this.loadCompleteCallback = undefined;
if (this.lastLoadCancellationToken) {
this.lastLoadCancellationToken.isCancelled = true;
}
this.cancellationToken.cancel();
}
public getStartIndex(): number {
@@ -65,17 +60,16 @@ class DataWindow<T> {
this._length = length;
this._data = undefined;
if (this.lastLoadCancellationToken) {
this.lastLoadCancellationToken.isCancelled = true;
}
this.cancellationToken.cancel();
this.cancellationToken = new CancellationTokenSource();
const currentCancellation = this.cancellationToken;
if (length === 0) {
return;
}
this.lastLoadCancellationToken = new LoadCancellationToken();
this.loadFunction(offset, length).then(data => {
if (!this.lastLoadCancellationToken.isCancelled) {
if (!currentCancellation.token.isCancellationRequested) {
this._data = data;
this.loadCompleteCallback(this._offsetFromDataSource, this._offsetFromDataSource + this._length);
}