//#region src/stream/projections/runtime.ts
/**
* Shared async subscription lifecycle for projection runtimes.
*/
function openProjectionSubscription({ thread, channels, namespace, depth = 1, resumeOnPause = false, onSubscribe, onEvent, onFinally }) {
	let handle;
	let disposed = false;
	const start = async () => {
		try {
			const subscribeParams = {
				channels: [...channels],
				namespaces: namespace.length > 0 ? [[...namespace]] : [[]],
				depth
			};
			const subscription = await thread.subscribe(subscribeParams);
			handle = subscription;
			if (disposed) {
				await subscription.unsubscribe();
				return;
			}
			onSubscribe?.();
			do {
				for await (const event of subscription) {
					if (disposed) break;
					onEvent(event);
				}
				if (disposed || !resumeOnPause || !subscription.isPaused) break;
				await subscription.waitForResume();
			} while (!disposed);
		} catch {} finally {
			onFinally?.();
		}
	};
	start();
	return { async dispose() {
		disposed = true;
		try {
			await handle?.unsubscribe();
		} catch {}
	} };
}
//#endregion
exports.openProjectionSubscription = openProjectionSubscription;

//# sourceMappingURL=runtime.cjs.map