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

92.72% Statements 51/55
95.23% Branches 20/21
91.3% Functions 21/23
92.72% Lines 51/55

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338                  16x 16x 16x   16x                                                                                     3x   3x                                 1x   1x                           13x   8x                           3x   3x                               9x 9x 9x 5x 7x 5x       9x 3x     9x                         10x                                             1x                     1x                       2x 1x   2x                                                 1x 1x 1x 1x     1x                                                           1x 1x 1x 1x         1x 1x     1x           1x 1x 1x                                           4x 1x       3x 3x       3x             3x 3x 3x         2x                    
/*!
 * WikiLambda Vue editor: Vuex module for advanced type features.
 * Handles parsers and renderers.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ).methods,
	performFunctionCall = require( '../../mixins/api.js' ).methods.performFunctionCall;
 
module.exports = exports = {
	state: {
		/**
		 * Collection of renderers indexed by type
		 */
		renderers: {},
		/**
		 * Collection of parsers indexed by type
		 */
		parsers: {},
		/**
		 * Collection of renderer examples indexed by
		 * rendererZid and testerZid
		 * {
		 *  <rendererZid>: {
		 *    <testerZid>: <exampleResult>,
		 *    <testerZid>: <exampleResult>
		 *  }
		 * }
		 */
		rendererExamples: {},
		/**
		 * Array of promises of pending running parser
		 * functions
		 */
		parserPromises: []
	},
	getters: {
		/**
		 * Returns the renderer Zid for a given type
		 * or undefined if it does not exist.
		 * TODO: differentiate between "it hasn't been fetched" vs.
		 * "it has been fetched and it doesn't exist"
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getRendererZid: function ( state ) {
			/**
			 * @param {string} type
			 * @return {string | undefined}
			 */
			function findRenderer( type ) {
				return state.renderers[ type ];
			}
			return findRenderer;
		},
		/**
		 * Returns the renderer Zid for a given type
		 * or undefined if it does not exist.
		 * TODO: differentiate between "it hasn't been fetched" vs.
		 * "it has been fetched and it doesn't exist"
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getParserZid: function ( state ) {
			/**
			 * @param {string} type
			 * @return {string | undefined}
			 */
			function findParser( type ) {
				return state.parsers[ type ];
			}
			return findParser;
		},
		/**
		 * Returns whether the given type has a renderer in storage
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		hasRenderer: function ( state ) {
			/**
			 * @param {string} type
			 * @return {boolean}
			 */
			function typeHasRenderer( type ) {
				return ( ( type in state.renderers ) && !!state.renderers[ type ] );
			}
			return typeHasRenderer;
		},
		/**
		 * Returns whether the given type has a parser in storage
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		hasParser: function ( state ) {
			/**
			 * @param {string} type
			 * @return {boolean}
			 */
			function typeHasParser( type ) {
				return ( ( type in state.parsers ) && !!state.parsers[ type ] );
			}
			return typeHasParser;
		},
		/**
		 * Returns an array with the generated examples for a given renderer.
		 * If testZid is present as an argument, filters the results to return only that value.
		 *
		 * @param {Object} state
		 * @return {Function}
		 */
		getRendererExamples: function ( state ) {
			/**
			 * @param {string} rendererZid
			 * @param {string|undefined} testZid
			 * @return {Array}
			 */
			function getExamples( rendererZid, testZid = undefined ) {
				const filteredExamples = [];
				const examples = state.rendererExamples[ rendererZid ];
				if ( examples ) {
					for ( const key of Object.keys( examples ) ) {
						if ( examples[ key ] ) {
							filteredExamples.push( { testZid: key, result: examples[ key ] } );
						}
					}
				}
				return testZid ?
					filteredExamples.filter( ( item ) => item.testZid === testZid ) :
					filteredExamples;
			}
			return getExamples;
		},
		/**
		 * Returns a Promise that will be resolved when all pending
		 * parsers will finish running. Those which run successfully will
		 * be resolved once the parser response is persisted in the state.
		 * The ones which fail will be resolved immediately, as nothing is
		 * persisted in the state.
		 *
		 * @param {Object} state
		 * @return {Promise}
		 */
		waitForRunningParsers: function ( state ) {
			return Promise.all( state.parserPromises );
		}
	},
	mutations: {
		/**
		 * Add a running parser promise into the parserPromises
		 * state array.
		 *
		 * @param {Object} state
		 * @param {Promise} promise
		 */
		addParserPromise: function ( state, promise ) {
			state.parserPromises.push( promise );
		},
		/**
		 * Add renderer to the renderer collection
		 *
		 * @param {Object} state
		 * @param {Object} payload
		 * @param {string} payload.type
		 * @param {string} payload.renderer
		 */
		setRenderer: function ( state, payload ) {
			state.renderers[ payload.type ] = payload.renderer;
		},
		/**
		 * Add parser to the parser collection
		 *
		 * @param {Object} state
		 * @param {Object} payload
		 * @param {string} payload.type
		 * @param {string} payload.parser
		 */
		setParser: function ( state, payload ) {
			state.parsers[ payload.type ] = payload.parser;
		},
		/**
		 * Sets the value of a renderer example result
		 *
		 * @param {Object} state
		 * @param {Object} payload
		 * @param {string} payload.rendererZid
		 * @param {string} payload.testZid
		 * @param {Promise|string} payload.example
		 */
		setRendererExample: function ( state, payload ) {
			if ( !( payload.rendererZid in state.rendererExamples ) ) {
				state.rendererExamples[ payload.rendererZid ] = {};
			}
			state.rendererExamples[ payload.rendererZid ][ payload.testZid ] = payload.example;
		}
	},
	actions: {
		/**
		 * Given any Object/Z1 and a Language/Z60, it runs
		 * its renderer and returns the resulting Object.
		 * TODO: currently this will return a String/Z6 object,
		 * but in the future this can return other types)
		 *
		 * @param {Object} _context
		 * @param {Object} payload
		 * @param {string} payload.rendererZid
		 * @param {Object|Array|string} payload.zobject
		 * @param {string} payload.zlang
		 * @return {Promise}
		 */
		runRenderer: function ( _context, payload ) {
			// 1. Create a function call
			// {
			//   Z1K1: Z7,
			//   Z7K1: payload.rendererZid,
			//   <rendererZid>K1: payload.zobject,
			//   <rendererZid>K2: payload.zlang
			// }
			const rendererCall = typeUtils.getScaffolding( Constants.Z_FUNCTION_CALL );
			rendererCall[ Constants.Z_FUNCTION_CALL_FUNCTION ] = payload.rendererZid;
			rendererCall[ `${ payload.rendererZid }K1` ] = payload.zobject;
			rendererCall[ `${ payload.rendererZid }K2` ] = payload.zlang;
 
			// 2. Run this function call by calling wikilambda_function_call_zobject and return
			return performFunctionCall( rendererCall );
		},
		/**
		 * Given any Object/Z1 and a Language/Z60, it runs
		 * its parser and returns the resulting Object.
		 *
		 * Sometimes the response of the parser should be persisted in the
		 * store before other actions (like submission or call function)
		 * take place. The flag wait indicates whether the response of
		 * this parser function should be waited for.
		 *
		 * TODO: currently this will accept a String/Z6 object,
		 * but in the future it may accept other types)
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.parserZid
		 * @param {Object|Array|string} payload.zobject
		 * @param {string} payload.zlang
		 * @param {boolean} payload.wait whether this parser should block API calls
		 * @return {Promise}
		 */
		runParser: function ( context, payload ) {
			// 1. Create a function call
			// {
			//   Z1K1: Z7,
			//   Z7K1: <parserZid>,
			//   <parserZid>K1: payload.zobject,
			//   <parserZid>K2: payload.zlang
			// }
			const parserCall = typeUtils.getScaffolding( Constants.Z_FUNCTION_CALL );
			parserCall[ Constants.Z_FUNCTION_CALL_FUNCTION ] = payload.parserZid;
			parserCall[ `${ payload.parserZid }K1` ] = payload.zobject;
			parserCall[ `${ payload.parserZid }K2` ] = payload.zlang;
 
			// 2. Add the parser promise to the parserPromises state array
			// and keep the resolver function to be returned back to the caller.
			let resolver;
			const parserPromise = new Promise( ( resolve, reject ) => {
				resolver = { resolve, reject };
			} );
			// If we want this parser to block other API calls, we add to the parserPromise array
			Iif ( payload.wait ) {
				context.commit( 'addParserPromise', parserPromise );
			}
 
			// 3. Run this function call by calling wikilambda_function_call_zobject
			// and return the response and the Promise resolver function
			return performFunctionCall( parserCall ).then( ( response ) => {
				response.resolver = resolver;
				return response;
			} ).catch( ( e ) => {
				resolver.resolve();
				throw e;
			} );
		},
		/**
		 * Generates a renderer example by running its test with the
		 * current user language and saves it in the store for other
		 * similar fields to access it without re-running the same functions
		 * multiple times.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.rendererZid
		 * @param {string} payload.testZid
		 * @param {Object} payload.test
		 * @param {string} payload.zlang
		 * @return {Promise|void}
		 */
		runRendererTest: function ( context, payload ) {
			// 1. If example is already rendered, ignore this
			if ( context.getters.getRendererExamples( payload.rendererZid, payload.testZid ).length > 0 ) {
				return;
			}
 
			// 2. Build test object with current user lang Zid
			const rendererCall = payload.test[ Constants.Z_TESTER_CALL ];
			rendererCall[ `${ payload.rendererZid }K2` ] = payload.zlang;
 
			// 3. Save empty value in the store under the rendererZid.testZid
			// to avoid re-running the same example multiple times
			context.commit( 'setRendererExample', {
				rendererZid: payload.rendererZid,
				testZid: payload.testZid,
				example: undefined
			} );
 
			// 4. Run renderer function
			return performFunctionCall( rendererCall ).then( ( data ) => {
				const response = data.response[ Constants.Z_RESPONSEENVELOPE_VALUE ];
				if (
					( response !== Constants.Z_VOID ) &&
					( typeUtils.getZObjectType( response ) === Constants.Z_STRING )
				) {
					// If rendered value from the test is valid, save in examples
					context.commit( 'setRendererExample', {
						rendererZid: payload.rendererZid,
						testZid: payload.testZid,
						example: response
					} );
				}
			} );
		}
	}
};