Skip to content

A full-featured download manager.

refactor: protocol manager init setup

Dr_rOotfddaa72ac67c
diff --git a/src/main/Application.js b/src/main/Application.js
index acbc2ae9..73b45fac 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -197,7 +197,10 @@ export default class Application extends EventEmitter {
if (is.dev() || is.mas()) {
return
}
- this.protocolManager = new ProtocolManager()
+ const protocols = this.configManager.getUserConfig('protocols', {})
+ this.protocolManager = new ProtocolManager({
+ protocols
+ })
}
handleProtocol (url) {
diff --git a/src/main/core/ProtocolManager.js b/src/main/core/ProtocolManager.js
index e62e3dc3..2f7a1021 100644
--- a/src/main/core/ProtocolManager.js
+++ b/src/main/core/ProtocolManager.js
@@ -9,20 +9,33 @@ export default class ProtocolManager extends EventEmitter {
super()
this.options = options
+ // package.json:build.protocols[].schemes[]
+ // options.protocols: { 'magnet': true, 'thunder': false }
+ this.protocols = {
+ mo: true,
+ motrix: true,
+ ...options.protocols
+ }
+
this.init()
}
init () {
- // package.json:build.protocols[].schemes[]
- if (!app.isDefaultProtocolClient('mo')) {
- app.setAsDefaultProtocolClient('mo')
- }
- if (!app.isDefaultProtocolClient('motrix')) {
- app.setAsDefaultProtocolClient('motrix')
- }
- if (!app.isDefaultProtocolClient('magnet')) {
- app.setAsDefaultProtocolClient('magnet')
- }
+ const { protocols } = this
+ this.setup(protocols)
+ }
+
+ setup (protocols) {
+ Object.keys(protocols).forEach((protocol) => {
+ const enabled = protocols[protocol]
+ if (enabled) {
+ if (!app.isDefaultProtocolClient(protocol)) {
+ app.setAsDefaultProtocolClient(protocol)
+ }
+ } else {
+ app.removeAsDefaultProtocolClient(protocol)
+ }
+ })
}
handle (url) {