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

Restructure Project to support Dependency Injection #17

Merged
merged 3 commits into from
Nov 9, 2022

Conversation

acupofjose
Copy link
Collaborator

This pull represents a restructuring of the entire project to support Dependency Injection and Nullability (ref: supabase-community/supabase-csharp#34 and #16). Unfortunately this introduces some breaking API changes.

  • Client is no longer a singleton class.
  • Channel has a new constructor that uses ChannelOptions
  • Channel.Parameters has been changed in favor of Channel.Options
  • Channel and Push are now directly dependent on having Socket and SerializerSettings passed in as opposed to referencing the Singleton instance.
  • All publicly facing classes (that offer functionality) now include an Interface.

In reality, not much should affect the developer as most of these classes/methods are only being referenced internally by the Client. The removal of the Singleton aspect may offer some design changes for those leveraging this library by itself (as opposed to using it only in supabase-csharp.)

// What was:
var client = Supabase.Realtime.Client.Initialize(options);
await client.ConnectAsync()

// Becomes:
var client = new Client(options);
await client.ConnectAsync()

@acupofjose
Copy link
Collaborator Author

@HunteRoi would you be up to take a look at this one? It's a bit bigger than the previous one.

@acupofjose
Copy link
Collaborator Author

Fixes #16

@acupofjose acupofjose merged commit f74d113 into master Nov 9, 2022
@acupofjose acupofjose deleted the dev/restructure-to-support-di branch November 9, 2022 02:57
Realtime/Channel.cs Show resolved Hide resolved
Realtime/Socket.cs Show resolved Hide resolved

/// <summary>
/// The function to decode incoming messages.
/// </summary>
public Action<string, Action<SocketResponse>> Decode { get; set; } = (payload, callback) => callback(JsonConvert.DeserializeObject<SocketResponse>(payload, Client.Instance.SerializerSettings));
public Action<string, Action<SocketResponse?>>? Decode { get; set; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Socket code that uses this asserts that the value is not null with the ! postfix operator. I don't see anything obvious that actually verifies this so it might be a good idea to add something or change this to be non-nullable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's set in the constructor!

public Client(string realtimeUrl, ClientOptions? options = null)
{
this.realtimeUrl = realtimeUrl;
if (options == null)
options = new ClientOptions();
if (options.Encode == null)
options.Encode = (payload, callback) => callback(JsonConvert.SerializeObject(payload, SerializerSettings));
if (options.Decode == null)
{
options.Decode = (payload, callback) =>
{
var response = new SocketResponse(SerializerSettings);
JsonConvert.PopulateObject(payload, response, SerializerSettings);
callback(response);
};
}
Options = options;
subscriptions = new Dictionary<string, Channel>();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants