-
Notifications
You must be signed in to change notification settings - Fork 190
/
vue-rx.js
150 lines (137 loc) · 4.21 KB
/
vue-rx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function () {
function VueRx (Vue, Rx) {
var warn = Vue.util.warn || function () {}
function hasRx (vm) {
if (!Rx) {
warn(
'$watchAsObservable requires Rx to be present globally or ' +
'be passed to Vue.use() as the second argument.',
vm
)
return false
}
return true
}
function defineReactive (vm, key, val) {
if (key in vm) {
vm[key] = val
} else {
Vue.util.defineReactive(vm, key, val)
}
}
Vue.mixin({
created: function init () {
var vm = this
var obs = vm.$options.subscriptions
if (typeof obs === 'function') {
obs = obs.call(vm)
}
if (!obs) return
vm.$subscriptions = {}
vm._obSubscriptions = []
Object.keys(obs).forEach(function (key) {
defineReactive(vm, key, undefined)
var ob = vm.$subscriptions[key] = obs[key]
if (!ob || typeof ob.subscribe !== 'function') {
warn(
'Invalid Observable found in subscriptions option with key "' + key + '".',
vm
)
return
}
vm._obSubscriptions.push(obs[key].subscribe(function (value) {
vm[key] = value
}))
})
},
beforeDestroy: function () {
if (this._obSubscriptions) {
this._obSubscriptions.forEach(function (handle) {
if (handle.dispose) {
handle.dispose()
} else if (handle.unsubscribe) {
handle.unsubscribe()
}
})
}
}
})
Vue.prototype.$watchAsObservable = function (expOrFn, options) {
if (!hasRx()) {
return
}
var vm = this
var obs$ = Rx.Observable.create(function (observer) {
var _unwatch
function watch () {
_unwatch = vm.$watch(expOrFn, function (newValue, oldValue) {
observer.next({ oldValue: oldValue, newValue: newValue })
}, options)
}
function unwatch () {
_unwatch && _unwatch()
}
// if $watchAsObservable is called inside the subscriptions function,
// because data hasn't been observed yet, the watcher will not work.
// in that case, wait until created hook to watch.
if (vm._data) {
watch()
} else {
vm.$once('hook:created', watch)
}
// Returns function which disconnects the $watch expression
var disposable
if (Rx.Subscription) { // Rx5
disposable = new Rx.Subscription(unwatch)
} else { // Rx4
disposable = Rx.Disposable.create(unwatch)
}
return disposable
}).publish().refCount()
;(vm._obSubscriptions || (vm._obSubscriptions = [])).push(obs$)
return obs$
}
Vue.prototype.$fromDOMEvent = function (selector, event) {
if (!hasRx()) {
return
}
if (typeof window === 'undefined') {
return Rx.Observable.empty()
}
var vm = this
var doc = document.documentElement
var obs$ = Rx.Observable.create(function (observer) {
function listener (e) {
if (vm.$el && vm.$el.querySelector(selector) === e.target) {
observer.next(e)
}
}
doc.addEventListener(event, listener)
function unwatch () {
doc.removeEventListener(event, listener)
}
// Returns function which disconnects the $watch expression
var disposable
if (Rx.Subscription) { // Rx5
disposable = new Rx.Subscription(unwatch)
} else { // Rx4
disposable = Rx.Disposable.create(unwatch)
}
return disposable
}).publish().refCount()
;(vm._obSubscriptions || (vm._obSubscriptions = [])).push(obs$)
return obs$
}
}
// auto install
if (typeof Vue !== 'undefined' && typeof Rx !== 'undefined') {
Vue.use(VueRx, Rx)
}
if (typeof exports === 'object' && typeof module === 'object') {
module.exports = VueRx
} else if (typeof define === 'function' && define.amd) {
define(function () { return VueRx })
} else if (typeof window !== 'undefined') {
window.VueRx = VueRx
}
})()