fjgandrade / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

Event code generation ignores custom accessors #338

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Compile the following code:

    [JsType(JsMode.Clr, Filename = "res/Default.js")]
    public class TestClasses
    {
        public event Action CustomEvent
        {
            add {}
            remove {}
        }

        public TestClasses()
        {
            CustomEvent += () => HtmlContext.console.log("foo");
        }
    }

Examine the compiled output:

        add_CustomEvent: function (value)
        {
            this.CustomEvent = $CombineDelegates(this.CustomEvent, value);
        },
        remove_CustomEvent: function (value)
        {
            this.CustomEvent = $RemoveDelegate(this.CustomEvent, value);
        }

It should have been:

        add_CustomEvent: function (value)
        {
        },
        remove_CustomEvent: function (value)
        {
        }

There are various situations where custom event accessors could be helpful, 
most importantly when wrapping JS events and determining whether a listener 
needs to be added to a DOM object.

Original issue reported on code.google.com by kirk.w...@gmail.com on 9 Dec 2013 at 10:40

GoogleCodeExporter commented 9 years ago
It looks like a bug, I think that if there was code inside the add/remove 
accessors, it would have worked, but maybe because it's empty, SharpKit 
confuses it as if it's an automatic event.

Original comment by DanelK...@gmail.com on 12 Dec 2013 at 12:40

GoogleCodeExporter commented 9 years ago
Oh, sorry I wasn't clear, the code I posted above was intended to illustrate a 
SSCCE.  My real code was trying to wrap a javascript DOM event with my own 
event, but I only wanted to attach the event handler to the DOM element if I 
actually add an event handler to my custom class.  Here's another 
less-contrived example that will illustrate the same problem:

        private Delegate customEvent;
        private bool customEventsAdded;

        public event Action CustomEvent
        {
            add
            {
                customEvent = Delegate.Combine(customEvent, value);
                customEventsAdded = true;
            }
            remove
            {
                customEvent = Delegate.Remove(customEvent, value);                
            }
        }

And the compiled result:

        add_CustomEvent: function (value)
        {
            this.CustomEvent = $CombineDelegates(this.CustomEvent, value);
        },
        remove_CustomEvent: function (value)
        {
            this.CustomEvent = $RemoveDelegate(this.CustomEvent, value);
        }

You'll notice that it's the same generated output, notably omitting the 
assignment to my field.

Original comment by kirk.w...@sqor.com on 14 Dec 2013 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by DanelK...@gmail.com on 11 Jan 2014 at 7:49