@@ -5,6 +5,7 @@ use std::{
55 mem:: replace,
66 panic,
77 pin:: Pin ,
8+ sync:: Arc ,
89} ;
910
1011use anyhow:: { anyhow, Result } ;
@@ -98,10 +99,10 @@ impl EffectInstance {
9899 listener. await ;
99100 }
100101 State :: NotStarted ( EffectInner { future } ) => {
101- let join_handle = tokio:: spawn (
102+ let join_handle = tokio:: spawn ( ApplyEffectsContext :: in_current_scope (
102103 turbo_tasks_future_scope ( turbo_tasks:: turbo_tasks ( ) , future)
103104 . instrument ( Span :: current ( ) ) ,
104- ) ;
105+ ) ) ;
105106 let result = match join_handle. await {
106107 Ok ( Err ( err) ) => Err ( SharedError :: new ( err) ) ,
107108 Err ( err) => {
@@ -279,15 +280,26 @@ impl Effects {
279280
280281task_local ! {
281282 /// The context of the current effects application.
282- static APPLY_EFFECTS_CONTEXT : Mutex <ApplyEffectContext >;
283+ static APPLY_EFFECTS_CONTEXT : Arc < Mutex <ApplyEffectsContext > >;
283284}
284285
285286#[ derive( Default ) ]
286- pub struct ApplyEffectContext {
287+ pub struct ApplyEffectsContext {
287288 data : FxHashMap < TypeId , Box < dyn Any + Send + Sync > > ,
288289}
289290
290- impl ApplyEffectContext {
291+ impl ApplyEffectsContext {
292+ fn in_current_scope < F : Future > ( f : F ) -> impl Future < Output = F :: Output > {
293+ let current = Self :: current ( ) ;
294+ APPLY_EFFECTS_CONTEXT . scope ( current, f)
295+ }
296+
297+ fn current ( ) -> Arc < Mutex < Self > > {
298+ APPLY_EFFECTS_CONTEXT
299+ . try_with ( |mutex| mutex. clone ( ) )
300+ . expect ( "No effect context found" )
301+ }
302+
291303 fn with_context < T , F : FnOnce ( & mut Self ) -> T > ( f : F ) -> T {
292304 APPLY_EFFECTS_CONTEXT
293305 . try_with ( |mutex| f ( & mut mutex. lock ( ) ) )
0 commit comments