Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: Immediately fetch polling subscriptions #228

Merged
merged 1 commit into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/api/PollingSubscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ hide_title: true
# PollingSubscription implements [Subscription](./SubscriptionManager.md)

Will dispatch a `fetch` action at the minimum interval of all subscriptions to this
resource. Pauses when offline. Immediately fetches when online status returns.
resource.

- Pauses when offline.
- Immediately fetches when online status returns.
- Immediately fetches any new subscriptions.

```tsx
import { SubscriptionManager, PollingSubscription, CacheProvider } from 'rest-hooks';
import {
SubscriptionManager,
PollingSubscription,
CacheProvider,
} from 'rest-hooks';
import ReactDOM from 'react-dom';

const subscriptionManager = new SubscriptionManager(PollingSubscription);
Expand All @@ -19,7 +27,7 @@ ReactDOM.render(
<CacheProvider subscriptionManager={subscriptionManager}>
<App />
</CacheProvider>,
document.body
document.body,
);
```

Expand Down
1 change: 1 addition & 0 deletions packages/rest-hooks/src/state/PollingSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class PollingSubscription implements Subscription {
this.url = url;
this.frequencyHistogram.set(this.frequency, 1);
this.dispatch = dispatch;
if (isOnline()) this.update();
this.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ describe('PollingSubscription', () => {
).toThrow();
});

it('should not call immediately', () => {
expect(dispatch.mock.calls.length).toBe(0);
it('should call immediately', () => {
expect(dispatch.mock.calls.length).toBe(1);
});

it('should call after period', () => {
dispatch.mockReset();
jest.advanceTimersByTime(5000);
expect(dispatch.mock.calls.length).toBe(1);
expect(dispatch.mock.calls[0]).toMatchSnapshot();
Expand Down
42 changes: 42 additions & 0 deletions website/versioned_docs/version-4.0/api/PollingSubscription.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: PollingSubscription implements Subscription
sidebar_label: PollingSubscription
hide_title: true
id: version-4.0-PollingSubscription
original_id: PollingSubscription
---

# PollingSubscription implements [Subscription](./SubscriptionManager.md)

Will dispatch a `fetch` action at the minimum interval of all subscriptions to this
resource.

- Pauses when offline.
- Immediately fetches when online status returns.
- Immediately fetches any new subscriptions.

```tsx
import {
SubscriptionManager,
PollingSubscription,
CacheProvider,
} from 'rest-hooks';
import ReactDOM from 'react-dom';

const subscriptionManager = new SubscriptionManager(PollingSubscription);

ReactDOM.render(
<CacheProvider subscriptionManager={subscriptionManager}>
<App />
</CacheProvider>,
document.body,
);
```

## Dispatched Actions

- 'rest-hooks/fetch'

> #### Note:
>
> This is already used by `CacheProvider` by default.