1 | import {inherit} from './utils/objects'
|
2 | import {VALUE, ERROR, END} from './constants'
|
3 | import {callSubscriber} from './dispatcher'
|
4 | import Observable from './observable'
|
5 |
|
6 | function Property() {
|
7 | Observable.call(this)
|
8 | this._currentEvent = null
|
9 | }
|
10 |
|
11 | inherit(Property, Observable, {
|
12 | _name: 'property',
|
13 |
|
14 | _emitValue(value) {
|
15 | if (this._alive) {
|
16 | this._currentEvent = {type: VALUE, value}
|
17 | if (!this._activating) {
|
18 | this._dispatcher.dispatch({type: VALUE, value})
|
19 | }
|
20 | }
|
21 | },
|
22 |
|
23 | _emitError(value) {
|
24 | if (this._alive) {
|
25 | this._currentEvent = {type: ERROR, value}
|
26 | if (!this._activating) {
|
27 | this._dispatcher.dispatch({type: ERROR, value})
|
28 | }
|
29 | }
|
30 | },
|
31 |
|
32 | _emitEnd() {
|
33 | if (this._alive) {
|
34 | this._alive = false
|
35 | if (!this._activating) {
|
36 | this._dispatcher.dispatch({type: END})
|
37 | }
|
38 | this._clear()
|
39 | }
|
40 | },
|
41 |
|
42 | _on(type, fn) {
|
43 | if (this._alive) {
|
44 | this._dispatcher.add(type, fn)
|
45 | this._setActive(true)
|
46 | }
|
47 | if (this._currentEvent !== null) {
|
48 | callSubscriber(type, fn, this._currentEvent)
|
49 | }
|
50 | if (!this._alive) {
|
51 | callSubscriber(type, fn, {type: END})
|
52 | }
|
53 | return this
|
54 | },
|
55 |
|
56 | getType() {
|
57 | return 'property'
|
58 | },
|
59 | })
|
60 |
|
61 | export default Property
|