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 | 1x 1x 1x 1x 113x 1x 141x 141x 141x 171x 171x 141x 141x 141x 141x 105x 105x 141x 17x 17x 30x 36x 6x 20x 315x 315x 315x 315x 315x 3x 3x 3x 7x 208x 208x 208x 208x 208x 4x 4x 4x 4x 4x 204x 204x 141x 310x 141x 310x 141x 296x 141x 308x 141x 242x 141x 3x 3x 171x 141x 105x 90x 105x 105x 141x 141x 141x 171x 63x 1x 37x 37x 80x 37x 124x 37x 7x 7x 37x | import { js2xml, xml2js, Element as XMLElement } from 'xml-js'; import { EventObject, ActionObject } from './types'; // import * as xstate from './index'; import { StateNode, Machine } from './index'; import { mapValues, getActionType, keys } from './utils'; import * as actions from './actions'; function getAttribute( element: XMLElement, attribute: string ): string | number | undefined { return element.attributes ? element.attributes[attribute] : undefined; } function stateNodeToSCXML(stateNode: StateNode) { const { parallel } = stateNode; const scxmlElement: XMLElement = { type: 'element', name: parallel ? 'parallel' : 'state', attributes: { id: stateNode.id }, elements: [ !parallel && stateNode.initial ? { type: 'element', name: 'initial', elements: [ { type: 'element', name: 'transition', attributes: { target: stateNode.states[stateNode.initial as string].id } } ] } : undefined, stateNode.onEntry && { type: 'element', name: 'onentry', elements: stateNode.onEntry.map(event => { return { type: 'element', name: 'send', attributes: { event: getActionType(event) } }; }) }, stateNode.onExit && { type: 'element', name: 'onexit', elements: stateNode.onExit.map(event => { return { type: 'element', name: 'send', attributes: { event: getActionType(event) } }; }) }, ...keys(stateNode.states).map(stateKey => { const subStateNode = stateNode.states[stateKey]; return stateNodeToSCXML(subStateNode); }), ...keys(stateNode.on) .map( (event): XMLElement[] => { const transition = stateNode.on![event]; if (!transition) { return []; } return transition.map(targetTransition => { const { target } = targetTransition; return { type: 'element', name: 'transition', attributes: { ...(event ? { event } : undefined), ...(target ? { target: stateNode.parent!.getRelativeStateNodes( target )[0]!.id } : undefined), // TODO: fixme ...(targetTransition.cond ? { cond: targetTransition.cond.toString() } : undefined) }, elements: targetTransition.actions ? targetTransition.actions.map(action => ({ type: 'element', name: 'send', attributes: { event: getActionType(action) } })) : undefined }; }); } ) .reduce((a, b) => a.concat(b)) ].filter(Boolean) as XMLElement[] }; return scxmlElement; } export function fromMachine(machine: StateNode): string { const scxmlDocument: XMLElement = { declaration: { attributes: { version: '1.0', encoding: 'utf-8' } }, elements: [ { type: 'instruction', name: 'access-control', instruction: 'allow="*"' }, { type: 'element', name: 'scxml', attributes: { version: '1.0', initial: machine.id } }, stateNodeToSCXML(machine) ] }; return js2xml(scxmlDocument, { spaces: 2 }); } function indexedRecord<T extends {}>( items: T[], identifier: string | ((item: T) => string) ): Record<string, T> { const record: Record<string, T> = {}; const identifierFn = typeof identifier === 'string' ? item => item[identifier] : identifier; items.forEach(item => { const key = identifierFn(item); record[key] = item; }); return record; } function indexedAggregateRecord<T extends {}>( items: T[], identifier: string | ((item: T) => string) ): Record<string, T[]> { const record: Record<string, T[]> = {}; const identifierFn = typeof identifier === 'string' ? item => item[identifier] : identifier; items.forEach(item => { const key = identifierFn(item); (record[key] = record[key] || ([] as T[])).push(item); }); return record; } function executableContent(elements: XMLElement[]) { const transition: any = { actions: mapActions(elements) }; return transition; } function mapActions<TContext extends object>( elements: XMLElement[] ): Array<ActionObject<TContext>> { return elements.map(element => { switch (element.name) { case 'raise': return actions.raise(element.attributes!.event! as string); case 'assign': return actions.assign(xs => { const literalKeyExprs = xs ? keys(xs) .map(key => `const ${key} = xs['${key}'];`) .join('\n') : ''; const fnStr = ` const xs = arguments[0]; ${literalKeyExprs}; return {'${element.attributes!.location}': ${ element.attributes!.expr }}; `; const fn = new Function(fnStr); return fn(xs); }); case 'send': const delay = element.attributes!.delay!; const numberDelay = delay ? typeof delay === 'number' ? delay : /(\d+)ms/.test(delay) ? +/(\d+)ms/.exec(delay)![1] : 0 : 0; return actions.send(element.attributes!.event! as string, { delay: numberDelay }); default: return { type: 'not-implemented' }; } }); } function toConfig( nodeJson: XMLElement, id: string, options: ScxmlToMachineOptions, extState?: {} ) { const { evalCond } = options; const parallel = nodeJson.name === 'parallel'; let initial = parallel ? undefined : nodeJson.attributes!.initial; let states: Record<string, any>; let on: Record<string, any>; const { elements } = nodeJson; switch (nodeJson.name) { case 'history': { Iif (!elements) { return { id, history: nodeJson.attributes!.type || 'shallow' }; } const [transitionElement] = elements.filter( element => element.name === 'transition' ); const target = getAttribute(transitionElement, 'target'); const history = getAttribute(nodeJson, 'type') || 'shallow'; return { id, history, target: target ? `#${target}` : undefined }; } default: break; } if (nodeJson.elements) { const stateElements = nodeJson.elements.filter( element => element.name === 'state' || element.name === 'parallel' || element.name === 'history' ); const transitionElements = nodeJson.elements.filter( element => element.name === 'transition' ); const onEntryElement = nodeJson.elements.find( element => element.name === 'onentry' ); const onExitElement = nodeJson.elements.find( element => element.name === 'onexit' ); const initialElement = !initial ? nodeJson.elements.find(element => element.name === 'initial') : undefined; if (initialElement && initialElement.elements!.length) { initial = initialElement.elements!.find( element => element.name === 'transition' )!.attributes!.target; } states = indexedRecord(stateElements, item => `${item.attributes!.id}`); on = mapValues( indexedAggregateRecord( transitionElements, item => (item.attributes ? item.attributes.event || '' : '') as string ), (values: XMLElement[]) => { return values.map(value => { const target = getAttribute(value, 'target'); return { target: target ? `#${target}` : undefined, ...(value.elements ? executableContent(value.elements) : undefined), ...(value.attributes && value.attributes.cond ? { cond: evalCond(value.attributes.cond as string, extState) } : undefined) }; }); } ); const onEntry = onEntryElement ? mapActions(onEntryElement.elements!) : undefined; const onExit = onExitElement ? mapActions(onExitElement.elements!) : undefined; return { id, ...(initial ? { initial } : undefined), ...(parallel ? { parallel } : undefined), ...(stateElements.length ? { states: mapValues(states, (state, key) => toConfig(state, key, options, extState) ) } : undefined), ...(transitionElements.length ? { on } : undefined), ...(onEntry ? { onEntry } : undefined), ...(onExit ? { onExit } : undefined) }; } return { id }; } export interface ScxmlToMachineOptions { evalCond: ( expr: string, extState?: object ) => // tslint:disable-next-line:ban-types ((extState: any, event: EventObject) => boolean) | Function; delimiter?: string; } export function toMachine( xml: string, options: ScxmlToMachineOptions ): StateNode { const json = xml2js(xml) as XMLElement; const machineElement = json.elements!.filter( element => element.name === 'scxml' )[0]; const dataModelEl = machineElement.elements!.filter( element => element.name === 'datamodel' )[0]; const extState = dataModelEl ? dataModelEl.elements!.reduce((acc, element) => { acc[element.attributes!.id!] = element.attributes!.expr; return acc; }, {}) : undefined; return Machine( { ...toConfig(machineElement, '(machine)', options, extState), delimiter: options.delimiter }, undefined, extState ); } |