Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit f775cad

Browse files
authored
Merge pull request #155 from data-provider/fix-154-throttle
Release v2.8.1
2 parents 5340472 + 01063e8 commit f775cad

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111
### Removed
1212

13+
## [2.8.1] - 2020-11-27
14+
15+
### Fixed
16+
- fix(#154): Pass original arguments to throttled methods
17+
1318
## [2.8.0] - 2020-11-23
1419

1520
### Added

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@data-provider/core",
3-
"version": "2.8.0",
3+
"version": "2.8.1",
44
"description": "Async Data Provider agnostic about data origins",
55
"keywords": [
66
"data",

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sonar.organization=data-provider
22
sonar.projectKey=data-provider-core
3-
sonar.projectVersion=2.8.0
3+
sonar.projectVersion=2.8.1
44

55
sonar.sources=src,test
66
sonar.exclusions=node_modules/**

src/helpers.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ export function throttle(func, limit) {
116116
let inThrottle = false;
117117
let calledWhileInThrottle = false;
118118
let finishThrottleTimeout = null;
119-
const checkFinishThrottle = (context) => {
119+
const checkFinishThrottle = (context, args) => {
120120
finishThrottleTimeout = setTimeout(() => {
121121
if (calledWhileInThrottle) {
122+
inThrottle = true;
122123
calledWhileInThrottle = false;
123-
checkFinishThrottle(context);
124-
func.apply(context, arguments);
124+
checkFinishThrottle(context, args);
125+
func.apply(context, args);
125126
} else {
126127
inThrottle = false;
127128
}
@@ -135,7 +136,7 @@ export function throttle(func, limit) {
135136
}
136137
inThrottle = true;
137138
calledWhileInThrottle = false;
138-
checkFinishThrottle(context);
139+
checkFinishThrottle(context, arguments);
139140
func.apply(context, arguments);
140141
} else {
141142
calledWhileInThrottle = true;

test/provider/clean-cache-throttle.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("Provider cleanCacheThrottle option", () => {
7373
});
7474
});
7575

76-
describe("withot cleanCacheThrottle option", () => {
76+
describe("with cleanCacheThrottle option", () => {
7777
it("should execute read method all times cleanCache is called if option value is 0", async () => {
7878
provider.config({
7979
cleanCacheThrottle: 0,
@@ -119,6 +119,33 @@ describe("Provider cleanCacheThrottle option", () => {
119119
expect(spies.read.callCount).toEqual(2);
120120
});
121121

122+
it("should pass same options to cleanCache method when it is throttled", async () => {
123+
expect.assertions(7);
124+
const options = { foo: "foo" };
125+
const originalUnthrottledCleanCache = provider._unthrottledCleanCache.bind(provider);
126+
provider._unthrottledCleanCache = function (options) {
127+
originalUnthrottledCleanCache(options);
128+
};
129+
const spy = sandbox.spy(provider, "_unthrottledCleanCache");
130+
provider.config({
131+
cleanCacheThrottle: 500,
132+
});
133+
sandbox.spy(provider, "_throttledCleanCache");
134+
await provider.read();
135+
provider.cleanCache(options); // This one cleans the cache
136+
expect(provider._throttledCleanCache.getCall(0).args[0]).toBe(options);
137+
expect(spy.getCall(0).args[0]).toBe(options);
138+
provider.cleanCache(); // throttled
139+
provider.cleanCache(); // throttled
140+
expect(provider._throttledCleanCache.callCount).toEqual(3);
141+
expect(spy.callCount).toEqual(1);
142+
await wait(700);
143+
// It should have cleaned cache again after throttle time
144+
expect(provider._throttledCleanCache.callCount).toEqual(3);
145+
expect(spy.callCount).toEqual(2);
146+
expect(spy.getCall(1).args[0]).toBe(options);
147+
});
148+
122149
it("should clean cache one more time after throttle time if it receive calls while throttled", async () => {
123150
expect.assertions(4);
124151
provider.config({

0 commit comments

Comments
 (0)