-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
feat(LayoutManager): Expose objects registration #9661
Changes from 3 commits
670a688
72c4872
91774c6
37667fb
3f9dd02
08377f0
51b4322
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,12 @@ import { | |
LAYOUT_TYPE_OBJECT_MODIFIED, | ||
LAYOUT_TYPE_OBJECT_MODIFYING, | ||
} from './constants'; | ||
import type { LayoutContext, LayoutResult, StrictLayoutContext } from './types'; | ||
import type { | ||
LayoutContext, | ||
LayoutResult, | ||
RegistrationContext, | ||
StrictLayoutContext, | ||
} from './types'; | ||
import { classRegistry } from '../ClassRegistry'; | ||
|
||
const LAYOUT_MANAGER = 'layoutManager'; | ||
|
@@ -59,14 +64,14 @@ export class LayoutManager { | |
/** | ||
* subscribe to object layout triggers | ||
*/ | ||
protected subscribe(object: FabricObject, context: StrictLayoutContext) { | ||
protected subscribe(object: FabricObject, context: { target: Group }) { | ||
const { target } = context; | ||
this.unsubscribe(object, context); | ||
const disposers = [ | ||
object.on('modified', (e) => | ||
this.performLayout({ | ||
trigger: 'modified', | ||
e: { ...e, target: object }, | ||
e, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the object modified, target is already available in the event, we don't need to repeat it. At leat this says typescript There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on line 88 ( now 93 ) we do the same for the other event, but in my opinion we shouldn't. If we want to forward the original event that is fine, but we are adding the target itself in the object event, we don't need to copy it in the forwarded event. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember something about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that there is a target prop to this object, that is why i don't see any reason to duplicate it on the event. |
||
type: LAYOUT_TYPE_OBJECT_MODIFIED, | ||
target, | ||
}) | ||
|
@@ -99,13 +104,17 @@ export class LayoutManager { | |
* unsubscribe object layout triggers | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
protected unsubscribe(object: FabricObject, context?: StrictLayoutContext) { | ||
protected unsubscribe(object: FabricObject, context?: { target: Group }) { | ||
(this._subscriptions.get(object) || []).forEach((d) => d()); | ||
this._subscriptions.delete(object); | ||
} | ||
|
||
unsubscribeTarget(target: Group) { | ||
target.forEachObject((object) => this.unsubscribe(object)); | ||
unsubscribeTargets(context: RegistrationContext) { | ||
context.targets.forEach((object) => this.unsubscribe(object, context)); | ||
} | ||
|
||
subscribeTargets(context: RegistrationContext) { | ||
context.targets.forEach((object) => this.subscribe(object, context)); | ||
} | ||
|
||
protected onBeforeLayout(context: StrictLayoutContext) { | ||
|
@@ -116,9 +125,9 @@ export class LayoutManager { | |
context.type === LAYOUT_TYPE_INITIALIZATION || | ||
context.type === LAYOUT_TYPE_ADDED | ||
) { | ||
context.targets.forEach((object) => this.subscribe(object, context)); | ||
this.subscribeTargets(context); | ||
} else if (context.type === LAYOUT_TYPE_REMOVED) { | ||
context.targets.forEach((object) => this.unsubscribe(object, context)); | ||
this.unsubscribeTargets(context); | ||
} | ||
// fire layout event (event will fire only for layouts after initialization layout) | ||
target.fire('layout:before', { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this 3 tests were failing because i wrote bad code. And it was not immediate to me to understand what they were testing for 2 reasons:
The connection between the array of data and the test variables force me to look up and down every time to understandand what is action and what is trigger, and the lifecycle array was a convoluted way to spell 3 times what has been calle, but without clearly saying with what has been called.
In general we should keep those array method for tests either for simple cases [true, false] or for large cases in which we need to use points or something else.
In the other cases i really prefer to spell clearly what the test is doing