MCPcopy
hub / github.com/msgbyte/tianji / useSocketSubscribeList

Function useSocketSubscribeList

src/client/api/socketio.ts:159–191  ·  view source on GitHub ↗
(name: K, options: UseSocketSubscribeListOptions<K, T> = {})

Source from the content-addressed store, hash-verified

157}
158const defaultFilter = () => true;
159export function useSocketSubscribeList<
160 K extends keyof SubscribeEventMap = keyof SubscribeEventMap,
161 T = SubscribeEventData<K>,
162>(name: K, options: UseSocketSubscribeListOptions<K, T> = {}): T[] {
163 const { filter = defaultFilter } = options;
164 const { subscribe } = useSocket();
165 const isLogined = useIsLogined();
166 const [list, push] = useReducer(
167 (state: T[], data: T) => [...state, data],
168 [] as T[]
169 );
170
171 const cb = useEvent((_data) => {
172 if (filter(_data)) {
173 push(_data);
174 }
175 });
176
177 useEffect(() => {
178 if (!isLogined) {
179 console.warn('Skip socket subscribe login because of not login');
180 return;
181 }
182
183 const unsubscribe = subscribe(name, cb);
184
185 return () => {
186 unsubscribe();
187 };
188 }, [name]);
189
190 return list;
191}

Callers 4

PageComponentFunction · 0.90

Calls 6

useIsLoginedFunction · 0.90
useEventFunction · 0.90
useSocketFunction · 0.85
subscribeFunction · 0.85
unsubscribeFunction · 0.85
warnMethod · 0.80

Tested by

no test coverage detected