Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder committed Dec 17, 2023
1 parent cbc7586 commit d89de7d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
36 changes: 13 additions & 23 deletions MiniTwitch.Helix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,33 @@ public class Program

static async Task Main()
{
Helix = new HelixWrapper("Access token", 7654321);
Helix = new HelixWrapper("Access token", 987654321);

await GetFirst1000Usernames(12345678);
await GetAllUsernames(12345678);
var chatters = await GetAllChatters(12345678);
}

private static async Task<IReadOnlyList<string>> GetFirst1000Usernames(long broadcasterId)
private static async Task<List<string>> GetAllChatters(long broadcasterId)
{
List<string> usernames = new();
List<string> usernames = [];
HelixResult<Chatters> chatters = await Helix.GetChatters(broadcasterId, first: 1000);

// Make sure the result is a success and the value contains data
if (!chatters.Success) return Array.Empty<string>();

foreach (var chatter in chatters.Value.Data)
if (!chatters.Success)
{
usernames.Add(chatter.Username);
return [];
}

return usernames;
}

private static async Task<IReadOnlyList<string>> GetAllUsernames(long broadcasterId)
{
List<string> usernames = new();
HelixResult<Chatters> chatters = await Helix.GetChatters(broadcasterId, first: 1000);

if (!chatters.Success) return Array.Empty<string>();

foreach (var chatter in chatters.Value.Data)
{
usernames.Add(chatter.Username);
}

// No more users - return what we got
if (!chatters.CanPaginate) return usernames;
if (!chatters.CanPaginate)
{
return usernames;
}

// Continue paginating if the result is a success and there is content
// Continue paginating if the result is a success
while (await chatters.Paginate() is { Success: true } next)
{
foreach (var chatter in next.Value.Data)
Expand All @@ -83,7 +72,8 @@ public class Program
}

// Return when pagination is no longer possible
if (!next.CanPaginate) return usernames;
if (!next.CanPaginate)
return usernames;

chatters = next;
}
Expand Down
51 changes: 21 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,53 +193,43 @@ public class Program

static async Task Main()
{
Helix = new HelixWrapper("Access token", "Client ID");
Helix = new HelixWrapper("Access token", 987654321);

await GetFirst1000Usernames(12345678, 12345678);
await GetAllUsernames(12345678, 12345678);
var chatters = await GetAllChatters(12345678);
}

private static async Task<IReadOnlyList<string>> GetFirst1000Usernames(long broadcasterId, long moderatorId)
private static async Task<List<string>> GetAllChatters(long broadcasterId)
{
List<string> usernames = new();
HelixResult<Chatters> chatters = await Helix.GetChatters(broadcasterId, moderatorId, first: 1000);
List<string> usernames = [];
HelixResult<Chatters> chatters = await Helix.GetChatters(broadcasterId, first: 1000);

// Make sure the result is a success and the value contains data
if (!chatters.Success || !chatters.Value.HasContent) return Array.Empty<string>();

foreach (var chatter in chatters.Value.Data)
if (!chatters.Success)
{
usernames.Add(chatter.Username);
return [];
}

return usernames;
}

private static async Task<IReadOnlyList<string>> GetAllUsernames(long broadcasterId, long moderatorId)
{
List<string> usernames = new();
HelixResult<Chatters> chatters = await Helix.GetChatters(broadcasterId, moderatorId, first: 1000);

if (!chatters.Success || !chatters.Value.HasContent) return Array.Empty<string>();

foreach (var chatter in chatters.Value.Data)
{
usernames.Add(chatter.Username);
}

// No more users - return what we got
if (!chatters.CanPaginate) return usernames;
if (!chatters.CanPaginate)
{
return usernames;
}

// Continue paginating if the result is a success and there is content
while (await chatters.Paginate() is { Success: true, Value.HasContent: true } next)
// Continue paginating if the result is a success
while (await chatters.Paginate() is { Success: true } next)
{
foreach (var chatter in next.Value.Data)
{
usernames.Add(chatter.Username);
}

// Return when pagination is no longer possible
if (!next.CanPaginate) return usernames;
if (!next.CanPaginate)
return usernames;

chatters = next;
}
Expand All @@ -253,10 +243,12 @@ public class Program

### Subscribing to events

All events use either the `Func<T, ValueTask>` or `Func<ValueTask>` delegates. Meaning that subscribing methods must have the return type `ValueTask` and account for the `T` parameter when it's present (see `Client.OnMessage` in the example above), the parameter name you set does not matter. Also, if the method doesn't have any asynchronous code you should not mark it as `async` and return `ValueTask.CompletedTask`, `default`or some `ValueTask` object instead.
All events use either the `Func<T, ValueTask>`, `Func<T1, T2, ValueTask>` or `Func<ValueTask>` delegates. Meaning that subscribing methods must have the return type `ValueTask` and account for the `T` parameters when present (see `client.OnStreamUp` in the PubSub example above), the parameter name you set does not matter.

If the method doesn't have any asynchronous code, it is recommended to not mark it as `async` and return `ValueTask.CompletedTask`, `default` or some `ValueTask` object instead.

Subscribing can be done either programmatically:

Subscribing can be done either by declaring methods:

```c#
Client.OnMessage += MessageEvent;
Expand All @@ -276,7 +268,7 @@ Client.OnMessage += async message =>
};
```

Note that you cannot unsubscribe from events if you used anonymous functions.
Note that you cannot unsubscribe from anonymous functions.

Read more about event subscriptions [here](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events)

Expand All @@ -288,9 +280,8 @@ You can add the packages to your project by searching for them with the package

```c#
Install-Package MiniTwitch.Irc
```
```c#
Install-Package MiniTwitch.PubSub
Install-Package MiniTwitch.Helix
```

## Dependencies
Expand Down

0 comments on commit d89de7d

Please sign in to comment.