View Issue Details

IDProjectCategoryView StatusLast Update
0038845pas2jsrtlpublic2021-05-03 18:58
ReporterYuri Serebrennikov Assigned ToMattias Gaertner  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Summary0038845: Removing an event handler does not work
DescriptionIf I describe deleting an event handler in a class, then it doesn't work.
If I describe in a procedural paradigm, then it works.

I have a code for demo.
When click left mouse in browser, i see two message
C Button = 0
P Button = 0

When i click right button event handlers should be removed, but removed only one handler.
Steps To Reproduceprogram project1;

{$mode objfpc}

uses
  JS, Classes, SysUtils, Web;

type

  { MyClass }

  { TMyClass }

  TMyClass = class
  private
    function MouseDownC(aEvent: TJSMouseEvent): boolean;
  public
    procedure Start;
  end;

function MouseDownP(aEvent: TJSMouseEvent): boolean;
begin
  writeln('P Button = ', aEvent.button);
  if aEvent.button = 2 then
    document.removeEventListener('mousedown', @MouseDownP);
end;

{ MyClass }

function TMyClass.MouseDownC(aEvent: TJSMouseEvent): boolean;
begin
  writeln('C Button = ', aEvent.button);
  if aEvent.button = 2 then
    document.removeEventListener('mousedown', @MouseDownC);
end;

procedure TMyClass.Start;
begin
  document.addEventListener('mousedown', @MouseDownC);
end;

var
  MyClass: TMyClass;

begin
  // clacc implementation
  MyClass := TMyClass.Create;
  MyClass.Start;

  // procedure implementation
  document.addEventListener('mousedown', @MouseDownP);
end.
TagsNo tags attached.
Fixed in Revision49331.
Attached Files

Activities

Yuri Serebrennikov

2021-05-02 22:13

reporter   ~0130732

in JavaScript remove looks like
      if (aEvent.button === 2) document.removeEventListener("mousedown",rtl.createCallback(this,"MouseDownC"));

Mattias Gaertner

2021-05-03 07:21

manager   ~0130733

The "@MouseDown" creates a temporary object.
You can do this:

var MyMouseDownHandler: TJSEventHandler;
MyMouseDownHandler:=@MouseDownP;
document.addEventListener('mousedown', MyMouseDownHandler);

document.removeEventListener('mousedown', MyMouseDownHandler);

Yuri Serebrennikov

2021-05-03 08:35

reporter   ~0130735

Thank you. I will do this.

Mattias Gaertner

2021-05-03 18:58

manager   ~0130743

Trunk now creates unique method pointers.

Issue History

Date Modified Username Field Change
2021-05-02 22:04 Yuri Serebrennikov New Issue
2021-05-02 22:13 Yuri Serebrennikov Note Added: 0130732
2021-05-03 07:21 Mattias Gaertner Note Added: 0130733
2021-05-03 08:35 Yuri Serebrennikov Note Added: 0130735
2021-05-03 18:58 Mattias Gaertner Assigned To => Mattias Gaertner
2021-05-03 18:58 Mattias Gaertner Status new => resolved
2021-05-03 18:58 Mattias Gaertner Resolution open => fixed
2021-05-03 18:58 Mattias Gaertner Fixed in Revision => 49331.
2021-05-03 18:58 Mattias Gaertner Note Added: 0130743