Skip to content

Commit 5f75a07

Browse files
authored
Merge pull request #27 from tusharmath/from-dom-event
feat(fromDOM): add fromDOM source
2 parents 967e6a3 + 555165d commit 5f75a07

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export {reduce} from './operators/Reduce'
1717
// Sources
1818
export {fromArray} from './sources/FromArray'
1919
export {interval} from './sources/Interval'
20+
export {fromDOM} from './sources/FromDOM'

src/sources/FromDOM.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Created by tushar.mathur on 14/10/16.
3+
*/
4+
5+
6+
import {IObservable} from '../types/core/IObservable'
7+
import {IObserver} from '../types/core/IObserver'
8+
import {IScheduler} from '../types/IScheduler'
9+
import {ISubscription} from '../types/core/ISubscription'
10+
import {IListener} from '../types/IListener'
11+
12+
13+
export class DOMSubscription implements ISubscription {
14+
closed: boolean = false
15+
16+
constructor (private element: HTMLElement, private listener: IListener, private name: string) {
17+
}
18+
19+
unsubscribe (): void {
20+
this.element.removeEventListener(this.name, this.listener)
21+
}
22+
}
23+
24+
export class DOMObservable implements IObservable<Event> {
25+
constructor (private element: HTMLElement, private name: string) {
26+
}
27+
28+
subscribe (observer: IObserver<Event>, scheduler: IScheduler): ISubscription {
29+
const listener = (e: Event) => observer.next(e)
30+
this.element.addEventListener(this.name, listener)
31+
return new DOMSubscription(this.element, listener, this.name)
32+
}
33+
34+
}
35+
36+
export function fromDOM (element: HTMLElement, name: string) {
37+
return new DOMObservable(element, name)
38+
}

src/types/IListener.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Created by tushar.mathur on 14/10/16.
3+
*/
4+
5+
export interface IListener {
6+
(e: Event): void
7+
}

0 commit comments

Comments
 (0)