@@ -11,7 +11,6 @@ import { showToast } from "@opencode-ai/ui/toast"
1111import { getFilename } from "@opencode-ai/util/path"
1212import {
1313 createContext ,
14- createEffect ,
1514 getOwner ,
1615 Match ,
1716 onCleanup ,
@@ -35,7 +34,6 @@ import { trimSessions } from "./global-sync/session-trim"
3534import type { ProjectMeta } from "./global-sync/types"
3635import { SESSION_RECENT_LIMIT } from "./global-sync/types"
3736import { sanitizeProject } from "./global-sync/utils"
38- import { usePlatform } from "./platform"
3937import { formatServerError } from "@/utils/server-errors"
4038
4139type GlobalStore = {
@@ -54,7 +52,6 @@ type GlobalStore = {
5452
5553function createGlobalSync ( ) {
5654 const globalSDK = useGlobalSDK ( )
57- const platform = usePlatform ( )
5855 const language = useLanguage ( )
5956 const owner = getOwner ( )
6057 if ( ! owner ) throw new Error ( "GlobalSync must be created within owner" )
@@ -64,7 +61,7 @@ function createGlobalSync() {
6461 const sessionLoads = new Map < string , Promise < void > > ( )
6562 const sessionMeta = new Map < string , { limit : number } > ( )
6663
67- const [ projectCache , setProjectCache , , projectCacheReady ] = persisted (
64+ const [ projectCache , setProjectCache , projectInit ] = persisted (
6865 Persist . global ( "globalSync.project" , [ "globalSync.project.v1" ] ) ,
6966 createStore ( { value : [ ] as Project [ ] } ) ,
7067 )
@@ -80,6 +77,57 @@ function createGlobalSync() {
8077 reload : undefined ,
8178 } )
8279
80+ let active = true
81+ let projectWritten = false
82+
83+ onCleanup ( ( ) => {
84+ active = false
85+ } )
86+
87+ const cacheProjects = ( ) => {
88+ setProjectCache (
89+ "value" ,
90+ untrack ( ( ) => globalStore . project . map ( sanitizeProject ) ) ,
91+ )
92+ }
93+
94+ const setProjects = ( next : Project [ ] | ( ( draft : Project [ ] ) => void ) ) => {
95+ projectWritten = true
96+ if ( typeof next === "function" ) {
97+ setGlobalStore ( "project" , produce ( next ) )
98+ cacheProjects ( )
99+ return
100+ }
101+ setGlobalStore ( "project" , next )
102+ cacheProjects ( )
103+ }
104+
105+ const setBootStore = ( ( ...input : unknown [ ] ) => {
106+ if ( input [ 0 ] === "project" && Array . isArray ( input [ 1 ] ) ) {
107+ setProjects ( input [ 1 ] as Project [ ] )
108+ return input [ 1 ]
109+ }
110+ return ( setGlobalStore as ( ...args : unknown [ ] ) => unknown ) ( ...input )
111+ } ) as typeof setGlobalStore
112+
113+ const set = ( ( ...input : unknown [ ] ) => {
114+ if ( input [ 0 ] === "project" && ( Array . isArray ( input [ 1 ] ) || typeof input [ 1 ] === "function" ) ) {
115+ setProjects ( input [ 1 ] as Project [ ] | ( ( draft : Project [ ] ) => void ) )
116+ return input [ 1 ]
117+ }
118+ return ( setGlobalStore as ( ...args : unknown [ ] ) => unknown ) ( ...input )
119+ } ) as typeof setGlobalStore
120+
121+ if ( projectInit instanceof Promise ) {
122+ void projectInit . then ( ( ) => {
123+ if ( ! active ) return
124+ if ( projectWritten ) return
125+ const cached = projectCache . value
126+ if ( cached . length === 0 ) return
127+ setGlobalStore ( "project" , cached )
128+ } )
129+ }
130+
83131 const setSessionTodo = ( sessionID : string , todos : Todo [ ] | undefined ) => {
84132 if ( ! sessionID ) return
85133 if ( ! todos ) {
@@ -127,30 +175,6 @@ function createGlobalSync() {
127175 return sdk
128176 }
129177
130- createEffect ( ( ) => {
131- if ( ! projectCacheReady ( ) ) return
132- if ( globalStore . project . length !== 0 ) return
133- const cached = projectCache . value
134- if ( cached . length === 0 ) return
135- setGlobalStore ( "project" , cached )
136- } )
137-
138- createEffect ( ( ) => {
139- if ( ! projectCacheReady ( ) ) return
140- const projects = globalStore . project
141- if ( projects . length === 0 ) {
142- const cachedLength = untrack ( ( ) => projectCache . value . length )
143- if ( cachedLength !== 0 ) return
144- }
145- setProjectCache ( "value" , projects . map ( sanitizeProject ) )
146- } )
147-
148- createEffect ( ( ) => {
149- if ( globalStore . reload !== "complete" ) return
150- setGlobalStore ( "reload" , undefined )
151- queue . refresh ( )
152- } )
153-
154178 async function loadSessions ( directory : string ) {
155179 const pending = sessionLoads . get ( directory )
156180 if ( pending ) return pending
@@ -259,13 +283,7 @@ function createGlobalSync() {
259283 event,
260284 project : globalStore . project ,
261285 refresh : queue . refresh ,
262- setGlobalProject ( next ) {
263- if ( typeof next === "function" ) {
264- setGlobalStore ( "project" , produce ( next ) )
265- return
266- }
267- setGlobalStore ( "project" , next )
268- } ,
286+ setGlobalProject : setProjects ,
269287 } )
270288 if ( event . type === "server.connected" || event . type === "global.disposed" ) {
271289 for ( const directory of Object . keys ( children . children ) ) {
@@ -316,7 +334,7 @@ function createGlobalSync() {
316334 unknownError : language . t ( "error.chain.unknown" ) ,
317335 invalidConfigurationError : language . t ( "error.server.invalidConfiguration" ) ,
318336 formatMoreCount : ( count ) => language . t ( "common.moreCountSuffix" , { count } ) ,
319- setGlobalStore,
337+ setGlobalStore : setBootStore ,
320338 } )
321339 }
322340
@@ -340,7 +358,9 @@ function createGlobalSync() {
340358 . update ( { config } )
341359 . then ( bootstrap )
342360 . then ( ( ) => {
343- setGlobalStore ( "reload" , "complete" )
361+ queue . refresh ( )
362+ setGlobalStore ( "reload" , undefined )
363+ queue . refresh ( )
344364 } )
345365 . catch ( ( error ) => {
346366 setGlobalStore ( "reload" , undefined )
@@ -350,7 +370,7 @@ function createGlobalSync() {
350370
351371 return {
352372 data : globalStore ,
353- set : setGlobalStore ,
373+ set,
354374 get ready ( ) {
355375 return globalStore . ready
356376 } ,
0 commit comments