Skip to content

Commit 13c6edf

Browse files
committed
Merge remote-tracking branch 'origin/main' into sanitize-github-ref/in-deployment
2 parents e50a755 + f3d5739 commit 13c6edf

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55

66
## [Unreleased]
77

8+
### Added
9+
- The web component `runCompleted` event now returns `duration: null` if the host page's tab loses focus during the code run (#192)
10+
811
### Changed
912
- Deploy branches to a single-level in the directory hierarchy (#194)
1013

cypress/e2e/missionZero-wc.cy.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,34 @@ it("picks up errors from the editor", () => {
9595
cy.get("editor-wc").shadow().find(".btn--run").click()
9696
cy.get("#results").should("contain", '"isErrorFree":false')
9797
})
98+
99+
it("does not return null duration if no change in focus", () => {
100+
cy.get("editor-wc").shadow().find("div[class=cm-content]").invoke('text', 'from sense_hat import SenseHat\nsense = SenseHat()\nsense.send_message("a")')
101+
cy.get("editor-wc").shadow().find(".btn--run").click()
102+
cy.get('#results').should("not.contain", '"duration":null')
103+
})
104+
105+
it("does not return null duration if focus changed before code run", () => {
106+
cy.get("editor-wc").shadow().find("div[class=cm-content]").invoke('text', 'from sense_hat import SenseHat\nsense = SenseHat()\nsense.send_message("a")')
107+
cy.window().blur()
108+
cy.window().focus()
109+
cy.get("editor-wc").shadow().find(".btn--run").click()
110+
cy.get('#results').should("not.contain", '"duration":null')
111+
})
112+
113+
it("returns duration of null if focus is lost", () => {
114+
cy.get("editor-wc").shadow().find("div[class=cm-content]").invoke('text', 'from sense_hat import SenseHat\nsense = SenseHat()\nsense.send_message("a")')
115+
cy.get("editor-wc").shadow().find(".btn--run").click()
116+
cy.window().blur()
117+
cy.window().focus()
118+
cy.get('#results').should("contain", '"duration":null')
119+
})
120+
121+
it("does not return duration of null if code rerun after focus lost", () => {
122+
cy.get("editor-wc").shadow().find("div[class=cm-content]").invoke('text', 'from sense_hat import SenseHat\nsense = SenseHat()\nsense.send_message("a")')
123+
cy.get("editor-wc").shadow().find(".btn--run").click()
124+
cy.window().blur()
125+
cy.window().focus()
126+
cy.get("editor-wc").shadow().find(".btn--run").click()
127+
cy.get('#results').should("not.contain", '"duration":null')
128+
})

src/components/AstroPiModel/AstroPiControls/Stopwatch.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react'
1+
import React, { useEffect, useState } from 'react'
22
import { useSelector } from 'react-redux';
33
import { useStopwatch } from 'react-timer-hook';
44
import Sk from 'skulpt'
@@ -12,16 +12,24 @@ const Stopwatch = () => {
1212
pause,
1313
reset
1414
} = useStopwatch({ autoStart: false })
15+
const [hasLostFocus, setHasLostFocus] = useState(false)
16+
17+
useEffect(() => {
18+
window.addEventListener('blur', () => {
19+
setHasLostFocus(true)
20+
})
21+
}, [])
1522

1623
useEffect(() => {
1724
if (codeRunTriggered && !isRunning) {
25+
setHasLostFocus(false)
1826
reset()
1927
}
2028
if (!codeRunTriggered && isRunning){
2129
pause()
22-
Sk.sense_hat.mz_criteria.duration = minutes * 60 + seconds
30+
Sk.sense_hat.mz_criteria.duration = hasLostFocus ? null : minutes * 60 + seconds
2331
}
24-
}, [codeRunTriggered, minutes, seconds, isRunning, pause, reset])
32+
}, [codeRunTriggered, hasLostFocus, minutes, seconds, isRunning, pause, reset])
2533

2634

2735
return (

0 commit comments

Comments
 (0)