-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2795a3
commit c03264f
Showing
2 changed files
with
140 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// devtoolsDetector.ts | ||
export interface DevtoolsDetectorConfig { | ||
pollingIntervalSeconds: number | ||
maxMillisBeforeAckWhenClosed: number | ||
moreAnnoyingDebuggerStatements: number | ||
onDetectOpen?: () => void | ||
onDetectClose?: () => void | ||
startup: 'asap' | 'manual' | 'domContentLoaded' | ||
onCheckOpennessWhilePaused: 'returnStaleValue' | 'throw' | ||
} | ||
|
||
export interface DevtoolsDetector { | ||
config: DevtoolsDetectorConfig | ||
readonly isOpen: boolean | ||
paused: boolean | ||
} | ||
|
||
export const setupDevtoolsDetector = (): DevtoolsDetector => { | ||
const config: DevtoolsDetectorConfig = { | ||
pollingIntervalSeconds: 0.25, | ||
maxMillisBeforeAckWhenClosed: 100, | ||
moreAnnoyingDebuggerStatements: 1, | ||
onDetectOpen: undefined, | ||
onDetectClose: undefined, | ||
startup: 'asap', | ||
onCheckOpennessWhilePaused: 'returnStaleValue', | ||
} | ||
|
||
Object.seal(config) | ||
|
||
const heart = new Worker( | ||
URL.createObjectURL( | ||
new Blob( | ||
[ | ||
`"use strict"; | ||
onmessage = (ev) => { postMessage({ isOpenBeat: true }); | ||
debugger; | ||
for (let i = 0; i < ev.data.moreDebugs; i++) { | ||
debugger; | ||
} | ||
postMessage({ isOpenBeat: false }); | ||
};`, | ||
], | ||
{ type: 'text/javascript' } | ||
) | ||
) | ||
) | ||
|
||
let _isDevtoolsOpen = false | ||
let _isDetectorPaused = true | ||
let resolveVerdict: (value: boolean | null) => void = () => {} | ||
let nextPulse$: NodeJS.Timeout | number = NaN | ||
|
||
const onHeartMsg = (msg: MessageEvent<{ isOpenBeat: boolean }>) => { | ||
if (msg.data.isOpenBeat) { | ||
const p = new Promise<boolean | null>((_resolveVerdict) => { | ||
resolveVerdict = _resolveVerdict | ||
let wait$: NodeJS.Timeout | number = setTimeout(() => { | ||
wait$ = NaN | ||
resolveVerdict(true) | ||
}, config.maxMillisBeforeAckWhenClosed + 1) | ||
}) | ||
|
||
p.then((verdict) => { | ||
if (verdict === null) return | ||
if (verdict !== _isDevtoolsOpen) { | ||
_isDevtoolsOpen = verdict | ||
const cb = { true: config.onDetectOpen, false: config.onDetectClose }[ | ||
verdict + '' | ||
] | ||
if (cb) cb() | ||
} | ||
nextPulse$ = setTimeout(() => { | ||
nextPulse$ = NaN | ||
doOnePulse() | ||
}, config.pollingIntervalSeconds * 1000) | ||
}) | ||
} else { | ||
resolveVerdict(false) | ||
} | ||
} | ||
|
||
const doOnePulse = () => { | ||
heart.postMessage({ moreDebugs: config.moreAnnoyingDebuggerStatements }) | ||
} | ||
|
||
const detector: DevtoolsDetector = { | ||
config, | ||
get isOpen() { | ||
if (_isDetectorPaused && config.onCheckOpennessWhilePaused === 'throw') { | ||
throw new Error('`onCheckOpennessWhilePaused` is set to `"throw"`.') | ||
} | ||
return _isDevtoolsOpen | ||
}, | ||
get paused() { | ||
return _isDetectorPaused | ||
}, | ||
set paused(pause: boolean) { | ||
if (_isDetectorPaused === pause) return | ||
_isDetectorPaused = pause | ||
if (pause) { | ||
heart.removeEventListener('message', onHeartMsg as EventListener) | ||
clearTimeout(nextPulse$) | ||
nextPulse$ = NaN | ||
resolveVerdict(null) | ||
} else { | ||
heart.addEventListener('message', onHeartMsg as EventListener) | ||
doOnePulse() | ||
} | ||
}, | ||
} | ||
|
||
Object.freeze(detector) | ||
return detector | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters