Skip to content
This repository was archived by the owner on May 27, 2022. It is now read-only.

Commit 92cd4d0

Browse files
authored
Merge pull request #106 from data-provider/release
Release v1.5.0
2 parents 2695426 + 9cf212f commit 92cd4d0

File tree

9 files changed

+161
-22
lines changed

9 files changed

+161
-22
lines changed

CHANGELOG.md

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

13+
## [1.5.0] - 2020-11-16
14+
### Added
15+
- feat: Supports passing options to cleanCache methods in usePolling and withPolling
16+
1317
## [1.4.0] - 2020-11-09
1418
### Added
1519
- feat(hocs): Add withDataLoadedError, withDataLoadingError, withDataLoadingErrorComponents, withDataLoadedErrorComponents

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ const BooksList = () => {
190190
};
191191
```
192192

193-
### `usePolling(provider, [interval])`
193+
### `usePolling(provider, [interval/options], [options])`
194194

195195
Triggers `cleanDependenciesCache` method of the provider each `interval` miliseconds while the component is "alive". It can be used in multiple components at the same time for the same provider. In that case, the used interval will be the lower one, and it will be recalculated each time a component is added or removed.
196196

@@ -199,9 +199,10 @@ This hook can also be used with [Data Provider selectors][data-provider-selector
199199
#### Arguments
200200

201201
* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance.
202-
* `interval` _(Object)_: Interval in miliseconds to clean the provider dependencies cache. Default is 5000.
202+
* `interval` _(Number)_: Interval in miliseconds to clean the provider dependencies cache. Default is 5000.
203+
* `options` _(Object)_: Options object that will be passed as is to the `cleanCache` method of providers or `cleanDependenciesCache` method of selectors. Check the [data-provider API documentation](https://www.data-provider.org/docs/api-providers-and-selectors-methods) for further info. Options can be defined as second argument if interval is omitted.
203204

204-
#### Example
205+
#### Examples
205206

206207
```jsx
207208
import { useData, usePolling } from "@data-provider/react";
@@ -215,6 +216,20 @@ const BooksList = () => {
215216
};
216217
```
217218

219+
```jsx
220+
import { useData, usePolling } from "@data-provider/react";
221+
222+
import { booksAndAuthors, books } from "../data/books";
223+
224+
const BooksList = () => {
225+
const data = useData(books);
226+
usePolling(booksAndAuthors, {
227+
except: [books]
228+
});
229+
// Do your stuff here. booksAndAuthors selector dependencies will fetched again from server every 3 seconds, except the "books" provider.
230+
};
231+
```
232+
218233
## HOCs
219234

220235
### `withDataLoadingError(provider, [customPropertiesNames])(Component)`
@@ -430,14 +445,15 @@ const BooksList = ({ booksError }) => {
430445
export default withError(books, "booksError")(BooksList);
431446
```
432447

433-
### `withPolling(provider, [interval])(Component)`
448+
### `withPolling(provider, [interval/options], [options])(Component)`
434449

435450
This High Order Component works as the hook `usePolling` described above.
436451

437452
#### Arguments
438453

