1 | import xs, {Stream, MemoryStream} from 'xstream';
|
2 | import {adapt} from '@cycle/run/lib/adapt';
|
3 | import {DevToolEnabledSource} from '@cycle/run';
|
4 | import {EventsFnOptions, DOMSource} from './DOMSource';
|
5 | import {fromEvent} from './fromEvent';
|
6 |
|
7 | export class DocumentDOMSource {
|
8 | constructor(private _name: string) {}
|
9 |
|
10 | public select(selector: string): DocumentDOMSource {
|
11 |
|
12 | return this;
|
13 | }
|
14 |
|
15 | public elements(): MemoryStream<Array<Document>> {
|
16 | const out: DevToolEnabledSource & MemoryStream<Array<Document>> = adapt(
|
17 | xs.of([document])
|
18 | );
|
19 | out._isCycleSource = this._name;
|
20 | return out;
|
21 | }
|
22 |
|
23 | public element(): MemoryStream<Document> {
|
24 | const out: DevToolEnabledSource & MemoryStream<Document> = adapt(
|
25 | xs.of(document)
|
26 | );
|
27 | out._isCycleSource = this._name;
|
28 | return out;
|
29 | }
|
30 |
|
31 | public events<K extends keyof DocumentEventMap>(
|
32 | eventType: K,
|
33 | options?: EventsFnOptions,
|
34 | bubbles?: boolean
|
35 | ): Stream<DocumentEventMap[K]>;
|
36 | public events(
|
37 | eventType: string,
|
38 | options: EventsFnOptions = {},
|
39 | bubbles?: boolean
|
40 | ): Stream<Event> {
|
41 | let stream: Stream<Event>;
|
42 |
|
43 | stream = fromEvent(
|
44 | document,
|
45 | eventType,
|
46 | options.useCapture,
|
47 | options.preventDefault
|
48 | );
|
49 |
|
50 | const out: DevToolEnabledSource & Stream<Event> = adapt(stream);
|
51 | out._isCycleSource = this._name;
|
52 | return out;
|
53 | }
|
54 | }
|