@@ -17,10 +17,14 @@ mod js {
17
17
18
18
#[ wasm_bindgen( module = "/sortable.esm.js" ) ]
19
19
extern "C" {
20
+ #[ wasm_bindgen( extends = js_sys:: Object ) ]
20
21
pub type Sortable ;
21
22
22
23
#[ wasm_bindgen( constructor) ]
23
24
pub fn new ( elt : & web_sys:: Element , opts : & js_sys:: Object ) -> Sortable ;
25
+
26
+ #[ wasm_bindgen( method) ]
27
+ pub fn destroy ( item : & Sortable ) ;
24
28
}
25
29
}
26
30
@@ -226,19 +230,36 @@ impl Options {
226
230
/// long as you want the callbacks to be callable, as JS code will error out
227
231
/// should an event happen after it was dropped.
228
232
pub fn apply ( & self , elt : & web_sys:: Element ) -> Sortable {
229
- js:: Sortable :: new ( elt, & self . options ) ;
233
+ let sortable = js:: Sortable :: new ( elt, & self . options ) ;
234
+ let object_ref: & js_sys:: Object = sortable. as_ref ( ) ;
235
+ let raw_object = object_ref. clone ( ) ;
230
236
Sortable {
237
+ raw_object,
238
+ sortable,
231
239
_callbacks : self . callbacks . clone ( ) ,
232
240
}
233
241
}
234
242
}
235
243
236
244
/// Data related to the Sortable instance
237
245
///
238
- /// It must be kept alive on the rust sideas long as the instance can call
239
- /// callbacks, as otherwise the link between the js-side callback and the
240
- /// rust-side callback would be lost .
246
+ /// When it is dropped, the list is made non-sortable again. This is required
247
+ /// because callbacks could be called otherwise. If it is a problem for you, you
248
+ /// can leak it, but be aware of the fact that it is a leak .
241
249
pub struct Sortable {
250
+ /// Raw Sortable JS object, should this crate not expose the necessary
251
+ /// methods
252
+ pub raw_object : js_sys:: Object ,
253
+
254
+ /// raw_object but with the proper type
255
+ sortable : js:: Sortable ,
256
+
242
257
/// Keep the callbacks alive
243
258
_callbacks : [ Option < Rc < Closure < dyn FnMut ( js_sys:: Object ) > > > ; CallbackId :: _Total as usize ] ,
244
259
}
260
+
261
+ impl Drop for Sortable {
262
+ fn drop ( & mut self ) {
263
+ self . sortable . destroy ( ) ;
264
+ }
265
+ }
0 commit comments