Loading...
(function(){<br />
let canvas = document.createElement('canvas'),<br />
ctx = canvas.getContext('2d'),<br />
w = canvas.width = innerWidth,<br />
h = canvas.height = innerHeight,<br />
particles = [],<br />
properties = {<br />
bgColor : 'rgba(17, 17, 19, 1)',<br />
particleColor : 'rgba(255, 40, 40, 1)',<br />
particleRadius : 3,<br />
particleCount : 60,<br />
particleMaxVelocity : 0.5,<br />
lineLength : 150,<br />
particleLife : 6,<br />
};<br />
<br />
document.querySelector('body').appendChild(canvas);<br />
<br />
window.onresize = function() {<br />
w = canvas.width = innerWidth;<br />
h = canvas.height = innerHeight;<br />
}<br />
<br />
class Particle {<br />
constructor() {<br />
this.x = Math.random()*w;<br />Loading...
To update data via a WebSocket in RTK Query, there is a function called onCacheEntryAdded.
In the example below:
If the data is an array, you update it using push, as shown in the RTK Query documentation:
1updateCachedData((draft) => {2 draft.push(data)3})
If the data is an object, you update its fields directly:
1updateCachedData((draft) => {2 draft.price = data.price;3 draft.count = data.count;4});
Full RTK Query API file example:
1// mock data example2// {3// price: 123,4// count 245// }67import type { Action, PayloadAction } from "@reduxjs/toolkit";8import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";9import { HYDRATE } from "next-redux-wrapper";10import { TPriceResponse } from "@/types";11import { isMessage } from "./schemaValidators";1213type RootState = any;1415function isHydrateAction(action: Action): action is PayloadAction<RootState> {16 return action.type === HYDRATE;17}1819export const baseUrl = process.env.NEXT_PUBLIC_BASE_URL;20export const baseWebsocketUrl =21 process.env.NEXT_PUBLIC_BASE_WEBSOCKET_URL;2223export const mainApi = createApi({24 baseQuery: fetchBaseQuery({ baseUrl: baseUrl }),25 extractRehydrationInfo(action, { reducerPath }): any {26 if (isHydrateAction(action)) {27 return action.payload[reducerPath];28 }29 },30 tagTypes: [`Prices`],31 endpoints: (builder) => ({32 getPrice: builder.query<TPriceResponse, undefined>({33 query: () => `/price`,34 async onCacheEntryAdded(35 arg,36 { updateCachedData, cacheDataLoaded, cacheEntryRemoved }37 ) {38 const ws = new WebSocket(baseWebsocketUrl);39 try {40 await cacheDataLoaded;41 const listener = (event: MessageEvent) => {42 const data = JSON.parse(event.data);43 if (!isMessage(data)) return;44 updateCachedData((draft) => {45 draft.price = data.price;46 draft.count = data.count;47 });48 };495051 ws.addEventListener("message", listener);52 } catch {}53 await cacheEntryRemoved;54 ws.close();55 },56 providesTags: [`Prices`],57 }),58 }),59});6061export const {62 useGetPriceQuery,63 util: { getRunningQueriesThunk },64} = mainApi;
Example isMessage function:
1import { TPriceResponse } from "@/types";23export function isMessage(message: TPriceResponse) {4 if (message?.price || message?.count) {5 return true;6 }789 return false;10}