Try to update profile after proxy is applied. Fix #148.

This commit is contained in:
FelisCatus 2015-01-02 21:25:41 +08:00
parent 436adb56f8
commit 0c6d9dde3c
2 changed files with 33 additions and 9 deletions

View File

@ -321,7 +321,9 @@ module.exports = exports =
else else
profile.pacUrl profile.pacUrl
update: (profile, data) -> update: (profile, data) ->
return false if profile.pacScript == data
profile.pacScript = data profile.pacScript = data
return true
'AutoDetectProfile': 'PacProfile' 'AutoDetectProfile': 'PacProfile'
'SwitchProfile': 'SwitchProfile':
includable: true includable: true
@ -428,7 +430,9 @@ module.exports = exports =
formatHandler = RuleList[format] formatHandler = RuleList[format]
if formatHandler.preprocess? if formatHandler.preprocess?
data = formatHandler.preprocess(data) data = formatHandler.preprocess(data)
return false if profile.ruleList == data
profile.ruleList = data profile.ruleList = data
return true
'SwitchyRuleListProfile': 'RuleListProfile' 'SwitchyRuleListProfile': 'RuleListProfile'
'AutoProxyRuleListProfile': 'RuleListProfile' 'AutoProxyRuleListProfile': 'RuleListProfile'
# coffeelint: enable=missing_fat_arrows # coffeelint: enable=missing_fat_arrows

View File

@ -237,7 +237,7 @@ class Options
when 'removed' when 'removed'
@applyProfile(@fallbackProfileName) @applyProfile(@fallbackProfileName)
when 'changed' when 'changed'
@applyProfile(@_currentProfileName) @applyProfile(@_currentProfileName, update: false)
else else
@_setAvailableProfiles() if profilesChanged @_setAvailableProfiles() if profilesChanged
if args?.persist ? true if args?.persist ? true
@ -372,6 +372,8 @@ class Options
# @param {?string} name The name of the profile, or null for default. # @param {?string} name The name of the profile, or null for default.
# @param {?{}} options Some options # @param {?{}} options Some options
# @param {bool=true} options.proxy Set proxy for the applied profile if true # @param {bool=true} options.proxy Set proxy for the applied profile if true
# @param {bool=true} options.update Try to update this profile and referenced
# profiles after the proxy is set.
# @param {bool=false} options.system Whether options is in system mode. # @param {bool=false} options.system Whether options is in system mode.
# @param {{}=undefined} options.reason will be passed to currentProfileChanged # @param {{}=undefined} options.reason will be passed to currentProfileChanged
# @returns {Promise} A promise which is fulfilled when the profile is applied. # @returns {Promise} A promise which is fulfilled when the profile is applied.
@ -418,9 +420,21 @@ class Options
@_watchingProfiles = OmegaPac.Profiles.allReferenceSet(@_tempProfile, @_watchingProfiles = OmegaPac.Profiles.allReferenceSet(@_tempProfile,
@_options, profileNotFound: @_profileNotFound.bind(this)) @_options, profileNotFound: @_profileNotFound.bind(this))
@applyProfileProxy(@_tempProfile, profile) applyProxy = @applyProfileProxy(@_tempProfile, profile)
else else
@applyProfileProxy(profile) applyProxy = @applyProfileProxy(profile)
return applyProxy if options? and options.update == false
applyProxy.then =>
return unless @_options['-downloadInterval'] > 0
return unless @_currentProfileName == profile.name
updateProfiles = []
for key, name of @_watchingProfiles
updateProfiles.push(name)
if updateProfiles.length > 0
@updateProfile(updateProfiles)
return applyProxy
###* ###*
# Get the current applied profile. # Get the current applied profile.
@ -500,17 +514,23 @@ class Options
@log.method('Options#updateProfile', this, arguments) @log.method('Options#updateProfile', this, arguments)
results = {} results = {}
OmegaPac.Profiles.each @_options, (key, profile) => OmegaPac.Profiles.each @_options, (key, profile) =>
return if name? and profile.name != name if name?
if Array.isArray(name)
return unless name.indexOf(profile.name) >= 0
else
return unless profile.name == name
url = OmegaPac.Profiles.updateUrl(profile) url = OmegaPac.Profiles.updateUrl(profile)
if url if url
results[key] = @fetchUrl(url, opt_bypass_cache).then((data) => results[key] = @fetchUrl(url, opt_bypass_cache).then((data) =>
profile = OmegaPac.Profiles.byKey(key, @_options) profile = OmegaPac.Profiles.byKey(key, @_options)
OmegaPac.Profiles.update(profile, data)
profile.lastUpdate = new Date().toISOString() profile.lastUpdate = new Date().toISOString()
OmegaPac.Profiles.updateRevision(profile) if OmegaPac.Profiles.update(profile, data)
changes = {} OmegaPac.Profiles.updateRevision(profile)
changes[key] = profile changes = {}
@_setOptions(changes).return(profile) changes[key] = profile
@_setOptions(changes).return(profile)
else
return profile
).catch (reason) -> ).catch (reason) ->
if reason instanceof Error then reason else new Error(reason) if reason instanceof Error then reason else new Error(reason)