mobx#transaction JavaScript Examples

The following examples show how to use mobx#transaction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: action-context-initializer.js    From albedo with MIT License 5 votes vote down vote up
/**
 * Set current context based on the request params.
 * @param {Object} params - Intent request parameters received from a caller app.
 * @return {Promise}
 */
export async function setActionContext(params) {
    function reject(error) {
        actionContext.setIntentError(error)
        return actionContext.rejectRequest()
    }

    try {
        //first reset context properties
        actionContext.reset()
        //retrieve known parameters
        const {intent, __albedo_intent_version, app_origin, __reqid, ...intentParams} = params
        //resolve caller app origin
        actionContext.origin = (app_origin || '').toLowerCase().replace(/https?:\/\//, '')
        actionContext.requestId = __reqid
        //resolve Stellar network properties for provided intent parameters
        const networkParams = resolveNetworkParams(intentParams)

        const actionContextParams = {
            status: ActionContextStatus.pending,
            selectedAccount: accountManager.activeAccount,
            networkParams
        }

        //intent should be present
        if (!intent)
            return reject('Parameter "intent" is required.')

        //validate requested public key if provided by a caller
        if (intentParams.pubkey) {
            if (!StrKey.isValidEd25519PublicKey(intentParams.pubkey))
                return reject('Invalid "pubkey" parameter. Stellar account public key expected.')
            actionContextParams.requestedPubkey = intentParams.pubkey
            //set requested account as selected if it matches stored account
            actionContextParams.selectedAccount = accountManager.get(intentParams.pubkey)
        }

        //set basic intent params for further references
        actionContextParams.intentParams = {intent, ...intentParams}

        //parse and validate provided intent request
        const isBatchRequest = intent === 'batch'
        let requests
        if (isBatchRequest) {
            requests = extractBatchedIntents(intentParams.intents, networkParams, actionContextParams)
            if (typeof requests === 'string') return reject(requests)
        } else {
            requests = [parseIntentRequest(intent, intentParams, networkParams, actionContextParams)]
        }

        //set nested intent requests
        actionContextParams.intentRequests = await Promise.all(requests)

        //check for errors within nested params
        const intentErrors = aggregateIntentErrors(actionContextParams.intentRequests.map(r => r.intentErrors), isBatchRequest)
        if (intentErrors)
            return reject(intentErrors)

        //check whether we deal with an implicit intent
        if (isImplicitIntentRequested({intent, ...intentParams})) {
            //try to find corresponding session
            actionContextParams.implicitSession = await restoreImplicitSession(intentParams.session)
        }

        //update action context parameters
        transaction(() => Object.assign(actionContext, actionContextParams))

        if (actionContext.implicitSession) {
            actionContext.selectAccount(Account.ephemeral(actionContext.implicitSession.secret))
        }
    } catch (e) {
        return reject(e)
    }
}