Skip to content

Commit

Permalink
chore: Add subscription example (#2480)
Browse files Browse the repository at this point in the history
* Add subscription example

* update example

* move out the sub source
  • Loading branch information
huozhi authored Mar 6, 2023
1 parent 185fa09 commit 4247b80
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/subscription/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react"
import useSWRSubscription from "swr/subscription"
import EventEmitter from "events"

const event = new EventEmitter()

// Simulating an external data source.
let stopped = false
async function start () {
for (let i = 0; i < 100; i++) {
await new Promise(res => setTimeout(res, 1000))
if (stopped) return
if (i % 3 === 0 && i !== 0) {
event.emit("error", new Error("error: " + i));
} else {
event.emit("data", "state: " + i);
}
}
}

export default function page() {
const { data, error } = useSWRSubscription('my-sub', (key, { next }) => {
event.on("data", (value) => next(undefined, value));
event.on("error", (err) => next(err));
start();
return () => {
stopped = true;
};
})

return (
<div>
<h3>SWR Subscription</h3>
<h4>Received every second, error when data is times of 3</h4>
<div>{data}</div>
<div>{error ? error.message : ""}</div>
</div>
)
}

0 comments on commit 4247b80

Please sign in to comment.