Skip to content

Commit 267569d

Browse files
regouyyx990803
authored andcommitted
Add feature watchAsObservable (#11)
* watch as observable * use rx5 * support 4.0
1 parent 3f554dd commit 267569d

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

example/example.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
2-
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.lite.js"></script>
2+
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.0-rc.1/Rx.min.js"></script>
33
<script src="https://unpkg.com/vue/dist/vue.js"></script>
44
<script src="../vue-rx.js"></script>
55

@@ -29,18 +29,18 @@ <h1>{{ results.term }}</h1>
2929
.filter(function (text) {
3030
return text.length > 2
3131
})
32-
.debounce(500)
32+
.debounceTime(500)
3333
.distinctUntilChanged()
34-
.flatMapLatest(function (term) {
35-
return $.ajax({
34+
.switchMap(function (term) {
35+
return Rx.Observable.fromPromise($.ajax({
3636
url: 'http://en.wikipedia.org/w/api.php',
3737
dataType: 'jsonp',
3838
data: {
3939
action: 'opensearch',
4040
format: 'json',
4141
search: term
4242
}
43-
}).promise()
43+
}).promise())
4444
})
4545
// format the final data a little bit
4646
.map(function (res) {
@@ -60,7 +60,22 @@ <h1>{{ results.term }}</h1>
6060
var vm = new Vue({
6161
el: '#el',
6262
data: {
63+
a:5,
6364
results: dataSource
6465
}
65-
})
66+
});
67+
68+
69+
setTimeout(function () {
70+
vm.a = 17;
71+
},5000)
72+
73+
vm.$watchAsObservable('a')
74+
.subscribe(function (val) {
75+
console.log('stream value',val);
76+
},function (err) {
77+
console.error(err)
78+
},function () {
79+
console.log('complete');
80+
})
6681
</script>

vue-rx.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function () {
2-
function VueRx (Vue) {
2+
function VueRx (Vue,Rx) {
33
var VueVersion = Number(Vue.version && Vue.version.split('.')[0])
44
var initHook = VueVersion && VueVersion > 1 ? 'beforeCreate' : 'init'
55

@@ -39,11 +39,38 @@
3939
}
4040

4141
Vue.mixin(mixin)
42+
43+
44+
Vue.prototype.$watchAsObservable = function (expOrFn,options) {
45+
var self = this;
46+
47+
var obs$ = Rx.Observable.create(function (observer) {
48+
// Create function to handle old and new Value
49+
function listener (newValue, oldValue) {
50+
observer.next({ oldValue: oldValue, newValue: newValue });
51+
}
52+
53+
// Returns function which disconnects the $watch expression
54+
var disposable;
55+
if(Rx.Subscription){//Rx5
56+
disposable = new Rx.Subscription(self.$watch(expOrFn,listener,options));
57+
}else{//Rx4
58+
disposable = Rx.Disposable.create(self.$watch(expOrFn,listener,options));
59+
}
60+
61+
return disposable;
62+
}).publish().refCount();
63+
64+
(self._rxHandles || (self._rxHandles = [])).push(obs$);
65+
66+
return obs$;
67+
}
68+
4269
}
4370

4471
// auto install
4572
if (typeof Vue !== 'undefined') {
46-
Vue.use(VueRx)
73+
Vue.use(VueRx,Rx)
4774
}
4875

4976
if(typeof exports === 'object' && typeof module === 'object') {

0 commit comments

Comments
 (0)