Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
remove finally() since it is not supported in older browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Mar 5, 2019
1 parent cc27f98 commit 965e356
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/misc/sequentialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ export function sequentialize(cacheKey: (args: any[]) => string) {
return response;
}

response = originalMethod.apply(this, args).finally(() => {
const onFinally = () => {
cache.delete(argsCacheKey);
if (cache.size === 0) {
caches.delete(this);
}
};
response = originalMethod.apply(this, args).then((val: any) => {
onFinally();
return val;
}).catch((err: any) => {
onFinally();
throw err;
});

cache.set(argsCacheKey, response);
Expand Down
9 changes: 8 additions & 1 deletion src/misc/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function throttle(limit: number, throttleKey: (args: any[]) => string) {

return new Promise<T>((resolve, reject) => {
const exec = () => {
originalMethod.apply(this, args).finally(() => {
const onFinally = () => {
queue.splice(queue.indexOf(exec), 1);
if (queue.length >= limit) {
queue[limit - 1]();
Expand All @@ -42,6 +42,13 @@ export function throttle(limit: number, throttleKey: (args: any[]) => string) {
allThrottles.delete(this);
}
}
};
originalMethod.apply(this, args).then((val: any) => {
onFinally();
return val;
}).catch((err: any) => {
onFinally();
throw err;
}).then(resolve, reject);
};
queue.push(exec);
Expand Down

2 comments on commit 965e356

@kspearrin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcfedr What do you think? ref: #26 (comment)

@mcfedr
Copy link
Contributor

@mcfedr mcfedr commented on 965e356 Mar 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Please sign in to comment.