Skip to content

Commit e3c34f7

Browse files
author
Meepster23
committed
Fix deadlock in non-console applications
1 parent f94fefa commit e3c34f7

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

RedditSharp/Reddit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public Listing<Subreddit> SearchSubreddits(string query)
493493
protected async internal Task<T> GetThingAsync<T>(string url) where T : Thing
494494
{
495495
var request = WebAgent.CreateGet(url);
496-
var response = request.GetResponse();
496+
var response = await request.GetResponseAsync();
497497
var data = WebAgent.GetResponseString(response.GetResponseStream());
498498
var json = JToken.Parse(data);
499499
var ret = await Thing.ParseAsync(this, json, WebAgent);

RedditSharp/Things/AuthenticatedUser.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@ public class AuthenticatedUser : RedditUser
1818

1919
public new async Task<AuthenticatedUser> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
2020
{
21-
await CommonInit(reddit, json, webAgent);
21+
await CommonInitAsync(reddit, json, webAgent);
2222
await JsonConvert.PopulateObjectAsync(json["name"] == null ? json["data"].ToString() : json.ToString(), this,
2323
reddit.JsonSerializerSettings);
2424
return this;
2525
}
2626
public new AuthenticatedUser Init(Reddit reddit, JToken json, IWebAgent webAgent)
2727
{
28-
CommonInit(reddit, json, webAgent).Wait();
28+
CommonInit(reddit, json, webAgent);
2929
JsonConvert.PopulateObject(json["name"] == null ? json["data"].ToString() : json.ToString(), this,
3030
reddit.JsonSerializerSettings);
3131
return this;
3232
}
33-
private async Task CommonInit(Reddit reddit, JToken json, IWebAgent webAgent)
33+
private void CommonInit(Reddit reddit, JToken json, IWebAgent webAgent)
3434
{
35-
await base.InitAsync(reddit, json, webAgent);
35+
base.Init(reddit, json, webAgent);
36+
}
37+
private async Task CommonInitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
38+
{
39+
await base.InitAsync(reddit, json, webAgent).ConfigureAwait(false);
3640
}
37-
3841
public Listing<Subreddit> ModeratorSubreddits
3942
{
4043
get

RedditSharp/Things/Comment.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ public class Comment : VotableThing
2525

2626
public async Task<Comment> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
2727
{
28-
var data = await CommonInit(reddit, json, webAgent, sender);
29-
await ParseComments(reddit, json, webAgent, sender);
28+
var data = await CommonInitAsync(reddit, json, webAgent, sender);
29+
await ParseCommentsAsync(reddit, json, webAgent, sender);
3030
await JsonConvert.PopulateObjectAsync(data.ToString(), this, reddit.JsonSerializerSettings);
3131
return this;
3232
}
3333

3434
public Comment Init(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
3535
{
3636
var data = CommonInit(reddit, json, webAgent, sender);
37-
ParseComments(reddit, json, webAgent, sender).Wait();
37+
ParseComments(reddit, json, webAgent, sender);
3838
JsonConvert.PopulateObject(data.ToString(), this, reddit.JsonSerializerSettings);
3939
return this;
4040
}
4141

42-
private async Task<JToken> CommonInit(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
42+
private JToken CommonInit(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
4343
{
44-
await InitAsync(reddit, webAgent, json);
44+
Init(reddit, webAgent, json);
4545
var data = json["data"];
4646
Reddit = reddit;
4747
WebAgent = webAgent;
@@ -56,8 +56,38 @@ private async Task<JToken> CommonInit(Reddit reddit, JToken json, IWebAgent webA
5656

5757
return data;
5858
}
59+
private async Task<JToken> CommonInitAsync(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
60+
{
61+
await InitAsync(reddit, webAgent, json);
62+
var data = json["data"];
63+
Reddit = reddit;
64+
WebAgent = webAgent;
65+
Parent = sender;
66+
67+
// Handle Reddit's API being horrible
68+
if (data["context"] != null)
69+
{
70+
var context = data["context"].Value<string>();
71+
LinkId = context.Split('/')[4];
72+
}
73+
74+
return data;
75+
}
76+
77+
private void ParseComments(Reddit reddit, JToken data, IWebAgent webAgent, Thing sender)
78+
{
79+
// Parse sub comments
80+
var replies = data["data"]["replies"];
81+
var subComments = new List<Comment>();
82+
if (replies != null && replies.Count() > 0)
83+
{
84+
foreach (var comment in replies["data"]["children"])
85+
subComments.Add( new Comment().Init(reddit, comment, webAgent, sender));
86+
}
87+
Comments = subComments.ToArray();
88+
}
5989

60-
private async Task ParseComments(Reddit reddit, JToken data, IWebAgent webAgent, Thing sender)
90+
private async Task ParseCommentsAsync(Reddit reddit, JToken data, IWebAgent webAgent, Thing sender)
6191
{
6292
// Parse sub comments
6393
var replies = data["data"]["replies"];

RedditSharp/Things/CreatedThing.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ public class CreatedThing : Thing
99
{
1010
private Reddit Reddit { get; set; }
1111

12-
protected async Task<CreatedThing> Init(Reddit reddit, JToken json)
12+
protected CreatedThing Init(Reddit reddit, JToken json)
13+
{
14+
CommonInit(reddit, json);
15+
JsonConvert.PopulateObject(json["data"].ToString(), this, reddit.JsonSerializerSettings);
16+
return this;
17+
}
18+
protected async Task<CreatedThing> InitAsync(Reddit reddit, JToken json)
1319
{
1420
CommonInit(reddit, json);
1521
await JsonConvert.PopulateObjectAsync(json["data"].ToString(), this, reddit.JsonSerializerSettings);

RedditSharp/Things/Post.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,23 @@ public class Post : VotableThing
3939
/// <returns></returns>
4040
public async Task<Post> InitAsync(Reddit reddit, JToken post, IWebAgent webAgent)
4141
{
42-
await CommonInit(reddit, post, webAgent);
42+
await CommonInitAsync(reddit, post, webAgent);
4343
await JsonConvert.PopulateObjectAsync(post["data"].ToString(), this, reddit.JsonSerializerSettings);
4444
return this;
4545
}
4646
public Post Init(Reddit reddit, JToken post, IWebAgent webAgent)
4747
{
48-
CommonInit(reddit, post, webAgent).Wait();
48+
CommonInit(reddit, post, webAgent);
4949
JsonConvert.PopulateObject(post["data"].ToString(), this, reddit.JsonSerializerSettings);
5050
return this;
5151
}
52-
private async Task CommonInit(Reddit reddit, JToken post, IWebAgent webAgent)
52+
private void CommonInit(Reddit reddit, JToken post, IWebAgent webAgent)
53+
{
54+
base.Init(reddit, webAgent, post);
55+
Reddit = reddit;
56+
WebAgent = webAgent;
57+
}
58+
private async Task CommonInitAsync(Reddit reddit, JToken post, IWebAgent webAgent)
5359
{
5460
await base.InitAsync(reddit, webAgent, post);
5561
Reddit = reddit;

RedditSharp/Things/VotableThing.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,25 @@ public enum DistinguishType
4848

4949
protected async Task<VotableThing> InitAsync(Reddit reddit, IWebAgent webAgent, JToken json)
5050
{
51-
await CommonInit(reddit, webAgent, json);
51+
await CommonInitAsync(reddit, webAgent, json);
5252
await JsonConvert.PopulateObjectAsync(json["data"].ToString(), this, Reddit.JsonSerializerSettings);
5353
return this;
5454
}
5555
protected VotableThing Init(Reddit reddit, IWebAgent webAgent, JToken json)
5656
{
57-
CommonInit(reddit, webAgent, json).Wait();
57+
CommonInit(reddit, webAgent, json);
5858
JsonConvert.PopulateObject(json["data"].ToString(), this, Reddit.JsonSerializerSettings);
5959
return this;
6060
}
61-
private async Task CommonInit(Reddit reddit, IWebAgent webAgent, JToken json)
61+
private void CommonInit(Reddit reddit, IWebAgent webAgent, JToken json)
6262
{
63-
await Init(reddit, json);
63+
Init(reddit, json);
64+
Reddit = reddit;
65+
WebAgent = webAgent;
66+
}
67+
private async Task CommonInitAsync(Reddit reddit, IWebAgent webAgent, JToken json)
68+
{
69+
await InitAsync(reddit, json);
6470
Reddit = reddit;
6571
WebAgent = webAgent;
6672
}

0 commit comments

Comments
 (0)