Skip to content

Commit 5b6f1fc

Browse files
committed
feat: import serializers stuff from livekit-client instead
1 parent 9ed1b0b commit 5b6f1fc

3 files changed

Lines changed: 10 additions & 110 deletions

File tree

packages/react/etc/components-react.api.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -675,20 +675,6 @@ export const ScreenShareIcon: (props: SVGProps<SVGSVGElement>) => React_2.JSX.El
675675
// @internal (undocumented)
676676
export const ScreenShareStopIcon: (props: SVGProps<SVGSVGElement>) => React_2.JSX.Element;
677677

678-
// @beta
679-
export type Serializer<Input = any, Output = any> = {
680-
symbol: typeof SerializerSymbol;
681-
parse: (raw: string) => Input;
682-
serialize: (val: Output) => string;
683-
};
684-
685-
// @beta
686-
export const serializers: {
687-
json: typeof json;
688-
raw: typeof raw;
689-
custom: typeof custom;
690-
};
691-
692678
// @beta (undocumented)
693679
export type SessionCallbacks = {
694680
[SessionEvent.ConnectionStateChanged]: (newAgentConnectionState: ConnectionState_2) => void;
@@ -1525,9 +1511,6 @@ export { WidgetState }
15251511
//
15261512
// src/context/layout-context.ts:10:3 - (ae-forgotten-export) The symbol "PinContextType" needs to be exported by the entry point index.docs.d.ts
15271513
// src/context/layout-context.ts:11:3 - (ae-forgotten-export) The symbol "WidgetContextType" needs to be exported by the entry point index.docs.d.ts
1528-
// src/hooks/useRpc.ts:95:25 - (ae-forgotten-export) The symbol "json" needs to be exported by the entry point index.docs.d.ts
1529-
// src/hooks/useRpc.ts:95:25 - (ae-forgotten-export) The symbol "raw" needs to be exported by the entry point index.docs.d.ts
1530-
// src/hooks/useRpc.ts:95:25 - (ae-forgotten-export) The symbol "custom" needs to be exported by the entry point index.docs.d.ts
15311514

15321515
// (No @packageDocumentation comment for this package)
15331516

packages/react/src/hooks/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,10 @@ export {
7171
export * from './useEvents';
7272
export * from './useSessionMessages';
7373
export {
74-
type Serializer,
7574
type RpcHandler,
7675
type RpcCallParams,
7776
type UseRpcOptions,
7877
type RpcCallFn,
7978
type UseRpcReturn,
80-
serializers,
8179
useRpc,
8280
} from './useRpc';

packages/react/src/hooks/useRpc.ts

Lines changed: 10 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,18 @@
11
import * as React from 'react';
2-
import { RpcError, type RpcInvocationData, type PerformRpcParams } from 'livekit-client';
2+
import {
3+
RpcError,
4+
type RpcInvocationData,
5+
type PerformRpcParams,
6+
type Serializer,
7+
isSerializer,
8+
type SerializerInput,
9+
type SerializerOutput,
10+
serializers,
11+
} from 'livekit-client';
312

413
import { useEnsureSession } from '../context';
514
import { isUseSessionReturn, type UseSessionReturn } from './useSession';
615

7-
// ---------------------------------------------------------------------------
8-
// Serializer types + infrastructure
9-
// ---------------------------------------------------------------------------
10-
11-
const SerializerSymbol = Symbol.for('lk.serializer');
12-
13-
/**
14-
* A bidirectional data format descriptor for RPC payloads.
15-
*
16-
* - `parse(raw)` decodes an incoming wire string into `Input` (used by handlers)
17-
* - `serialize(val)` encodes an `Output` value to a wire string (used by handlers)
18-
*
19-
* For symmetric serializers (`serializers.raw`), `Input === Output === string`.
20-
* For `serializers.json`, both default to `any` so each handler can annotate its own types.
21-
* Use `serializers.custom` to supply your own `parse`/`serialize` pair.
22-
*
23-
* @beta
24-
*/
25-
export type Serializer<Input = any, Output = any> = {
26-
symbol: typeof SerializerSymbol;
27-
parse: (raw: string) => Input;
28-
serialize: (val: Output) => string;
29-
};
30-
31-
function isSerializer(v: unknown): v is Serializer<any, any> {
32-
return (
33-
typeof v === 'object' &&
34-
v !== null &&
35-
'symbol' in v &&
36-
(v as Record<string, unknown>)['symbol'] === SerializerSymbol
37-
);
38-
}
39-
40-
// Extract Input/Output from a Serializer type for use in handler/performRpc constraints.
41-
type SerializerInput<S> = S extends Serializer<infer Input, any> ? Input : any;
42-
type SerializerOutput<S> = S extends Serializer<any, infer Output> ? Output : any;
43-
44-
/** @internal */
45-
function base<Input = any, Output = any>(
46-
params: Omit<Serializer<Input, Output>, 'symbol'>,
47-
): Serializer<Input, Output> {
48-
return { ...params, symbol: SerializerSymbol };
49-
}
50-
51-
/**
52-
* JSON serializer — `JSON.parse` on the way in, `JSON.stringify` on the way out.
53-
* Defaults to `any` so individual handlers can annotate their own payload types.
54-
*/
55-
function json<Input = any, Output = any>(): Serializer<Input, Output> {
56-
return base({
57-
parse: (raw: string) => JSON.parse(raw) as Input,
58-
serialize: (val: unknown) => JSON.stringify(val),
59-
});
60-
}
61-
62-
/** Raw string serializer — passes payloads through as plain strings with no encoding. */
63-
function raw() {
64-
return base({
65-
parse: (raw: string) => raw,
66-
serialize: (val: string) => val,
67-
});
68-
}
69-
70-
/** Custom serializer - allows custom defined parse and serialize functions */
71-
function custom<Input = any, Output = any>(
72-
params: Omit<Serializer<Input, Output>, 'symbol'>,
73-
): Serializer<Input, Output> {
74-
return base(params);
75-
}
76-
77-
/**
78-
* Serializer helpers for RPC payload encoding.
79-
*
80-
* @example
81-
* ```ts
82-
* // Inline handler — types inferred
83-
* useRpc(session, "myMethod", async (payload: MyInput) => myOutput);
84-
*
85-
* // Override default serializer for a handler
86-
* useRpc(session, "myMethod", async (payload) => payload, { serializer: serializers.raw() });
87-
*
88-
* // Manual serializer instances
89-
* const a = serializers.raw(); // Serializer<string, string>
90-
* const b = serializer.json<{ foo: string }, { bar: string }>(); // Serializer<{ foo: string }, { bar: string }>
91-
* ```
92-
*
93-
* @beta
94-
*/
95-
export const serializers = { json, raw, custom };
96-
9716
// ---------------------------------------------------------------------------
9817
// RPC types
9918
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)