All files / ext.wikilambda.edit/store/modules callZFunction.js

100% Statements 18/18
100% Branches 2/2
100% Functions 4/4
100% Lines 18/18

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              16x 16x 16x 16x   16x                         18x 18x   7x 7x     11x 11x 11x   18x                       3x   2x 2x     2x               1x                
/*!
 * WikiLambda Vue editor: Handle generating records in the ZObject list and storing records of function calls.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const Constants = require( '../../Constants.js' ),
	Row = require( '../classes/Row.js' ),
	extractZIDs = require( '../../mixins/schemata.js' ).methods.extractZIDs,
	performFunctionCall = require( '../../mixins/api.js' ).methods.performFunctionCall;
 
module.exports = exports = {
	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 performFunctionCall( payload.functionCall ).then( ( data ) => {
				// Asynchronously collect the necessary labels
				const zids = extractZIDs( data.response );
				context.dispatch( 'fetchZids', { zids } );
 
				// Success, we inject the response object in the resultRowId
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: payload.resultRowId,
					value: data.response
				} );
			} ).catch( ( error ) => {
				// Failure, we inject the error object in the resultRowId
				// FIXME: figure out how we handle API failures correctly,
				// do we get a zobject here, or an error message?
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: payload.resultRowId,
					value: error
				} );
			} );
		}
	}
};