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 }) 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() => {
await authState.keys.transaction(
async() => { async() => {
logger.debug({ patch: patchCreate }, 'applying app patch') 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]) 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',
@@ -531,14 +537,16 @@ export const makeChatsSocket = (config: SocketConfig) => {
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 */
const fetchAbt = async() => { const fetchAbt = async() => {
@@ -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'))
} ))
)
} }
}) })