MCPcopy
hub / github.com/streamlit/streamlit / handleScriptFinished

Method handleScriptFinished

frontend/app/src/App.tsx:1593–1658  ·  view source on GitHub ↗

* Handler for ForwardMsg.scriptFinished messages * @param status the ScriptFinishedStatus that the script finished with

(status: ForwardMsg.ScriptFinishedStatus)

Source from the content-addressed store, hash-verified

1591 * @param status the ScriptFinishedStatus that the script finished with
1592 */
1593 handleScriptFinished(status: ForwardMsg.ScriptFinishedStatus): void {
1594 if (
1595 status === ForwardMsg.ScriptFinishedStatus.FINISHED_SUCCESSFULLY ||
1596 status === ForwardMsg.ScriptFinishedStatus.FINISHED_EARLY_FOR_RERUN ||
1597 status ===
1598 ForwardMsg.ScriptFinishedStatus.FINISHED_FRAGMENT_RUN_SUCCESSFULLY
1599 ) {
1600 // eslint-disable-next-line @typescript-eslint/no-floating-promises -- TODO: Fix this
1601 Promise.resolve().then(() => {
1602 // Notify any subscribers of this event (and do it on the next cycle of
1603 // the event loop)
1604 this.state.scriptFinishedHandlers.forEach(handler => handler())
1605 })
1606
1607 if (
1608 status === ForwardMsg.ScriptFinishedStatus.FINISHED_SUCCESSFULLY ||
1609 status ===
1610 ForwardMsg.ScriptFinishedStatus.FINISHED_FRAGMENT_RUN_SUCCESSFULLY
1611 ) {
1612 // Clear any stale elements left over from the previous run.
1613 // We only do that for completed runs, not for runs that were finished early
1614 // due to reruns; this is to avoid flickering of elements where they disappear for
1615 // a moment and then are readded by a new session. After the new session finished,
1616 // leftover elements will be cleared after finished successfully.
1617 // We also don't do this if our script had a compilation error and didn't
1618 // finish successfully.
1619 this.setState(
1620 ({ scriptRunId, fragmentIdsThisRun, elements }) => {
1621 return {
1622 // Apply any pending elements that haven't been applied.
1623 elements: elements.clearStaleNodes(
1624 scriptRunId,
1625 fragmentIdsThisRun
1626 ),
1627 }
1628 },
1629 () => {
1630 this.removeInactiveWidgetState()
1631 }
1632 )
1633 }
1634
1635 // Tell the ConnectionManager to increment the message cache run
1636 // count. This will result in expired ForwardMsgs being removed from
1637 // the cache. We expect the sessionInfo to be populated at this point,
1638 // but we have observed race conditions tied to a rerun occurring
1639 // before a NewSession message is processed. This issue should not
1640 // disrupt users and is not a critical need for the message cache
1641 if (
1642 this.connectionManager !== null &&
1643 status !== ForwardMsg.ScriptFinishedStatus.FINISHED_EARLY_FOR_RERUN &&
1644 this.sessionInfo.isSet &&
1645 // We only increment the message cache run count if we have received
1646 // a NewSession message after the latest rerun request. This is done
1647 // to ignore finished messages from previous script runs, which would
1648 // cause issues with deleting cached messages that are needed for the
1649 // current script run.
1650 this.hasReceivedNewSession

Callers 1

AppClass · 0.95

Calls 5

handlerFunction · 0.85
forEachMethod · 0.80
clearStaleNodesMethod · 0.80

Tested by

no test coverage detected