forked from microsoft/vscode-java-pack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscheduler.ts
More file actions
48 lines (39 loc) · 1.31 KB
/
Copy pathscheduler.ts
File metadata and controls
48 lines (39 loc) · 1.31 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as vscode from "vscode";
import { onIdle } from "./idle";
import * as _ from "lodash";
interface Action {
name: string;
resolve: (name: string) => void;
}
const actionQueue: Action[] = [];
const pastActions: string[] = [];
export function initialize(context: vscode.ExtensionContext) {
context.subscriptions.push(onIdle(() => idleHandler()));
}
// This is to queue the actions that need attention from users. One thing at a time, only on idle.
export function scheduleAction(name: string, isImmediate: boolean = false, isOneTime: boolean = false): Promise<string> {
const isPastAction = _.some(actionQueue, (action) => action.name === name) || _.some(pastActions, name);
if (isOneTime && isPastAction) {
return Promise.reject(`Action (${name}) was already scheduled or performed once.`);
}
return new Promise((resolve, reject) => {
if (isImmediate) {
setImmediate(() => resolve(name));
return;
}
actionQueue.push({
name: name,
resolve: resolve
});
});
}
function idleHandler() {
if (_.isEmpty(actionQueue)) {
return;
}
const action = actionQueue.shift();
pastActions.push(action && action.name || "");
action && action.resolve(action && action.name);
}