@@ -53,6 +53,7 @@ export type SpaceStorageEntry = {
5353
5454interface StoreContext {
5555 spaces : SpaceStorageEntry [ ] ;
56+ spacesLoadingIsPending : boolean ;
5657 updatesInFlight : string [ ] ;
5758 invitations : Invitation [ ] ;
5859 repo : Repo | null ;
@@ -73,6 +74,7 @@ interface StoreContext {
7374
7475const initialStoreContext : StoreContext = {
7576 spaces : [ ] ,
77+ spacesLoadingIsPending : true ,
7678 updatesInFlight : [ ] ,
7779 invitations : [ ] ,
7880 repo : null ,
@@ -90,7 +92,7 @@ type StoreEvent =
9092 | { type : 'reset' }
9193 | { type : 'addUpdateInFlight' ; updateId : string }
9294 | { type : 'removeUpdateInFlight' ; updateId : string }
93- | { type : 'setSpaceFromList ' ; spaceId : string ; name : string }
95+ | { type : 'setSpacesList ' ; spaces : { id : string ; name : string } [ ] }
9496 | { type : 'applyEvent' ; spaceId : string ; event : SpaceEvent ; state : SpaceState }
9597 | { type : 'updateConfirmed' ; spaceId : string ; clock : number }
9698 | { type : 'applyUpdate' ; spaceId : string ; firstUpdateClock : number ; lastUpdateClock : number }
@@ -182,58 +184,67 @@ export const store: Store<StoreContext, StoreEvent, GenericEventObject> = create
182184 updatesInFlight : context . updatesInFlight . filter ( ( id ) => id !== event . updateId ) ,
183185 } ;
184186 } ,
185- setSpaceFromList : ( context , event : { spaceId : string ; name : string } ) => {
187+ setSpacesList : ( context , event : { spaces : { id : string ; name : string } [ ] } ) => {
186188 if ( ! context . repo ) {
187189 return context ;
188190 }
189- const existingSpace = context . spaces . find ( ( s ) => s . id === event . spaceId ) ;
190- const lastUpdateClock = context . lastUpdateClock [ event . spaceId ] ?? - 1 ;
191- const result = context . repo . findWithProgress ( idToAutomergeId ( event . spaceId ) as AnyDocumentId ) ;
192191
193- // set it to ready to interact with the document
194- result . handle . doneLoading ( ) ;
192+ let storeContext : StoreContext = { ...context , spacesLoadingIsPending : false } ;
195193
196- if ( existingSpace ) {
197- return {
198- ...context ,
199- spaces : context . spaces . map ( ( existingSpace ) => {
200- if ( existingSpace . id === event . spaceId ) {
201- const newSpace : SpaceStorageEntry = {
202- id : existingSpace . id ,
203- name : existingSpace . name ,
204- events : existingSpace . events ?? [ ] ,
205- state : existingSpace . state ,
206- keys : existingSpace . keys ?? [ ] ,
207- automergeDocHandle : result . handle ,
208- inboxes : existingSpace . inboxes ?? [ ] ,
209- } ;
210- return newSpace ;
211- }
212- return existingSpace ;
213- } ) ,
194+ for ( const space of event . spaces ) {
195+ const existingSpace = context . spaces . find ( ( s ) => s . id === space . id ) ;
196+ const lastUpdateClock = context . lastUpdateClock [ space . id ] ?? - 1 ;
197+ const result = context . repo . findWithProgress ( idToAutomergeId ( space . id ) as AnyDocumentId ) ;
198+
199+ // set it to ready to interact with the document
200+ result . handle . doneLoading ( ) ;
201+
202+ if ( existingSpace ) {
203+ storeContext = {
204+ ...storeContext ,
205+ spaces : storeContext . spaces . map ( ( existingSpace ) => {
206+ if ( existingSpace . id === space . id ) {
207+ const newSpace : SpaceStorageEntry = {
208+ id : existingSpace . id ,
209+ name : existingSpace . name ,
210+ events : existingSpace . events ?? [ ] ,
211+ state : existingSpace . state ,
212+ keys : existingSpace . keys ?? [ ] ,
213+ automergeDocHandle : result . handle ,
214+ inboxes : existingSpace . inboxes ?? [ ] ,
215+ } ;
216+ return newSpace ;
217+ }
218+ return existingSpace ;
219+ } ) ,
220+ lastUpdateClock : {
221+ ...storeContext . lastUpdateClock ,
222+ [ space . id ] : lastUpdateClock ,
223+ } ,
224+ } ;
225+ }
226+ storeContext = {
227+ ...storeContext ,
228+ spaces : [
229+ ...storeContext . spaces ,
230+ {
231+ id : space . id ,
232+ name : space . name ,
233+ events : [ ] ,
234+ state : undefined ,
235+ keys : [ ] ,
236+ inboxes : [ ] ,
237+ automergeDocHandle : result . handle ,
238+ } ,
239+ ] ,
214240 lastUpdateClock : {
215- ...context . lastUpdateClock ,
216- [ event . spaceId ] : lastUpdateClock ,
241+ ...storeContext . lastUpdateClock ,
242+ [ space . id ] : - 1 ,
217243 } ,
218244 } ;
219245 }
220- return {
221- ...context ,
222- spaces : [
223- ...context . spaces ,
224- {
225- id : event . spaceId ,
226- name : event . name ,
227- events : [ ] ,
228- state : undefined ,
229- keys : [ ] ,
230- inboxes : [ ] ,
231- updates : [ ] ,
232- lastUpdateClock : - 1 ,
233- automergeDocHandle : result . handle ,
234- } ,
235- ] ,
236- } ;
246+
247+ return storeContext ;
237248 } ,
238249 applyEvent : ( context , event : { spaceId : string ; event : SpaceEvent ; state : SpaceState } ) => {
239250 return {
0 commit comments