Skip to content

Commit d01957a

Browse files
authored
SubscribeMulti + UnsubscribeMulti, multiplicities (#249)
## Description of Changes Add SubscribeMulti and UnsubscribeMulti from upstream. Fix unsubscribe bug found by testing against Bitcraft: Calling `UnsubscribeThen` with any (non-null) callback would result in an exception incorrectly telling the user that `Unsubscribe` had been called twice. Multiplicity support is implemented with unit tests + manual testing of quickstart-chat. Also, rows in the client cache are now looked up by primary key if available, which I suspect is going to be a large performance boost. ## API - [x] This is an API breaking change to the SDK *If the API is breaking, please state below what will break* ## Requires SpacetimeDB PRs *List any PRs here that are required for this SDK change to work* ## Testsuite *If you would like to run the your SDK changes in this PR against a specific SpacetimeDB branch, specify that here. This can be a branch name or a link to a PR.* SpacetimeDB branch name: jgilles/final-cs-codegen-changes ## Testing *Write instructions for a test that you performed for this PR* - [x] unit tests - [ ] Working with Ryan to add a test suite. - [x] blackholio
1 parent c102182 commit d01957a

24 files changed

+1879
-506
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
This repository contains the [Unity](https://unity.com/) SDK for SpacetimeDB. The SDK allows to interact with the database server and is prepared to work with code generated from a SpacetimeDB backend code.
5+
This repository contains the C# and [Unity](https://unity.com/) SDKs for SpacetimeDB. The SDK allows to interact with the database server and is prepared to work with code generated from a SpacetimeDB backend code.
66

77
## Documentation
88

examples~/quickstart/client/Program.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ void PrintMessage(RemoteTables tables, Message message)
9494

9595
void Message_OnInsert(EventContext ctx, Message insertedValue)
9696
{
97+
9798
if (ctx.Event is not Event<Reducer>.SubscribeApplied)
9899
{
99100
PrintMessage(ctx.Db, insertedValue);
@@ -123,25 +124,15 @@ void OnConnect(DbConnection conn, Identity identity, string authToken)
123124
local_identity = identity;
124125
AuthToken.SaveToken(authToken);
125126

126-
var subscriptions = 0;
127-
Action<SubscriptionEventContext> waitForSubscriptions = (SubscriptionEventContext ctx) =>
128-
{
129-
// Note: callbacks are always invoked on the main thread, so you don't need to
130-
// worry about thread synchronization or anything like that.
131-
subscriptions += 1;
132-
133-
if (subscriptions == 2)
134-
{
135-
OnSubscriptionApplied(ctx);
136-
}
137-
};
138-
139-
var userSubscription = conn.SubscriptionBuilder()
140-
.OnApplied(waitForSubscriptions)
141-
.Subscribe("SELECT * FROM user");
142-
var messageSubscription = conn.SubscriptionBuilder()
143-
.OnApplied(waitForSubscriptions)
144-
.Subscribe("SELECT * FROM message");
127+
var subscription = conn.SubscriptionBuilder()
128+
.OnApplied(OnSubscriptionApplied)
129+
.Subscribe(new string[] {
130+
"SELECT * FROM user",
131+
"SELECT * FROM message",
132+
// It is legal to have redundant subscriptions.
133+
// However, keep in mind that data will be sent over the wire multiple times,
134+
// once for each subscriptions. This can cause slowdowns if you aren't careful.
135+
"SELECT * FROM message" });
145136

146137
// You can also use SubscribeToAllTables, but it should be avoided if you have any large tables:
147138
// conn.SubscriptionBuilder().OnApplied(OnSubscriptionApplied).SubscribeToAllTables();

0 commit comments

Comments
 (0)