vscode#CancellationTokenSource TypeScript Examples
The following examples show how to use
vscode#CancellationTokenSource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LookupControllerV3.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
public createCancelSource() {
const tokenSource = new CancellationTokenSource();
if (this._cancelTokenSource) {
this._cancelTokenSource.cancel();
this._cancelTokenSource.dispose();
}
this._cancelTokenSource = tokenSource;
return tokenSource;
}
Example #2
Source File: SchemaLookupProvider.ts From dendron with GNU Affero General Public License v3.0 | 6 votes |
async provide(opts: ProvideOpts) {
const { quickpick, token } = opts;
const onUpdatePickerItems = _.bind(this.onUpdatePickerItems, this);
const onUpdateDebounced = _.debounce(
() => {
onUpdatePickerItems({
picker: quickpick,
token: token.token,
} as OnUpdatePickerItemsOpts);
},
100,
{
leading: true,
maxWait: 200,
}
);
quickpick.onDidChangeValue(onUpdateDebounced);
quickpick.onDidAccept(async () => {
Logger.info({
ctx: "SchemaLookupProvider:onDidAccept",
quickpick: quickpick.value,
});
onUpdateDebounced.cancel();
if (_.isEmpty(quickpick.selectedItems)) {
await onUpdatePickerItems({
picker: quickpick,
token: new CancellationTokenSource().token,
});
}
this.onDidAccept({ quickpick, cancellationToken: token })();
});
return;
}
Example #3
Source File: LookupControllerV3.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
private _cancelTokenSource?: CancellationTokenSource;
Example #4
Source File: NoteLookupProvider.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
async provide(opts: {
quickpick: DendronQuickPickerV2;
token: CancellationTokenSource;
fuzzThreshold: number;
}) {
const ctx = "NoteLookupProvider.provide";
Logger.info({ ctx, msg: "enter" });
const { quickpick, token } = opts;
const onUpdatePickerItems = _.bind(this.onUpdatePickerItems, this);
const onUpdateDebounced = _.debounce(
() => {
const ctx = "NoteLookupProvider.onUpdateDebounced";
Logger.debug({ ctx, msg: "enter" });
const out = onUpdatePickerItems({
picker: quickpick,
token: token.token,
} as OnUpdatePickerItemsOpts);
Logger.debug({ ctx, msg: "exit" });
return out;
},
100,
{
// Use trailing to make sure we get the latest letters typed by the user
// before accepting.
leading: false,
}
);
quickpick.onDidChangeValue(onUpdateDebounced);
quickpick.onDidAccept(async () => {
Logger.info({
ctx: "NoteLookupProvider:onDidAccept",
quickpick: quickpick.value,
});
await onUpdateDebounced.flush();
if (_.isEmpty(quickpick.selectedItems)) {
await onUpdatePickerItems({
picker: quickpick,
token: new CancellationTokenSource().token,
});
}
this.onDidAccept({ quickpick, cancellationToken: token })();
});
Logger.info({ ctx, msg: "exit" });
return;
}
Example #5
Source File: vsCodeUtils.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
static createCancelSource(existingSource?: CancellationTokenSource) {
const tokenSource = new CancellationTokenSource();
if (existingSource) {
existingSource.cancel();
existingSource.dispose();
}
return tokenSource;
}
Example #6
Source File: NoteLookupProvider.ts From dendron with GNU Affero General Public License v3.0 | 4 votes |
/**
* Takes selection and runs accept, followed by hooks.
* @param opts
* @returns
*/
onDidAccept(opts: {
quickpick: DendronQuickPickerV2;
cancellationToken: CancellationTokenSource;
}) {
return async () => {
const ctx = "NoteLookupProvider:onDidAccept";
const { quickpick: picker, cancellationToken } = opts;
picker.buttons.forEach((button) => {
AnalyticsUtils.track(LookupEvents.LookupModifiersSetOnAccept, {
command: this.id,
type: (button as IDendronQuickInputButton).type,
pressed: (button as IDendronQuickInputButton).pressed,
});
});
let selectedItems = NotePickerUtils.getSelection(picker);
Logger.debug({
ctx,
selectedItems: selectedItems.map((item) => NoteUtils.toLogObj(item)),
});
// NOTE: if user presses <ENTER> before picker has a chance to process, this will be `[]`
// In this case we want to calculate picker item from current value
if (_.isEmpty(selectedItems)) {
selectedItems = await NotePickerUtils.fetchPickerResultsNoInput({
picker,
});
}
// when doing lookup, opening existing notes don't require vault picker
if (
PickerUtilsV2.hasNextPicker(picker, {
selectedItems,
providerId: this.id,
})
) {
Logger.debug({ ctx, msg: "nextPicker:pre" });
picker.state = DendronQuickPickState.PENDING_NEXT_PICK;
picker.vault = await picker.nextPicker({ note: selectedItems[0] });
// check if we exited from selecting a vault
if (_.isUndefined(picker.vault)) {
HistoryService.instance().add({
source: "lookupProvider",
action: "done",
id: this.id,
data: { cancel: true },
});
return;
}
}
// last chance to cancel
cancellationToken.cancel();
if (!this.opts.noHidePickerOnAccept) {
picker.state = DendronQuickPickState.FULFILLED;
picker.hide();
}
const onAcceptHookResp = await Promise.all(
this._onAcceptHooks.map((hook) =>
hook({ quickpick: picker, selectedItems })
)
);
const errors = _.filter(onAcceptHookResp, (ent) => ent.error);
if (!_.isEmpty(errors)) {
HistoryService.instance().add({
source: "lookupProvider",
action: "error",
id: this.id,
data: { error: errors[0] },
});
} else {
HistoryService.instance().add({
source: "lookupProvider",
action: "done",
id: this.id,
data: {
selectedItems,
onAcceptHookResp: _.map(onAcceptHookResp, (ent) => ent.data!),
} as NoteLookupProviderSuccessResp<OldNewLocation>,
});
}
};
}
Example #7
Source File: SchemaLookupProvider.ts From dendron with GNU Affero General Public License v3.0 | 4 votes |
/**
* Takes selection and runs accept, followed by hooks.
* @param opts
* @returns
*/
onDidAccept(opts: {
quickpick: DendronQuickPickerV2;
cancellationToken: CancellationTokenSource;
}) {
return async () => {
const ctx = "SchemaLookupProvider:onDidAccept";
const { quickpick: picker, cancellationToken } = opts;
let selectedItems = NotePickerUtils.getSelection(picker);
Logger.debug({
ctx,
selectedItems: selectedItems.map((item) => NoteUtils.toLogObj(item)),
});
if (_.isEmpty(selectedItems)) {
selectedItems =
await SchemaPickerUtils.fetchPickerResultsWithCurrentValue({
picker,
});
}
if (
PickerUtilsV2.hasNextPicker(picker, {
selectedItems,
providerId: this.id,
})
) {
Logger.debug({ ctx, msg: "nextPicker:pre" });
picker.state = DendronQuickPickState.PENDING_NEXT_PICK;
picker.vault = await picker.nextPicker({ note: selectedItems[0] });
// check if we exited from selecting a vault
if (_.isUndefined(picker.vault)) {
HistoryService.instance().add({
source: "lookupProvider",
action: "done",
id: this.id,
data: { cancel: true },
});
return;
}
}
const isMultiLevel = picker.value.split(".").length > 1;
if (isMultiLevel) {
window
.showInformationMessage(
"It looks like you are trying to create a multi-level [schema](https://wiki.dendron.so/notes/c5e5adde-5459-409b-b34d-a0d75cbb1052.html). This is not supported. If you are trying to create a note instead, run the `> Note Lookup` command or click on `Note Lookup`",
...["Note Lookup"]
)
.then(async (selection) => {
if (selection === "Note Lookup") {
await new NoteLookupCommand().run({
initialValue: picker.value,
});
}
});
HistoryService.instance().add({
source: "lookupProvider",
action: "done",
id: this.id,
data: { cancel: true },
});
return;
}
// last chance to cancel
cancellationToken.cancel();
if (!this.opts.noHidePickerOnAccept) {
picker.hide();
}
const onAcceptHookResp = await Promise.all(
this._onAcceptHooks.map((hook) =>
hook({ quickpick: picker, selectedItems })
)
);
const errors = _.filter(onAcceptHookResp, (ent) => ent.error);
if (!_.isEmpty(errors)) {
HistoryService.instance().add({
source: "lookupProvider",
action: "error",
id: this.id,
data: { error: errors[0] },
});
} else {
HistoryService.instance().add({
source: "lookupProvider",
action: "done",
id: this.id,
data: {
selectedItems,
onAcceptHookResp: _.map(onAcceptHookResp, (ent) => ent.data!),
} as SchemaLookupProviderSuccessResp<OldNewLocation>,
});
}
};
}
Example #8
Source File: extension.test.ts From vscode-file-downloader with MIT License | 4 votes |
suite(`Integration Tests`, () => {
window.showInformationMessage(`Start all tests.`);
let fileDownloader: IFileDownloader;
suiteSetup(async () => {
const downloaderExtension = extensions.getExtension(`mindaro-dev.file-downloader`);
assert(downloaderExtension);
await downloaderExtension.activate();
fileDownloader = downloaderExtension.exports;
});
teardown(async () => {
await rimrafAsync(MockGlobalStoragePath);
});
test(`Activate extension`, () => {
assert(fileDownloader);
});
test(`Simple download`, async () => {
assert(
await fileDownloader.downloadFile(
TestDownloadUri,
TestDownloadFilename,
MockExtensionContext
)
);
});
test(`Get item with no downloads`, async () => {
await assert.rejects(fileDownloader.getItem(TestDownloadFilename, MockExtensionContext));
});
test(`Get item with one download`, async () => {
const downloadedUri: Uri = await fileDownloader.downloadFile(TestDownloadUri, TestDownloadFilename, MockExtensionContext);
const getItemResult: Uri = await fileDownloader.getItem(TestDownloadFilename, MockExtensionContext);
assert.deepStrictEqual(downloadedUri, getItemResult);
await assert.doesNotReject(fs.promises.access(getItemResult.fsPath));
});
test(`Get item with multiple downloads`, async () => {
const downloadedItem1 = await fileDownloader.downloadFile(TestDownloadUri, `1.pdf`, MockExtensionContext);
const downloadedItem2 = await fileDownloader.downloadFile(TestDownloadUri, `2.pdf`, MockExtensionContext);
const downloadedItem3 = await fileDownloader.downloadFile(TestDownloadUri, `3.pdf`, MockExtensionContext);
const getItem1 = await fileDownloader.getItem(`1.pdf`, MockExtensionContext);
const getItem2 = await fileDownloader.getItem(`2.pdf`, MockExtensionContext);
const getItem3 = await fileDownloader.getItem(`3.pdf`, MockExtensionContext);
assert.deepStrictEqual(downloadedItem1, getItem1);
assert.deepStrictEqual(downloadedItem2, getItem2);
assert.deepStrictEqual(downloadedItem3, getItem3);
});
test(`Overwrite existing folder`, async () => {
const fullDownloadFilePath = path.join(MockGlobalStoragePath, `file-downloader-downloads`, `file`);
await fs.promises.mkdir(fullDownloadFilePath, { recursive: true });
await assert.doesNotReject(fileDownloader.downloadFile(TestDownloadUri, `file`, MockExtensionContext));
});
test(`tryGetItem with no downloads`, async () => {
const getItemResult = await fileDownloader.tryGetItem(TestDownloadFilename, MockExtensionContext);
assert.equal(getItemResult, undefined);
});
test(`tryGetItem with one download`, async () => {
const downloadedUri: Uri = await fileDownloader.downloadFile(TestDownloadUri, TestDownloadFilename, MockExtensionContext);
const getItemResult: Uri | undefined = await fileDownloader.tryGetItem(TestDownloadFilename, MockExtensionContext);
assert(getItemResult != null);
assert.deepStrictEqual(downloadedUri, getItemResult);
await assert.doesNotReject(fs.promises.access(getItemResult.fsPath));
});
test(`List items with no downloads`, async () => {
const result: Uri[] = await fileDownloader.listDownloadedItems(MockExtensionContext);
assert.equal(result.length, 0);
});
test(`List items with one download`, async () => {
const downloadedItemUri = await fileDownloader.downloadFile(TestDownloadUri, TestDownloadFilename, MockExtensionContext);
const result: Uri[] = await fileDownloader.listDownloadedItems(MockExtensionContext);
assert.equal(result.length, 1);
assert.deepStrictEqual(downloadedItemUri, result[0]);
});
test(`List items with multiple downloads`, async () => {
const downloadedItem1 = await fileDownloader.downloadFile(TestDownloadUri, `1.pdf`, MockExtensionContext);
const downloadedItem2 = await fileDownloader.downloadFile(TestDownloadUri, `2.pdf`, MockExtensionContext);
const downloadedItem3 = await fileDownloader.downloadFile(TestDownloadUri, `3.pdf`, MockExtensionContext);
const result: Uri[] = await fileDownloader.listDownloadedItems(MockExtensionContext);
assert.equal(result.length, 3);
assert.deepStrictEqual(downloadedItem1.toString(), result[0].toString());
assert.deepStrictEqual(downloadedItem2.toString(), result[1].toString());
assert.deepStrictEqual(downloadedItem3.toString(), result[2].toString());
});
test(`Delete one item`, async () => {
const downloadedItemUri = await fileDownloader.downloadFile(TestDownloadUri, TestDownloadFilename, MockExtensionContext);
await assert.doesNotReject(fileDownloader.deleteItem(TestDownloadFilename, MockExtensionContext));
assert.deepStrictEqual(await fileDownloader.listDownloadedItems(MockExtensionContext), []);
});
test(`Delete one item with no downloads`, async () => {
await assert.doesNotReject(fileDownloader.deleteItem(TestDownloadFilename, MockExtensionContext));
});
test(`Delete one item out of multiple`, async () => {
const downloadedItem1 = await fileDownloader.downloadFile(TestDownloadUri, `1.pdf`, MockExtensionContext);
const downloadedItem2 = await fileDownloader.downloadFile(TestDownloadUri, `2.pdf`, MockExtensionContext);
const downloadedItem3 = await fileDownloader.downloadFile(TestDownloadUri, `3.pdf`, MockExtensionContext);
await fileDownloader.deleteItem(`1.pdf`, MockExtensionContext);
const result: Uri[] = await fileDownloader.listDownloadedItems(MockExtensionContext);
assert.equal(result.length, 2);
assert.deepStrictEqual(downloadedItem2.toString(), result[0].toString());
assert.deepStrictEqual(downloadedItem3.toString(), result[1].toString());
});
test(`Delete all items`, async () => {
const downloadedItem1 = await fileDownloader.downloadFile(TestDownloadUri, `1.pdf`, MockExtensionContext);
const downloadedItem2 = await fileDownloader.downloadFile(TestDownloadUri, `2.pdf`, MockExtensionContext);
const downloadedItem3 = await fileDownloader.downloadFile(TestDownloadUri, `3.pdf`, MockExtensionContext);
await fileDownloader.deleteAllItems(MockExtensionContext);
assert.deepStrictEqual(await fileDownloader.listDownloadedItems(MockExtensionContext), []);
});
test(`Delete all items with no downloads`, async () => {
await assert.doesNotReject(fileDownloader.deleteAllItems(MockExtensionContext));
});
test(`Download progress callback`, async () => {
let reportedTotalBytes;
let reportedDownloadedBytes;
const downloadProgressCallback = (downloadedBytes: number, totalBytes: number | undefined) => {
reportedDownloadedBytes = downloadedBytes;
reportedTotalBytes = totalBytes ?? 0;
assert(reportedTotalBytes === 0 || reportedDownloadedBytes <= reportedTotalBytes);
};
await fileDownloader.downloadFile(
TestDownloadUri,
TestDownloadFilename,
MockExtensionContext,
/* cancellationToken */ undefined,
downloadProgressCallback
);
assert(reportedTotalBytes);
assert(reportedDownloadedBytes);
assert.equal(reportedDownloadedBytes, reportedTotalBytes);
});
test(`Decompress zip file`, async () => {
const filePath = await fileDownloader.downloadFile(
Uri.parse(`https://github.com/microsoft/cascadia-code/releases/download/v2005.15/CascadiaCode_2005.15.zip`),
`cascadia2`,
MockExtensionContext,
/* cancellationToken */ undefined,
/* onDownloadProgressChange */ undefined,
{ shouldUnzip: true }
);
assert(filePath);
assert(await fs.promises.readdir(filePath.fsPath));
});
test(`Decompress non-zip file`, () => {
assert.rejects(
fileDownloader.downloadFile(
TestDownloadUri,
TestDownloadFilename,
MockExtensionContext,
/* cancellationToken */ undefined,
/* onDownloadProgressChange */ undefined,
{ shouldUnzip: true }
)
);
});
test(`Delete extracted folder`, async () => {
const downloadedItemUri = await fileDownloader.downloadFile(
Uri.parse(`https://github.com/microsoft/cascadia-code/releases/download/v2005.15/CascadiaCode_2005.15.zip`),
`cascadia`,
MockExtensionContext,
/* cancellationToken */ undefined,
/* onDownloadProgressChange */ undefined,
{ shouldUnzip: true }
);
await fileDownloader.deleteItem(`cascadia`, MockExtensionContext);
const items = await fileDownloader.listDownloadedItems(MockExtensionContext);
for (const item of items) {
assert(!item.fsPath.includes(`cascadia`));
}
});
test(`Uri with no scheme`, async () => {
await assert.rejects(
fileDownloader.downloadFile(
Uri.parse(`ipv4.download.thinkbroadband.com/5MB.zip`),
TestDownloadFilename,
MockExtensionContext
)
);
});
test(`Uri with improper scheme`, async () => {
await assert.rejects(
fileDownloader.downloadFile(
Uri.parse(`scheme://ipv4.download.thinkbroadband.com/5MB.zip`),
TestDownloadFilename,
MockExtensionContext
)
);
});
test(`Nonexistent website with correct Uri format`, async () => {
try {
await fileDownloader.downloadFile(
Uri.parse(`https://c0de104c1e68625629646025d15a6129a2b4b6496cd9ceacd7f7b5078e1849ba.com`),
TestDownloadFilename,
MockExtensionContext
);
assert.fail();
}
catch (error) {
if (error instanceof Error) {
assert(error.name.includes(`RetriesExceededError`));
}
else {
assert.fail(`unexpected error type`);
}
}
});
test(`404 status code`, async () => {
try {
await fileDownloader.downloadFile(
Uri.parse(`http://httpstat.us/404`),
TestDownloadFilename,
MockExtensionContext
);
assert.fail();
}
catch (error) {
if (error instanceof Error) {
if (`response` in error) {
// eslint-disable-next-line @typescript-eslint/dot-notation
assert(error[`response`][`status`] === 404);
}
assert.notEqual(error.name, `RetriesExceededError`);
}
else {
assert.fail(`unexpected error type`);
}
}
});
test(`Cancel download`, async () => {
const cancellationTokenSource = new CancellationTokenSource();
const cancellationToken = cancellationTokenSource.token;
const downloadPromise = fileDownloader.downloadFile(
Uri.parse(`http://ipv4.download.thinkbroadband.com/50MB.zip`),
`50MB.zip`,
MockExtensionContext,
cancellationToken
);
setTimeout(() => {
cancellationTokenSource.cancel();
}, 1000);
try {
await downloadPromise;
assert.fail();
}
catch (error) {
if (ErrorUtils.isErrnoException(error)) {
assert.equal(error.code, `ERR_STREAM_PREMATURE_CLOSE`);
}
else {
assert.fail(`unexpected error type`);
}
}
const downloadedItems = await fileDownloader.listDownloadedItems(MockExtensionContext);
for (const item of downloadedItems) {
assert(!item.fsPath.includes(`50MB.zip`));
}
});
});