Skip to content

Commit

Permalink
Merge branch 'feat/issue-#17' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Frikki committed Dec 11, 2015
2 parents 047383d + 977352e commit 5cfa0a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/fromEvent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Stream from 'most/lib/Stream'
import MulticastSource from 'most/lib/source/MulticastSource'
import forEach from 'fast.js/array/forEach'

const tryEvent =
(t, x, sink) => {
Expand Down Expand Up @@ -40,12 +41,17 @@ EventAdapter.prototype.dispose = function dispose() {

const initEventTarget =
(source, event, addEvent, useCapture) => { // eslint-disable-line
source.addEventListener(event, addEvent, useCapture)
forEach(
source,
s => s.addEventListener(event, addEvent, useCapture)
)

const dispose =
(_event, target) => {
target.removeEventListener(_event, addEvent, useCapture)
}
const dispose = (_event, target) => {
forEach(
target,
t => t.removeEventListener(_event, addEvent, useCapture)
)
}

return dispose
}
Expand All @@ -69,8 +75,15 @@ EventTargetSource.prototype.run = function run(sink, scheduler) {

const fromEvent =
(event, source, useCapture = false) => {
// is not a NodeList
if (!source.length) {
throw new Error(
`source must be a NodeList or an Array of DOM Nodes`
)
}

let s
if (source.addEventListener && source.removeEventListener) {
if (source[0].addEventListener && source[0].removeEventListener) {
s = new MulticastSource(
new EventTargetSource(event, source, useCapture)
)
Expand Down
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ const makeEventsSelector =
if (!elements) {
return most.empty()
}
return most.merge(
...fastMap(elements, el => {
return fromEvent(eventName, el, useCapture)
})
)
return fromEvent(eventName, elements, useCapture)
}).switch().multicast()
}

Expand Down
37 changes: 36 additions & 1 deletion test/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import assert from 'assert'
import {run} from '@motorcycle/core'
import {makeDOMDriver, div, p, span, h2, h3, h4} from '../../src'
import fromEvent from '../../src/fromEvent'
import most from 'most'

function click(el) {
Expand Down Expand Up @@ -222,7 +223,6 @@ describe(`Rendering`, () => {
// Make assertions
sources.DOM.select(`:root`).observable
.observe(root => {
console.log(root)
assert.notStrictEqual(root, null)
assert.notStrictEqual(typeof root, `undefined`)
assert.strictEqual(root.tagName, `H3`)
Expand Down Expand Up @@ -395,3 +395,38 @@ describe(`Rendering`, () => {
})
})
})

function createRenderTargetWithChildren(id = null) {
const element = createRenderTarget()
const child = document.createElement('h1')
child.textContent = 'Hello'
element.appendChild(child)
return element
}

describe(`fromEvent`, () => {
it(`should accept a NodeList as input`, done => {
const element = createRenderTargetWithChildren()
const source = element.querySelectorAll('h1')

const event$ = fromEvent('click', source, false)

event$.observe(event => {
assert.strictEqual(event.type, 'click')
assert.strictEqual(event.target.textContent, 'Hello')
done()
})

click(source[0])
})

it(`should throw error if not given a NodeList`, done => {
const element = createRenderTargetWithChildren()
const source = element.querySelector('h1')
assert.throws(
() => fromEvent('click', source, false),
/source must be a NodeList or an Array of DOM Nodes/
)
done()
})
})

0 comments on commit 5cfa0a0

Please sign in to comment.