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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 212x 212x 212x 212x 212x 212x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 275x 275x 275x 275x 275x 275x 106x 106x 106x 106x 106x 106x 106x 106x 106x 233x 233x 233x 233x 233x 233x 106x 106x 106x 106x 106x 106x 106x 106x 230x 230x 230x 230x 230x 230x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 283x 283x 283x 283x 283x 283x 18x 18x 18x 5x 7x 5x 5x 7x 5x 18x 18x 18x 283x 283x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 26x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 305x 305x 305x 305x 305x 305x 305x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 1x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 1x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 1x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 2x 1x 1x 2x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 5x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 10x 5x 5x 5x 5x 5x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 13x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 8x 8x 8x 8x 2x 2x 6x 6x 8x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 5x 1x 1x 1x 5x 5x 5x 5x 5x 5x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 106x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 3x 4x 106x 106x | /*!
* WikiLambda Vue editor: Pinia store 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' );
const { performFunctionCall } = require( '../../utils/apiUtils.js' );
const {
getZObjectType,
createParserCall,
createRendererCall
} = require( '../../utils/zobjectUtils.js' );
module.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: [],
/**
* Cache of renderer data indexed by cache key.
* Cache key format: `${JSON.stringify(canonicalZobject)}|${rendererZid}|${zlang}`
* {
* <cacheKey>: <dataObject>
* // Full data object returned from performFunctionCall
* // with data.response.Z_RESPONSEENVELOPE_VALUE
* // and data.response.Z_RESPONSEENVELOPE_METADATA
* }
*/
rendererData: {},
/**
* Promises of pending renderer requests indexed by cache key.
* Cache key format: `${JSON.stringify(canonicalZobject)}|${rendererZid}|${zlang}`
* {
* <cacheKey>: <Promise>
* }
*/
rendererPromises: {}
},
getters: {
/**
* Returns the renderer Zid for a given type
* or undefined if it does not exist.
* TODO (T406159): 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}
*/
const findRendererZid = ( type ) => state.renderers[ type ];
return findRendererZid;
},
/**
* Returns the renderer Zid for a given type
* or undefined if it does not exist.
* TODO (T406159): 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}
*/
const findParserZid = ( type ) => state.parsers[ type ];
return findParserZid;
},
/**
* Returns whether the given type has a renderer in storage
*
* @param {Object} state
* @return {Function}
*/
hasRenderer: function ( state ) {
/**
* @param {string} type
* @return {boolean}
*/
const checkRenderer = ( type ) => type in state.renderers && !!state.renderers[ type ];
return checkRenderer;
},
/**
* Returns whether the given type has a parser in storage
*
* @param {Object} state
* @return {Function}
*/
hasParser: function ( state ) {
/**
* @param {string} type
* @return {boolean}
*/
const checkParser = ( type ) => type in state.parsers && !!state.parsers[ type ];
return checkParser;
},
/**
* 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}
*/
const findRendererExamples = ( 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 findRendererExamples;
},
/**
* 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 );
},
/**
* Returns the cached rendered value data for a given zobject, rendererZid, and zlang.
* Returns undefined if not cached.
*
* @param {Object} state
* @return {Function}
*/
getRendererData: function ( state ) {
/**
* @param {string} cacheKey
* @return {Object|undefined} Full data object with data.response.Z_RESPONSEENVELOPE_VALUE
*/
const findRendererData = ( cacheKey ) => state.rendererData[ cacheKey ];
return findRendererData;
}
},
actions: {
/**
* Add a running parser promise into the parserPromises
* state array.
*
* @param {Promise} promise
*/
addParserPromise: function ( promise ) {
this.parserPromises.push( promise );
},
/**
* Add renderer to the renderer collection
*
* @param {Object} payload
* @param {string} payload.type
* @param {string} payload.renderer
*/
setRenderer: function ( payload ) {
this.renderers[ payload.type ] = payload.renderer;
},
/**
* Add parser to the parser collection
*
* @param {Object} payload
* @param {string} payload.type
* @param {string} payload.parser
*/
setParser: function ( payload ) {
this.parsers[ payload.type ] = payload.parser;
},
/**
* Sets the value of a renderer example result
*
* @param {Object} payload
* @param {string} payload.rendererZid
* @param {string} payload.testZid
* @param {Promise|string} payload.example
*/
setRendererExample: function ( payload ) {
if ( !( payload.rendererZid in this.rendererExamples ) ) {
this.rendererExamples[ payload.rendererZid ] = {};
}
this.rendererExamples[ payload.rendererZid ][ payload.testZid ] = payload.example;
},
/**
* Stores a rendered value data in the cache.
*
* @param {Object} payload
* @param {string} payload.cacheKey
* @param {Object} payload.data Full data object returned from performFunctionCall
*/
setRendererData: function ( payload ) {
this.rendererData[ payload.cacheKey ] = payload.data;
},
/**
* Set or unset the unresolved promise to the renderer API call for a given cache key.
*
* @param {Object} payload
* @param {string} payload.cacheKey
* @param {Promise} payload.promise
*/
setRendererPromise: function ( payload ) {
if ( 'promise' in payload ) {
this.rendererPromises[ payload.cacheKey ] = payload.promise;
} else {
// Set as a resolved Promise if the tests for this function have been fetched
delete this.rendererPromises[ payload.cacheKey ];
}
},
/**
* Returns the cache key for a given renderer.
*
* @param {Object} payload
* @param {Object|Array|string} payload.zobject
* @param {string} payload.rendererZid
* @param {string} payload.zlang
* @return {string}
*/
getRendererCacheKey: function ( payload ) {
return `${ JSON.stringify( payload.zobject ) }|${ payload.rendererZid }|${ payload.zlang }`;
},
/**
* Given any Object/Z1 and a Language/Z60, it runs
* its renderer and returns the resulting Object.
* TODO (T406160): currently this will return a String/Z6 object,
* but in the future this can return other types)
*
* @param {Object} payload
* @param {string} payload.rendererZid
* @param {Object|Array|string} payload.zobject
* @param {string} payload.zlang
* @param {AbortSignal} payload.signal optional signal to abort the request
* @return {Promise}
*/
runRenderer: function ( payload ) {
// 1. Check cache first - if value is already cached, return a resolved promise immediately
const cacheKey = this.getRendererCacheKey( payload );
const cachedData = this.getRendererData( cacheKey );
if ( cachedData ) {
return Promise.resolve( cachedData );
}
// 2. If a renderer request is already in flight for this cache key, return that promise
if ( cacheKey in this.rendererPromises ) {
return this.rendererPromises[ cacheKey ];
}
// 3. Create a function call
const rendererCall = createRendererCall( payload );
// 4. Run this function call by calling wikilambda_function_call_zobject and return
const run = () => performFunctionCall( {
functionCall: rendererCall,
language: this.getUserLangCode,
signal: payload.signal
} ).then( ( data ) => {
this.setRendererData( {
cacheKey: cacheKey,
data
} );
this.setRendererPromise( { cacheKey } );
return data;
} ).catch( ( error ) => {
// Remove promise from store on error
this.setRendererPromise( { cacheKey } );
throw error;
} );
const job = this.enqueue( run );
// Store the promise so other components can wait for the same request
this.setRendererPromise( { cacheKey, promise: job.promise } );
return job.promise;
},
/**
* 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 (T406160): currently this will accept a String/Z6 object,
* but in the future it may accept other types)
*
* @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
* @param {AbortSignal} payload.signal optional signal to abort the request
* @return {Promise}
*/
runParser: function ( payload ) {
// 1. Create a function call
const parserCall = createParserCall( payload );
// 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
if ( payload.wait ) {
this.addParserPromise( parserPromise );
}
// 3. Run this function call by calling wikilambda_function_call_zobject
// and return the response and the Promise resolver function
const run = () => performFunctionCall( {
functionCall: parserCall,
language: this.getUserLangCode,
signal: payload.signal
} ).then( ( response ) => {
response.resolver = resolver;
return response;
} ).catch( ( e ) => {
resolver.resolve();
throw e;
} );
const job = this.enqueue( run );
return job.promise;
},
/**
* 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} payload
* @param {string} payload.rendererZid
* @param {string} payload.testZid
* @param {Object} payload.test
* @param {string} payload.zlang
* @return {Promise}
*/
runRendererTest: function ( payload ) {
// 1. If example is already rendered, ignore this
if ( this.getRendererExamples( payload.rendererZid, payload.testZid ).length > 0 ) {
return Promise.resolve();
}
// 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
this.setRendererExample( {
rendererZid: payload.rendererZid,
testZid: payload.testZid,
example: undefined
} );
// 4. Run renderer function
return performFunctionCall( {
functionCall: rendererCall,
language: this.getUserLangCode
} ).then( ( data ) => {
const response = data.response[ Constants.Z_RESPONSEENVELOPE_VALUE ];
if ( response !== Constants.Z_VOID && getZObjectType( response ) === Constants.Z_STRING ) {
// If rendered value from the test is valid, save in examples
this.setRendererExample( {
rendererZid: payload.rendererZid,
testZid: payload.testZid,
example: response
} );
}
} );
}
}
};
|