All files / ext.wikilambda.app/store/modules functionCall.js

97.84% Statements 91/93
92.85% Branches 13/14
100% Functions 6/6
97.84% Lines 91/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 947x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 7x 7x 1x     1x 1x 1x 7x 7x 7x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 16x 16x 16x 6x 6x 6x 14x 10x 10x 10x 10x 10x 16x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 3x 3x 7x 7x  
/*!
 * WikiLambda Vue editor: Handle the request of function calls to the orchestrator.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const Constants = require( '../../Constants.js' ),
	Row = require( '../classes/Row.js' ),
	{ extractZIDs, hybridToCanonical } = require( '../../mixins/schemata.js' ).methods,
	apiUtils = require( '../../mixins/api.js' ).methods;
 
module.exports = exports = {
	state: {
		metadata: undefined
	},
	getters: {
		getMetadata: function ( state ) {
			return state.metadata;
		},
		getMetadataError: function ( state ) {
			if ( !state.metadata ) {
				return undefined;
			}
			const map = state.metadata[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ].slice( 1 );
			return map.find( ( item ) => item[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ] === 'errors' );
		}
	},
	mutations: {
		setMetadata: function ( state, metadata ) {
			state.metadata = metadata;
		}
	},
	actions: {
		/**
		 * Create a new result record, that is detached from the main zObject.
		 * If the given record exists, it resets it by removing all children.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 *
		 * @return {number} resultId
		 */
		initializeResultId: function ( context, payload ) {
			let rowId;
			const row = context.getters.getRowById( payload );
			if ( row ) {
				// payload is a valid row Id, remove its children
				rowId = row.id;
				context.dispatch( 'removeRowChildren', { rowId } );
			} else {
				// payload is not a row ID, add new rowId
				rowId = context.getters.getNextRowId;
				const rootRow = new Row( rowId, undefined, Constants.ROW_VALUE_OBJECT, undefined );
				context.commit( 'pushRow', rootRow );
			}
			return rowId;
		},
		/**
		 * Calls orchestrator and sets orchestrationResult
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Object} payload.functionCall
		 * @param {Object} payload.resultRowId
		 * @return {Promise}
		 */
		callZFunction: function ( context, payload ) {
			return apiUtils.performFunctionCall( payload.functionCall ).then( ( data ) => {
				// Asynchronously collect the necessary labels
				const zids = extractZIDs( data.response );
				context.dispatch( 'fetchZids', { zids } );
 
				// Success:
				// We save the metadata JSON object, and
				const metadata = hybridToCanonical( data.response.Z22K2 );
				context.commit( 'setMetadata', metadata );
				// We inject the response object in the resultRowId
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: payload.resultRowId,
					value: data.response.Z22K1
				} );
 
			} ).catch( ( /* ApiError */ error ) => {
				context.commit( 'setError', {
					rowId: payload.resultRowId,
					errorType: Constants.errorTypes.ERROR,
					errorMessage: error.messageOrFallback( Constants.errorCodes.UNKNOWN_EXEC_ERROR )
				} );
			} );
		}
	}
};