Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify update handling as per consistent filtering proposal #93

Merged
merged 9 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions examples/quickstart/client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using SpacetimeDB;
using SpacetimeDB.Types;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SpacetimeDB;
using SpacetimeDB.Types;

// our local client SpacetimeDB identity
Identity? local_identity = null;
// declare a thread safe queue to store commands in format (command, args)
ConcurrentQueue<(string,string)> input_queue = new ConcurrentQueue<(string, string)>();
// declare a thread safe queue to store commands
var input_queue = new ConcurrentQueue<(string Command, string Args)>();
// declare a threadsafe cancel token to cancel the process loop
CancellationTokenSource cancel_token = new CancellationTokenSource();
var cancel_token = new CancellationTokenSource();

void Main()
{
Expand Down Expand Up @@ -41,25 +45,25 @@ void RegisterCallbacks()
Reducer.OnSendMessageEvent += Reducer_OnSendMessageEvent;
}

string UserNameOrIdentity(User user) => user.Name ?? user.Identity.ToString()!.Substring(0, 8);
string UserNameOrIdentity(User user) => user.Name ?? user.Identity.ToString()[..8];

void User_OnInsert(User insertedValue, ReducerEvent? dbEvent)
{
if(insertedValue.Online)
if (insertedValue.Online)
{
Console.WriteLine($"{UserNameOrIdentity(insertedValue)} is online");
}
}

void User_OnUpdate(User oldValue, User newValue, ReducerEvent? dbEvent)
{
if(oldValue.Name != newValue.Name)
if (oldValue.Name != newValue.Name)
{
Console.WriteLine($"{UserNameOrIdentity(oldValue)} renamed to {newValue.Name}");
}
if(oldValue.Online != newValue.Online)
if (oldValue.Online != newValue.Online)
{
if(newValue.Online)
if (newValue.Online)
{
Console.WriteLine($"{UserNameOrIdentity(newValue)} connected.");
}
Expand All @@ -72,9 +76,9 @@ void User_OnUpdate(User oldValue, User newValue, ReducerEvent? dbEvent)

void PrintMessage(Message message)
{
var sender = User.FilterByIdentity(message.Sender);
var sender = User.FindByIdentity(message.Sender);
var senderName = "unknown";
if(sender != null)
if (sender != null)
{
senderName = UserNameOrIdentity(sender);
}
Expand All @@ -84,15 +88,15 @@ void PrintMessage(Message message)

void Message_OnInsert(Message insertedValue, ReducerEvent? dbEvent)
{
if(dbEvent != null)
if (dbEvent != null)
{
PrintMessage(insertedValue);
}
}

void Reducer_OnSetNameEvent(ReducerEvent reducerEvent, string name)
{
if(reducerEvent.Identity == local_identity && reducerEvent.Status == ClientApi.Event.Types.Status.Failed)
if (reducerEvent.Identity == local_identity && reducerEvent.Status == ClientApi.Event.Types.Status.Failed)
{
Console.Write($"Failed to change name to {name}");
}
Expand Down Expand Up @@ -156,14 +160,14 @@ void InputLoop()
while (true)
{
var input = Console.ReadLine();
if(input == null)
if (input == null)
{
break;
}

if(input.StartsWith("/name "))
if (input.StartsWith("/name "))
{
input_queue.Enqueue(("name", input.Substring(6)));
input_queue.Enqueue(("name", input[6..]));
continue;
}
else
Expand All @@ -178,13 +182,13 @@ void ProcessCommands()
// process input queue commands
while (input_queue.TryDequeue(out var command))
{
switch (command.Item1)
switch (command.Command)
{
case "message":
Reducer.SendMessage(command.Item2);
Reducer.SendMessage(command.Args);
break;
case "name":
Reducer.SetName(command.Item2);
Reducer.SetName(command.Args);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/quickstart/client/client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
58 changes: 18 additions & 40 deletions examples/quickstart/client/module_bindings/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;
using System.Collections.Generic;
using SpacetimeDB;
using System.Collections.Generic;
using System.Linq;

namespace SpacetimeDB.Types
{
Expand Down Expand Up @@ -43,9 +44,7 @@ public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType()

public static explicit operator Message(SpacetimeDB.SATS.AlgebraicValue value)
{
if (value == null) {
return null;
}
RReverser marked this conversation as resolved.
Show resolved Hide resolved
if (value == null) return null;
var productValue = value.AsProductValue();
return new Message
{
Expand All @@ -55,58 +54,37 @@ public static explicit operator Message(SpacetimeDB.SATS.AlgebraicValue value)
};
}

public static System.Collections.Generic.IEnumerable<Message> Iter()
public static IEnumerable<Message> Iter()
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
yield return (Message)entry.Item2;
}
return SpacetimeDBClient.clientDB.GetObjects("Message").Cast<Message>();
}
public static int Count()

public static IEnumerable<Message> Query(Func<Message, bool> filter)
{
return SpacetimeDBClient.clientDB.Count("Message");
return Iter().Where(filter);
}
public static System.Collections.Generic.IEnumerable<Message> FilterBySender(SpacetimeDB.Identity value)

public static int Count()
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
var productValue = entry.Item1.AsProductValue();
var compareValue = Identity.From(productValue.elements[0].AsProductValue().elements[0].AsBytes());
if (compareValue == value) {
yield return (Message)entry.Item2;
}
}
return SpacetimeDBClient.clientDB.Count("Message");
}

public static System.Collections.Generic.IEnumerable<Message> FilterBySent(ulong value)
public static IEnumerable<Message> FilterBySender(SpacetimeDB.Identity value)
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
var productValue = entry.Item1.AsProductValue();
var compareValue = (ulong)productValue.elements[1].AsU64();
if (compareValue == value) {
yield return (Message)entry.Item2;
}
}
return Query(x => x.Sender == value);
}

public static System.Collections.Generic.IEnumerable<Message> FilterByText(string value)
public static IEnumerable<Message> FilterBySent(ulong value)
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
var productValue = entry.Item1.AsProductValue();
var compareValue = (string)productValue.elements[2].AsString();
if (compareValue == value) {
yield return (Message)entry.Item2;
}
}
return Query(x => x.Sent == value);
}

