Skip to content

Commit

Permalink
fix(synchronizers): support for async event handlers in Synchronizer …
Browse files Browse the repository at this point in the history
…class. (#883)

* Add support for async event handlers in
Synchronizer class.

* Build cast fix

* Refactor annotations filtering in CrosshairsTool

* Update annotations type in CrosshairsTool

* Fix annotation retrieval in CrosshairsTool.

---------

Co-authored-by: Bill Wallace <[email protected]>
  • Loading branch information
sedghi and wayfarer3130 authored Nov 13, 2023
1 parent dde6ed0 commit b012b4b
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 50 deletions.
4 changes: 2 additions & 2 deletions common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ export class CrosshairsTool extends AnnotationTool {
// (undocumented)
_filterViewportWithSameOrientation: (enabledElement: any, referenceAnnotation: any, annotations: any) => any;
// (undocumented)
_getAnnotations: (enabledElement: Types_2.IEnabledElement) => Annotations;
_getAnnotations: (enabledElement: Types_2.IEnabledElement) => Annotation[];
// (undocumented)
_getAnnotationsForViewportsWithDifferentCameras: (enabledElement: any, annotations: any) => any;
// (undocumented)
Expand Down Expand Up @@ -3233,7 +3233,7 @@ function isViewportPreScaled(viewport: Types_2.IStackViewport | Types_2.IVolumeV
// @public (undocumented)
interface ISynchronizerEventHandler {
// (undocumented)
(synchronizer: Synchronizer, sourceViewport: Types_2.IViewportId, targetViewport: Types_2.IViewportId, sourceEvent: any, options?: any): void;
(synchronizer: Synchronizer, sourceViewport: Types_2.IViewportId, targetViewport: Types_2.IViewportId, sourceEvent: any, options?: any): Promise<void> | void;
}

// @public (undocumented)
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const DICTIONARY = Symbol('Dictionary');
export default class Settings {
constructor(base?: Settings) {
const dictionary = Object.create(
base instanceof Settings && DICTIONARY in base ? base[DICTIONARY] : null
(base instanceof Settings && DICTIONARY in base
? base[DICTIONARY]
: null) as object
);
Object.seal(
Object.defineProperty(this, DICTIONARY, {
Expand Down
20 changes: 12 additions & 8 deletions packages/tools/src/store/SynchronizerManager/Synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,19 @@ class Synchronizer {
if (targetIsSource) {
continue;
}
promises.push(
this._eventHandler(
this,
sourceViewport,
targetViewport,
sourceEvent,
this._options
)
const result = this._eventHandler(
this,
sourceViewport,
targetViewport,
sourceEvent,
this._options
);

// if the result is a promise, then add it to the list of promises
// to wait for before setting _ignoreFiredEvents to false
if (result instanceof Promise) {
promises.push(result);
}
}
} catch (ex) {
console.warn(`Synchronizer, for: ${this._eventName}`, ex);
Expand Down
14 changes: 13 additions & 1 deletion packages/tools/src/tools/CrosshairsTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,19 @@ class CrosshairsTool extends AnnotationTool {

_getAnnotations = (enabledElement: Types.IEnabledElement) => {
const { viewport } = enabledElement;
return getAnnotations(this.getToolName(), viewport.element);
const annotations =
getAnnotations(this.getToolName(), viewport.element) || [];
const viewportIds = this._getViewportsInfo().map(
({ viewportId }) => viewportId
);

// filter the annotations to only keep that are for this toolGroup
const toolGroupAnnotations = annotations.filter((annotation) => {
const { data } = annotation;
return viewportIds.includes(data.viewportId);
});

return toolGroupAnnotations;
};

_onNewVolume = (e: any) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/src/types/ISynchronizerEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export default interface ISynchronizerEventHandler {
targetViewport: Types.IViewportId,
sourceEvent: any,
options?: any
): void;
): Promise<void> | void;
}
Loading

0 comments on commit b012b4b

Please sign in to comment.