Skip to content
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(ElementEvents): batch subscription #586

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 51 additions & 35 deletions dist/aurelia-templating.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ import {
import {
TaskQueue
} from 'aurelia-task-queue';
export declare interface EventHandler {
eventName: string;
bubbles: boolean;
dispose: Function;
handler: Function;
}

/**
* Specifies how a view should be created.
Expand Down Expand Up @@ -472,10 +466,29 @@ export declare class ViewEngineHooksResource {
}
export declare function viewEngineHooks(target?: any): any;

/**
* Dispatches subscribets to and publishes events in the DOM.
* @param element
*/

export declare interface EventHandler {
eventName: string;
bubbles: boolean;
dispose: Function;
handler: Function;
}

export declare interface SubscriptionHandlerConfig {
handler: Function
capture?: boolean
passive?: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this passive prop for? I see it nowhere handled in code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used when we pass the whole object to EventTarget.prototype.addEventListener third paramter. Same with passive in spec.

once?: boolean
}

export declare interface BatchSubscriptionConfig {
[eventName: string]: Function | SubscriptionHandlerConfig
}

export declare interface EventSubscriptions {
[eventName: string]: EventHandler
}

/**
* Dispatches subscribets to and publishes events in the DOM.
* @param element
Expand All @@ -484,41 +497,44 @@ export declare class ElementEvents {
constructor(element: EventTarget);

/**
* Dispatches an Event on the context element.
* @param eventName
* @param detail
* @param bubbles
* @param cancelable
*/
* Dispatches an Event on the context element.
* @param eventName
* @param detail
* @param bubbles
* @param cancelable
*/
publish(eventName: string, detail?: Object, bubbles?: boolean, cancelable?: boolean): any;

/**
* Adds and Event Listener on the context element.
* @param eventName
* @param handler
* @param bubbles
* @return Returns the eventHandler containing a dispose method
*/
subscribe(eventName: string, handler: Function, bubbles?: boolean): EventHandler;
* Adds and Event Listener on the context element.
* @return Returns the eventHandler containing a dispose method
*/
subscribe(events: TEvents): EventSubscriptions;
subscribe(eventName: string, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions): EventHandler;
subscribe(configOrEventName: string | BatchSubscriptionConfig, handler?: Function, bubbles?: Boolean): EventHandler | EventSubscriptions;

/**
* Adds an Event Listener on the context element, that will be disposed on the first trigger.
* @param eventName
* @param handler
* @param bubbles
* @return Returns the eventHandler containing a dispose method
*/
subscribeOnce(eventName: String, handler: Function, bubbles?: Boolean): EventHandler;
* Adds an Event Listener on the context element, that will be disposed on the first trigger.
* @return Returns the eventHandler containing a dispose method
*/
subscribeOnce(events: BatchSubscriptionConfig): EventSubscriptions;
subscribeOnce(eventName: string, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions): EventHandler;
subscribeOnce(configOrEventName: string | BatchSubscriptionConfig, handler?: Function, bubbles?: Boolean): EventHandler | EventSubscriptions;

/**
* Removes all events that are listening to the specified eventName.
* @param eventName
*/
* Add multiple event listeners at once, with option to specify once over the whole set
*/
batchSubscribe(events: BatchSubscriptionConfig, once?: boolean): EventSubscriptions

/**
* Removes all events that are listening to the specified eventName.
* @param eventName
*/
dispose(eventName: string): void;

/**
* Removes all event handlers.
*/
* Removes all event handlers.
*/
disposeAll(): any;
}

Expand Down
66 changes: 55 additions & 11 deletions src/element-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ interface EventHandler {
handler: Function;
}

// Don't extends AddEventListenerOptions, make explicit for readability
interface SubscriptionHandlerConfig {
handler: Function;
capture?: boolean;
passive?: boolean;
once?: boolean;
}

interface BatchSubscriptionConfig {
[eventName: string]: Function | SubscriptionHandlerConfig
}

interface EventSubscriptions {
[eventName: string]: EventHandler
}

/**
* Dispatches subscribets to and publishes events in the DOM.
* @param element
Expand Down Expand Up @@ -51,26 +67,54 @@ export class ElementEvents {
* Adds and Event Listener on the context element.
* @return Returns the eventHandler containing a dispose method
*/
subscribe(eventName: string, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions = true): EventHandler {
if (typeof handler === 'function') {
const eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, false);
return eventHandler;
}
subscribe(configOrEventName: string | BatchSubscriptionConfig, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions = true): EventSubscriptions | EventHandler {
if (typeof configOrEventName === 'string') {
if (typeof handler === 'function') {
const eventHandler = new EventHandlerImpl(this, configOrEventName, handler, captureOrOptions, false);
return eventHandler;
}

return undefined;
return undefined;
} else {
return this.batchSubscribe(configOrEventName, false);
}
}

/**
* Adds an Event Listener on the context element, that will be disposed on the first trigger.
* @return Returns the eventHandler containing a dispose method
*/
subscribeOnce(eventName: String, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions = true): EventHandler {
if (typeof handler === 'function') {
const eventHandler = new EventHandlerImpl(this, eventName, handler, captureOrOptions, true);
return eventHandler;
subscribeOnce(configOrEventName: string | BatchSubscriptionConfig, handler: Function, captureOrOptions?: boolean | AddEventListenerOptions = true): EventSubscriptions | EventHandler {
if (typeof configOrEventName === 'string') {
if (typeof handler === 'function') {
const eventHandler = new EventHandlerImpl(this, configOrEventName, handler, captureOrOptions, true);
return eventHandler;
}

return undefined;
} else {
return this.batchSubscribe(configOrEventName, true);
}
}

return undefined;
batchSubscribe(config: BatchSubscriptionConfig, once: boolean = false): EventSubscriptions {
const subscriptions: EventSubscriptions = {};
for (let eventName in config) {
let handlerOrOptions = config[eventName];
let handler: Function;
let listenerOptions: boolean | AddEventListenerOptions;
let _once = once;
if (typeof handlerOrOptions === 'function') {
handler = handlerOrOptions;
listenerOptions = false;
} else {
handler = handlerOrOptions.handler;
listenerOptions = handlerOrOptions;
_once = handlerOrOptions.once === undefined ? _once : !!handlerOrOptions.once;
}
subscriptions[eventName] = new EventHandlerImpl(this, eventName, handler, listenerOptions, _once);
}
return subscriptions;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions test/element-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,53 @@ describe('ElementEvents', () => {
expect(callCount).toBe(1);
});

it('should batch subscribe', () => {
let value;
let callCount = 0;

const subscriptions = elementEvents.subscribe({
input: () => {
callCount++;
value = input.value;
},
blur: () => {
callCount++;
},
focus: {
handler: () => {
callCount++;
},
once: true
}
});

const newValue = 1234;
input.value = newValue;
input.dispatchEvent(new CustomEvent('input'));

expect(value === newValue.toString()).toBe(true);
expect(callCount).toBe(1);

subscriptions.input.dispose();

input.dispatchEvent(new CustomEvent('input'));
expect(callCount).toBe(1);

input.dispatchEvent(new CustomEvent('blur'));
expect(callCount).toBe(2);

input.dispatchEvent(new CustomEvent('focus'));
expect(callCount).toBe(3);

input.dispatchEvent(new CustomEvent('focus'));
expect(callCount).toBe(3);

elementEvents.disposeAll();

input.dispatchEvent(new CustomEvent('blur'));
expect(callCount).toBe(3);
})

it('should subscribe once', () => {
let value;
let callCount = 0;
Expand Down