Skip to content

Commit 80c3323

Browse files
committed
docs(demo): Skip logging websocket updates; Stream uses Entities
1 parent beef8d1 commit 80c3323

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

examples/coin-app/src/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import {
1212
getDefaultManagers,
1313
DevToolsManager,
1414
NetworkManager,
15+
actionTypes,
1516
} from '@data-client/react';
1617
import StreamManager from 'resources/StreamManager';
17-
import { getTicker } from 'resources/Ticker';
18+
import { Ticker } from 'resources/Ticker';
1819

1920
import App from './App';
2021
import { createRouter } from './routing';
@@ -32,7 +33,7 @@ const spouts = JSONSpout()(
3233
return [
3334
new StreamManager(
3435
() => new WebSocket('wss://ws-feed.exchange.coinbase.com'),
35-
{ ticker: getTicker },
36+
{ ticker: Ticker },
3637
),
3738
...getManagers(),
3839
];
@@ -59,12 +60,11 @@ function getManagers() {
5960
{
6061
// double latency to help with high frequency updates
6162
latency: 1000,
62-
// don't use replacer because it is way too slow for our fast updating
63-
serialize: {
64-
options: undefined,
65-
},
63+
// skip websocket updates as these are too spammy
64+
predicate: (state, action) =>
65+
action.type !== actionTypes.SET_TYPE || action.schema !== Ticker,
6666
},
67-
networkManager?.skipLogging?.bind?.(networkManager),
67+
networkManager && (action => networkManager.skipLogging(action)),
6868
),
6969
);
7070
}

examples/coin-app/src/resources/StreamManager.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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';
32
import { ActionTypes, Controller, actionTypes } from '@data-client/react';
3+
import type { Entity } from '@data-client/rest';
44

55
/** Updates crypto data using Coinbase websocket stream
66
*
@@ -9,7 +9,7 @@ import { ActionTypes, Controller, actionTypes } from '@data-client/react';
99
export default class StreamManager implements Manager {
1010
protected declare middleware: Middleware<ActionTypes>;
1111
protected declare evtSource: WebSocket; // | EventSource;
12-
protected declare endpoints: Record<string, EndpointInterface>;
12+
protected declare entities: Record<string, typeof Entity>;
1313
protected msgQueue: (string | ArrayBufferLike | Blob | ArrayBufferView)[] =
1414
[];
1515

@@ -19,9 +19,9 @@ export default class StreamManager implements Manager {
1919

2020
constructor(
2121
evtSource: () => WebSocket, // | EventSource,
22-
endpoints: Record<string, EndpointInterface>,
22+
entities: Record<string, typeof Entity>,
2323
) {
24-
this.endpoints = endpoints;
24+
this.entities = entities;
2525

2626
this.middleware = controller => {
2727
this.connect = () => {
@@ -55,8 +55,9 @@ export default class StreamManager implements Manager {
5555
case actionTypes.SUBSCRIBE_TYPE:
5656
// only process registered endpoints
5757
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,
6061
)
6162
)
6263
break;
@@ -70,8 +71,9 @@ export default class StreamManager implements Manager {
7071
case actionTypes.UNSUBSCRIBE_TYPE:
7172
// only process registered endpoints
7273
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,
7577
)
7678
)
7779
break;
@@ -122,9 +124,15 @@ export default class StreamManager implements Manager {
122124
this.product_ids = [];
123125
}
124126

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+
}
128136
}
129137

130138
init() {

examples/coin-app/src/resources/Ticker.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@ export class Ticker extends Entity {
55
product_id = '';
66
trade_id = 0;
77
price = 0;
8-
size = '0';
98
time = new Date(0);
10-
bid = '0';
11-
ask = '0';
12-
volume = '';
9+
// last_size = '0';
10+
// best_bid = '0';
11+
// best_ask = '0';
12+
// volume_24h = '';
13+
// volume_30d = '';
14+
open_24h = 0;
1315

1416
pk(): string {
1517
return this.product_id;
1618
}
1719

1820
// implementing `key` makes us robust against class name mangling
19-
static key = 'Ticker';
21+
static key = 'ticker';
2022

2123
// convert price to a float and time to a Date
2224
// see https://dataclient.io/rest/api/Entity#schema
2325
static schema = {
2426
price: Number,
27+
open_24h: Number,
2528
time: (iso: string) => new Date(iso),
2629
};
2730

@@ -42,9 +45,12 @@ export class Ticker extends Entity {
4245
args: any[],
4346
): any {
4447
const value = { ...input };
48+
// sometimes product_id is not included in the API response
4549
if (args[0].product_id) {
4650
value.product_id = args[0].product_id;
4751
}
52+
// fallback to current price to show no gain if we don't have 24 hour
53+
if (!value.open_24h) value.open_24h = value.price;
4854
return value;
4955
}
5056
}
@@ -54,4 +60,5 @@ export const getTicker = new RestEndpoint({
5460
path: '/products/:product_id/ticker',
5561
schema: Ticker,
5662
channel: 'ticker',
63+
pollFrequency: 2000,
5764
});

0 commit comments

Comments
 (0)