-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #1063 ## Changes: - Implementation of handler cleanup
- Loading branch information
Showing
5 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export interface NativeEventable { | ||
addEventListener(name: string, handler: (...any: any[]) => any): any; | ||
removeEventListener(name: string, handler: (...any: any[]) => any): any; | ||
} | ||
|
||
export class BrowserComponent<T extends NativeEventable> { | ||
private _paused = false; | ||
private _nativeHandlers: { [key: string]: (handler: any) => void } = {}; | ||
|
||
on(eventName: string, handler: (evt: any) => void): void { | ||
if (this._nativeHandlers[eventName]) { | ||
this.off(eventName, this._nativeHandlers[eventName]); | ||
} | ||
this._nativeHandlers[eventName] = this._decorate(handler); | ||
this.nativeComponet.addEventListener(eventName, this._nativeHandlers[eventName]); | ||
} | ||
off(eventName: string, handler?: (event: any) => void): void { | ||
if (!handler) { | ||
handler = this._nativeHandlers[eventName]; | ||
} | ||
this.nativeComponet.removeEventListener(eventName, handler); | ||
this._nativeHandlers[eventName] = null; | ||
} | ||
|
||
private _decorate(handler: (evt: any) => void): (evt: any) => void { | ||
return (evt: any) => { | ||
if (!this._paused) { | ||
handler(evt); | ||
} | ||
}; | ||
} | ||
|
||
public pause() { | ||
this._paused = true; | ||
} | ||
|
||
public resume() { | ||
this._paused = false; | ||
} | ||
|
||
public clear() { | ||
for (const event in this._nativeHandlers) { | ||
this.off(event); | ||
} | ||
} | ||
|
||
constructor(public nativeComponet: T) {} | ||
} | ||
|
||
export class BrowserEvents { | ||
private _windowComponent: BrowserComponent<Window>; | ||
private _documentComponent: BrowserComponent<Document>; | ||
constructor(private _windowGlobal: Window, private _documentGlobal: Document) { | ||
this._windowComponent = new BrowserComponent(this._windowGlobal); | ||
this._documentComponent = new BrowserComponent(this._documentGlobal); | ||
} | ||
|
||
public get window(): BrowserComponent<Window> { | ||
return this._windowComponent; | ||
} | ||
|
||
public get document(): BrowserComponent<Document> { | ||
return this._documentComponent; | ||
} | ||
|
||
public pause() { | ||
this.window.pause(); | ||
this.document.pause(); | ||
} | ||
|
||
public resume() { | ||
this.window.resume(); | ||
this.document.resume(); | ||
} | ||
|
||
public clear() { | ||
this.window.clear(); | ||
this.document.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as ex from '../../build/dist/excalibur'; | ||
|
||
describe('The BrowserEvents facade', () => { | ||
let browser: ex.BrowserEvents; | ||
beforeEach(() => { | ||
browser = new ex.BrowserEvents(window, document); | ||
}); | ||
|
||
afterEach(() => { | ||
browser.clear(); | ||
}); | ||
|
||
it('should exist', () => { | ||
expect(ex.BrowserEvents).toBeDefined(); | ||
}); | ||
|
||
it('can be created', () => { | ||
expect(browser).toBeDefined(); | ||
}); | ||
|
||
it('can register handlers on window', (done) => { | ||
browser.window.on('someevent', () => { | ||
done(); | ||
}); | ||
|
||
window.dispatchEvent(new Event('someevent')); | ||
}); | ||
|
||
it('can pause handlers on window', () => { | ||
browser.window.on('somewindowevent', () => { | ||
fail(); | ||
}); | ||
|
||
browser.pause(); | ||
window.dispatchEvent(new Event('somewindowevent')); | ||
}); | ||
|
||
it('can register handlers on document', (done) => { | ||
browser.document.on('someevent', () => { | ||
done(); | ||
}); | ||
|
||
document.dispatchEvent(new Event('someevent')); | ||
}); | ||
|
||
it('can pause handlers on document', () => { | ||
browser.document.on('somedocumentevent', () => { | ||
fail(); | ||
}); | ||
|
||
browser.pause(); | ||
window.dispatchEvent(new Event('somedocumentevent')); | ||
}); | ||
|
||
it('can clear handlers on window', () => { | ||
browser.window.on('somewindowevent2', () => { | ||
fail(); | ||
}); | ||
|
||
browser.clear(); | ||
window.dispatchEvent(new Event('somewindowevent2')); | ||
}); | ||
|
||
it('can clear handlers on window', () => { | ||
browser.document.on('somedocevent2', () => { | ||
fail(); | ||
}); | ||
|
||
browser.clear(); | ||
document.dispatchEvent(new Event('somedocevent2')); | ||
}); | ||
|
||
it('can only have 1 handler per event name at a time', (done) => { | ||
browser.window.on('couldfailevent', () => { | ||
fail(); | ||
}); | ||
|
||
browser.window.on('couldfailevent', () => { | ||
done(); | ||
}); | ||
|
||
window.dispatchEvent(new Event('couldfailevent')); | ||
}); | ||
|
||
it('can resume handlers', () => { | ||
const spy = jasmine.createSpy(); | ||
browser.document.on('somedocumentevent', spy); | ||
|
||
browser.pause(); | ||
document.dispatchEvent(new Event('somedocumentevent')); | ||
|
||
browser.resume(); | ||
document.dispatchEvent(new Event('somedocumentevent')); | ||
|
||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |