@@ -41,28 +41,34 @@ export const openDataKey = Symbol('text:opendata') as InjectionKey<
4141 * @param  props.relativePath Relative path to the file. 
4242 * @param  props.initialSession Initial session handed to the editor in direct editing 
4343 * @param  props.shareToken Share token of the file. 
44+  * @param  getBaseVersionEtag Async getter function for the base version etag. 
45+  * @param  setBaseVersionEtag Async setter function for the base version etag. 
4446 */ 
45- export  function  provideConnection ( props : { 
46- 	fileId : number 
47- 	relativePath : string 
48- 	initialSession ?: InitialData 
49- 	shareToken ?: string 
50- } )  { 
51- 	let  baseVersionEtag : string  |  undefined 
47+ export  function  provideConnection ( 
48+ 	props : { 
49+ 		fileId : number 
50+ 		relativePath : string 
51+ 		initialSession ?: InitialData 
52+ 		shareToken ?: string 
53+ 	} , 
54+ 	getBaseVersionEtag : ( )  =>  Promise < string  |  undefined > , 
55+ 	setBaseVersionEtag : ( val : string )  =>  Promise < string  |  undefined > , 
56+ )  { 
5257	const  connection  =  shallowRef < Connection  |  undefined > ( undefined ) 
5358	const  openData  =  shallowRef < OpenData  |  undefined > ( undefined ) 
5459	const  openConnection  =  async  ( )  =>  { 
60+ 		const  baseVersionEtag  =  await  getBaseVersionEtag ( ) 
5561		const  guestName  =  localStorage . getItem ( 'nick' )  ??  '' 
5662		const  {  connection : opened ,  data }  = 
57- 			openInitialSession ( props ) 
63+ 			openInitialSession ( props ,   baseVersionEtag ) 
5864			||  ( await  open ( { 
5965				fileId : props . fileId , 
6066				guestName, 
6167				token : props . shareToken , 
6268				filePath : props . relativePath , 
6369				baseVersionEtag, 
6470			} ) ) 
65- 		baseVersionEtag   =   data . document . baseVersionEtag 
71+ 		await   setBaseVersionEtag ( data . document . baseVersionEtag ) 
6672		connection . value  =  opened 
6773		openData . value  =  data 
6874		return  data 
@@ -84,14 +90,27 @@ export const useConnection = () => {
8490 * @param  props.relativePath Relative path to the file. 
8591 * @param  props.initialSession Initial session handed to the editor in direct editing 
8692 * @param  props.shareToken Share token of the file. 
93+  * @param  baseVersionEtag Etag from the last editing session. 
8794 */ 
88- function  openInitialSession ( props : { 
89- 	relativePath : string 
90- 	initialSession ?: InitialData 
91- 	shareToken ?: string 
92- } )  { 
95+ function  openInitialSession ( 
96+ 	props : { 
97+ 		relativePath : string 
98+ 		initialSession ?: InitialData 
99+ 		shareToken ?: string 
100+ 	} , 
101+ 	baseVersionEtag : string  |  undefined , 
102+ )  { 
93103	if  ( props . initialSession )  { 
94104		const  {  document,  session }  =  props . initialSession 
105+ 		if  ( baseVersionEtag  !==  document . baseVersionEtag )  { 
106+ 			throw  new  Error ( 
107+ 				'Base version etag did not match when opening initial session.' , 
108+ 			) 
109+ 			// In order to handle this properly we'd need to: 
110+ 			// * fetch the file content. 
111+ 			// * throw the same exception as a 409 response. 
112+ 			// * include the file content as `outsideChange` in the error. 
113+ 		} 
95114		const  connection  =  { 
96115			documentId : document . id , 
97116			sessionId : session . id , 
0 commit comments