-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaddRealtimeMethodsWithFakeTransport.ts
44 lines (39 loc) · 1.47 KB
/
addRealtimeMethodsWithFakeTransport.ts
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
import { DataProvider } from 'react-admin';
import { RealTimeDataProvider, Subscriptions } from '@react-admin/ra-realtime';
let subscriptions: Subscriptions = [];
const addRealtimeMethodsWithFakeTransport = (
dataProvider: DataProvider
): DataProvider & RealTimeDataProvider => {
return {
...dataProvider,
subscribe: async (topic, subscriptionCallback) => {
subscriptions.push({ topic, subscriptionCallback });
return Promise.resolve({ data: null });
},
unsubscribe: async (topic, subscriptionCallback) => {
subscriptions = subscriptions.filter(
subscription =>
subscription.topic !== topic ||
subscription.subscriptionCallback !== subscriptionCallback
);
return Promise.resolve({ data: null });
},
publish: (topic, event) => {
if (!topic) {
return Promise.reject(new Error('The topic is missing'));
}
if (!event.type) {
return Promise.reject(
new Error('The type of the event is missing')
);
}
subscriptions.map(
subscription =>
topic === subscription.topic &&
subscription.subscriptionCallback(event)
);
return Promise.resolve({ data: null });
},
};
};
export default addRealtimeMethodsWithFakeTransport;