{"version":3,"file":"tsrScript.cjs","sources":["../../src/tsrScript.ts?script-string"],"sourcesContent":["import type { ControllablePromise } from '@tanstack/react-router'\nimport type { StartSsrGlobal } from '@tanstack/start-client'\n\nconst __TSR_SSR__: StartSsrGlobal = {\n  matches: [],\n  streamedValues: {},\n  initMatch: (match) => {\n    __TSR_SSR__.matches.push(match)\n\n    match.extracted?.forEach((ex) => {\n      if (ex.type === 'stream') {\n        let controller\n        ex.value = new ReadableStream({\n          start(c) {\n            controller = {\n              enqueue: (chunk: unknown) => {\n                try {\n                  c.enqueue(chunk)\n                } catch {}\n              },\n              close: () => {\n                try {\n                  c.close()\n                } catch {}\n              },\n            }\n          },\n        })\n        ex.value.controller = controller\n      } else {\n        let resolve: ControllablePromise['reject'] | undefined\n        let reject: ControllablePromise['reject'] | undefined\n\n        ex.value = new Promise((_resolve, _reject) => {\n          reject = _reject\n          resolve = _resolve\n        }) as ControllablePromise\n        ex.value.reject = reject!\n        ex.value.resolve = resolve!\n      }\n    })\n\n    return true\n  },\n  resolvePromise: ({ matchId, id, promiseState }) => {\n    const match = __TSR_SSR__.matches.find((m) => m.id === matchId)\n    if (match) {\n      const ex = match.extracted?.[id]\n      if (\n        ex &&\n        ex.type === 'promise' &&\n        ex.value &&\n        promiseState.status === 'success'\n      ) {\n        ex.value.resolve(promiseState.data)\n        return true\n      }\n    }\n    return false\n  },\n  injectChunk: ({ matchId, id, chunk }) => {\n    const match = __TSR_SSR__.matches.find((m) => m.id === matchId)\n\n    if (match) {\n      const ex = match.extracted?.[id]\n      if (ex && ex.type === 'stream' && ex.value?.controller) {\n        ex.value.controller.enqueue(new TextEncoder().encode(chunk.toString()))\n        return true\n      }\n    }\n    return false\n  },\n  closeStream: ({ matchId, id }) => {\n    const match = __TSR_SSR__.matches.find((m) => m.id === matchId)\n    if (match) {\n      const ex = match.extracted?.[id]\n      if (ex && ex.type === 'stream' && ex.value?.controller) {\n        ex.value.controller.close()\n        return true\n      }\n    }\n    return false\n  },\n  cleanScripts: () => {\n    document.querySelectorAll('.tsr-once').forEach((el) => {\n      el.remove()\n    })\n  },\n}\n\nwindow.__TSR_SSR__ = __TSR_SSR__\n"],"names":[],"mappings":";AAAA,MAAA,6BAAe;;"}