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,63 +481,71 @@ export const makeChatsSocket = (config: SocketConfig) => {
throw new Boom('App state key not present!', { statusCode: 400 }) throw new Boom('App state key not present!', { statusCode: 400 })
} }
let initial: LTHashState
let encodeResult: { patch: proto.ISyncdPatch, state: LTHashState }
await mutationMutex.mutex( await mutationMutex.mutex(
async() => { async() => {
logger.debug({ patch: patchCreate }, 'applying app patch') await authState.keys.transaction(
async() => {
logger.debug({ patch: patchCreate }, 'applying app patch')
await resyncAppState([name]) await resyncAppState([name])
let { [name]: initial } = await authState.keys.get('app-state-sync-version', [name]) let { [name]: initial } = await authState.keys.get('app-state-sync-version', [name])
initial = initial || newLTHashState() initial = initial || newLTHashState()
const { patch, state } = await encodeSyncdPatch( encodeResult = await encodeSyncdPatch(
patchCreate, patchCreate,
myAppStateKeyId, myAppStateKeyId,
initial, initial,
getAppStateSyncKey, getAppStateSyncKey,
) )
const { patch, state } = encodeResult
const node: BinaryNode = { const node: BinaryNode = {
tag: 'iq', tag: 'iq',
attrs: { attrs: {
to: S_WHATSAPP_NET, to: S_WHATSAPP_NET,
type: 'set', type: 'set',
xmlns: 'w:sync:app:state' xmlns: 'w:sync:app:state'
}, },
content: [
{
tag: 'sync',
attrs: { },
content: [ content: [
{ {
tag: 'collection', tag: 'sync',
attrs: { attrs: { },
name,
version: (state.version - 1).toString(),
return_snapshot: 'false'
},
content: [ content: [
{ {
tag: 'patch', tag: 'collection',
attrs: { }, attrs: {
content: proto.SyncdPatch.encode(patch).finish() name,
version: (state.version - 1).toString(),
return_snapshot: 'false'
},
content: [
{
tag: 'patch',
attrs: { },
content: proto.SyncdPatch.encode(patch).finish()
}
]
} }
] ]
} }
] ]
} }
] await query(node)
}
await query(node)
await authState.keys.set({ 'app-state-sync-version': { [name]: state } }) 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 */ /** sending abt props may fix QR scan fail if server expects */
@@ -645,12 +653,10 @@ export const makeChatsSocket = (config: SocketConfig) => {
const update = getBinaryNodeChild(node, 'collection') const update = getBinaryNodeChild(node, 'collection')
if(update) { if(update) {
const name = update.attrs.name as WAPatchName const name = update.attrs.name as WAPatchName
mutationMutex.mutex( mutationMutex.mutex(() => (
async() => { resyncAppState([name])
await resyncAppState([name]) .catch(err => logger.error({ trace: err.stack, node }, 'failed to sync state'))
.catch(err => logger.error({ trace: err.stack, node }, 'failed to sync state')) ))
}
)
} }
}) })