Skip to content

Commit b73877b

Browse files
jsdtcloutiertyler
andauthored
Handle duplicate rows and delay callbacks (#146)
* Regen the api types from the server. * fmt the client api code * Use the multi versions of subscription messages. * Add ref counting of rows and delay callbacks. * WIP debugging tests. * Regen test app * A lot of changes. * Use new subscription api. * remove commented out code. * Fix update and cleanup. * Fix path to spacetimedb * Run tests before checking diff in CI * Update packages/sdk/tests/db_connection.test.ts Co-authored-by: Tyler Cloutier <[email protected]> * Update packages/sdk/tests/db_connection.test.ts Co-authored-by: Tyler Cloutier <[email protected]> * Update packages/sdk/tests/db_connection.test.ts Co-authored-by: Tyler Cloutier <[email protected]> * Update packages/sdk/tests/db_connection.test.ts Co-authored-by: Tyler Cloutier <[email protected]> * Various cleanups. * Comment out the CI test for the generated code being up to date. * Regen. * Add sed command. --------- Co-authored-by: Tyler Cloutier <[email protected]>
1 parent 4da0973 commit b73877b

38 files changed

+1026
-325
lines changed

.github/workflows/test.yml

+69-67
Original file line numberDiff line numberDiff line change
@@ -41,73 +41,75 @@ jobs:
4141
- name: Compile
4242
run: pnpm compile
4343

44-
- name: Extract SpacetimeDB branch name from file
45-
id: extract-branch
46-
run: |
47-
# Define the path to the branch file
48-
BRANCH_FILE=".github/spacetimedb-branch.txt"
49-
50-
# Default to master if file doesn't exist
51-
if [ ! -f "$BRANCH_FILE" ]; then
52-
echo "::notice::No SpacetimeDB branch file found, using 'master'"
53-
echo "branch=master" >> $GITHUB_OUTPUT
54-
exit 0
55-
fi
56-
57-
# Read and trim whitespace from the file
58-
branch=$(cat "$BRANCH_FILE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
59-
60-
# Fallback to master if empty
61-
if [ -z "$branch" ]; then
62-
echo "::warning::SpacetimeDB branch file is empty, using 'master'"
63-
branch="master"
64-
fi
65-
66-
echo "branch=$branch" >> $GITHUB_OUTPUT
67-
echo "Using SpacetimeDB branch from file: $branch"
68-
69-
- name: Checkout SpacetimeDB
70-
uses: actions/checkout@v4
71-
with:
72-
repository: clockworklabs/SpacetimeDB
73-
ref: ${{ steps.extract-branch.outputs.branch }}
74-
path: SpacetimeDB
75-
76-
- name: Install Rust toolchain
77-
uses: dtolnay/rust-toolchain@stable
78-
79-
- name: Cache Rust dependencies
80-
uses: Swatinem/rust-cache@v2
81-
with:
82-
workspaces: SpacetimeDB/modules/quickstart-chat
83-
shared-key: quickstart-chat-test
84-
85-
- name: Install SpacetimeDB CLI from the local checkout
86-
run: |
87-
cargo install --force --path SpacetimeDB/crates/cli --locked --message-format=short
88-
cargo install --force --path SpacetimeDB/crates/standalone --locked --message-format=short
89-
# Add a handy alias using the old binary name, so that we don't have to rewrite all scripts (incl. in submodules).
90-
rm $HOME/.cargo/bin/spacetime || echo "haven't run on this host before"
91-
ln -s $HOME/.cargo/bin/spacetimedb-cli $HOME/.cargo/bin/spacetime
92-
# Clear any existing information
93-
spacetime server clear -y
94-
env:
95-
# Share the target directory with our local project to avoid rebuilding same SpacetimeDB crates twice.
96-
CARGO_TARGET_DIR: SpacetimeDB/modules/quickstart-chat/target
97-
98-
- name: Generate client bindings
99-
working-directory: SpacetimeDB/modules/quickstart-chat
100-
run: |
101-
spacetime generate --lang typescript --out-dir ../../../examples/quickstart-chat/src/module_bindings
102-
pnpm lint --write
103-
104-
- name: Check for changes
105-
run: |
106-
git diff --exit-code examples/quickstart-chat/src/module_bindings || {
107-
echo "Error: Bindings are dirty. Please generate bindings again and commit them to this branch."
108-
exit 1
109-
}
110-
44+
- name: Run sdk tests
45+
working-directory: packages/sdk
46+
run: pnpm test
47+
48+
# - name: Extract SpacetimeDB branch name from file
49+
# id: extract-branch
50+
# run: |
51+
# # Define the path to the branch file
52+
# BRANCH_FILE=".github/spacetimedb-branch.txt"
53+
54+
# # Default to master if file doesn't exist
55+
# if [ ! -f "$BRANCH_FILE" ]; then
56+
# echo "::notice::No SpacetimeDB branch file found, using 'master'"
57+
# echo "branch=master" >> $GITHUB_OUTPUT
58+
# exit 0
59+
# fi
60+
61+
# # Read and trim whitespace from the file
62+
# branch=$(cat "$BRANCH_FILE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
63+
64+
# # Fallback to master if empty
65+
# if [ -z "$branch" ]; then
66+
# echo "::warning::SpacetimeDB branch file is empty, using 'master'"
67+
# branch="master"
68+
# fi
69+
70+
# echo "branch=$branch" >> $GITHUB_OUTPUT
71+
# echo "Using SpacetimeDB branch from file: $branch"
72+
73+
# - name: Checkout SpacetimeDB
74+
# uses: actions/checkout@v4
75+
# with:
76+
# repository: clockworklabs/SpacetimeDB
77+
# ref: ${{ steps.extract-branch.outputs.branch }}
78+
# path: SpacetimeDB
79+
80+
# - name: Install Rust toolchain
81+
# uses: dtolnay/rust-toolchain@stable
82+
83+
# - name: Cache Rust dependencies
84+
# uses: Swatinem/rust-cache@v2
85+
# with:
86+
# workspaces: SpacetimeDB/modules/quickstart-chat
87+
# shared-key: quickstart-chat-test
88+
89+
# - name: Install SpacetimeDB CLI from the local checkout
90+
# run: |
91+
# cargo install --force --path SpacetimeDB/crates/cli --locked --message-format=short
92+
# cargo install --force --path SpacetimeDB/crates/standalone --locked --message-format=short
93+
# # Add a handy alias using the old binary name, so that we don't have to rewrite all scripts (incl. in submodules).
94+
# rm $HOME/.cargo/bin/spacetime || echo "haven't run on this host before"
95+
# ln -s $HOME/.cargo/bin/spacetimedb-cli $HOME/.cargo/bin/spacetime
96+
# # Clear any existing information
97+
# spacetime server clear -y
98+
# env:
99+
# # Share the target directory with our local project to avoid rebuilding same SpacetimeDB crates twice.
100+
# CARGO_TARGET_DIR: SpacetimeDB/modules/quickstart-chat/target
101+
102+
# - name: Generate client bindings
103+
# working-directory: SpacetimeDB/modules/quickstart-chat
104+
# run: |
105+
# spacetime generate --lang typescript --out-dir ../../../examples/quickstart-chat/src/module_bindings
106+
# pnpm lint --write
107+
108+
# - name: Check for changes
109+
# run: |
110+
# git diff --exit-code examples/quickstart-chat/src/module_bindings || {
111+
# echo "Error: Bindings are dirty. Please generate bindings again and commit them to this branch."
112+
# exit 1
111113
# - name: Start SpacetimeDB
112114
# run: |
113115
# spacetime start &

DEVELOP.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Within each generated file:
1111

1212
- Change the import from `"@clockworklabs/spacetimedb-sdk"` to `"../index"`.
1313

14+
On a mac, you can do that by running this in the directory: `find . -type f -exec sed -i '' 's/"@clockworklabs\/spacetimedb-sdk"/"..\/index"/g' {} \;`.
15+
1416
## Releases and publishing
1517

1618
Every Pull Request with a public-facing change (Bug fix, perf, feature etc) must be accompanied by a changeset. Any person working on a patch or feature needs to run `pnpm -w changeset` command, which will prompt them to select packages changed. Choose `@clockworklabs/spacetimedb-sdk`

examples/quickstart-chat/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@testing-library/jest-dom": "^6.6.3",
2424
"@testing-library/react": "^16.2.0",
2525
"@testing-library/user-event": "^14.6.1",
26+
"@types/jest": "^29.5.14",
2627
"@types/react": "^18.3.18",
2728
"@types/react-dom": "^18.3.5",
2829
"@vitejs/plugin-react": "^4.3.4",

examples/quickstart-chat/src/App.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ function App() {
9292

9393
useEffect(() => {
9494
const subscribeToQueries = (conn: DbConnection, queries: string[]) => {
95-
let count = 0;
9695
for (const query of queries) {
9796
conn
9897
?.subscriptionBuilder()
9998
.onApplied(() => {
100-
count++;
101-
if (count === queries.length) {
102-
console.log('SDK client cache initialized.');
103-
}
99+
console.log('SDK client cache initialized.');
104100
})
105101
.subscribe(query);
106102
}

examples/quickstart-chat/src/module_bindings/identity_connected_reducer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133

examples/quickstart-chat/src/module_bindings/identity_disconnected_reducer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133

examples/quickstart-chat/src/module_bindings/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133

@@ -227,12 +229,12 @@ export class DbConnection extends DbConnectionImpl<
227229
static builder = (): DbConnectionBuilder<
228230
DbConnection,
229231
ErrorContext,
230-
SubscriptionEventContex
232+
SubscriptionEventContext
231233
> => {
232234
return new DbConnectionBuilder<
233235
DbConnection,
234236
ErrorContext,
235-
SubscriptionEventContex
237+
SubscriptionEventContext
236238
>(REMOTE_MODULE, (imp: DbConnectionImpl) => imp as DbConnection);
237239
};
238240
subscriptionBuilder = (): SubscriptionBuilder => {

examples/quickstart-chat/src/module_bindings/message_table.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133
import { Message } from './message_type';

examples/quickstart-chat/src/module_bindings/message_type.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,11 +26,13 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133
export type Message = {
3234
sender: Identity;
33-
sent: bigint;
35+
sent: Timestamp;
3436
text: string;
3537
};
3638

@@ -45,7 +47,7 @@ export namespace Message {
4547
export function getTypeScriptAlgebraicType(): AlgebraicType {
4648
return AlgebraicType.createProductType([
4749
new ProductTypeElement('sender', AlgebraicType.createIdentityType()),
48-
new ProductTypeElement('sent', AlgebraicType.createU64Type()),
50+
new ProductTypeElement('sent', AlgebraicType.createTimestampType()),
4951
new ProductTypeElement('text', AlgebraicType.createStringType()),
5052
]);
5153
}

examples/quickstart-chat/src/module_bindings/send_message_reducer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133

examples/quickstart-chat/src/module_bindings/set_name_reducer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133

examples/quickstart-chat/src/module_bindings/user_table.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
/* tslint:disable */
66
// @ts-nocheck
77
import {
8-
Address,
98
AlgebraicType,
109
AlgebraicValue,
1110
BinaryReader,
1211
BinaryWriter,
1312
CallReducerFlags,
13+
ConnectionId,
1414
DbConnectionBuilder,
1515
DbConnectionImpl,
1616
DbContext,
@@ -26,6 +26,8 @@ import {
2626
SumType,
2727
SumTypeVariant,
2828
TableCache,
29+
TimeDuration,
30+
Timestamp,
2931
deepEqual,
3032
} from '@clockworklabs/spacetimedb-sdk';
3133
import { User } from './user_type';

0 commit comments

Comments
 (0)