Skip to content

Commit

Permalink
migrated sync context to use weak ref for events; resolves #308
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Oct 30, 2019
1 parent 243c5f0 commit 81fd456
Show file tree
Hide file tree
Showing 31 changed files with 166 additions and 106 deletions.
4 changes: 2 additions & 2 deletions Manatee.Trello.UnitTests/DevTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Manatee.Trello.UnitTests
{
[TestFixture]
[Ignore("This test fixture for development purposes only.")]
//[Ignore("This test fixture for development purposes only.")]
public class DevTest
{
private readonly TrelloFactory _factory = new TrelloFactory();
Expand All @@ -41,7 +41,7 @@ await Run(async ct =>
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine();
//TrelloConfiguration.Cache.Clear();
TrelloConfiguration.Cache.Clear();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public async Task SynchronizeRaisesSynchronizedEvent()
{
NewData = new SynchronizedData {Test = "one"}
};
target.Synchronized += properties => counter++;
target.PublicSynchronized += properties => counter++;

await target.Synchronize(false, CancellationToken.None);

Expand Down Expand Up @@ -195,7 +195,7 @@ public void MergeRaisesSynchronizedEvent()
{
NewData = new SynchronizedData {Test = "one"}
};
target.Synchronized += properties => counter++;
target.PublicSynchronized += properties => counter++;

target.Merge(new SynchronizedData {Test = "two"});

Expand Down Expand Up @@ -226,7 +226,7 @@ public async Task CancelUpdatePreventsSubmittingPendingChanges()
}

[Test]
public async Task DependantContextRequestsChangeTriggersSubmission()
public async Task DependentContextRequestsChangeTriggersSubmission()
{
MockHost.MockJson();
MockHost.JsonFactory.Setup(f => f.Create<SynchronizedData>())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Manatee.Trello.Internal.DataAccess;
Expand All @@ -15,6 +16,8 @@ internal class SynchronizedObject : SynchronizationContext<SynchronizedData>
public SynchronizedData NewData { get; set; }
public bool SupportsUpdates { get; set; } = true;

public event Action<List<string>> PublicSynchronized;

static SynchronizedObject()
{
Properties = new Dictionary<string, Property<SynchronizedData>>
Expand Down Expand Up @@ -70,5 +73,11 @@ protected override bool CanUpdate()
{
return SupportsUpdates;
}

protected override void OnMerged(List<string> properties)
{
PublicSynchronized?.Invoke(properties);
base.OnMerged(properties);
}
}
}
6 changes: 3 additions & 3 deletions Manatee.Trello/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents an action performed on Trello objects.
/// </summary>
public class Action : IAction, IMergeJson<IJsonAction>, IBatchRefresh
public class Action : IAction, IMergeJson<IJsonAction>, IBatchRefresh, IHandleSynchronization
{
/// <summary>
/// Enumerates the data which can be pulled for actions.
Expand Down Expand Up @@ -226,7 +226,7 @@ public Action(string id, TrelloAuthorization auth = null)
{
Id = id;
_context = new ActionContext(id, auth);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);

_creator = new Field<Member>(_context, nameof(Creator));
_date = new Field<DateTime?>(_context, nameof(Date));
Expand Down Expand Up @@ -290,7 +290,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = _context.Data.Id;
var handler = Updated;
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents an attachment to a card.
/// </summary>
public class Attachment : IAttachment, IMergeJson<IJsonAttachment>, IBatchRefresh
public class Attachment : IAttachment, IMergeJson<IJsonAttachment>, IBatchRefresh, IHandleSynchronization
{
/// <summary>
/// Enumerates the data which can be pulled for attachments.
Expand Down Expand Up @@ -198,7 +198,7 @@ internal Attachment(IJsonAttachment json, string ownerId, TrelloAuthorization au
{
Id = json.Id;
_context = new AttachmentContext(Id, ownerId, auth);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);

_bytes = new Field<int?>(_context, nameof(Bytes));
_date = new Field<DateTime?>(_context, nameof(Date));
Expand Down Expand Up @@ -264,7 +264,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = _context.Data.Id;
var handler = Updated;
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents a board.
/// </summary>
public class Board : IBoard, IMergeJson<IJsonBoard>, IBatchRefresh
public class Board : IBoard, IMergeJson<IJsonBoard>, IBatchRefresh, IHandleSynchronization
{
/// <summary>
/// Enumerates the data which can be pulled for boards.
Expand Down Expand Up @@ -376,7 +376,7 @@ static Board()
public Board(string id, TrelloAuthorization auth = null)
{
_context = new BoardContext(id, auth);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);
Id = id;

_description = new Field<string>(_context, nameof(Description));
Expand Down Expand Up @@ -470,7 +470,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
if (Id != _context.Data.Id)
{
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/BoardMembership.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents the permission level a member has on a board.
/// </summary>
public class BoardMembership : IBoardMembership, IMergeJson<IJsonBoardMembership>, IBatchRefresh
public class BoardMembership : IBoardMembership, IMergeJson<IJsonBoardMembership>, IBatchRefresh, IHandleSynchronization
{
private readonly Field<Member> _member;
private readonly Field<BoardMembershipType?> _memberType;
Expand Down Expand Up @@ -70,7 +70,7 @@ internal BoardMembership(IJsonBoardMembership json, string ownerId, TrelloAuthor
{
Id = json.Id;
_context = new BoardMembershipContext(Id, ownerId, auth);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);

_member = new Field<Member>(_context, nameof(Member));
_memberType = new Field<BoardMembershipType?>(_context, nameof(MemberType));
Expand Down Expand Up @@ -117,7 +117,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = _context.Data.Id;
var handler = Updated;
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents a card.
/// </summary>
public class Card : ICard, IMergeJson<IJsonCard>, IBatchRefresh
public class Card : ICard, IMergeJson<IJsonCard>, IBatchRefresh, IHandleSynchronization
{
/// <summary>
/// Enumerates the data which can be pulled for cards.
Expand Down Expand Up @@ -426,7 +426,7 @@ public Card(string id, TrelloAuthorization auth = null)
{
Id = id;
_context = new CardContext(id, auth);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);

Badges = new Badges(_context.BadgesContext);
_board = new Field<Board>(_context, nameof(Board));
Expand Down Expand Up @@ -521,7 +521,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
if (Id != _context.Data.Id)
{
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/CheckItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents a checklist item.
/// </summary>
public class CheckItem : ICheckItem, IMergeJson<IJsonCheckItem>, IBatchRefresh
public class CheckItem : ICheckItem, IMergeJson<IJsonCheckItem>, IBatchRefresh, IHandleSynchronization
{
/// <summary>
/// Enumerates the data which can be pulled for check items.
Expand Down Expand Up @@ -150,7 +150,7 @@ internal CheckItem(IJsonCheckItem json, string checkListId, TrelloAuthorization
TrelloConfiguration.Cache.Add(this);

_context.Merge(json);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);
}

/// <summary>
Expand Down Expand Up @@ -201,7 +201,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = _context.Data.Id;
var handler = Updated;
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/CheckList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents a checklist.
/// </summary>
public class CheckList : ICheckList, IMergeJson<IJsonCheckList>, IBatchRefresh
public class CheckList : ICheckList, IMergeJson<IJsonCheckList>, IBatchRefresh, IHandleSynchronization
{
/// <summary>
/// Enumerates the data which can be pulled for check lists.
Expand Down Expand Up @@ -166,7 +166,7 @@ public CheckList(string id, TrelloAuthorization auth = null)
{
Id = id;
_context = new CheckListContext(id, auth);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);

_board = new Field<Board>(_context, nameof(Board));
_card = new Field<Card>(_context, nameof(Card));
Expand Down Expand Up @@ -233,7 +233,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = _context.Data.Id;
var handler = Updated;
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/CustomField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents a custom field instance.
/// </summary>
public abstract class CustomField : ICustomField, IMergeJson<IJsonCustomField>
public abstract class CustomField : ICustomField, IMergeJson<IJsonCustomField>, IHandleSynchronization
{
private readonly Field<ICustomFieldDefinition> _definition;

Expand Down Expand Up @@ -49,7 +49,7 @@ internal CustomField(IJsonCustomField json, string cardId, TrelloAuthorization a
TrelloConfiguration.Cache.Add(this);

Context.Merge(json);
Context.Synchronized += Synchronized;
Context.Synchronized.Add(this);
}

/// <summary>
Expand All @@ -67,7 +67,7 @@ void IMergeJson<IJsonCustomField>.Merge(IJsonCustomField json, bool overwrite)
Context.Merge(json, overwrite);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = Context.Data.Id;
var handler = Updated;
Expand Down
6 changes: 3 additions & 3 deletions Manatee.Trello/CustomFieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Manatee.Trello
/// <summary>
/// Represents a custom field definition.
/// </summary>
public class CustomFieldDefinition : ICustomFieldDefinition, IMergeJson<IJsonCustomFieldDefinition>, IBatchRefresh
public class CustomFieldDefinition : ICustomFieldDefinition, IMergeJson<IJsonCustomFieldDefinition>, IBatchRefresh, IHandleSynchronization
{
private readonly Field<IBoard> _board;
private readonly Field<string> _fieldGroup;
Expand Down Expand Up @@ -98,7 +98,7 @@ internal CustomFieldDefinition(IJsonCustomFieldDefinition json, TrelloAuthorizat
TrelloConfiguration.Cache.Add(this);

_context.Merge(json);
_context.Synchronized += Synchronized;
_context.Synchronized.Add(this);
}

/// <summary>
Expand Down Expand Up @@ -225,7 +225,7 @@ void IBatchRefresh.Apply(string content)
_context.Merge(json);
}

private void Synchronized(IEnumerable<string> properties)
void IHandleSynchronization.HandleSynchronized(IEnumerable<string> properties)
{
Id = _context.Data.Id;
var handler = Updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public AttachmentContext(string id, string ownerId, TrelloAuthorization auth)
Data.Id = id;

Previews = new ReadOnlyAttachmentPreviewCollection(this, Auth);
Previews.Refreshed += (s, e) => OnMerged(new[] {nameof(Previews)});
Previews.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Previews)});
}

public static void UpdateParameters()
Expand Down
19 changes: 10 additions & 9 deletions Manatee.Trello/Internal/Synchronization/BoardContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,30 @@ static BoardContext()
},
};
}

public BoardContext(string id, TrelloAuthorization auth)
: base(auth)
{
Data.Id = id;

Actions = new ReadOnlyActionCollection(typeof(Board), () => Data.Id, auth);
Actions.Refreshed += (s, e) => OnMerged(new[] {nameof(Actions) });
Actions.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Actions)});
Cards = new ReadOnlyCardCollection(typeof(Board), () => Data.Id, auth);
Cards.Refreshed += (s, e) => OnMerged(new[] {nameof(Cards) });
Cards.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Cards)});
CustomFields = new CustomFieldDefinitionCollection(() => Data.Id, auth);
CustomFields.Refreshed += (s, e) => OnMerged(new[] {nameof(CustomFields) });
CustomFields.Refreshed += (s, e) => OnMerged(new List<string> {nameof(CustomFields)});
Labels = new BoardLabelCollection(() => Data.Id, auth);
Labels.Refreshed += (s, e) => OnMerged(new[] {nameof(Labels) });
Labels.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Labels)});
Lists = new ListCollection(() => Data.Id, auth);
Lists.Refreshed += (s, e) => OnMerged(new[] {nameof(Lists) });
Lists.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Lists)});
Members = new ReadOnlyMemberCollection(EntityRequestType.Board_Read_Members, () => Data.Id, auth);
Members.Refreshed += (s, e) => OnMerged(new[] {nameof(Members) });
Members.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Members)});
Memberships = new BoardMembershipCollection(() => Data.Id, auth);
Memberships.Refreshed += (s, e) => OnMerged(new[] {nameof(Memberships) });
Memberships.Refreshed += (s, e) => OnMerged(new List<string> {nameof(Memberships)});
PowerUps = new PowerUpCollection(() => Data.Id, auth);
PowerUps.Refreshed += (s, e) => OnMerged(new[] {nameof(PowerUps) });
PowerUps.Refreshed += (s, e) => OnMerged(new List<string> {nameof(PowerUps)});
PowerUpData = new ReadOnlyPowerUpDataCollection(EntityRequestType.Board_Read_PowerUpData, () => Data.Id, auth);
PowerUpData.Refreshed += (s, e) => OnMerged(new[] { nameof(PowerUpData) });
PowerUpData.Refreshed += (s, e) => OnMerged(new List<string> {nameof(PowerUpData)});

BoardPreferencesContext = new BoardPreferencesContext(Auth);
BoardPreferencesContext.SubmitRequested += ct => HandleSubmitRequested("Preferences", ct);
Expand Down
Loading

0 comments on commit 81fd456

Please sign in to comment.