@@ -13,7 +13,7 @@ export type ExecutionResultWithErrors<Data> = Omit<ExecutionResult<Data, unknown
1313 errors : ExecutionResult [ 'errors' ] ;
1414} ;
1515
16- export type EffectCallback < Result , Variables > = ( {
16+ export type OnDataEffectCallback < Result , Variables > = ( {
1717 operation,
1818 result,
1919 variables,
@@ -23,6 +23,14 @@ export type EffectCallback<Result, Variables> = ({
2323 variables ?: Variables ;
2424} ) => void ;
2525
26+ export type OnCompleteEffectCallback < Result , Variables > = ( {
27+ operation,
28+ variables,
29+ } : {
30+ operation : StringDocumentNode < Result , Variables > ;
31+ variables ?: Variables ;
32+ } ) => void ;
33+
2634export function GraphQLReactWS < ConnectionInitPayload extends Record < string , unknown > > ( {
2735 graphqlWsOptions,
2836} : {
@@ -204,12 +212,12 @@ export function GraphQLReactWS<ConnectionInitPayload extends Record<string, unkn
204212 next : result => {
205213 generator . resolveNext ( result ) ;
206214
207- const effects = effectsStore [ query ] ;
215+ const effects = effectsOnDataStore [ query ] ;
208216
209217 if ( effects && result . data ) {
210218 for ( const effect of effects ) {
211219 try {
212- Promise . all ( [
220+ Promise . resolve (
213221 effect ( {
214222 operation : query as StringDocumentNode < unknown , unknown > ,
215223 result : {
@@ -218,7 +226,7 @@ export function GraphQLReactWS<ConnectionInitPayload extends Record<string, unkn
218226 } ,
219227 variables,
220228 } ) ,
221- ] ) . catch ( ( ) => null ) ;
229+ ) . catch ( ( ) => null ) ;
222230 } catch ( err ) { }
223231 }
224232 }
@@ -231,6 +239,21 @@ export function GraphQLReactWS<ConnectionInitPayload extends Record<string, unkn
231239 complete : ( ) => {
232240 generator . resolveCompleted ( ) ;
233241
242+ const effects = effectsOnCompleteStore [ query ] ;
243+
244+ if ( effects ) {
245+ for ( const effect of effects ) {
246+ try {
247+ Promise . resolve (
248+ effect ( {
249+ operation : query as StringDocumentNode < unknown , unknown > ,
250+ variables,
251+ } ) ,
252+ ) . catch ( ( ) => null ) ;
253+ } catch ( err ) { }
254+ }
255+ }
256+
234257 cleanupGenerator ( ) ;
235258 } ,
236259 } ) ;
@@ -259,7 +282,12 @@ export function GraphQLReactWS<ConnectionInitPayload extends Record<string, unkn
259282 subscriptionsControllers : Set < AbortController > ;
260283 } ;
261284
262- const effectsStore : Record < string , Set < EffectCallback < unknown , unknown > > | null > = { } ;
285+ const effectsOnDataStore : Record < string , Set < OnDataEffectCallback < unknown , unknown > > | null > = { } ;
286+
287+ const effectsOnCompleteStore : Record <
288+ string ,
289+ Set < OnCompleteEffectCallback < unknown , unknown > > | null
290+ > = { } ;
263291
264292 const Effects = {
265293 /**
@@ -268,26 +296,54 @@ export function GraphQLReactWS<ConnectionInitPayload extends Record<string, unkn
268296 * It returns a callback that's going to stop the effect from being called
269297 *
270298 * @example
271- * Effects.onCompleted (TestQuery, ({ operation, result: { data }, variables }) => {
299+ * Effects.onData (TestQuery, ({ operation, result: { data }, variables }) => {
272300 * console.log({
273301 * operation,
274302 * data,
275303 * variables
276304 * });
277305 * });
278306 */
279- onCompleted < Result , Variables > (
307+ onData < Result , Variables > (
308+ operation : StringDocumentNode < Result , Variables > ,
309+ callback : OnDataEffectCallback < Result , Variables > ,
310+ ) {
311+ const effects = ( effectsOnDataStore [ operation ] ||= new Set ( ) ) ;
312+
313+ effects . add ( callback as OnDataEffectCallback < unknown , unknown > ) ;
314+
315+ return function removeEffect ( ) {
316+ effects . delete ( callback as OnDataEffectCallback < unknown , unknown > ) ;
317+
318+ if ( effects . size === 0 ) effectsOnDataStore [ operation ] = null ;
319+ } ;
320+ } ,
321+
322+ /**
323+ * Add an effect callback to be called every time the specified operation was flagged as complete or stopped
324+ *
325+ * It returns a callback that's going to stop the effect from being called
326+ *
327+ * @example
328+ * Effects.onComplete(TestQuery, ({ operation, variables }) => {
329+ * console.log({
330+ * operation,
331+ * variables
332+ * });
333+ * });
334+ */
335+ onComplete < Result , Variables > (
280336 operation : StringDocumentNode < Result , Variables > ,
281- callback : EffectCallback < Result , Variables > ,
337+ callback : OnCompleteEffectCallback < Result , Variables > ,
282338 ) {
283- const effects = ( effectsStore [ operation ] ||= new Set ( ) ) ;
339+ const effects = ( effectsOnCompleteStore [ operation ] ||= new Set ( ) ) ;
284340
285- effects . add ( callback as EffectCallback < unknown , unknown > ) ;
341+ effects . add ( callback as OnCompleteEffectCallback < unknown , unknown > ) ;
286342
287343 return function removeEffect ( ) {
288- effects . delete ( callback as EffectCallback < unknown , unknown > ) ;
344+ effects . delete ( callback as OnCompleteEffectCallback < unknown , unknown > ) ;
289345
290- if ( effects . size === 0 ) effectsStore [ operation ] = null ;
346+ if ( effects . size === 0 ) effectsOnCompleteStore [ operation ] = null ;
291347 } ;
292348 } ,
293349 } as const ;
0 commit comments