Skip to content

Commit

Permalink
Add alternative helper for bypassing file amendments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Dec 11, 2023
1 parent 4f92be9 commit 30c9c24
Show file tree
Hide file tree
Showing 3 changed files with 1,019 additions and 0 deletions.
33 changes: 33 additions & 0 deletions react-native/services/SyncableStateHelper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,37 @@ export class SyncableStateHelper<
})
}
}

/**
* Inserts or updates a collection item. Does NOT manage changes to files. You likely want "upsertCollection", not this.
* This has one known use case: that you are soft-deleting a child record of the item, and that the sync system should NOT attempt to delete its associated media.
* @param state The current state.
* @param collectionKey The key of the collection in which to make changes.
* @param uuid The UUID of the collection item to insert or update.
* @param data The data of the collection item to insert or update.
* @param setState Called when the next state has been computed.
*/
upsertCollectionWithoutAmendingFiles<T extends keyof TSchema['collections']>(
state: SyncableState<TSchema>,
collectionKey: T,
uuid: string,
data: TSchema['collections'][T],
setState: (to: SyncableState<TSchema>) => void
): void {
const collection = state.collections[collectionKey]

setState({
...state,
collections: {
...state.collections,
[collectionKey]: {
...collection,
[uuid]: {
status: 'awaitingPush',
data
}
}
}
})
}
}
20 changes: 20 additions & 0 deletions react-native/services/SyncableStateHelper/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ syncableStateHelper.upsertCollection(
setState,
);
```

### Without amending files

A call to `upsertCollection` will enqueue file uploads and deletions based upon the differences between the new data and the previous data.

There are some rare cases in which this should be skipped; for example, if a record had embedded child records with files, removal of one such child record should not lead the sync system to start trying to delete its associated files from the server. Use `upsertCollectionWithoutAmendingFiles` to handle this:

```tsx
import { SyncableStateHelper } from "react-native-app-helpers";

const syncableStateHelper = new SyncableStateHelper(syncConfiguration);

syncableStateHelper.upsertCollectionWithoutAmendingFiles(
state,
`exampleCollectionKey`,
`76a7d202-7971-45d4-8fb6-586403a312b0`,
`Example Data`,
setState,
);
```
Loading

0 comments on commit 30c9c24

Please sign in to comment.