Skip to content

Commit 71d4005

Browse files
authored
Merge pull request #162 from HarperDB/update-cache-subscribe-code-sample
Small update to code to show an interval for polling external source …
2 parents 283a491 + 6d80425 commit 71d4005

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

docs/developers/applications/caching.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ export class MyTableEndpoint extends MyTable {
116116
117117
### Subscriptions
118118
119-
We can provide more control of an active cache with subscriptions. If there is a way to receive notifications from the external data source of data changes, we can implement this data source as an "active" data source for our cache by implementing a `subscribe` method. A `subscribe` method should return an asynchronous iterable that iterates and returns events indicating the updates. One straightforward way of creating an asynchronous iterable is by defining the `subscribe` method as an asynchronous generator. If we had an endpoint that we could poll for changes, we could implement this like:
119+
We can provide more control of an active cache with subscriptions. If there is a way to receive notifications from the external data source of data changes, we can implement this data source as an "active" data source for our cache by implementing a `subscribe` method. A `subscribe` method should return an asynchronous iterable that iterates and returns events indicating the updates. One straightforward way of creating an asynchronous iterable is by defining the `subscribe` method as an asynchronous generator. If we had an endpoint that we could poll for changes every second, we could implement this like:
120120
121121
```javascript
122122
class ThirdPartyAPI extends Resource {
123123
async *subscribe() {
124-
do {
124+
setInterval(() => { // every second retrieve more data
125125
// get the next data change event from the source
126126
let update = (await fetch(`http://some-api.com/latest-update`)).json();
127127
const event = { // define the change event (which will update the cache)
@@ -131,7 +131,7 @@ class ThirdPartyAPI extends Resource {
131131
timestamp: // the timestamp of when the data change occurred
132132
};
133133
yield event; // this returns this event, notifying the cache of the change
134-
} while(true);
134+
}, 1000);
135135
}
136136
async get() {
137137
...

0 commit comments

Comments
 (0)