11import * as Context from "effect/Context" ;
22import * as Effect from "effect/Effect" ;
33import * as Layer from "effect/Layer" ;
4- import * as Option from "effect/Option" ;
54import * as Ref from "effect/Ref" ;
65import * as Scope from "effect/Scope" ;
76
8- import type * as Electron from "electron" ;
7+ import * as Electron from "electron" ;
98
109import * as ElectronApp from "../electron/ElectronApp.ts" ;
1110import * as ElectronProtocol from "../electron/ElectronProtocol.ts" ;
@@ -41,19 +40,52 @@ export function extractPairingLink(
4140 return null ;
4241}
4342
43+ /**
44+ * macOS delivers a cold-start URL through `open-url` moments after launch,
45+ * before async startup reaches `DesktopPairingLink.register`. This listener is
46+ * installed with the synchronous pre-ready setup and holds URLs until the
47+ * service subscribes.
48+ */
49+ export class DesktopOpenUrls extends Context . Service <
50+ DesktopOpenUrls ,
51+ {
52+ readonly subscribe : ( listener : ( url : string ) => void ) => Effect . Effect < void > ;
53+ }
54+ > ( ) ( "@t3tools/desktop/app/DesktopPairingLink/DesktopOpenUrls" ) { }
55+
56+ export const layerOpenUrls = Layer . effect (
57+ DesktopOpenUrls ,
58+ Effect . sync ( ( ) => {
59+ const buffered : Array < string > = [ ] ;
60+ let subscriber : ( ( url : string ) => void ) | null = null ;
61+ Electron . app . on ( "open-url" , ( _event , url ) => {
62+ if ( subscriber === null ) {
63+ buffered . push ( url ) ;
64+ return ;
65+ }
66+ subscriber ( url ) ;
67+ } ) ;
68+ return DesktopOpenUrls . of ( {
69+ subscribe : ( listener ) =>
70+ Effect . sync ( ( ) => {
71+ subscriber = listener ;
72+ for ( const url of buffered . splice ( 0 ) ) listener ( url ) ;
73+ } ) ,
74+ } ) ;
75+ } ) ,
76+ ) ;
77+
4478export class DesktopPairingLink extends Context . Service <
4579 DesktopPairingLink ,
4680 {
47- /**
48- * Start listening for pairing deep links. Registers before `ready` so a
49- * macOS `open-url` delivered at cold start is not missed.
50- */
81+ /** Start collecting pairing deep links from the OS. */
5182 readonly register : Effect . Effect < void , never , Scope . Scope > ;
5283 /**
53- * The renderer signals when its coordinator is mounted; a link that
54- * arrived earlier (cold start) is delivered then.
84+ * Hand every queued link to the renderer, oldest first. Links only leave
85+ * the queue this way, so a renderer that is reloading or not yet mounted
86+ * cannot lose one; it drains the queue when its coordinator mounts.
5587 */
56- readonly setRendererReady : ( ready : boolean ) => Effect . Effect < void > ;
88+ readonly takePending : Effect . Effect < ReadonlyArray < string > > ;
5789 }
5890> ( ) ( "@t3tools/desktop/app/DesktopPairingLink" ) { }
5991
@@ -63,29 +95,19 @@ const { logInfo, logWarning } = makeComponentLogger("desktop-pairing-link");
6395export const make = Effect . gen ( function * ( ) {
6496 const environment = yield * DesktopEnvironment . DesktopEnvironment ;
6597 const electronApp = yield * ElectronApp . ElectronApp ;
98+ const openUrls = yield * DesktopOpenUrls ;
6699 const desktopWindow = yield * DesktopWindow . DesktopWindow ;
67100 const runPromise = Effect . runPromiseWith ( yield * Effect . context < never > ( ) ) ;
68101 const scheme = ElectronProtocol . getDesktopScheme ( environment . isDevelopment ) ;
69102
70- const pendingRef = yield * Ref . make ( Option . none < string > ( ) ) ;
71- const rendererReadyRef = yield * Ref . make ( false ) ;
72-
73- const deliver = Effect . fn ( "desktop.pairingLink.deliver" ) ( function * ( link : string ) {
74- yield * logInfo ( "delivering pairing link to renderer" ) ;
75- yield * desktopWindow . dispatchPairingLink ( link ) ;
76- } ) ;
103+ const queueRef = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
77104
78- // Reveal the window right away so the click feels acknowledged, but hold
79- // the link until the renderer says it can act on it .
105+ // Queue first, then nudge. A running renderer pulls on the nudge; one that
106+ // is still loading pulls when it mounts and finds the link waiting .
80107 const receive = Effect . fn ( "desktop.pairingLink.receive" ) ( function * ( link : string ) {
81- yield * Ref . set ( pendingRef , Option . some ( link ) ) ;
82- if ( yield * Ref . get ( rendererReadyRef ) ) {
83- yield * Ref . set ( pendingRef , Option . none ( ) ) ;
84- yield * deliver ( link ) ;
85- return ;
86- }
87- yield * logInfo ( "holding pairing link until renderer is ready" ) ;
88- yield * desktopWindow . activate ;
108+ yield * Ref . update ( queueRef , ( queue ) => [ ...queue , link ] ) ;
109+ yield * logInfo ( "queued pairing link" ) ;
110+ yield * desktopWindow . dispatchPairingLinkAvailable ;
89111 } ) ;
90112
91113 const receiveFromCandidates = ( candidates : ReadonlyArray < string > ) => {
@@ -99,9 +121,9 @@ export const make = Effect.gen(function* () {
99121 } ;
100122
101123 const register = Effect . gen ( function * ( ) {
102- // macOS hands URLs to the running (or launching) app via open-url. The
103- // Clerk bridge also listens here but only claims its own renderer origin.
104- yield * electronApp . on < [ Electron . Event , string ] > ( "open-url" , ( _event , url ) => {
124+ // macOS: open-url, including any buffered from before this service existed.
125+ // The Clerk bridge also listens here but only claims its own renderer origin.
126+ yield * openUrls . subscribe ( ( url ) => {
105127 receiveFromCandidates ( [ url ] ) ;
106128 } ) ;
107129 // Windows and Linux launch a second process with the URL in argv; the
@@ -116,20 +138,11 @@ export const make = Effect.gen(function* () {
116138 receiveFromCandidates ( process . argv ) ;
117139 } ) . pipe ( Effect . withSpan ( "desktop.pairingLink.register" ) ) ;
118140
119- const setRendererReady = Effect . fn ( "desktop.pairingLink.setRendererReady" ) ( function * (
120- ready : boolean ,
121- ) {
122- yield * Ref . set ( rendererReadyRef , ready ) ;
123- if ( ! ready ) return ;
124- const pending = yield * Ref . getAndSet ( pendingRef , Option . none ( ) ) ;
125- if ( Option . isSome ( pending ) ) {
126- yield * deliver ( pending . value ) . pipe (
127- Effect . catchCause ( ( cause ) => logWarning ( "failed to deliver pairing link" , { cause } ) ) ,
128- ) ;
129- }
130- } ) ;
141+ const takePending = Ref . getAndSet ( queueRef , [ ] ) . pipe (
142+ Effect . withSpan ( "desktop.pairingLink.takePending" ) ,
143+ ) ;
131144
132- return DesktopPairingLink . of ( { register, setRendererReady } ) ;
145+ return DesktopPairingLink . of ( { register, takePending } ) ;
133146} ) ;
134147
135148export const layer = Layer . effect ( DesktopPairingLink , make ) ;
0 commit comments