Skip to content

Commit

Permalink
A bit more nesting work
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 25, 2024
1 parent 6e6177e commit 6c96933
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public sealed class RemoteTables
public readonly RemoteTableHandle<EventContext, User> User = new();
}

public sealed class RemoteReducers : RemoteBase<DbConnection>
public sealed class RemoteReducers
{
internal RemoteReducers(DbConnection conn) : base(conn) {}
private readonly DbConnection conn;

internal RemoteReducers(DbConnection conn)
{
this.conn = conn;
}

public delegate void SendMessageHandler(EventContext ctx, string text);
public event SendMessageHandler? OnSendMessage;
Expand Down
22 changes: 14 additions & 8 deletions src/SpacetimeDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public DbConnectionBuilder<DbConnection, EventContext> OnDisconnect(Action<DbCon
}
}

public abstract class DbConnectionBase<DbConnection, EventContext>
public interface IDbConnection
{
internal void Subscribe(string query);
}

public abstract class DbConnectionBase<DbConnection, EventContext> : IDbConnection
where DbConnection : DbConnectionBase<DbConnection, EventContext>, new()
where EventContext : class, IEventContext
{
Expand Down Expand Up @@ -707,20 +712,21 @@ public void InternalCallReducer<T>(T args)
));
}

public void Subscribe(List<string> queries)
void IDbConnection.Subscribe(string query)
{
if (!webSocket.IsConnected)
{
Logger.LogError("Cannot subscribe, not connected to server!");
return;
}

var request = new Subscribe
{
RequestId = stats.SubscriptionRequestTracker.StartTrackingRequest(),
};
request.QueryStrings.AddRange(queries);
webSocket.Send(new ClientMessage.Subscribe(request));
webSocket.Send(new ClientMessage.Subscribe(
new Subscribe
{
RequestId = stats.SubscriptionRequestTracker.StartTrackingRequest(),
QueryStrings = { query }
}
));
}

/// Usage: SpacetimeDBClientBase.instance.OneOffQuery<Message>("WHERE sender = \"bob\"");
Expand Down
98 changes: 46 additions & 52 deletions src/Stubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,24 @@ public interface IEventContext {
bool InvokeHandler();
}

public abstract class DbContext<DbView, ReducerView, EventContext> : DbContext<DbView>
where DbView : class, new()
where ReducerView : class
where EventContext : EventContextBase<EventContext, DbView, ReducerView>
{
public readonly ReducerView Reducers;

public DbContext(DbView db, ReducerView reducers) : base(db) => Reducers = reducers;

// TODO: this needs to become a concrete EventContext type.
public SubscriptionBuilder<EventContext> SubscriptionBuilder() => new();
}

public sealed class SubscriptionBuilder<TEventContext>
where TEventContext : IEventContext
{
// TODO: this class should become abstract, with a separate generated concrete type.
public class SubscriptionHandle
{
internal SubscriptionHandle(Action<TEventContext>? onApplied, Action<TEventContext>? onError, string querySql) {}

public void Unsubscribe() => throw new NotImplementedException();
public void UnsuscribeThen(Action<TEventContext> onEnd) => throw new NotImplementedException();
public bool IsEnded => throw new NotImplementedException();
public bool IsActive => throw new NotImplementedException();
}

private event Action<TEventContext>? Applied;
private event Action<TEventContext>? Error;

public SubscriptionBuilder<TEventContext> OnApplied(Action<TEventContext> callback)
{
Applied += callback;
return this;
}

public SubscriptionBuilder<TEventContext> OnError(Action<TEventContext> callback)
{
Error += callback;
return this;
}

public SubscriptionHandle Subscribe(string querySql) => new(Applied, Error, querySql);
}

public abstract class EventContextBase<EventContext, RemoteTables, RemoteReducers> : DbContext<RemoteTables, RemoteReducers, EventContext>, IEventContext
public abstract class EventContextBase<EventContext, RemoteTables, RemoteReducers> : DbContext<RemoteTables>, IEventContext
where EventContext : EventContextBase<EventContext, RemoteTables, RemoteReducers>
where RemoteTables : class, new()
where RemoteReducers : class
{
private readonly IDbConnection conn;
public readonly RemoteReducers Reducers;

public ulong Timestamp { get; }
public Identity? Identity { get; }
public Address? CallerAddress { get; }
public string? ErrMessage { get; }
public UpdateStatus? Status { get; }

public EventContextBase(RemoteTables db, RemoteReducers reducers, TransactionUpdate update) : base(db, reducers)
public EventContextBase(IDbConnection conn, RemoteTables db, RemoteReducers reducers, TransactionUpdate update) : base(db)
{
this.conn = conn;
Reducers = reducers;
Timestamp = update.Timestamp.Microseconds;
Identity = update.CallerIdentity;
CallerAddress = update.CallerAddress;
Expand All @@ -88,14 +48,48 @@ public EventContextBase(RemoteTables db, RemoteReducers reducers, TransactionUpd
}

public abstract bool InvokeHandler();
}

public abstract class RemoteBase<DbConnection> {
protected readonly DbConnection conn;
public sealed class SubscriptionBuilderBase
{
// TODO: this class should become abstract, with a separate generated concrete type.
public class SubscriptionHandle
{
internal SubscriptionHandle(IDbConnection conn, Action<EventContext>? onApplied, Action<EventContext>? onError, string querySql)
{
conn.Subscribe(querySql);
}

public void Unsubscribe() => throw new NotImplementedException();
public void UnsuscribeThen(Action<EventContext> onEnd) => throw new NotImplementedException();
public bool IsEnded => throw new NotImplementedException();
public bool IsActive => throw new NotImplementedException();
}

protected RemoteBase(DbConnection conn) {
this.conn = conn;
private readonly IDbConnection conn;
private event Action<EventContext>? Applied;
private event Action<EventContext>? Error;

internal SubscriptionBuilderBase(IDbConnection conn)
{
this.conn = conn;
}

public SubscriptionBuilderBase OnApplied(Action<EventContext> callback)
{
Applied += callback;
return this;
}

public SubscriptionBuilderBase OnError(Action<EventContext> callback)
{
Error += callback;
return this;
}

public SubscriptionHandle Subscribe(string querySql) => new(conn, Applied, Error, querySql);
}

public SubscriptionBuilderBase SubscriptionBuilder() => new(conn);
}

public class RemoteTableHandle<EventContext, T> where EventContext : class, IEventContext {
Expand Down

0 comments on commit 6c96933

Please sign in to comment.