Skip to content

Commit 64c0894

Browse files
committed
Add ability to use with multiple stream libraries
Fixes #9
1 parent 18e5806 commit 64c0894

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

examples/horizontal/src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import xs, { Stream } from 'xstream';
2-
import { run } from '@cycle/run';
3-
import { ul, li, div, h3, makeDOMDriver, DOMSource, VNode } from '@cycle/dom';
1+
import { Observable } from 'rxjs';
2+
import { run } from '@cycle/rxjs-run';
3+
import { ul, li, div, h3, p, makeDOMDriver, DOMSource, VNode } from '@cycle/dom';
44

55
import { makeSortable } from '../../../src/makeSortable';
66

@@ -9,14 +9,15 @@ type Sources = {
99
};
1010

1111
type Sinks = {
12-
DOM : Stream<VNode>;
12+
DOM : Observable<VNode>;
1313
};
1414

1515
function main({ DOM } : Sources) : Sinks
1616
{
17-
const vdom$ : Stream<VNode> = xs.of(
17+
const vdom$ : Observable<VNode> = Observable.of(
1818
div([
1919
h3('Horizontal too!'),
20+
p('this is running with RxJS'),
2021
ul('.ul', [
2122
li('.li', '', ['Option 1']),
2223
li('.li', '', ['Option 2']),
@@ -27,7 +28,7 @@ function main({ DOM } : Sources) : Sinks
2728
])
2829
])
2930
)
30-
.compose(makeSortable(DOM, {
31+
.let(makeSortable<Observable<VNode>>(DOM, {
3132
parentSelector: '.ul'
3233
}));
3334

examples/parentSelector/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function main({ DOM } : Sources) : Sinks
2727
])
2828
])
2929
)
30-
.compose(makeSortable(DOM, {
30+
.compose(makeSortable<Stream<VNode>>(DOM, {
3131
parentSelector: '.ul'
3232
}));
3333

examples/simple/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function main({ DOM } : Sources) : Sinks
2424
li('.li', '', ['Option 6']),
2525
])
2626
)
27-
.compose(makeSortable(DOM));
27+
.compose(makeSortable<Stream<VNode>>(DOM));
2828

2929
return {
3030
DOM: vdom$

examples/updateEvent/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function main({ DOM } : Sources) : Sinks
2424
li('.li', '', ['Option 6']),
2525
])
2626
)
27-
.compose(makeSortable(DOM));
27+
.compose(makeSortable<Stream<VNode>>(DOM));
2828

2929
const update$ : Stream<VNode> = getUpdateEvent(DOM, '.ul')
3030
.map(o => 'You changed item Number ' + (o.oldIndex + 1) + ' to postion ' + (o.newIndex + 1))

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@
3333
"@cycle/dom": ">=16.0.0"
3434
},
3535
"dependencies": {
36+
"@cycle/run": "^3.0.0",
3637
"snabbdom-selector": "^1.1.1",
3738
"xstream": "^10.3.0"
3839
},
3940
"devDependencies": {
4041
"@cycle/dom": "^16.0.0",
41-
"@cycle/run": "^3.0.0",
42+
"@cycle/rxjs-run": "^7.0.0",
4243
"babel-preset-es2015": "^6.14.0",
4344
"babelify": "^7.3.0",
4445
"browserify": "^14.1.0",
4546
"cp-cli": "^1.0.2",
4647
"open-cli": "^1.0.5",
4748
"rimraf": "^2.5.4",
49+
"rxjs": "^5.2.0",
4850
"typedoc": "^0.5.8",
4951
"typescript": "^2.2.1"
5052
}

src/makeSortable.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import xs, { Stream } from 'xstream';
22
import delay from 'xstream/extra/delay';
33
import { DOMSource, VNode } from '@cycle/dom';
4+
import { adapt } from '@cycle/run/lib/adapt';
45

56
import { SortableOptions, Transform, EventDetails } from './definitions';
67
import { applyDefaults, addKeys } from './helpers';
@@ -15,29 +16,30 @@ export { SortableOptions, Transform, EventHandler, EventDetails } from './defini
1516
* @param {SortableOptions} options @see {SortableOptions}
1617
* @return {Transform<VNode, VNode>} A function to be composed with a view VNode stream
1718
*/
18-
export function makeSortable(dom : DOMSource, options? : SortableOptions) : Transform<VNode, VNode>
19+
export function makeSortable<T>(dom : DOMSource, options? : SortableOptions) : (s : T) => T
1920
{
20-
return sortable => sortable
21+
return sortable => adapt(
22+
(xs.fromObservable(sortable as any) as Stream<VNode>)
2123
.map(node => {
2224
const defaults : SortableOptions = applyDefaults(options || {}, node);
2325

24-
const mousedown$ : Stream<MouseEvent> = dom.select(defaults.handle)
25-
.events('mousedown') as Stream<MouseEvent>;
26+
const mousedown$ : Stream<MouseEvent> = xs.fromObservable(dom.select(defaults.handle).events('mousedown')) as Stream<MouseEvent>;
2627

2728
const mouseup$ : Stream<MouseEvent> = mousedown$
28-
.mapTo(dom.select('body').events('mouseup').take(1))
29+
.mapTo(xs.fromObservable(dom.select('body').events('mouseup').take(1)))
2930
.flatten() as Stream<MouseEvent>;
3031

3132
const mousemove$ : Stream<MouseEvent> = mousedown$
32-
.mapTo(dom.select('body').events('mousemove'))
33+
.mapTo(xs.fromObservable(dom.select('body').events('mousemove')))
3334
.flatten()
3435
.compose(emitBetween(mousedown$, mouseup$)) as Stream<MouseEvent>;
3536

3637
const event$ : Stream<MouseEvent> = xs.merge(mousedown$, mouseup$, mousemove$);
3738

3839
return event$.fold((acc, curr) => handleEvent(acc, curr, defaults), node);
3940
})
40-
.flatten();
41+
.flatten()
42+
);
4143
}
4244

4345
/**

0 commit comments

Comments
 (0)