forked from docmirror/dev-sidecar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: Windows操作系统,开着DS应用重启电脑后无法上网的问题修复 (docmirror#377)
(cherry picked from commit 113fd10)
- Loading branch information
1 parent
865c158
commit 953c36b
Showing
4 changed files
with
175 additions
and
3 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
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
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,135 @@ | ||
import { powerMonitor as _powerMonitor } from 'electron' | ||
import { setMainWindowHandle, insertWndProcHook, removeWndProcHook, releaseShutdownBlock, acquireShutdownBlock } from '@natmri/platform-napi' | ||
|
||
class PowerMonitor { | ||
constructor () { | ||
this.setup = false | ||
this._listeners = [] | ||
this._shutdownCallback = null | ||
} | ||
|
||
/** | ||
* @param {BrowserWindow} window | ||
*/ | ||
setupMainWindow (window) { | ||
if (!this.setup) { | ||
setMainWindowHandle(window.getNativeWindowHandle().readBigInt64LE()) | ||
this.setup = true | ||
} | ||
} | ||
|
||
addListener (event, listener) { | ||
return this.on(event, listener) | ||
} | ||
|
||
removeListener (event, listener) { | ||
return this.off(event, listener) | ||
} | ||
|
||
removeAllListeners (event) { | ||
if (event === 'shutdown' && process.platform === 'win32') { | ||
this._listeners = [] | ||
if (this._shutdownCallback) { | ||
removeWndProcHook() | ||
releaseShutdownBlock() | ||
this._shutdownCallback = null | ||
} | ||
} else { | ||
return _powerMonitor.removeAllListeners(event) | ||
} | ||
} | ||
|
||
on (event, listener) { | ||
if (event === 'shutdown' && process.platform === 'win32') { | ||
if (!this._shutdownCallback) { | ||
this._shutdownCallback = async () => { | ||
await Promise.all(this._listeners.map((fn) => fn())) | ||
releaseShutdownBlock() | ||
} | ||
insertWndProcHook(this._shutdownCallback) | ||
acquireShutdownBlock('正在停止 DevSidecar 代理') | ||
} | ||
this._listeners.push(listener) | ||
} else { | ||
return _powerMonitor.on(event, listener) | ||
} | ||
} | ||
|
||
off (event, listener) { | ||
if (event === 'shutdown' && process.platform === 'win32') { | ||
this._listeners = this._listeners.filter((fn) => fn !== listener) | ||
} else { | ||
return _powerMonitor.off(event, listener) | ||
} | ||
} | ||
|
||
once (event, listener) { | ||
if (event === 'shutdown' && process.platform === 'win32') { | ||
return this.on(event, listener) | ||
} else { | ||
return _powerMonitor.once(event, listener) | ||
} | ||
} | ||
|
||
emit (event, ...args) { | ||
return _powerMonitor.emit(event, ...args) | ||
} | ||
|
||
eventNames () { | ||
return _powerMonitor.eventNames() | ||
} | ||
|
||
getMaxListeners () { | ||
return _powerMonitor.getMaxListeners() | ||
} | ||
|
||
listeners (event) { | ||
return _powerMonitor.listeners(event) | ||
} | ||
|
||
rawListeners (event) { | ||
return _powerMonitor.rawListeners(event) | ||
} | ||
|
||
listenerCount (event, listener) { | ||
return _powerMonitor.listenerCount(event, listener) | ||
} | ||
|
||
/** | ||
* @returns {boolean} | ||
*/ | ||
get onBatteryPower () { | ||
return _powerMonitor.onBatteryPower | ||
} | ||
|
||
/** | ||
* @param {number} idleThreshold | ||
* @returns {'active'|'idle'|'locked'|'unknown'} | ||
*/ | ||
getSystemIdleState (idleThreshold) { | ||
return _powerMonitor.getSystemIdleState(idleThreshold) | ||
} | ||
|
||
/** | ||
* @returns {number} | ||
*/ | ||
getSystemIdleTime () { | ||
return _powerMonitor.getSystemIdleTime() | ||
} | ||
|
||
/** | ||
* @returns {'unknown'|'nominal'|'fair'|'serious'|'critical'} | ||
*/ | ||
getCurrentThermalState () { | ||
return _powerMonitor.getCurrentThermalState() | ||
} | ||
|
||
/** | ||
* @returns {boolean} | ||
*/ | ||
isOnBatteryPower () { | ||
return _powerMonitor.isOnBatteryPower() | ||
} | ||
} | ||
|
||
export const powerMonitor = new PowerMonitor() |
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