Skip to content

Commit 1d5e85b

Browse files
committed
adjust v-stream syntax
1 parent 51b1c4e commit 1d5e85b

File tree

3 files changed

+80
-81
lines changed

3 files changed

+80
-81
lines changed

example/counter_dir.html

Lines changed: 0 additions & 41 deletions
This file was deleted.

example/couter-with-directive.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script src="https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js"></script>
2+
<script src="https://unpkg.com/vue/dist/vue.js"></script>
3+
<script src="../vue-rx.js"></script>
4+
5+
<div id="app">
6+
<div>{{ count }}</div>
7+
<button v-stream:click="plus$">Add on Click</button>
8+
<button
9+
v-stream:click="minus$"
10+
v-stream:mousemove="minus$">
11+
Minus on Click &amp; Mousemove
12+
</button>
13+
</div>
14+
15+
<script>
16+
new Vue({
17+
el: '#app',
18+
data:function () {
19+
return {
20+
delta1: 1,
21+
delta2: -1
22+
}
23+
},
24+
created:function () {
25+
//Speed up after 10s
26+
setTimeout(() => {
27+
this.delta2 = -5
28+
}, 10 * 1000)
29+
},
30+
domStreams: ['plus$', 'minus$'],
31+
subscriptions:function () {
32+
var plus = this.plus$.map(o => this.delta1)
33+
var minus = this.minus$.map(o => this.delta2)
34+
var count$ = Rx.Observable.merge(plus, minus)
35+
.startWith(0)
36+
.scan((total, change) => total + change);
37+
38+
return {
39+
count: count$
40+
}
41+
}
42+
})
43+
</script>

vue-rx.js

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,35 @@
5353
Vue.mixin({
5454
created: function init () {
5555
var vm = this
56+
var domStreams = vm.$option.domStreams
57+
if (domStreams) {
58+
domStreams.forEach(function (key) {
59+
vm[key] = new Rx.Subject()
60+
})
61+
}
62+
5663
var obs = vm.$options.subscriptions
5764
if (typeof obs === 'function') {
5865
obs = obs.call(vm)
5966
}
60-
if (!obs) return
61-
vm.$observables = {}
62-
vm._obSubscriptions = []
63-
Object.keys(obs).forEach(function (key) {
64-
defineReactive(vm, key, undefined)
65-
var ob = vm.$observables[key] = obs[key]
66-
if (!isObservable(ob)) {
67-
warn(
68-
'Invalid Observable found in subscriptions option with key "' + key + '".',
69-
vm
70-
)
71-
return
72-
}
73-
vm._obSubscriptions.push(obs[key].subscribe(function (value) {
74-
vm[key] = value
75-
}))
76-
})
67+
if (obs) {
68+
vm.$observables = {}
69+
vm._obSubscriptions = []
70+
Object.keys(obs).forEach(function (key) {
71+
defineReactive(vm, key, undefined)
72+
var ob = vm.$observables[key] = obs[key]
73+
if (!isObservable(ob)) {
74+
warn(
75+
'Invalid Observable found in subscriptions option with key "' + key + '".',
76+
vm
77+
)
78+
return
79+
}
80+
vm._obSubscriptions.push(obs[key].subscribe(function (value) {
81+
vm[key] = value
82+
}))
83+
})
84+
}
7785
},
7886
beforeDestroy: function () {
7987
if (this._obSubscriptions) {
@@ -157,37 +165,26 @@
157165
if (!hasRx()) {
158166
return
159167
}
160-
var streamName = binding.arg
161-
var vmStream = vnode.context[streamName] || vnode.context.$observables[streamName]
162-
var eventNames = Object.keys(binding.modifiers)
163-
164-
if (isSubject(vmStream)) {
165-
var onNext = (vmStream.next || vmStream.onNext).bind(vmStream) // Rx4 Rx5
166-
el.vStreamData = binding.value
167-
el._obs$ = eventNames.map(function (evtName) {
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-
})
168+
var event = binding.arg
169+
var stream = binding.value
170+
var streamName = binding.expression
171+
172+
if (isSubject(stream)) {
173+
var onNext = (stream.next || stream.onNext).bind(stream) // Rx4 Rx5
174+
el._ob$ = Rx.Observable.fromEvent(el, event).subscribe(function (evt) {
175+
onNext({ event: evt })
175176
})
176177
} else {
177178
warn(
178179
'Invalid Subject found in directive with key "' + streamName + '".' +
179-
'Please declare ' + streamName + ' as an new Rx.Subject'
180+
streamName + ' should be an instance of Rx.Subject',
181+
vnode.context
180182
)
181183
}
182184
},
183-
update: function (el, binding, vnode) {
184-
el.vStreamData = binding.value
185-
},
186185
unbind: function (el, binding, vnode) {
187-
if (Array.isArray(el._obs$)) {
188-
el._obs$.forEach(function (ob) {
189-
unsub(ob)
190-
})
186+
if (el._ob$) {
187+
unsub(el._ob$)
191188
}
192189
}
193190
})

0 commit comments

Comments
 (0)