Skip to content

Commit 30c9c24

Browse files
committed
Add alternative helper for bypassing file amendments.
1 parent 4f92be9 commit 30c9c24

File tree

3 files changed

+1019
-0
lines changed

3 files changed

+1019
-0
lines changed

react-native/services/SyncableStateHelper/index.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,37 @@ export class SyncableStateHelper<
108108
})
109109
}
110110
}
111+
112+
/**
113+
* Inserts or updates a collection item. Does NOT manage changes to files. You likely want "upsertCollection", not this.
114+
* 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.
115+
* @param state The current state.
116+
* @param collectionKey The key of the collection in which to make changes.
117+
* @param uuid The UUID of the collection item to insert or update.
118+
* @param data The data of the collection item to insert or update.
119+
* @param setState Called when the next state has been computed.
120+
*/
121+
upsertCollectionWithoutAmendingFiles<T extends keyof TSchema['collections']>(
122+
state: SyncableState<TSchema>,
123+
collectionKey: T,
124+
uuid: string,
125+
data: TSchema['collections'][T],
126+
setState: (to: SyncableState<TSchema>) => void
127+
): void {
128+
const collection = state.collections[collectionKey]
129+
130+
setState({
131+
...state,
132+
collections: {
133+
...state.collections,
134+
[collectionKey]: {
135+
...collection,
136+
[uuid]: {
137+
status: 'awaitingPush',
138+
data
139+
}
140+
}
141+
}
142+
})
143+
}
111144
}

react-native/services/SyncableStateHelper/readme.md

+20
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,23 @@ syncableStateHelper.upsertCollection(
1717
setState,
1818
);
1919
```
20+
21+
### Without amending files
22+
23+
A call to `upsertCollection` will enqueue file uploads and deletions based upon the differences between the new data and the previous data.
24+
25+
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:
26+
27+
```tsx
28+
import { SyncableStateHelper } from "react-native-app-helpers";
29+
30+
const syncableStateHelper = new SyncableStateHelper(syncConfiguration);
31+
32+
syncableStateHelper.upsertCollectionWithoutAmendingFiles(
33+
state,
34+
`exampleCollectionKey`,
35+
`76a7d202-7971-45d4-8fb6-586403a312b0`,
36+
`Example Data`,
37+
setState,
38+
);
39+
```

0 commit comments

Comments
 (0)