public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2)
public static IEnumerable<Message> FilterByText(string value)
{
return false;
return Query(x => x.Text == value);
}


public delegate void InsertEventHandler(Message insertedValue, SpacetimeDB.Types.ReducerEvent dbEvent);
public delegate void DeleteEventHandler(Message deletedValue, SpacetimeDB.Types.ReducerEvent dbEvent);
public static event InsertEventHandler OnInsert;
Expand Down
24 changes: 1 addition & 23 deletions examples/quickstart/client/module_bindings/ReducerEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;
using SpacetimeDB;
using ClientApi;
using Newtonsoft.Json.Linq;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
Expand Down Expand Up @@ -41,27 +41,5 @@ public SetNameArgsStruct SetNameArgs
return (SetNameArgsStruct)Args;
}
}

public object[] GetArgsAsObjectArray()
{
switch (Reducer)
{
case ReducerType.SendMessage:
{
var args = SendMessageArgs;
return new object[] {
args.Text,
};
}
case ReducerType.SetName:
{
var args = SetNameArgs;
return new object[] {
args.Name,
};
}
default: throw new System.Exception($"Unhandled reducer case: {Reducer}. Please run SpacetimeDB code generator");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;
using SpacetimeDB;

namespace SpacetimeDB.Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;
using SpacetimeDB;
using ClientApi;
using Newtonsoft.Json.Linq;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.

using System;
using SpacetimeDB;
using ClientApi;
using Newtonsoft.Json.Linq;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
Expand Down
Loading
Loading