Skip to content

refactor(popover-edit): remove circular reference #19679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions goldens/ts-circular-deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
"src/cdk-experimental/dialog/dialog-config.ts",
"src/cdk-experimental/dialog/dialog-container.ts"
],
[
"src/cdk-experimental/popover-edit/edit-event-dispatcher.ts",
"src/cdk-experimental/popover-edit/edit-ref.ts"
],
[
"src/cdk/drag-drop/directives/drag.ts",
"src/cdk/drag-drop/directives/drop-list.ts"
Expand Down
15 changes: 9 additions & 6 deletions src/cdk-experimental/popover-edit/edit-event-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {

import {CELL_SELECTOR, ROW_SELECTOR} from './constants';
import {closest} from './polyfill';
import {EditRef} from './edit-ref';

/** The delay applied to mouse events before hiding or showing hover content. */
const MOUSE_EVENT_DELAY_MS = 40;
Expand All @@ -42,11 +41,15 @@ export const enum HoverContentState {
ON,
}

// Note: this class is generic, rather than referencing EditRef directly, in order to avoid
// circular imports. If we were to reference it here, importing the registry into the
// class that is registering itself will introduce a circular import.

/**
* Service for sharing delegated events and state for triggering table edits.
*/
@Injectable()
export class EditEventDispatcher {
export class EditEventDispatcher<R> {
/** A subject that indicates which table cell is currently editing (unless it is disabled). */
readonly editing = new Subject<Element|null>();

Expand All @@ -70,10 +73,10 @@ export class EditEventDispatcher {
readonly disabledCells = new WeakMap<Element, boolean>();

/** The EditRef for the currently active edit lens (if any). */
get editRef(): EditRef<any>|null {
get editRef(): R|null {
return this._editRef;
}
private _editRef: EditRef<any>|null = null;
private _editRef: R|null = null;

// Optimization: Precompute common pipeable operators used per row/cell.
private readonly _distinctUntilChanged =
Expand Down Expand Up @@ -181,12 +184,12 @@ export class EditEventDispatcher {
}

/** Sets the currently active EditRef. */
setActiveEditRef(ref: EditRef<any>) {
setActiveEditRef(ref: R) {
this._editRef = ref;
}

/** Unsets the currently active EditRef, if the specified editRef is active. */
unsetActiveEditRef(ref: EditRef<any>) {
unsetActiveEditRef(ref: R) {
if (this._editRef !== ref) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cdk-experimental/popover-edit/edit-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class EditRef<FormValue> implements OnDestroy {

constructor(
@Self() private readonly _form: ControlContainer,
private readonly _editEventDispatcher: EditEventDispatcher,
private readonly _editEventDispatcher: EditEventDispatcher<EditRef<FormValue>>,
private readonly _ngZone: NgZone) {
this._editEventDispatcher.setActiveEditRef(this);
}
Expand Down
4 changes: 3 additions & 1 deletion src/cdk-experimental/popover-edit/edit-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
import {EditEventDispatcher} from './edit-event-dispatcher';
import {FocusDispatcher} from './focus-dispatcher';
import {PopoverEditPositionStrategyFactory} from './popover-edit-position-strategy-factory';
import {EditRef} from './edit-ref';

/**
* Optimization
Expand All @@ -26,7 +27,8 @@ import {PopoverEditPositionStrategyFactory} from './popover-edit-position-strate
export class EditServices {
constructor(
readonly directionality: Directionality,
readonly editEventDispatcher: EditEventDispatcher, readonly focusDispatcher: FocusDispatcher,
readonly editEventDispatcher: EditEventDispatcher<EditRef<unknown>>,
readonly focusDispatcher: FocusDispatcher,
readonly focusTrapFactory: FocusTrapFactory, readonly ngZone: NgZone,
readonly overlay: Overlay, readonly positionFactory: PopoverEditPositionStrategyFactory,
readonly scrollDispatcher: ScrollDispatcher, readonly viewportRuler: ViewportRuler) {}
Expand Down
5 changes: 3 additions & 2 deletions src/cdk-experimental/popover-edit/table-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
FocusEscapeNotifierFactory
} from './focus-escape-notifier';
import {closest} from './polyfill';
import {EditRef} from './edit-ref';

/**
* Describes the number of columns before and after the originating cell that the
Expand Down Expand Up @@ -69,7 +70,7 @@ export class CdkEditable implements AfterViewInit, OnDestroy {

constructor(
protected readonly elementRef: ElementRef,
protected readonly editEventDispatcher: EditEventDispatcher,
protected readonly editEventDispatcher: EditEventDispatcher<EditRef<unknown>>,
protected readonly focusDispatcher: FocusDispatcher, protected readonly ngZone: NgZone) {}

ngAfterViewInit(): void {
Expand Down Expand Up @@ -487,7 +488,7 @@ export class CdkRowHoverContent implements AfterViewInit, OnDestroy {
export class CdkEditOpen {
constructor(
protected readonly elementRef: ElementRef<HTMLElement>,
protected readonly editEventDispatcher: EditEventDispatcher) {
protected readonly editEventDispatcher: EditEventDispatcher<EditRef<unknown>>) {

const nativeElement = elementRef.nativeElement;

Expand Down