Skip to content

Commit 11766b7

Browse files
committed
fixed decorator to limit async calls
1 parent 6d70c33 commit 11766b7

File tree

4 files changed

+69
-77
lines changed

4 files changed

+69
-77
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
**v0.8.2**
2+
=============================================
3+
4+
## Bug Fixes
5+
- Fixed decorator to limit async calls.
6+
17
**v0.8.1**
28
=============================================
39

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "svn-scm",
33
"displayName": "svn-scm",
44
"description": "",
5-
"version": "0.8.1",
5+
"version": "0.8.2",
66
"publisher": "johnstoncode",
77
"engines": {
88
"vscode": "^1.16.0"

src/decorators.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
1-
module.exports.throttle = function(func, wait, context, s) {
2-
var args, result;
3-
var timeout = null;
4-
var previous = 0;
5-
if (!options) options = {};
6-
var later = function() {
7-
previous = options.leading === false ? 0 : Date.now();
8-
timeout = null;
9-
result = func.apply(context, args);
10-
if (!timeout) args = null;
11-
};
1+
module.exports.throttle = function(func, wait, context) {
2+
var timer = null;
3+
124
return function() {
13-
var now = Date.now();
14-
if (!previous && options.leading === false) previous = now;
15-
var remaining = wait - (now - previous);
16-
args = arguments;
17-
if (remaining <= 0 || remaining > wait) {
18-
if (timeout) {
19-
clearTimeout(timeout);
20-
timeout = null;
21-
}
22-
previous = now;
23-
result = func.apply(context, args);
24-
if (!timeout) args = null;
25-
} else if (!timeout && options.trailing !== false) {
26-
timeout = setTimeout(later, remaining);
27-
}
28-
return result;
5+
var args = arguments;
6+
7+
clearTimeout(timer);
8+
9+
timer = setTimeout(function() {
10+
fn.apply(context, args);
11+
}, wait);
2912
};
3013
};
3114

@@ -39,8 +22,8 @@ module.exports.throttleAsync = function(fn, key, context = this) {
3922
}
4023

4124
if (this[currentKey]) {
42-
this[nextKey] = done(this[currentKey]).then(() => {
43-
this[nextKey] = undefined;
25+
done(this[currentKey]).then(() => {
26+
this[nextKey] = false;
4427
return trigger.apply(context, args);
4528
});
4629

@@ -49,8 +32,9 @@ module.exports.throttleAsync = function(fn, key, context = this) {
4932

5033
this[currentKey] = fn.apply(context, args);
5134

52-
const clear = () => (this[currentKey] = undefined);
53-
done(this[currentKey]).then(clear, clear);
35+
this[currentKey].then(() => {
36+
this[currentKey] = false;
37+
});
5438

5539
return this[currentKey];
5640
};

src/repository.js

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function Repository(repository) {
2828
this.changes.hideWhenEmpty = true;
2929
this.notTracked.hideWhenEmpty = true;
3030

31-
this.addEventListeners();
3231
this.update();
32+
this.addEventListeners();
3333
}
3434

3535
Repository.prototype.addEventListeners = function() {
@@ -46,55 +46,57 @@ Repository.prototype.provideOriginalResource = uri => {
4646
return new Uri().with({ scheme: "svn", query: uri.path, path: uri.path });
4747
};
4848

49-
Repository.prototype.update = async function() {
50-
let changes = [];
51-
let notTracked = [];
49+
Repository.prototype.update = function() {
50+
return new Promise((resolve, reject) => {
51+
let changes = [];
52+
let notTracked = [];
5253

53-
this.changes.resourceStates = [];
54-
this.notTracked.resourceStates = [];
54+
this.changes.resourceStates = [];
55+
this.notTracked.resourceStates = [];
5556

56-
this.repository
57-
.getStatus()
58-
.then(result => {
59-
let changes = [];
60-
let notTracked = [];
57+
this.repository
58+
.getStatus()
59+
.then(result => {
60+
let changes = [];
61+
let notTracked = [];
6162

62-
result.forEach(item => {
63-
switch (item["wc-status"].$.item) {
64-
case "modified":
65-
case "deleted":
66-
case "conflicted":
67-
case "replaced":
68-
case "missing":
69-
case "added":
70-
changes.push(
71-
new Resource(
72-
this.repository.root,
73-
item.$.path,
74-
item["wc-status"].$.item
75-
)
76-
);
77-
break;
78-
case "unversioned":
79-
notTracked.push(
80-
new Resource(
81-
this.repository.root,
82-
item.$.path,
83-
item["wc-status"].$.item
84-
)
85-
);
86-
break;
87-
}
88-
});
63+
result.forEach(item => {
64+
switch (item["wc-status"].$.item) {
65+
case "modified":
66+
case "deleted":
67+
case "conflicted":
68+
case "replaced":
69+
case "missing":
70+
case "added":
71+
changes.push(
72+
new Resource(
73+
this.repository.root,
74+
item.$.path,
75+
item["wc-status"].$.item
76+
)
77+
);
78+
break;
79+
case "unversioned":
80+
notTracked.push(
81+
new Resource(
82+
this.repository.root,
83+
item.$.path,
84+
item["wc-status"].$.item
85+
)
86+
);
87+
break;
88+
}
89+
});
8990

90-
this.changes.resourceStates = changes;
91-
this.notTracked.resourceStates = notTracked;
91+
this.changes.resourceStates = changes;
92+
this.notTracked.resourceStates = notTracked;
9293

93-
// console.log(this.changes.resourceStates);
94-
})
95-
.catch(error => {
96-
// console.log(error);
97-
});
94+
resolve();
95+
})
96+
.catch(error => {
97+
reject();
98+
});
99+
});
98100
};
99101

100102
module.exports = Repository;

0 commit comments

Comments
 (0)