-
Notifications
You must be signed in to change notification settings - Fork 673
Dxclick: nodes-disposing #35014
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
base: main
Are you sure you want to change the base?
Dxclick: nodes-disposing #35014
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { | ||
| afterEach, describe, expect, it, jest, | ||
| } from '@jest/globals'; | ||
| import { removeEvent } from '@js/common/core/events/remove'; | ||
| import eventsEngine from '@ts/events/core/m_events_engine'; | ||
|
|
||
| import { name as clickEventName } from '../click'; | ||
|
|
||
| type ElementEventData = Record<string, { handleObjects: unknown[] } | undefined>; | ||
|
|
||
| const noop = (): void => {}; | ||
|
|
||
| const getRemoveHandlersCount = (element: Element): number => { | ||
| const elementData = eventsEngine.elementDataMap.get(element) as ElementEventData | undefined; | ||
|
Contributor
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 main The fix makes the helper smaller rather than bigger. Main types the map as const getRemoveHandlersCount = (element: Element): number => {
const elementData = eventsEngine.elementDataMap?.get(element);
return elementData?.[removeEvent]?.handleObjects.length ?? 0;
};I rebased the branch locally with just this and the import above changed, and both tests pass and the project type checks clean. |
||
|
|
||
| return elementData?.[removeEvent]?.handleObjects.length ?? 0; | ||
| }; | ||
|
|
||
| describe('dxclick nodes disposing (5025)', () => { | ||
| const clickableNodes: HTMLElement[] = []; | ||
|
|
||
| const createClickableNode = (): HTMLElement => { | ||
| const node = document.createElement('div'); | ||
|
|
||
| document.body.appendChild(node); | ||
| eventsEngine.on(node, clickEventName, noop); | ||
| clickableNodes.push(node); | ||
|
|
||
| return node; | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| clickableNodes.forEach((node) => { | ||
| eventsEngine.off(node); | ||
| node.remove(); | ||
| }); | ||
| clickableNodes.length = 0; | ||
| }); | ||
|
|
||
| it('keeps a foreign dxremove handler on the previously clicked node', () => { | ||
| const clicked = createClickableNode(); | ||
| const other = createClickableNode(); | ||
|
|
||
| clicked.click(); | ||
|
|
||
| const foreignHandler = jest.fn(); | ||
| eventsEngine.on(clicked, removeEvent, foreignHandler); | ||
|
|
||
| other.click(); | ||
| eventsEngine.triggerHandler(clicked, { type: removeEvent }); | ||
|
|
||
| expect(foreignHandler).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('removes only its own dxremove handler from the previously clicked node', () => { | ||
| const clicked = createClickableNode(); | ||
| const other = createClickableNode(); | ||
|
|
||
| clicked.click(); | ||
| expect(getRemoveHandlersCount(clicked)).toBe(1); | ||
|
|
||
| eventsEngine.on(clicked, removeEvent, noop); | ||
| expect(getRemoveHandlersCount(clicked)).toBe(2); | ||
|
|
||
| other.click(); | ||
|
|
||
| expect(getRemoveHandlersCount(clicked)).toBe(1); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,10 +28,11 @@ interface NodesDisposingSubscription { | |
|
|
||
| let prevented: boolean | null = null; | ||
| let lastFiredEvent: NativeClickEvent | null = null; | ||
| const subscriptions = new Map<NativeClickEvent, NodesDisposingSubscription>(); | ||
| let lastSubscription: NodesDisposingSubscription | null = null; | ||
|
|
||
| const onNodeRemove = (): void => { | ||
| lastFiredEvent = null; | ||
| lastSubscription = null; | ||
| }; | ||
|
|
||
| const clickHandler = function (e: EmitterEvent & { originalEvent: NativeClickEvent }): void { | ||
|
|
@@ -45,25 +46,19 @@ const clickHandler = function (e: EmitterEvent & { originalEvent: NativeClickEve | |
| originalEvent.DXCLICK_FIRED = true; | ||
| } | ||
|
|
||
| if (lastFiredEvent && subscriptions.has(lastFiredEvent)) { | ||
| // @ts-expect-error the subscription stores onceCallback, not callback, so this | ||
| // destructured callback is always undefined and off() drops every dxremove | ||
| // handler from the nodes | ||
| const { nodes, callback } = subscriptions.get(lastFiredEvent) as NodesDisposingSubscription; | ||
| if (lastFiredEvent && lastSubscription) { | ||
|
Contributor
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. Small one, the code is correct as it stands.
if (lastSubscription) {
unsubscribeNodesDisposing(null, lastSubscription.onceCallback, lastSubscription.nodes);
}
lastFiredEvent = originalEvent;
lastSubscription = originalEvent ? subscribeNodesDisposing(originalEvent, onNodeRemove) : null; |
||
| const { nodes, onceCallback } = lastSubscription; | ||
|
|
||
| unsubscribeNodesDisposing(lastFiredEvent, callback, nodes); | ||
| unsubscribeNodesDisposing(lastFiredEvent, onceCallback, nodes); | ||
|
|
||
| subscriptions.delete(lastFiredEvent); | ||
| lastSubscription = null; | ||
| } | ||
|
|
||
| lastFiredEvent = originalEvent; | ||
|
|
||
| const subscriptionData: NodesDisposingSubscription = subscribeNodesDisposing( | ||
| lastFiredEvent, | ||
| onNodeRemove, | ||
| ); | ||
|
|
||
| subscriptions.set(lastFiredEvent, subscriptionData); | ||
| if (originalEvent) { | ||
| lastSubscription = subscribeNodesDisposing(originalEvent, onNodeRemove); | ||
| } | ||
|
|
||
| fireEvent({ | ||
| type: CLICK_EVENT_NAME, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import $ from 'jquery'; | ||
| import { noop } from 'core/utils/common'; | ||
| import clickEvent from 'common/core/events/click'; | ||
| import { removeEvent } from 'common/core/events/remove'; | ||
| import domUtils from '__internal/core/utils/m_dom'; | ||
| import support from '__internal/core/utils/m_support'; | ||
| import devices from '__internal/core/m_devices'; | ||
|
|
@@ -425,3 +426,21 @@ QUnit.test('dxclick should not be fired twice when \'click\' is triggered from i | |
| pointer.start().down().up(); | ||
| $(document).off('dxclick', $.noop); | ||
| }); | ||
|
|
||
| QUnit.test('foreign dxremove handler on the previously clicked node should survive a dxclick on another node (5025)', function(assert) { | ||
|
Contributor
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. The test never checks that the two clicks actually reached the dxclick handler. If they ever stop firing, |
||
| const $clicked = $('#first').on('dxclick', noop); | ||
| const $other = $('#second').on('dxclick', noop); | ||
| let foreignHandlerCallCount = 0; | ||
|
|
||
| nativePointerMock($clicked).start().click(); | ||
|
|
||
| $clicked.on(removeEvent, function() { | ||
| foreignHandlerCallCount++; | ||
| }); | ||
|
|
||
| nativePointerMock($other).start().click(); | ||
|
|
||
| $clicked.triggerHandler({ type: removeEvent }); | ||
|
|
||
| assert.equal(foreignHandlerCallCount, 1, `foreign ${removeEvent} handler is still subscribed`); | ||
| }); | ||
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 path is gone on main. The
m_prefix was dropped after this branch was cut, so it is@ts/events/core/events_enginenow. As it stands the suite cannot even load after a rebase.The rebase also conflicts in
click.ts. Worth resolving by hand: git merges most of the file on its own but leavesunsubscribeNodesDisposing(lastFiredEvent, callback, nodes)outside the conflict markers, so just taking your side of the marked block leaves a reference to acallbackthat no longer exists.One thing you get for free from the rebase: main's
click.tsalready importsNodesDisposingSubscriptionfrom the util, so the localinterfacethis PR adds at the top ofclick.tsdisappears.