callbag-from-event
Advanced tools
Comparing version 1.1.0 to 1.2.0
23
index.js
@@ -1,10 +0,23 @@ | ||
const fromEvent = (node, name) => (start, sink) => { | ||
const fromEvent = (node, name, options) => (start, sink) => { | ||
if (start !== 0) return; | ||
const handler = ev => sink(1, ev); | ||
let disposed = false; | ||
const handler = ev => { | ||
sink(1, ev) | ||
}; | ||
sink(0, t => { | ||
if (t === 2) node.removeEventListener(name, handler); | ||
if (t !== 2) { | ||
return; | ||
} | ||
disposed = true; | ||
node.removeEventListener(name, handler, options); | ||
}); | ||
node.addEventListener(name, handler); | ||
if (disposed) { | ||
return; | ||
} | ||
node.addEventListener(name, handler, options); | ||
}; | ||
module.exports = fromEvent; | ||
export default fromEvent; |
{ | ||
"name": "callbag-from-event", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Create a callbag listenable source from events on a DOM node", | ||
@@ -9,10 +9,16 @@ "repository": { | ||
}, | ||
"main": "index.js", | ||
"main": "index.cjs.js", | ||
"module": "index.js", | ||
"types": "types.d.ts", | ||
"scripts": { | ||
"test": "tape test.js" | ||
"build": "rollup $npm_package_module -o $npm_package_main --f cjs", | ||
"pretest": "npm run build", | ||
"test": "tape test.js", | ||
"prepare": "npm test" | ||
}, | ||
"author": "staltz.com", | ||
"license": "MIT", | ||
"keywords": ["callbag"], | ||
"keywords": [ | ||
"callbag" | ||
], | ||
"dependencies": { | ||
@@ -22,4 +28,5 @@ "callbag": "^1.1.0" | ||
"devDependencies": { | ||
"rollup": "^0.67.0", | ||
"tape": "^4.8.0" | ||
} | ||
} |
148
test.js
const test = require('tape'); | ||
const fromEvent = require('./index'); | ||
const fromEvent = require('.'); | ||
@@ -63,1 +63,147 @@ test('it converts from (fake) DOM node events', (t) => { | ||
test('it handles sink immediately terminating source', (t) => { | ||
t.plan(5); | ||
const elem = { | ||
added: false, | ||
id: null, | ||
addEventListener: (name, listener) => { | ||
t.equals(name, 'click', 'addEventListener for click'); | ||
this.added = true; | ||
let i = 0; | ||
this.id = setInterval(() => listener((++i)*10)); | ||
}, | ||
removeEventListener: (name, listener) => { | ||
t.equals(name, 'click', 'removeEventListener for click'); | ||
this.added = false; | ||
clearInterval(this.id); | ||
} | ||
} | ||
const source = fromEvent(elem, 'click'); | ||
const downwardsExpectedType = [ | ||
[0, 'function'] | ||
]; | ||
function makeSink(type, data) { | ||
let talkback; | ||
return (type, data) => { | ||
const et = downwardsExpectedType.shift(); | ||
t.ok(et, 'downwards call was expected'); | ||
t.equals(type, et[0], 'downwards type is expected: ' + et[0]); | ||
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]); | ||
if (type === 0) { | ||
talkback = data; | ||
talkback(2); // Immediately end. | ||
} else { | ||
t.fail('incorrectly written test, this should never be called'); | ||
} | ||
}; | ||
} | ||
const sink = makeSink(); | ||
source(0, sink); | ||
setTimeout(() => { | ||
t.pass('nothing else happens after dispose()'); | ||
t.end(); | ||
}, 700); | ||
}); | ||
test('immediately terminating sink doesn\'t receive immediately emitted data', (t) => { | ||
t.plan(5); | ||
const elem = { | ||
added: false, | ||
id: null, | ||
addEventListener: (name, listener) => { | ||
t.equals(name, 'click', 'addEventListener for click'); | ||
this.added = true; | ||
let i = 0; | ||
listener(++i); | ||
this.id = setInterval(() => listener((++i)*10)); | ||
}, | ||
removeEventListener: (name, listener) => { | ||
t.equals(name, 'click', 'removeEventListener for click'); | ||
this.added = false; | ||
clearInterval(this.id); | ||
} | ||
} | ||
const source = fromEvent(elem, 'click'); | ||
const downwardsExpectedType = [ | ||
[0, 'function'] | ||
]; | ||
function makeSink(type, data) { | ||
let talkback; | ||
return (type, data) => { | ||
const et = downwardsExpectedType.shift(); | ||
t.ok(et, 'downwards call was expected'); | ||
t.equals(type, et[0], 'downwards type is expected: ' + et[0]); | ||
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]); | ||
if (type === 0) { | ||
talkback = data; | ||
talkback(2); // Immediately end. | ||
} else { | ||
t.fail('incorrectly written test, this should never be called'); | ||
} | ||
}; | ||
} | ||
const sink = makeSink(); | ||
source(0, sink); | ||
setTimeout(() => { | ||
t.pass('nothing else happens after dispose()'); | ||
t.end(); | ||
}, 700); | ||
}); | ||
test('it adds & removes listener with passed in options', (t) => { | ||
t.plan(8); | ||
const inputOptions = { passive: true } | ||
const elem = { | ||
addEventListener: (name, listener, options) => { | ||
t.equals(options, inputOptions) | ||
let i = 0; | ||
this.id = setInterval(() => listener((++i)*10)); | ||
}, | ||
removeEventListener: (name, listener, options) => { | ||
t.equals(options, inputOptions) | ||
clearInterval(this.id); | ||
} | ||
} | ||
const source = fromEvent(elem, 'click', inputOptions); | ||
const downwardsExpectedType = [ | ||
[0, 'function'], | ||
[1, 'number'], | ||
]; | ||
const downwardsExpected = [10]; | ||
function makeSink(type, data) { | ||
let talkback; | ||
return (type, data) => { | ||
const et = downwardsExpectedType.shift(); | ||
t.equals(type, et[0], 'downwards type is expected: ' + et[0]); | ||
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]); | ||
if (type === 0) { | ||
talkback = data; | ||
} | ||
if (type === 1) { | ||
const e = downwardsExpected.shift(); | ||
t.equals(data, e, 'downwards data is expected: ' + e); | ||
} | ||
if (downwardsExpected.length === 0) { | ||
talkback(2); | ||
} | ||
}; | ||
} | ||
const sink = makeSink(); | ||
source(0, sink); | ||
setTimeout(() => { | ||
t.pass('nothing else happens after dispose()'); | ||
t.end(); | ||
}, 700); | ||
}); |
import { Source } from 'callbag' | ||
export default function fromEvent(node: EventTarget, name: string): Source<Event>; | ||
export default function fromEvent<T extends Event = Event>( | ||
node: EventTarget, | ||
name: string, | ||
options?: boolean | AddEventListenerOptions | ||
): Source<T>; |
8776
8
229
2