@iebh/tera-fy 2.2.6 → 2.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,7 +20,7 @@ import {
20
20
  import marshal from '@momsfriendlydevco/marshal';
21
21
  import {nanoid} from 'nanoid';
22
22
  import PromiseRetry from 'p-retry';
23
- import {FirebaseApp} from 'firebase/app';
23
+ import {FirebaseApp, FirebaseError} from 'firebase/app';
24
24
  import { BoundSupabaseyFunction } from '@iebh/supabasey';
25
25
 
26
26
 
@@ -696,7 +696,7 @@ export default class Syncro {
696
696
  *
697
697
  * @returns {Promise} A promise which resolves when the operation has completed
698
698
  */
699
- setFirestoreState(state: any, options?: { method?: 'merge' | 'set' }): Promise<void> {
699
+ async setFirestoreState(state: any, options?: { method?: 'merge' | 'set' }, retries = 0): Promise<void> {
700
700
  let settings = {
701
701
  method: 'merge',
702
702
  ...options,
@@ -705,10 +705,29 @@ export default class Syncro {
705
705
 
706
706
  const firestoreData = Syncro.toFirestore(state);
707
707
 
708
- if (settings.method === 'merge') {
709
- return FirestoreUpdateDoc(this.docRef, firestoreData);
710
- } else { // method === 'set'
711
- return FirestoreSetDoc(this.docRef, firestoreData);
708
+ try {
709
+ if (settings.method === 'merge') {
710
+ return await FirestoreUpdateDoc(this.docRef, firestoreData);
711
+ } else { // method === 'set'
712
+ return await FirestoreSetDoc(this.docRef, firestoreData);
713
+ }
714
+ } catch(e) {
715
+ if (e instanceof FirebaseError && e.code === 'not-found') {
716
+ if (retries < 3) {
717
+ console.warn('Firebase syncro document does not exist during document update, reinitializing...');
718
+ // TODO: Reinitialize the firestore syncro document
719
+ const response = await fetch(`${this.config.syncroRegistryUrl}/${this.path}?force=1`);
720
+ if (!response.ok) {
721
+ console.error('Failed to reinitialize Syncro');
722
+ }
723
+ // Retry the request
724
+ return await this.setFirestoreState(state, options, retries + 1);
725
+ } else {
726
+ console.warn('Max retries exceeded while trying to recover firestore syncro document, throwing error')
727
+ }
728
+ }
729
+ console.error(`Error during Firestore operation (${settings.method}) on doc: ${this.docRef.path}`, e);
730
+ throw e;
712
731
  }
713
732
  }
714
733
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/tera-fy",
3
- "version": "2.2.6",
3
+ "version": "2.2.7",
4
4
  "description": "TERA website worker",
5
5
  "scripts": {
6
6
  "dev": "esbuild --platform=browser --format=esm --bundle lib/terafy.client.ts --outfile=dist/terafy.js --minify --serve --servedir=.",