1
- import type { Manager , Middleware } from '@data-client/core' ;
2
- import type { EndpointInterface } from '@data-client/endpoint' ;
1
+ import type { Manager , Middleware } from '@data-client/react' ;
3
2
import { ActionTypes , Controller , actionTypes } from '@data-client/react' ;
3
+ import type { Entity } from '@data-client/rest' ;
4
4
5
5
/** Updates crypto data using Coinbase websocket stream
6
6
*
@@ -9,7 +9,7 @@ import { ActionTypes, Controller, actionTypes } from '@data-client/react';
9
9
export default class StreamManager implements Manager {
10
10
protected declare middleware : Middleware < ActionTypes > ;
11
11
protected declare evtSource : WebSocket ; // | EventSource;
12
- protected declare endpoints : Record < string , EndpointInterface > ;
12
+ protected declare entities : Record < string , typeof Entity > ;
13
13
protected msgQueue : ( string | ArrayBufferLike | Blob | ArrayBufferView ) [ ] =
14
14
[ ] ;
15
15
@@ -19,9 +19,9 @@ export default class StreamManager implements Manager {
19
19
20
20
constructor (
21
21
evtSource : ( ) => WebSocket , // | EventSource,
22
- endpoints : Record < string , EndpointInterface > ,
22
+ entities : Record < string , typeof Entity > ,
23
23
) {
24
- this . endpoints = endpoints ;
24
+ this . entities = entities ;
25
25
26
26
this . middleware = controller => {
27
27
this . connect = ( ) => {
@@ -55,8 +55,9 @@ export default class StreamManager implements Manager {
55
55
case actionTypes . SUBSCRIBE_TYPE :
56
56
// only process registered endpoints
57
57
if (
58
- ! Object . values ( this . endpoints ) . find (
59
- endpoint => endpoint . key === action . endpoint . key ,
58
+ ! Object . values ( this . entities ) . find (
59
+ // @ts -expect-error
60
+ entity => entity . key === action . endpoint . schema ?. key ,
60
61
)
61
62
)
62
63
break ;
@@ -70,8 +71,9 @@ export default class StreamManager implements Manager {
70
71
case actionTypes . UNSUBSCRIBE_TYPE :
71
72
// only process registered endpoints
72
73
if (
73
- ! Object . values ( this . endpoints ) . find (
74
- endpoint => endpoint . key === action . endpoint . key ,
74
+ ! Object . values ( this . entities ) . find (
75
+ // @ts -expect-error
76
+ entity => entity . key === action . endpoint . schema ?. key ,
75
77
)
76
78
)
77
79
break ;
@@ -122,9 +124,15 @@ export default class StreamManager implements Manager {
122
124
this . product_ids = [ ] ;
123
125
}
124
126
125
- handleMessage ( controller : Controller , msg : any ) {
126
- if ( msg . type in this . endpoints )
127
- controller . setResponse ( this . endpoints [ msg . type ] , msg , msg ) ;
127
+ /** Every websocket message is sent here
128
+ *
129
+ * @param controller
130
+ * @param msg JSON parsed message
131
+ */
132
+ handleMessage ( ctrl : Controller , msg : any ) {
133
+ if ( msg . type in this . entities ) {
134
+ ctrl . set ( this . entities [ msg . type ] , msg , msg ) ;
135
+ }
128
136
}
129
137
130
138
init ( ) {
0 commit comments