@@ -8,10 +8,10 @@ import { adapt } from '@cycle/run/lib/adapt';
88import { addKeys } from './helpers' ;
99import { handleEvent } from './eventHandler' ;
1010
11- export type Sources = any ;
12- export type Sinks = any ;
13- export type Component = ( s : Sources ) => Sinks ;
14- export type HOC = ( c : Component ) => Component ;
11+ export type Component < So , Si > = ( s : So ) => Si ;
12+ export type HOC < So , Si > = (
13+ c : Component < So , Si >
14+ ) => Component < So , Si & SortableSinks > ;
1515
1616export interface SortableOptions {
1717 itemSelector : string ;
@@ -27,23 +27,31 @@ export interface UpdateOrder {
2727 newIndex : number ;
2828}
2929
30- export function toSortable ( options : SortableOptions ) : HOC {
30+ export interface SortableSinks {
31+ dragging : Stream < boolean > ;
32+ updateLive : Stream < UpdateOrder > ;
33+ updateDone : Stream < UpdateOrder > ;
34+ }
35+
36+ export function toSortable < Sources extends object , Sinks extends object > (
37+ options : SortableOptions
38+ ) : HOC < Sources , Sinks > {
3139 return component => makeSortable ( component , options ) ;
3240}
3341
34- export function makeSortable (
35- main : Component ,
42+ export function makeSortable < Sources extends object , Sinks extends object > (
43+ main : Component < Sources , Sinks > ,
3644 options : SortableOptions
37- ) : Component {
38- return function ( sources : Sources ) : Sinks {
45+ ) : Component < Sources , Sinks & SortableSinks > {
46+ return function ( sources : Sources ) : Sinks & SortableSinks {
3947 if ( ! options . DOMDriverKey ) {
4048 options . DOMDriverKey = 'DOM' ;
4149 }
4250 if ( ! options . TimeDriverKey ) {
4351 options . TimeDriverKey = 'Time' ;
4452 }
4553
46- const sinks = main ( sources ) ;
54+ const sinks : any = main ( sources ) ;
4755 const eventHandler = handleEvent ( options ) ;
4856
4957 const childDOM$ : Stream < VNode > = xs
@@ -102,27 +110,36 @@ export function makeSortable(
102110 . filter ( x => x !== undefined ) ;
103111
104112 const updateAccumulated$ : Stream < UpdateOrder > = mousedown$
105- . mapTo ( updateOrder$
106- . fold (
107- ( acc , curr ) => ( {
108- indexMap : acc . indexMap
109- ? Object . keys ( acc . indexMap )
110- . map ( k => ( {
111- [ k ] : curr . indexMap [ acc . indexMap [ k ] ]
112- } ) )
113- . reduce ( ( a , c ) => ( { ...a , ...c } ) , { } )
114- : curr . indexMap ,
115- oldIndex :
116- acc . oldIndex === - 1 ? curr . oldIndex : acc . oldIndex ,
117- newIndex : curr . newIndex
118- } ) ,
119- {
120- indexMap : undefined ,
121- oldIndex : - 1 ,
122- newIndex : - 1
123- }
124- )
125- . drop ( 1 ) as Stream < UpdateOrder > )
113+ . map (
114+ ( ) =>
115+ updateOrder$
116+ . fold (
117+ ( acc , curr ) => ( {
118+ indexMap : acc . indexMap
119+ ? Object . keys ( acc . indexMap )
120+ . map ( k => ( {
121+ [ k ] :
122+ curr . indexMap [ acc . indexMap [ k ] ]
123+ } ) )
124+ . reduce (
125+ ( a , c ) => ( { ...a , ...c } ) ,
126+ { }
127+ )
128+ : curr . indexMap ,
129+ oldIndex :
130+ acc . oldIndex === - 1
131+ ? curr . oldIndex
132+ : acc . oldIndex ,
133+ newIndex : curr . newIndex
134+ } ) ,
135+ {
136+ indexMap : undefined ,
137+ oldIndex : - 1 ,
138+ newIndex : - 1
139+ }
140+ )
141+ . drop ( 1 ) as Stream < UpdateOrder >
142+ )
126143 . flatten ( ) ;
127144
128145 const updateDone$ : Stream < UpdateOrder > = mouseup$
@@ -136,9 +153,9 @@ export function makeSortable(
136153 return {
137154 ...sinks ,
138155 DOM : adapt ( vdom$ ) ,
139- drag : adapt ( dragInProgress$ ) ,
156+ dragging : adapt ( dragInProgress$ ) ,
140157 updateLive : adapt ( updateOrder$ ) ,
141- updateDone$ : adapt ( updateDone$ )
158+ updateDone : adapt ( updateDone$ )
142159 } ;
143160 } ;
144161}
0 commit comments