439454
* `provider` _(Object)_: [Data Provider][data-provider] provider or selector instance, or a function as described in the [withDataLoadingError HOC docs](#withdataloadingerrorprovider-custompropertiesnamescomponent)
440-
* `interval` _(Object)_: Interval in miliseconds to clean the provider dependencies cache. Default is 5000.
455+
* `interval` _(Number)_: Interval in miliseconds to clean the provider dependencies cache. Default is 5000.
456+
* `options` _(Object)_: Options object that will be passed as is to the `cleanCache` method of providers or `cleanDependenciesCache` method of selectors. Check the [data-provider API documentation](https://www.data-provider.org/docs/api-providers-and-selectors-methods) for further info. Options can be defined as second argument if interval is omitted.
441457

442458
#### Example
443459

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/react",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "React bindings for @data-provider",
55
"keywords": [
66
"data-provider",

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-react
3-
sonar.projectVersion=1.4.0
3+
sonar.projectVersion=1.5.0
44

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

src/usePolling.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { useEffect } from "react";
1+
import { useEffect, useMemo } from "react";
22

3+
const DEFAULT_INTERVAL_TIME = 5000;
34
const pollingProviders = {};
45

56
class PollingHandler {
6-
constructor(provider, intervalTime) {
7+
constructor(provider, intervalTime, options) {
78
provider.cleanCache();
9+
this._options = options;
810
this._provider = provider;
911
this._id = provider.id;
1012
this._clients = 1;
@@ -16,7 +18,7 @@ class PollingHandler {
1618

1719
_setInterval() {
1820
this._interval = setInterval(() => {
19-
this._provider.cleanDependenciesCache();
21+
this._provider.cleanDependenciesCache(this._options);
2022
}, this._currentIntervalTime);
2123
}
2224

@@ -55,19 +57,32 @@ class PollingHandler {
5557
}
5658
}
5759

58-
export const usePolling = (provider, intervalTime = 5000) => {
60+
export const usePolling = (provider, intervalTimeOrOptions, options = {}) => {
61+
const [intervalTimeToUse, optionsToUse] = useMemo(() => {
62+
if (typeof intervalTimeOrOptions === "undefined") {
63+
return [DEFAULT_INTERVAL_TIME, options];
64+
} else if (typeof intervalTimeOrOptions === "object") {
65+
return [DEFAULT_INTERVAL_TIME, intervalTimeOrOptions];
66+
}
67+
return [intervalTimeOrOptions, options];
68+
}, [intervalTimeOrOptions, options]);
69+
5970
useEffect(() => {
6071
let clearProviderInterval;
6172
if (provider) {
6273
if (pollingProviders[provider.id]) {
63-
pollingProviders[provider.id].addClient(intervalTime);
74+
pollingProviders[provider.id].addClient(intervalTimeToUse);
6475
} else {
65-
pollingProviders[provider.id] = new PollingHandler(provider, intervalTime);
76+
pollingProviders[provider.id] = new PollingHandler(
77+
provider,
78+
intervalTimeToUse,
79+
optionsToUse
80+
);
6681
}
6782
clearProviderInterval = () => {
68-
pollingProviders[provider.id].removeClient(intervalTime);
83+
pollingProviders[provider.id].removeClient(intervalTimeToUse);
6984
};
7085
}
7186
return clearProviderInterval;
72-
}, [provider, intervalTime]);
87+
}, [provider, intervalTimeToUse]);
7388
};

src/withDataProvider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ export const withError = (provider, key) => (Component) => {
237237
return WithError;
238238
};
239239

240-
export const withPolling = (provider, interval) => (Component) => {
240+
export const withPolling = (provider, interval, options) => (Component) => {
241241
const WithPolling = (props) => {
242242
const providerToRead = useProvider(provider, props);
243-
usePolling(providerToRead, interval);
243+
usePolling(providerToRead, interval, options);
244244
return <Component {...props} />;
245245
};
246246
hoistNonReactStatics(WithPolling, Component);

test/usePolling.spec.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useState, useEffect } from "react";
44
import "@testing-library/jest-dom";
55
import { render, act } from "@testing-library/react";
6-
import { providers } from "@data-provider/core";
6+
import { providers, Selector } from "@data-provider/core";
77
import sinon from "sinon";
88

99
import { usePolling, useData } from "../src";
@@ -20,13 +20,14 @@ const wait = (time = 600) => {
2020
};
2121

2222
describe("usePolling", () => {
23-
let sandbox, provider, BooksComponent, Component;
23+
let sandbox, provider, BooksComponent, Component, selector;
2424

2525
beforeEach(() => {
2626
sandbox = sinon.createSandbox();
2727
provider = new MockProvider(BOOKS_ID, {
2828
data: BOOKS,
2929
});
30+
selector = new Selector(provider, (result) => result);
3031
sandbox.spy(provider, "cleanCache");
3132
});
3233

@@ -136,4 +137,54 @@ describe("usePolling", () => {
136137
expect(provider.cleanCache.callCount).toEqual(4);
137138
});
138139
});
140+
141+
describe("when using except option", () => {
142+
beforeEach(() => {
143+
const OPTIONS = {
144+
except: [provider],
145+
};
146+
BooksComponent = () => {
147+
const books = useData(selector);
148+
usePolling(selector, 500, OPTIONS);
149+
return <Books books={books} />;
150+
};
151+
152+
Component = () => (
153+
<ReduxProvider>
154+
<BooksComponent />
155+
</ReduxProvider>
156+
);
157+
});
158+
159+
it("should not clean the provider cache as it is defined in except option", async () => {
160+
render(<Component />);
161+
await wait(3000);
162+
expect(provider.cleanCache.callCount).toEqual(0);
163+
});
164+
});
165+
166+
describe("when using options as first argument", () => {
167+
beforeEach(() => {
168+
const OPTIONS = {
169+
except: [provider],
170+
};
171+
BooksComponent = () => {
172+
const books = useData(selector);
173+
usePolling(selector, OPTIONS);
174+
return <Books books={books} />;
175+
};
176+
177+
Component = () => (
178+
<ReduxProvider>
179+
<BooksComponent />
180+
</ReduxProvider>
181+
);
182+
});
183+
184+
it("should not clean the provider cache as it is defined in except option", async () => {
185+
render(<Component />);
186+
await wait(8000);
187+
expect(provider.cleanCache.callCount).toEqual(0);
188+
}, 10000);
189+
});
139190
});

test/withPolling.spec.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useState, useEffect } from "react";
44
import "@testing-library/jest-dom";
55
import { render, act } from "@testing-library/react";
6-
import { providers } from "@data-provider/core";
6+
import { providers, Selector } from "@data-provider/core";
77
import sinon from "sinon";
88

99
import { withPolling, useData } from "../src";
@@ -20,13 +20,14 @@ const wait = (time = 600) => {
2020
};
2121

2222
describe("withPolling", () => {
23-
let sandbox, provider, BooksComponent, BooksComponentToRender, Component;
23+
let sandbox, provider, BooksComponent, BooksComponentToRender, Component, selector;
2424

2525
beforeEach(() => {
2626
sandbox = sinon.createSandbox();
2727
provider = new MockProvider(BOOKS_ID, {
2828
data: BOOKS,
2929
});
30+
selector = new Selector(provider, (result) => result);
3031
sandbox.spy(provider, "cleanCache");
3132
});
3233

@@ -151,4 +152,56 @@ describe("withPolling", () => {
151152
expect(provider.cleanCache.callCount).toEqual(4);
152153
});
153154
});
155+
156+
describe("when using except option", () => {
157+
beforeEach(() => {
158+
const OPTIONS = {
159+
except: [provider],
160+
};
161+
BooksComponent = () => {
162+
const books = useData(selector);
163+
return <Books books={books} />;
164+
};
165+
166+
BooksComponentToRender = withPolling(selector, 500, OPTIONS)(BooksComponent);
167+
168+
Component = () => (
169+
<ReduxProvider>
170+
<BooksComponentToRender />
171+
</ReduxProvider>
172+
);
173+
});
174+
175+
it("should not clean the provider cache as it is defined in except option", async () => {
176+
render(<Component />);
177+
await wait(3000);
178+
expect(provider.cleanCache.callCount).toEqual(0);
179+
});
180+
});
181+
182+
describe("when using options as first argument", () => {
183+
beforeEach(() => {
184+
const OPTIONS = {
185+
except: [provider],
186+
};
187+
BooksComponent = () => {
188+
const books = useData(selector);
189+
return <Books books={books} />;
190+
};
191+
192+
BooksComponentToRender = withPolling(selector, OPTIONS)(BooksComponent);
193+
194+
Component = () => (
195+
<ReduxProvider>
196+
<BooksComponent />
197+
</ReduxProvider>
198+
);
199+
});
200+
201+
it("should not clean the provider cache as it is defined in except option", async () => {
202+
render(<Component />);
203+
await wait(8000);
204+
expect(provider.cleanCache.callCount).toEqual(0);
205+
}, 10000);
206+
});
154207
});

0 commit comments

Comments
 (0)