@@ -104,26 +104,46 @@ export class FederationBuilderImpl<TContextData>
104104 const { FederationImpl } = await import ( "./middleware.ts" ) ;
105105 const f = new FederationImpl ( options ) ;
106106
107+ // In order to ensure `build()` can be called multiple times and
108+ // each instance does not share their state, we clone everything
109+ // that is mutable. This includes the router and callbacks.
110+
107111 // Assign the existing router instance but preserve the settings
108112 // Keep the original trailingSlashInsensitive configuration
109113 const trailingSlashInsensitiveValue = f . router . trailingSlashInsensitive ;
110- f . router = this . router ;
114+ f . router = this . router . clone ( ) ;
111115 f . router . trailingSlashInsensitive = trailingSlashInsensitiveValue ;
112116 f . _initializeRouter ( ) ;
113117
114- f . actorCallbacks = this . actorCallbacks ;
118+ f . actorCallbacks = this . actorCallbacks == null
119+ ? undefined
120+ : { ...this . actorCallbacks } ;
115121 f . nodeInfoDispatcher = this . nodeInfoDispatcher ;
116- f . objectCallbacks = this . objectCallbacks ;
117- f . objectTypeIds = this . objectTypeIds ;
122+ f . objectCallbacks = { ... this . objectCallbacks } ;
123+ f . objectTypeIds = { ... this . objectTypeIds } ;
118124 f . inboxPath = this . inboxPath ;
119- f . inboxCallbacks = this . inboxCallbacks ;
120- f . outboxCallbacks = this . outboxCallbacks ;
121- f . followingCallbacks = this . followingCallbacks ;
122- f . followersCallbacks = this . followersCallbacks ;
123- f . likedCallbacks = this . likedCallbacks ;
124- f . featuredCallbacks = this . featuredCallbacks ;
125- f . featuredTagsCallbacks = this . featuredTagsCallbacks ;
126- f . inboxListeners = this . inboxListeners ;
125+ f . inboxCallbacks = this . inboxCallbacks == null
126+ ? undefined
127+ : { ...this . inboxCallbacks } ;
128+ f . outboxCallbacks = this . outboxCallbacks == null
129+ ? undefined
130+ : { ...this . outboxCallbacks } ;
131+ f . followingCallbacks = this . followingCallbacks == null
132+ ? undefined
133+ : { ...this . followingCallbacks } ;
134+ f . followersCallbacks = this . followersCallbacks == null
135+ ? undefined
136+ : { ...this . followersCallbacks } ;
137+ f . likedCallbacks = this . likedCallbacks == null
138+ ? undefined
139+ : { ...this . likedCallbacks } ;
140+ f . featuredCallbacks = this . featuredCallbacks == null
141+ ? undefined
142+ : { ...this . featuredCallbacks } ;
143+ f . featuredTagsCallbacks = this . featuredTagsCallbacks == null
144+ ? undefined
145+ : { ...this . featuredTagsCallbacks } ;
146+ f . inboxListeners = this . inboxListeners ?. clone ( ) ;
127147 f . inboxErrorHandler = this . inboxErrorHandler ;
128148 f . sharedInboxKeyDispatcher = this . sharedInboxKeyDispatcher ;
129149 return f ;
0 commit comments