Skip to content

Commit

Permalink
Only allow methods on classes as opposed to anonymous lambdas for Eve…
Browse files Browse the repository at this point in the history
…nt Subscription (#512)

* This is a workaround for Revit's order of operations when initializing

* Fix event listening

* Only allow methods on classes as opposed to anonymous lambdas

* formatting

* fix tests

* weakreference found should remove subscription
  • Loading branch information
adamhathcock authored Jan 22, 2025
1 parent c9ca1c0 commit 9846817
Show file tree
Hide file tree
Showing 25 changed files with 316 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,11 @@ ITopLevelExceptionHandler topLevelExceptionHandler
Parent = parent;
Commands = new SendBindingUICommands(parent);
SubscribeToArcGISEvents();
eventAggregator
.GetEvent<DocumentChangedEvent>()
.Subscribe(_ =>
{
_sendConversionCache.ClearCache();
});
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
}

private void OnDocumentStoreChangedEvent(object _) => _sendConversionCache.ClearCache();

private void SubscribeToArcGISEvents()
{
LayersRemovedEvent.Subscribe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ IEventAggregator eventAggregator
Parent = parent;
Commands = new BasicConnectorBindingCommands(parent);

eventAggregator
.GetEvent<DocumentChangedEvent>()
.Subscribe(async _ => await Commands.NotifyDocumentChanged().ConfigureAwait(false));
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
}

private async Task OnDocumentStoreChangedEvent(object _) => await Commands.NotifyDocumentChanged();

public string GetSourceApplicationName() => _speckleApplication.Slug;

public string GetSourceApplicationVersion() => _speckleApplication.HostApplicationVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override async Task OnDocumentStoreInitialized()
{
IsDocumentInit = true;
LoadState();
await _eventAggregator.GetEvent<DocumentChangedEvent>().PublishAsync(new object());
await _eventAggregator.GetEvent<DocumentStoreChangedEvent>().PublishAsync(new object());
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ private async void OnMapViewChanged(ActiveMapViewChangedEventArgs args)

IsDocumentInit = true;
LoadState();
await _eventAggregator.GetEvent<DocumentChangedEvent>().PublishAsync(new object());
await _eventAggregator.GetEvent<DocumentStoreChangedEvent>().PublishAsync(new object());
}

protected override void HostAppSaveState(string modelCardState) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ IThreadContext threadContext
_accountManager = accountManager;
_speckleApplication = speckleApplication;
Commands = new BasicConnectorBindingCommands(parent);
eventAggregator
.GetEvent<DocumentChangedEvent>()
.Subscribe(async _ =>
{
await Commands.NotifyDocumentChanged();
});
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
_logger = logger;
_threadContext = threadContext;
}

private async Task OnDocumentStoreChangedEvent(object _) => await Commands.NotifyDocumentChanged();

public string GetConnectorVersion() => _speckleApplication.SpeckleVersion;

public string GetSourceApplicationName() => _speckleApplication.Slug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ IEventAggregator eventAggregator
}
// Since ids of the objects generates from same seed, we should clear the cache always whenever doc swapped.

eventAggregator.GetEvent<DocumentChangedEvent>().Subscribe(_ => _sendConversionCache.ClearCache());
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
}

private void OnDocumentStoreChangedEvent(object _) => _sendConversionCache.ClearCache();

private readonly List<string> _docSubsTracker = new();

private void SubscribeToObjectChanges(Document doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private async void OnDocChangeInternal(Document? doc)

_previousDocName = currentDocName;
LoadState();
await _eventAggregator.GetEvent<DocumentChangedEvent>().PublishAsync(new object());
await _eventAggregator.GetEvent<DocumentStoreChangedEvent>().PublishAsync(new object());
}

protected override void LoadState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ IEventAggregator eventAggregator
_speckleApplication = speckleApplication;
Commands = new BasicConnectorBindingCommands(parent);

// POC: event binding?
eventAggregator
.GetEvent<DocumentChangedEvent>()
.Subscribe(async _ =>
{
await Commands.NotifyDocumentChanged();
});
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
}

private async Task OnDocumentStoreChangedEvent(object _) => await Commands.NotifyDocumentChanged();

public string GetConnectorVersion() => _speckleApplication.SpeckleVersion;

public string GetSourceApplicationName() => _speckleApplication.Slug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,11 @@ ITopLevelExceptionHandler topLevelExceptionHandler

revitContext.UIApplication.NotNull().Application.DocumentChanged += (_, e) =>
topLevelExceptionHandler.CatchUnhandled(() => DocChangeHandler(e));
eventAggregator
.GetEvent<DocumentChangedEvent>()
.Subscribe(async _ =>
{
await OnDocumentChanged().ConfigureAwait(false);
});
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
}

private async Task OnDocumentStoreChangedEvent(object _) => await Commands.NotifyDocumentChanged();

public List<ISendFilter> GetSendFilters() =>
[
new RevitSelectionFilter() { IsDefault = true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ITopLevelExceptionHandler topLevelExceptionHandler
}

public override Task OnDocumentStoreInitialized() =>
_eventAggregator.GetEvent<DocumentChangedEvent>().PublishAsync(new object());
_eventAggregator.GetEvent<DocumentStoreChangedEvent>().PublishAsync(new object());

/// <summary>
/// This is the place where we track document switch for new document -> Responsible to Read from new doc
Expand All @@ -80,7 +80,7 @@ private void OnViewActivated(object? _, ViewActivatedEventArgs e)
async () =>
{
LoadState();
await _eventAggregator.GetEvent<DocumentChangedEvent>().PublishAsync(new object());
await _eventAggregator.GetEvent<DocumentStoreChangedEvent>().PublishAsync(new object());
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ IEventAggregator eventAggregator
_speckleApplication = speckleApplication;
Commands = new BasicConnectorBindingCommands(parent);

eventAggregator
.GetEvent<DocumentChangedEvent>()
.Subscribe(async _ =>
{
await Commands.NotifyDocumentChanged();
// Note: this prevents scaling issues when copy-pasting from one rhino doc to another in the same session.
_sendConversionCache.ClearCache();
});
eventAggregator.GetEvent<DocumentStoreChangedEvent>().Subscribe(OnDocumentStoreChangedEvent);
}

private async Task OnDocumentStoreChangedEvent(object _)
{
await Commands.NotifyDocumentChanged();
// Note: this prevents scaling issues when copy-pasting from one rhino doc to another in the same session.
_sendConversionCache.ClearCache();
}

public string GetConnectorVersion() => _speckleApplication.SpeckleVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public RhinoSelectionBinding(IBrowserBridge parent, IEventAggregator eventAggreg
}

private void OnSelectionChange(EventArgs eventArgs) =>
_eventAggregator.GetEvent<IdleEvent>().OneTimeSubscribe(nameof(RhinoSelectionBinding), _ => UpdateSelection());
_eventAggregator.GetEvent<IdleEvent>().OneTimeSubscribe(nameof(RhinoSelectionBinding), UpdateSelection);

private void UpdateSelection()
private void UpdateSelection(object _)
{
SelectionInfo selInfo = GetSelection();
Parent.Send(SELECTION_EVENT, selInfo);
Expand Down
Loading

0 comments on commit 9846817

Please sign in to comment.