Skip to content

Commit

Permalink
feat(fromDOM): add fromDOM source
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Oct 14, 2016
1 parent 3b4d5ac commit 555165d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export {tap} from './operators/Tap'
// Sources
export {fromArray} from './sources/FromArray'
export {interval} from './sources/Interval'
export {fromDOM} from './sources/FromDOM'
38 changes: 38 additions & 0 deletions src/sources/FromDOM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Created by tushar.mathur on 14/10/16.
*/


import {IObservable} from '../types/core/IObservable'
import {IObserver} from '../types/core/IObserver'
import {IScheduler} from '../types/IScheduler'
import {ISubscription} from '../types/core/ISubscription'
import {IListener} from '../types/IListener'


export class DOMSubscription implements ISubscription {
closed: boolean = false

constructor (private element: HTMLElement, private listener: IListener, private name: string) {
}

unsubscribe (): void {
this.element.removeEventListener(this.name, this.listener)
}
}

export class DOMObservable implements IObservable<Event> {
constructor (private element: HTMLElement, private name: string) {
}

subscribe (observer: IObserver<Event>, scheduler: IScheduler): ISubscription {
const listener = (e: Event) => observer.next(e)
this.element.addEventListener(this.name, listener)
return new DOMSubscription(this.element, listener, this.name)
}

}

export function fromDOM (element: HTMLElement, name: string) {
return new DOMObservable(element, name)
}
7 changes: 7 additions & 0 deletions src/types/IListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Created by tushar.mathur on 14/10/16.
*/

export interface IListener {
(e: Event): void
}

0 comments on commit 555165d

Please sign in to comment.