-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
102 lines (95 loc) · 2.51 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { useEffect, useRef, useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import { Observable, Subscription } from 'rxjs';
import { Client, createClient, RequestParams } from "graphql-sse";
import './App.css'
// https://the-guild.dev/graphql/sse/recipes
export function toObservable(operation: RequestParams, client: Client) {
return new Observable((observer) =>
client.subscribe(operation, {
next: (data) => {
console.log('data', data);
observer.next(data);
},
error: (err) => {
console.error('error', err);
observer.error(err);
},
complete: () => {
console.log('complete');
observer.complete();
},
}),
);
}
function App() {
const [count, setCount] = useState(0)
const client = useRef(createClient({
url: '/graphql',
on: {
connecting: () => console.log('connecting'),
connected: () => {
console.log('connected');
},
message: (message) => console.log('message', message),
}
}));
const lol = useRef<Subscription | undefined>(undefined);
useEffect(() => {
if (lol.current) {
return;
}
const observable = toObservable({
query: `
subscription {
mqttSubscription {
payload
}
}
`, variables: {
tenantId: "test"
}
}, client.current);
const subscription = observable.subscribe({
next: (data) => {
console.log('data', data);
},
error: (error) => {
console.error('error', error);
},
complete: () => {
console.log('complete');
}
});
lol.current = subscription;
console.log('subscription', subscription);
// subscription.unsubscribe();
observable.pipe().subscribe({ next: (data) => console.log('data', data) });
}, []);
return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
}
export default App