Replace ScopeProperties with private SettingsFile class in package.js

This commit is contained in:
Max Brunsfeld 2018-08-24 13:53:32 -07:00
parent b2153a33dd
commit 231cc58e00
2 changed files with 39 additions and 33 deletions

View File

@ -7,7 +7,6 @@ const dedent = require('dedent')
const CompileCache = require('./compile-cache')
const ModuleCache = require('./module-cache')
const ScopedProperties = require('./scoped-properties')
const BufferedProcess = require('./buffered-process')
// Extended: Loads and activates a package's main module and resources such as
@ -103,7 +102,7 @@ class Package {
this.activateKeymaps()
this.activateMenus()
for (let settings of this.settings) {
settings.activate()
settings.activate(this.config)
}
this.settingsActivated = true
}
@ -318,7 +317,7 @@ class Package {
if (!this.settingsActivated) {
for (let settings of this.settings) {
settings.activate()
settings.activate(this.config)
}
this.settingsActivated = true
}
@ -636,14 +635,14 @@ class Package {
this.settings = []
const loadSettingsFile = (settingsPath, callback) => {
return ScopedProperties.load(settingsPath, this.config, (error, settings) => {
return SettingsFile.load(settingsPath, (error, settingsFile) => {
if (error) {
const detail = `${error.message} in ${settingsPath}`
const stack = `${error.stack}\n at ${settingsPath}:1:1`
this.notificationManager.addFatalError(`Failed to load the ${this.name} package settings`, {stack, detail, packageName: this.name, dismissable: true})
} else {
this.settings.push(settings)
if (this.settingsActivated) { settings.activate() }
this.settings.push(settingsFile)
if (this.settingsActivated) settingsFile.activate(this.config)
}
return callback()
})
@ -652,10 +651,10 @@ class Package {
return new Promise(resolve => {
if (this.preloadedPackage && this.packageManager.packagesCache[this.name]) {
for (let settingsPath in this.packageManager.packagesCache[this.name].settings) {
const scopedProperties = this.packageManager.packagesCache[this.name].settings[settingsPath]
const settings = new ScopedProperties(`core:${settingsPath}`, scopedProperties || {}, this.config)
this.settings.push(settings)
if (this.settingsActivated) { settings.activate() }
const properties = this.packageManager.packagesCache[this.name].settings[settingsPath]
const settingsFile = new SettingsFile(`core:${settingsPath}`, properties || {})
this.settings.push(settingsFile)
if (this.settingsActivated) settingsFile.activate(this.config)
}
return resolve()
} else {
@ -727,7 +726,7 @@ class Package {
grammar.deactivate()
}
for (let settings of this.settings) {
settings.deactivate()
settings.deactivate(this.config)
}
if (this.stylesheetDisposables) this.stylesheetDisposables.dispose()
@ -1105,3 +1104,32 @@ class Package {
})
}
}
class SettingsFile {
static load (path, callback) {
CSON.readFile(path, (error, properties = {}) => {
if (error) {
callback(error)
} else {
callback(null, new SettingsFile(path, properties))
}
})
}
constructor (path, properties) {
this.path = path
this.properties = properties
}
activate (config) {
for (let selector in this.properties) {
config.set(null, this.properties[selector], {scopeSelector: selector, source: this.path})
}
}
deactivate (config) {
for (let selector in this.properties) {
config.unset(null, {scopeSelector: selector, source: this.path})
}
}
}

View File

@ -1,22 +0,0 @@
CSON = require 'season'
module.exports =
class ScopedProperties
@load: (scopedPropertiesPath, config, callback) ->
CSON.readFile scopedPropertiesPath, (error, scopedProperties={}) ->
if error?
callback(error)
else
callback(null, new ScopedProperties(scopedPropertiesPath, scopedProperties, config))
constructor: (@path, @scopedProperties, @config) ->
activate: ->
for selector, properties of @scopedProperties
@config.set(null, properties, scopeSelector: selector, source: @path)
return
deactivate: ->
for selector of @scopedProperties
@config.unset(null, scopeSelector: selector, source: @path)
return