@@ -2,12 +2,27 @@ import { useAtomValue } from "@effect/atom-react";
22import type { DesktopBridge , DesktopUpdateState } from "@t3tools/contracts" ;
33import * as Effect from "effect/Effect" ;
44import * as Queue from "effect/Queue" ;
5+ import * as Schema from "effect/Schema" ;
56import * as Stream from "effect/Stream" ;
67import * as AsyncResult from "effect/unstable/reactivity/AsyncResult" ;
78import { Atom } from "effect/unstable/reactivity" ;
89
910type DesktopUpdateBridge = Pick < DesktopBridge , "getUpdateState" | "onUpdateState" > ;
1011
12+ const INITIAL_STATE_READ_ATTEMPT_COUNT = 3 ;
13+
14+ export class DesktopUpdateStateReadError extends Schema . TaggedErrorClass < DesktopUpdateStateReadError > ( ) (
15+ "DesktopUpdateStateReadError" ,
16+ {
17+ attemptCount : Schema . Number ,
18+ cause : Schema . Defect ( ) ,
19+ } ,
20+ ) {
21+ override get message ( ) : string {
22+ return `Failed to read the initial desktop update state after ${ this . attemptCount } attempts.` ;
23+ }
24+ }
25+
1126function getDesktopUpdateBridge ( ) : DesktopUpdateBridge | undefined {
1227 return typeof window === "undefined" ? undefined : window . desktopBridge ;
1328}
@@ -32,9 +47,23 @@ export function createDesktopUpdateStateAtom(getBridge: () => DesktopUpdateBridg
3247 ( unsubscribe ) => Effect . sync ( unsubscribe ) ,
3348 ) ;
3449
35- const initialState = yield * Effect . tryPromise ( ( ) => bridge . getUpdateState ( ) ) . pipe (
36- Effect . retry ( { times : 2 } ) ,
37- Effect . orElseSucceed ( ( ) => null ) ,
50+ const initialState = yield * Effect . tryPromise ( {
51+ try : ( ) => bridge . getUpdateState ( ) ,
52+ catch : ( cause ) =>
53+ new DesktopUpdateStateReadError ( {
54+ attemptCount : INITIAL_STATE_READ_ATTEMPT_COUNT ,
55+ cause,
56+ } ) ,
57+ } ) . pipe (
58+ Effect . retry ( { times : INITIAL_STATE_READ_ATTEMPT_COUNT - 1 } ) ,
59+ Effect . catchTags ( {
60+ DesktopUpdateStateReadError : ( error ) =>
61+ Effect . logError ( error . message , {
62+ errorTag : error . _tag ,
63+ attemptCount : error . attemptCount ,
64+ stack : error . stack ,
65+ } ) . pipe ( Effect . as ( null ) ) ,
66+ } ) ,
3867 ) ;
3968 if ( ! receivedUpdate && initialState !== null ) {
4069 Queue . offerUnsafe ( queue , initialState ) ;
0 commit comments