|
13 | 13 | }
|
14 | 14 | return true
|
15 | 15 | }
|
16 |
| - function isObservable(ob) { |
17 |
| - return ob && typeof ob.subscribe === 'function'; |
18 |
| - } |
19 |
| - function isSubject(subject) { |
20 |
| - return subject && (typeof subject.next === 'function' || typeof subject.onNext === 'function'); |
| 16 | + |
| 17 | + function isObservable (ob) { |
| 18 | + return ob && typeof ob.subscribe === 'function' |
| 19 | + } |
| 20 | + |
| 21 | + function isSubject (subject) { |
| 22 | + return subject && ( |
| 23 | + typeof subject.next === 'function' || |
| 24 | + typeof subject.onNext === 'function' |
| 25 | + ) |
21 | 26 | }
|
22 |
| - function unsub(handle) { |
23 |
| - if(!handle){return} |
| 27 | + |
| 28 | + function unsub (handle) { |
| 29 | + if (!handle) { return } |
24 | 30 | if (handle.dispose) {
|
25 | 31 | handle.dispose()
|
26 | 32 | } else if (handle.unsubscribe) {
|
27 | 33 | handle.unsubscribe()
|
28 | 34 | }
|
29 | 35 | }
|
30 |
| - function getDisposable(target) { |
| 36 | + |
| 37 | + function getDisposable (target) { |
31 | 38 | if (Rx.Subscription) { // Rx5
|
32 | 39 | return new Rx.Subscription(target)
|
33 | 40 | } else { // Rx4
|
|
102 | 109 | }
|
103 | 110 |
|
104 | 111 | // Returns function which disconnects the $watch expression
|
105 |
| - var disposable = getDisposable(unwatch); |
| 112 | + var disposable = getDisposable(unwatch) |
106 | 113 |
|
107 | 114 | return disposable
|
108 | 115 | })
|
|
123 | 130 | var doc = document.documentElement
|
124 | 131 | var obs$ = Rx.Observable.create(function (observer) {
|
125 | 132 | function listener (e) {
|
126 |
| - if (!vm.$el) return; |
| 133 | + if (!vm.$el) return |
127 | 134 | if (selector === null && vm.$el === e.target) return observer.next(e)
|
128 |
| - var els = vm.$el.querySelectorAll(selector); |
129 |
| - var el = e.target; |
| 135 | + var els = vm.$el.querySelectorAll(selector) |
| 136 | + var el = e.target |
130 | 137 | for (var i = 0, len = els.length; i < len; i++) {
|
131 | 138 | if (els[i] === el) return observer.next(e)
|
132 | 139 | }
|
|
144 | 151 | return obs$
|
145 | 152 | }
|
146 | 153 |
|
147 |
| - |
148 | 154 | Vue.directive('stream', {
|
149 |
| - //Example ./example/counter_dir.html |
| 155 | + // Example ./example/counter_dir.html |
150 | 156 | bind: function (el, binding, vnode) {
|
151 | 157 | if (!hasRx()) {
|
152 | 158 | return
|
153 | 159 | }
|
154 |
| - var streamName = binding.arg; |
155 |
| - var vmStream = vnode.context[streamName] || vnode.context.$observables[streamName]; |
156 |
| - var eventNames = Object.keys(binding.modifiers); |
| 160 | + var streamName = binding.arg |
| 161 | + var vmStream = vnode.context[streamName] || vnode.context.$observables[streamName] |
| 162 | + var eventNames = Object.keys(binding.modifiers) |
157 | 163 |
|
158 |
| - if(isSubject(vmStream)){ |
159 |
| - var onNext = (vmStream.next || vmStream.onNext).bind(vmStream); //Rx4 Rx5 |
160 |
| - el.vStreamData = binding.value; |
| 164 | + if (isSubject(vmStream)) { |
| 165 | + var onNext = (vmStream.next || vmStream.onNext).bind(vmStream) // Rx4 Rx5 |
| 166 | + el.vStreamData = binding.value |
161 | 167 | el._obs$ = eventNames.map(function (evtName) {
|
162 |
| - return Rx.Observable.fromEvent(el,evtName) |
163 |
| - .subscribe(function (evt) { |
164 |
| - onNext({ |
165 |
| - event:evt, |
166 |
| - data:el.vStreamData //Not using binding.value for data updating reason |
167 |
| - }); |
168 |
| - }); |
| 168 | + return Rx.Observable.fromEvent(el, evtName) |
| 169 | + .subscribe(function (evt) { |
| 170 | + onNext({ |
| 171 | + event: evt, |
| 172 | + data: el.vStreamData // Not using binding.value for data updating reason |
| 173 | + }) |
| 174 | + }) |
169 | 175 | })
|
170 |
| - }else{ |
| 176 | + } else { |
171 | 177 | warn(
|
172 |
| - 'Invalid Subject found in directive with key "' + streamName + '".' + 'Please declare ' + streamName + ' as an new Rx.Subject' |
| 178 | + 'Invalid Subject found in directive with key "' + streamName + '".' + |
| 179 | + 'Please declare ' + streamName + ' as an new Rx.Subject' |
173 | 180 | )
|
174 | 181 | }
|
175 | 182 | },
|
176 |
| - update:function (el, binding, vnode) { |
177 |
| - el.vStreamData = binding.value; |
| 183 | + update: function (el, binding, vnode) { |
| 184 | + el.vStreamData = binding.value |
178 | 185 | },
|
179 | 186 | unbind: function (el, binding, vnode) {
|
180 |
| - if(Array.isArray(el._obs$)){ |
| 187 | + if (Array.isArray(el._obs$)) { |
181 | 188 | el._obs$.forEach(function (ob) {
|
182 | 189 | unsub(ob)
|
183 | 190 | })
|
184 | 191 | }
|
185 | 192 | }
|
186 |
| - }); |
187 |
| - |
188 |
| - |
189 |
| - |
| 193 | + }) |
190 | 194 |
|
191 |
| - Vue.prototype.$subscribeTo = function(observable, next, error, complete) { |
| 195 | + Vue.prototype.$subscribeTo = function (observable, next, error, complete) { |
192 | 196 | var obs$ = observable.subscribe(next, error, complete)
|
193 | 197 | ;(this._obSubscriptions || (this._obSubscriptions = [])).push(obs$)
|
194 | 198 | return obs$
|
|
0 commit comments