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

58.33% Statements 7/12
0% Branches 0/4
80% Functions 4/5
58.33% Lines 7/12

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              2x   2x                                                         2x   1x   1x               1x                           2x                  
/*!
 * 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
 */
 
var performFunctionCall = require( '../../mixins/api.js' ).methods.performFunctionCall;
 
module.exports = exports = {
	actions: {
		/**
		 * Create a new result record, that is detached from the main zObject (using parent -1)
		 * if a record exist, it reset it by removing all childrens
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 *
		 * @return {number} resultId
		 */
		initializeResultId: function ( context, payload ) {
			var resultId = payload || context.getters.getNextObjectId;
			if ( resultId === context.getters.getNextObjectId ) {
				context.commit( 'addZObject', { id: resultId, key: undefined, parent: -1, value: 'object' } );
			} else {
				context.dispatch( 'removeZObjectChildren', resultId );
			}
 
			return resultId;
		},
		/**
		 * Calls orchestrator and sets orchestrationResult
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @return {Promise}
		 */
		callZFunction: function ( context, payload ) {
			return performFunctionCall( payload.zobject ).then( function ( data ) {
 
				var canonicalZObject = data.response;
 
				context.dispatch(
					'addZFunctionResultToTree',
					{
						result: canonicalZObject,
						resultId: payload.resultId
					}
				);
			} ).catch( function ( error ) {
				context.dispatch( 'addZFunctionResultToTree', {
					result: error,
					resultId: payload.resultId
				} );
			} );
		},
		/**
		 * attach a function result to the zObject tree.
		 * The result can either be a zObject or an error.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 */
		addZFunctionResultToTree: function ( context, payload ) {
			context.dispatch( 'injectZObject', {
				zobject: payload.result,
				key: '',
				id: payload.resultId,
				parent: ''
			} );
		}
	}
};