Skip to content

Commit deb5e94

Browse files
committed
introduce proper build setup
1 parent 38bdd32 commit deb5e94

File tree

16 files changed

+435
-253
lines changed

16 files changed

+435
-253
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
node_modules
3+
.DS_Store

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ var vm = new Vue({
189189

190190
#### `$fromDOMEvent(selector, event)`
191191

192-
> Deprecated in 3.0+. Prefer `v-stream` instead.
193-
194192
> This feature requires RxJS.
195193
196194
This is a prototype method added to instances. Use it to create an observable from DOM events within the instances' element. This is similar to `Rx.Observable.fromEvent`, but usable inside the `subscriptions` function even before the DOM is actually rendered.

example/counter-simple.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script src="https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js"></script>
22
<script src="https://unpkg.com/vue/dist/vue.js"></script>
3-
<script src="../vue-rx.js"></script>
3+
<script src="../dist/vue-rx.js"></script>
44

55
<div id="app">
66
<div>{{ count }}</div>

example/counter.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<script src="https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js"></script>
44
<script src="https://unpkg.com/vue/dist/vue.js"></script>
5-
<script src="../vue-rx.js"></script>
5+
<script src="../dist/vue-rx.js"></script>
66

77
<div id="app">
88
<div>{{ count }}</div>

example/wiki-search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script src="https://unpkg.com/jquery"></script>
22
<script src="https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js"></script>
33
<script src="https://unpkg.com/vue/dist/vue.js"></script>
4-
<script src="../vue-rx.js"></script>
4+
<script src="../dist/vue-rx.js"></script>
55

66
<p>Type to search Wikipedia</p>
77

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "vue-rx",
33
"version": "2.3.1",
44
"description": "RxJS bindings for Vue",
5-
"main": "vue-rx.js",
5+
"main": "dist/vue-rx.js",
66
"files": [
7-
"vue-rx.js"
7+
"dist"
88
],
99
"repository": {
1010
"type": "git",
@@ -20,5 +20,16 @@
2020
"bugs": {
2121
"url": "https://github.com/vuejs/vue-rx/issues"
2222
},
23-
"homepage": "https://github.com/vuejs/vue-rx#readme"
23+
"homepage": "https://github.com/vuejs/vue-rx#readme",
24+
"scripts": {
25+
"dev": "rollup -c rollup.config.js -w",
26+
"build": "rollup -c rollup.config.js",
27+
"prepublish": "npm run build"
28+
},
29+
"devDependencies": {
30+
"buble": "^0.15.2",
31+
"rollup": "^0.41.6",
32+
"rollup-plugin-buble": "^0.15.0",
33+
"rollup-watch": "^3.2.2"
34+
}
2435
}

rollup.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const buble = require('rollup-plugin-buble')
2+
3+
module.exports = {
4+
entry: 'src/index.js',
5+
dest: 'dist/vue-rx.js',
6+
format: 'umd',
7+
moduleName: 'VueRx',
8+
plugins: [buble()]
9+
}

src/directives/stream.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Rx, hasRx, isSubject, warn, getKey, unsub } from '../util'
2+
3+
export default {
4+
// Example ./example/counter_dir.html
5+
bind (el, binding, vnode) {
6+
if (!hasRx()) {
7+
return
8+
}
9+
10+
let handle = binding.value
11+
const event = binding.arg
12+
const streamName = binding.expression
13+
14+
if (isSubject(handle)) {
15+
handle = { subject: handle }
16+
} else if (!handle || !isSubject(handle.subject)) {
17+
warn(
18+
'Invalid Subject found in directive with key "' + streamName + '".' +
19+
streamName + ' should be an instance of Rx.Subject or have the ' +
20+
'type { subject: Rx.Subject, data: any }.',
21+
vnode.context
22+
)
23+
return
24+
}
25+
26+
const subject = handle.subject
27+
const next = (subject.next || subject.onNext).bind(subject)
28+
handle.subscription = Rx.Observable.fromEvent(el, event).subscribe(e => {
29+
next({
30+
event: e,
31+
data: handle.data
32+
})
33+
})
34+
35+
// store handle on element with a unique key for identifying
36+
// multiple v-stream directives on the same node
37+
;(el._rxHandles || (el._rxHandles = {}))[getKey(binding)] = handle
38+
},
39+
40+
update (el, binding) {
41+
const handle = binding.value
42+
const _handle = el._rxHandles && el._rxHandles[getKey(binding)]
43+
if (_handle && handle && isSubject(handle.subject)) {
44+
_handle.data = handle.data
45+
}
46+
},
47+
48+
unbind (el, binding) {
49+
const key = getKey(binding)
50+
const handle = el._rxHandles && el._rxHandles[key]
51+
if (handle) {
52+
unsub(handle.subscription)
53+
el._rxHandles[key] = null
54+
}
55+
}
56+
}

src/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { install } from './util'
2+
import rxMixin from './mixin'
3+
import streamDirective from './directives/stream'
4+
import watchAsObservable from './methods/watchAsObservable'
5+
import fromDOMEvent from './methods/fromDOMEvent'
6+
import subscribeTo from './methods/subscribeTo'
7+
8+
export default function VueRx (Vue, Rx) {
9+
install(Vue, Rx)
10+
Vue.mixin(rxMixin)
11+
Vue.directive('stream', streamDirective)
12+
Vue.prototype.$watchAsObservable = watchAsObservable
13+
Vue.prototype.$fromDOMEvent = fromDOMEvent
14+
Vue.prototype.$subscribeTo = subscribeTo
15+
}
16+
17+
// auto install
18+
if (typeof Vue !== 'undefined' && typeof Rx !== 'undefined') {
19+
Vue.use(VueRx, Rx)
20+
}

src/methods/fromDOMEvent.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Rx, hasRx, getDisposable } from '../util'
2+
3+
export default function fromDOMEvent (selector, event) {
4+
if (!hasRx()) {
5+
return
6+
}
7+
if (typeof window === 'undefined') {
8+
return Rx.Observable.create(() => {})
9+
}
10+
11+
const vm = this
12+
const doc = document.documentElement
13+
const obs$ = Rx.Observable.create(observer => {
14+
function listener (e) {
15+
if (!vm.$el) return
16+
if (selector === null && vm.$el === e.target) return observer.next(e)
17+
var els = vm.$el.querySelectorAll(selector)
18+
var el = e.target
19+
for (var i = 0, len = els.length; i < len; i++) {
20+
if (els[i] === el) return observer.next(e)
21+
}
22+
}
23+
doc.addEventListener(event, listener)
24+
// Returns function which disconnects the $watch expression
25+
return getDisposable(() => {
26+
doc.removeEventListener(event, listener)
27+
})
28+
})
29+
30+
;(vm._obSubscriptions || (vm._obSubscriptions = [])).push(obs$)
31+
return obs$
32+
}

0 commit comments

Comments
 (0)