Skip to content

Commit 626e209

Browse files
jsdtgefjoncloutiertyler
authored
Typescript v2 API (#4271)
# Description of Changes This update the typescript sdk to use the v2 protocol. The main user-facing API change is removing the existing reducer callbacks, and instead having reducers return a `Promise`. # API and ABI breaking changes The reducer functions `conn.reducers.onX` and `conn.reducers.removeOnX` no longer exist, and reducers now return `Promise<void>`. # Expected complexity level and risk 2.5. # Testing This has mostly been tested manually. I'm still updating some of the unit tests (I commented out the ones that used reducer callbacks). --------- Co-authored-by: Phoebe Goldman <phoebe@goldman-tribe.org> Co-authored-by: = <cloutiertyler@gmail.com>
1 parent 59a2824 commit 626e209

77 files changed

Lines changed: 918 additions & 1290 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/bindings-typescript/src/react/useReducer.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ type ParamsType<R extends UntypedReducerDef> = MaybeParams<
1313

1414
export function useReducer<ReducerDef extends UntypedReducerDef>(
1515
reducerDef: ReducerDef
16-
): (...params: ParamsType<ReducerDef>) => void {
16+
): (...params: ParamsType<ReducerDef>) => Promise<void> {
1717
const { getConnection, isActive } = useSpacetimeDB();
1818
const reducerName = reducerDef.accessorName;
1919

2020
// Holds calls made before the connection exists
21-
const queueRef = useRef<ParamsType<ReducerDef>[]>([]);
21+
const queueRef = useRef<
22+
{
23+
params: ParamsType<ReducerDef>;
24+
resolve: () => void;
25+
reject: (err: unknown) => void;
26+
}[]
27+
>([]);
2228

2329
// Flush when we finally have a connection
2430
useEffect(() => {
@@ -28,11 +34,11 @@ export function useReducer<ReducerDef extends UntypedReducerDef>(
2834
}
2935
const fn = (conn.reducers as any)[reducerName] as (
3036
...p: ParamsType<ReducerDef>
31-
) => void;
37+
) => Promise<void>;
3238
if (queueRef.current.length) {
3339
const pending = queueRef.current.splice(0);
34-
for (const params of pending) {
35-
fn(...params);
40+
for (const item of pending) {
41+
fn(...item.params).then(item.resolve, item.reject);
3642
}
3743
}
3844
}, [getConnection, reducerName, isActive]);
@@ -41,12 +47,13 @@ export function useReducer<ReducerDef extends UntypedReducerDef>(
4147
(...params: ParamsType<ReducerDef>) => {
4248
const conn = getConnection();
4349
if (!conn) {
44-
queueRef.current.push(params);
45-
return;
50+
return new Promise<void>((resolve, reject) => {
51+
queueRef.current.push({ params, resolve, reject });
52+
});
4653
}
4754
const fn = (conn.reducers as any)[reducerName] as (
4855
...p: ParamsType<ReducerDef>
49-
) => void;
56+
) => Promise<void>;
5057
return fn(...params);
5158
},
5259
[getConnection, reducerName]

crates/bindings-typescript/src/sdk/client_api/call_procedure_type.ts

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/call_reducer_type.ts

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/client_message_type.ts

Lines changed: 7 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/event_table_rows_type.ts

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/index.ts

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/initial_connection_type.ts

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/one_off_query_result_type.ts

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/one_off_query_type.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-typescript/src/sdk/client_api/persistent_table_rows_type.ts

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)