feat: put entire mutation in transaction

This commit is contained in:
Adhiraj Singh
2022-06-05 14:00:09 +05:30
parent 6f48bbb736
commit b7d8401f62

View File

@@ -481,7 +481,12 @@ export const makeChatsSocket = (config: SocketConfig) => {
throw new Boom('App state key not present!', { statusCode: 400 })
}
let initial: LTHashState
let encodeResult: { patch: proto.ISyncdPatch, state: LTHashState }
await mutationMutex.mutex(
async() => {
await authState.keys.transaction(
async() => {
logger.debug({ patch: patchCreate }, 'applying app patch')
@@ -490,12 +495,13 @@ export const makeChatsSocket = (config: SocketConfig) => {
let { [name]: initial } = await authState.keys.get('app-state-sync-version', [name])
initial = initial || newLTHashState()
const { patch, state } = await encodeSyncdPatch(
encodeResult = await encodeSyncdPatch(
patchCreate,
myAppStateKeyId,
initial,
getAppStateSyncKey,
)
const { patch, state } = encodeResult
const node: BinaryNode = {
tag: 'iq',
@@ -531,14 +537,16 @@ export const makeChatsSocket = (config: SocketConfig) => {
await query(node)
await authState.keys.set({ 'app-state-sync-version': { [name]: state } })
if(config.emitOwnEvents) {
const result = await decodePatches(name, [{ ...patch, version: { version: state.version }, }], initial, getAppStateSyncKey)
processSyncActionsLocal(result.newMutations)
}
}
)
}
)
if(config.emitOwnEvents) {
const result = await decodePatches(name, [{ ...encodeResult.patch, version: { version: encodeResult.state.version }, }], initial, getAppStateSyncKey)
processSyncActionsLocal(result.newMutations)
}
}
/** sending abt props may fix QR scan fail if server expects */
const fetchAbt = async() => {
@@ -645,12 +653,10 @@ export const makeChatsSocket = (config: SocketConfig) => {
const update = getBinaryNodeChild(node, 'collection')
if(update) {
const name = update.attrs.name as WAPatchName
mutationMutex.mutex(
async() => {
await resyncAppState([name])
mutationMutex.mutex(() => (
resyncAppState([name])
.catch(err => logger.error({ trace: err.stack, node }, 'failed to sync state'))
}
)
))
}
})