import { AppEvent , State , Run , SelectedEvent , ActionEvent , FunctionEvent } from "./api" ; import { get , Path } from "./immutable" ; export function isValueDisplayExpanded ( state , path ) { const expanded = state . detailsPaneExpandedPaths [ path ] ; if ( typeof expanded === "boolean" ) { return expanded ; } return path . split ( "." ) . length < 4 ; } export function getLatestRunId ( state ) { const runs = state . runs ; if ( runs . length > 0 ) { return runs [ runs . length - 1 ] ; } throw new Error ( "No run found." ) ; } export function getRun ( state , runId ) { const run = state . runsById [ runId ] ; if ( ! run ) throw new Error ( `No run with id =${ runId }` ) ; return run ; } export function isSelectedEvent ( state , event ) { const selected = state . selectedEvent ; if ( ! selected || ! event ) { return false ; } const run = state . runsById [ selected . runId ] ; return run && run . eventsById [ selected . eventId ] === event ; } export function getSelectedEvent ( state , event = state . selectedEvent ) { if ( ! event ) { return null ; } return state . runsById [ event . runId ] . eventsById [ event . eventId ] ; } export function getLatestRun ( state ) { return state . runsById [ getLatestRunId ( state ) ] ; } export function canTravelToSelectedEvent ( state ) { const run = getLatestRun ( state ) ; const event = getSelectedEvent ( state ) ; if ( ! run || ! event ) { return false ; } if ( event . type === "action" ) { return event . stateAfter && event . stateAfter !== run . currentState ; } if ( event . type === "init" ) { return event . state !== run . currentState ; } return false ; } function getCallArgsText ( event ) { if ( event . type === "action" ) { if ( event . data === undefined ) { return "" ; } return JSON . stringify ( event . data ) ; } if ( event . args . length === 0 ) { return "" ; } const str = JSON . stringify ( event . args ) ; return str . substring ( 1 , str . length - 1 ) ; } function getCallNameText ( event ) { return ( event . type === "action" ? "actions." : "" ) + event . name ; } export function getCallText ( event ) { return `${ getCallNameText ( event ) }(${ getCallArgsText ( event ) })` ; } export function getArgsFromCallText ( event , text ) { const prefix = getCallNameText ( event ) + "(" ; const suffix = ")" ; if ( ! text . startsWith ( prefix ) || ! text . endsWith ( suffix ) ) { throw new Error ( `Call must be of the form: "${ prefix }(arg1, arg2, ...)"` ) ; } return JSON . parse ( "[" + text . substring ( prefix . length , text . length - 1 ) + "]" ) ; }