Repository: trpc/trpc
File: packages/client/src/links/httpSubscriptionLink.ts (TypeScript)

Code before:
                eventSource: eventSource!.getEventSource(),
              },
            });
          };
          eventSource.addEventListener('open', onStarted, { once: true });

          const iterable = sseStreamConsumer<
            Partial<{
              id?: string;
              data: unknown;
            }>
          >({
            from: eventSource,
            deserialize: transformer.output.deserialize,
            tryHandleError: async (ev) => {
              if (
                'status' in ev &&
                typeof ev.status === 'number' &&
                typeof opts.shouldRecreateOnError === 'function' &&
                eventSource
              ) {
                let recreateOnErrorOpts: RecreateOnErrorOpt = {
                  type: 'raw',
                  event: ev,
                };
                if ('status' in ev && typeof ev.status === 'number') {
                  recreateOnErrorOpts = {
                    type: 'http-error',
                    status: ev.status,
                    event: ev,
                  };
                }

                const shouldRestart = await opts.shouldRecreateOnError(
                  recreateOnErrorOpts,
                );
                if (!shouldRestart) {
                  return false;
                }

                if (typeof opts.eventSourceOptions !== 'function') {
                  // eslint-disable-next-line no-console
                  console.warn(
                    'shouldRecreateOnError has returned true but eventSourceOptions is not a function. This means eventSourceOptions will not be updated',
                  );
                }

                eventSource.restart(
                  url,
                  await resultOf(opts.eventSourceOptions),
                );

Review comment (suggestion):
Avoid nested `if`s like this, this code is very hard to follow

do an early return or something instead

Code after:
                eventSource: eventSource!.getEventSource(),
              },
            });
          };
          eventSource.addEventListener('open', onStarted, { once: true });

          const iterable = sseStreamConsumer<
            Partial<{
              id?: string;
              data: unknown;
            }>
          >({
            from: eventSource,
            deserialize: transformer.output.deserialize,
            tryHandleError: async (ev) => {
              if (
                typeof opts.shouldRecreateOnError !== 'function' ||
                !eventSource
              ) {
                return false;
              }

              const recreateOnErrorOpts = createRecreateOnErrorOpts(ev);

              const shouldRestart = await opts.shouldRecreateOnError(
                recreateOnErrorOpts,
              );

              if (!shouldRestart) {
                return false;
              }

              eventSource.restart(
                getUrl({
                  transformer,
                  url: await urlWithConnectionParams(opts),
                  input,
                  path,
                  type,
                  signal: null,
                }),
                await resultOf(opts.eventSourceOptions),
              );

              return true;
            },
          });

          for await (const chunk of iterable) {
            if (!chunk.ok) {
              // TODO: handle in https://github.com/trpc/trpc/issues/5871