The Event class represents an event.
// Event is the signal type behind Button.click, Timer.tick
// and other GUI classes. Event can be also be directly used
// in your own code
class Playlist {
tracks = null;
trackAdded = null;
constructor() {
this.tracks = [];
this.trackAdded = new Event();
}
add(title) {
this.tracks.push(title);
// fire(sender, event_args) -- the two arguments every handler gets
this.trackAdded.fire(this, { title: title });
}
}
let log = "";
// connect(func) takes a bare function; connect(object, func) binds the
// handler to an object so it can reach that object through 'this'
const onAdded = function(sender, event_args) {
log += `added "${event_args.title}" (${sender.tracks.length} total)\n`;
};
const playlist = new Playlist();
playlist.trackAdded.connect(onAdded);
playlist.add("Bach - Goldberg Variations");
playlist.add("Mahler - Das Lied von der Erde");
log += `sinks: ${playlist.trackAdded.getSinkCount()}\n`;
// disconnect(func) drops one handler, disconnectAll() drops every one
playlist.trackAdded.disconnect(onAdded);
playlist.add("Blue Train"); // nothing is logged for this one
alert(log); Connects an event to a function specified by func or by object and func. For example, for a size event, this.size.connect(onFormResized) will connect the size event to the onFormResized function, such that onFormResized will be invoked when the size event occurs. Similarly, this.size.connect(this, onFormResized) will connect the size event to the onFormResized function using the this pointer specified in the object parameter
True if the event handler was successfully found and removed, false otherwise
Calling this method disconnects the specified event handler from the event object. When the event is subsequently fired, the specified handler will no longer be invoked. If the handler was found and removed by the disconnect() method, true is returned. If the handler was not found, false is returned.
Disconnects all event handlers from the event. When the event is subsequently fired, any previous event handlers will no longer be triggered
Fires an event. The parameters passed to this method will be passed on to the event sink(s).
An integer value indicating the number of event handlers
This method returns the number of event handlers (sinks) attached to the event. This normally corresponds to the number of times the connect() method was called
True if the event is handled, false otherwise
This method allows the caller to determine if the event is handled. If the event is handled by one or more handlers, the function will return true.