11use crate :: database:: { Database , VersionChangeEvent } ;
22use crate :: error:: { Error , UnexpectedDataError } ;
3+ use crate :: transaction:: { OnTransactionDrop , Transaction } ;
34use internal_macros:: generic_bounds;
45use std:: fmt:: { Debug , Display , Formatter } ;
56use std:: mem;
@@ -56,9 +57,16 @@ impl OpenDbListener {
5657 #[ cfg( feature = "async-upgrade" ) ]
5758 async_notify : Self :: fake_rx ( ) ,
5859 listener : Closure :: once ( move |evt : web_sys:: IdbVersionChangeEvent | {
59- let res = Database :: from_event ( & evt)
60- . and_then ( move |db| callback ( VersionChangeEvent :: new ( evt) , db) ) ;
61-
60+ let res = Database :: from_event ( & evt) . and_then ( |db| {
61+ Transaction :: from_raw_version_change_event ( & db, & evt) . and_then ( |mut tx| {
62+ callback ( VersionChangeEvent :: new ( evt) , & tx) . inspect ( |( ) | {
63+ // If the callback succeeded, we want to ensure that
64+ // the transaction is committed when dropped and not
65+ // aborted.
66+ tx. on_drop ( OnTransactionDrop :: Commit ) ;
67+ } )
68+ } )
69+ } ) ;
6270 Self :: handle_result ( LBL_UPGRADE , & status, res)
6371 } ) ,
6472 }
@@ -154,8 +162,8 @@ const _: () = {
154162 tokio:: sync:: mpsc:: unbounded_channel ( ) . 1
155163 }
156164
157- #[ generic_bounds( upgrade_async_cb( fun ( Fn ) , fut ( Fut ) ) ) ]
158- pub ( crate ) fn new_upgrade_fut < Fn , Fut > ( callback : Fn ) -> Self {
165+ #[ generic_bounds( upgrade_async_cb( Fn ) ) ]
166+ pub ( crate ) fn new_upgrade_fut < Fn > ( callback : Fn ) -> Self {
159167 let status = Status :: new ( ) ;
160168 let ( tx, rx) = tokio:: sync:: mpsc:: unbounded_channel ( ) ;
161169 Self {
@@ -168,11 +176,21 @@ const _: () = {
168176 } ;
169177
170178 Self :: set_status ( & status, Status :: Pending , LBL_UPGRADE ) ?;
171- let fut = callback ( VersionChangeEvent :: new ( evt) , db) ;
172-
173179 wasm_bindgen_futures:: spawn_local ( async move {
174- let result = match fut. await {
175- Ok ( ( ) ) => Status :: Ok ,
180+ let db = db;
181+ let result = match Transaction :: from_raw_version_change_event ( & db, & evt) {
182+ Ok ( mut transaction) => {
183+ match callback ( VersionChangeEvent :: new ( evt) , & transaction) . await {
184+ Ok ( ( ) ) => {
185+ // If the callback succeeded, we want to ensure that
186+ // the transaction is committed when dropped and not
187+ // aborted.
188+ transaction. on_drop ( OnTransactionDrop :: Commit ) ;
189+ Status :: Ok
190+ }
191+ Err ( e) => Status :: Err ( e) ,
192+ }
193+ }
176194 Err ( e) => Status :: Err ( e) ,
177195 } ;
178196 let _ = Self :: set_status ( & status, result, LBL_UPGRADE ) ;
0 commit comments