|
1 |
| -import { stringify } from "../../../utils/json"; |
| 1 | +import { stringify } from '../../../utils/json'; |
| 2 | +import { Path } from '../util/Path'; |
| 3 | +import { EventRegistration } from './EventRegistration'; |
| 4 | +import { DataSnapshot } from '../../api/DataSnapshot'; |
2 | 5 |
|
3 | 6 | /**
|
4 | 7 | * Encapsulates the data needed to raise an event
|
5 | 8 | * @interface
|
6 | 9 | */
|
7 |
| -export const Event = function() {}; |
| 10 | +export interface Event { |
| 11 | + /** |
| 12 | + * @return {!fb.core.util.Path} |
| 13 | + */ |
| 14 | + getPath(): Path; |
8 | 15 |
|
| 16 | + /** |
| 17 | + * @return {!string} |
| 18 | + */ |
| 19 | + getEventType(): string; |
9 | 20 |
|
10 |
| -/** |
11 |
| - * @return {!fb.core.util.Path} |
12 |
| - */ |
13 |
| -Event.prototype.getPath = () => {}; |
14 |
| - |
15 |
| - |
16 |
| -/** |
17 |
| - * @return {!string} |
18 |
| - */ |
19 |
| -Event.prototype.getEventType = () => {}; |
20 |
| - |
21 |
| - |
22 |
| -/** |
23 |
| - * @return {!function()} |
24 |
| - */ |
25 |
| -Event.prototype.getEventRunner = () => {}; |
26 |
| - |
27 |
| - |
28 |
| -/** |
29 |
| - * @return {!string} |
30 |
| - */ |
31 |
| -Event.prototype.toString = () => {}; |
| 21 | + /** |
| 22 | + * @return {!function()} |
| 23 | + */ |
| 24 | + getEventRunner(): () => void; |
32 | 25 |
|
| 26 | + /** |
| 27 | + * @return {!string} |
| 28 | + */ |
| 29 | + toString(): string; |
| 30 | +} |
33 | 31 |
|
34 | 32 |
|
35 | 33 | /**
|
36 | 34 | * Encapsulates the data needed to raise an event
|
37 |
| - * @param {!string} eventType One of: value, child_added, child_changed, child_moved, child_removed |
38 |
| - * @param {!EventRegistration} eventRegistration The function to call to with the event data. User provided |
39 |
| - * @param {!fb.api.DataSnapshot} snapshot The data backing the event |
40 |
| - * @param {?string=} opt_prevName Optional, the name of the previous child for child_* events. |
41 |
| - * @constructor |
42 | 35 | * @implements {Event}
|
43 | 36 | */
|
44 |
| -export const DataEvent = function(eventType, eventRegistration, snapshot, opt_prevName?) { |
45 |
| - this.eventRegistration = eventRegistration; |
46 |
| - this.snapshot = snapshot; |
47 |
| - this.prevName = opt_prevName; |
48 |
| - this.eventType = eventType; |
49 |
| -}; |
50 |
| - |
51 |
| - |
52 |
| -/** |
53 |
| - * @inheritDoc |
54 |
| - */ |
55 |
| -DataEvent.prototype.getPath = function() { |
56 |
| - var ref = this.snapshot.getRef(); |
57 |
| - if (this.eventType === 'value') { |
58 |
| - return ref.path; |
59 |
| - } else { |
60 |
| - return ref.getParent().path; |
| 37 | +export class DataEvent implements Event { |
| 38 | + /** |
| 39 | + * @param {!string} eventType One of: value, child_added, child_changed, child_moved, child_removed |
| 40 | + * @param {!EventRegistration} eventRegistration The function to call to with the event data. User provided |
| 41 | + * @param {!fb.api.DataSnapshot} snapshot The data backing the event |
| 42 | + * @param {?string=} prevName Optional, the name of the previous child for child_* events. |
| 43 | + */ |
| 44 | + constructor(public eventType: 'value' | ' child_added' | ' child_changed' | ' child_moved' | ' child_removed', |
| 45 | + public eventRegistration: EventRegistration, |
| 46 | + public snapshot: DataSnapshot, |
| 47 | + public prevName?: string | null) { |
61 | 48 | }
|
62 |
| -}; |
63 |
| - |
64 |
| - |
65 |
| -/** |
66 |
| - * @inheritDoc |
67 |
| - */ |
68 |
| -DataEvent.prototype.getEventType = function() { |
69 |
| - return this.eventType; |
70 |
| -}; |
71 | 49 |
|
| 50 | + /** |
| 51 | + * @inheritDoc |
| 52 | + */ |
| 53 | + getPath(): Path { |
| 54 | + const ref = this.snapshot.getRef(); |
| 55 | + if (this.eventType === 'value') { |
| 56 | + return ref.path; |
| 57 | + } else { |
| 58 | + return ref.getParent().path; |
| 59 | + } |
| 60 | + } |
72 | 61 |
|
73 |
| -/** |
74 |
| - * @inheritDoc |
75 |
| - */ |
76 |
| -DataEvent.prototype.getEventRunner = function() { |
77 |
| - return this.eventRegistration.getEventRunner(this); |
78 |
| -}; |
| 62 | + /** |
| 63 | + * @inheritDoc |
| 64 | + */ |
| 65 | + getEventType(): string { |
| 66 | + return this.eventType; |
| 67 | + } |
79 | 68 |
|
| 69 | + /** |
| 70 | + * @inheritDoc |
| 71 | + */ |
| 72 | + getEventRunner(): () => void { |
| 73 | + return this.eventRegistration.getEventRunner(this); |
| 74 | + } |
80 | 75 |
|
81 |
| -/** |
82 |
| - * @inheritDoc |
83 |
| - */ |
84 |
| -DataEvent.prototype.toString = function() { |
85 |
| - return this.getPath().toString() + ':' + this.eventType + ':' + |
| 76 | + /** |
| 77 | + * @inheritDoc |
| 78 | + */ |
| 79 | + toString(): string { |
| 80 | + return this.getPath().toString() + ':' + this.eventType + ':' + |
86 | 81 | stringify(this.snapshot.exportVal());
|
87 |
| -}; |
88 |
| - |
89 |
| - |
90 |
| - |
91 |
| -/** |
92 |
| - * @param {EventRegistration} eventRegistration |
93 |
| - * @param {Error} error |
94 |
| - * @param {!fb.core.util.Path} path |
95 |
| - * @constructor |
96 |
| - * @implements {Event} |
97 |
| - */ |
98 |
| -export const CancelEvent = function(eventRegistration, error, path) { |
99 |
| - this.eventRegistration = eventRegistration; |
100 |
| - this.error = error; |
101 |
| - this.path = path; |
102 |
| -}; |
103 |
| - |
104 |
| - |
105 |
| -/** |
106 |
| - * @inheritDoc |
107 |
| - */ |
108 |
| -CancelEvent.prototype.getPath = function() { |
109 |
| - return this.path; |
110 |
| -}; |
111 |
| - |
112 |
| - |
113 |
| -/** |
114 |
| - * @inheritDoc |
115 |
| - */ |
116 |
| -CancelEvent.prototype.getEventType = function() { |
117 |
| - return 'cancel'; |
118 |
| -}; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +export class CancelEvent implements Event { |
| 87 | + /** |
| 88 | + * @param {EventRegistration} eventRegistration |
| 89 | + * @param {Error} error |
| 90 | + * @param {!fb.core.util.Path} path |
| 91 | + */ |
| 92 | + constructor(public eventRegistration: EventRegistration, |
| 93 | + public error: Error, |
| 94 | + public path: Path) { |
| 95 | + } |
119 | 96 |
|
| 97 | + /** |
| 98 | + * @inheritDoc |
| 99 | + */ |
| 100 | + getPath() { |
| 101 | + return this.path; |
| 102 | + } |
120 | 103 |
|
121 |
| -/** |
122 |
| - * @inheritDoc |
123 |
| - */ |
124 |
| -CancelEvent.prototype.getEventRunner = function() { |
125 |
| - return this.eventRegistration.getEventRunner(this); |
126 |
| -}; |
| 104 | + /** |
| 105 | + * @inheritDoc |
| 106 | + */ |
| 107 | + getEventType() { |
| 108 | + return 'cancel'; |
| 109 | + } |
127 | 110 |
|
| 111 | + /** |
| 112 | + * @inheritDoc |
| 113 | + */ |
| 114 | + getEventRunner() { |
| 115 | + return this.eventRegistration.getEventRunner(this); |
| 116 | + } |
128 | 117 |
|
129 |
| -/** |
130 |
| - * @inheritDoc |
131 |
| - */ |
132 |
| -CancelEvent.prototype.toString = function() { |
133 |
| - return this.path.toString() + ':cancel'; |
134 |
| -}; |
| 118 | + /** |
| 119 | + * @inheritDoc |
| 120 | + */ |
| 121 | + toString() { |
| 122 | + return this.path.toString() + ':cancel'; |
| 123 | + } |
| 124 | +} |
0 commit comments