Skip to content

Commit

Permalink
Merge pull request #433 from lawgsy/feature/settings-on-initialize
Browse files Browse the repository at this point in the history
Pass 'settings' object to plugins on initalize() and initializeAsync()
  • Loading branch information
KELiON authored Feb 2, 2018
2 parents 9636435 + c5b243d commit 74dc73a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import ReactDOM from 'react-dom'
import plugins from 'plugins'
import { on, send } from 'lib/rpc'
import { modulesDirectory } from 'lib/plugins'
import { settings as pluginSettings, modulesDirectory } from 'lib/plugins'

require('fix-path')()

Expand All @@ -28,7 +28,7 @@ on('initializePluginAsync', ({ name }) => {
name,
data,
})
})
}, pluginSettings.getUserSettings(name))
} catch (err) {
console.log('Failed', err)
}
Expand Down
3 changes: 2 additions & 1 deletion app/lib/initializePlugins.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { on, send } from 'lib/rpc'
import plugins from 'plugins'
import { settings as pluginSettings } from 'lib/plugins'

export const initializePlugin = (name) => {
const { initialize, initializeAsync } = plugins[name]
if (initialize) {
// Foreground plugin initialization
try {
initialize()
initialize(pluginSettings.getUserSettings(name))
} catch (e) {
console.error(`Failed to initialize plugin: ${name}`, e)
}
Expand Down
16 changes: 15 additions & 1 deletion app/lib/plugins/settings/get.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import config from 'lib/config'
import plugins from 'plugins'

export default pluginName => config.get('plugins')[pluginName] || {}
const getSettings = pluginName => config.get('plugins')[pluginName] || {}

const getUserSettings = (pluginName) => {
const settings = getSettings(pluginName)
if (plugins[pluginName].settings) {
// Provide default values if nothing is set by user
Object.keys(plugins[pluginName].settings).forEach((key) => {
settings[key] = settings[key] || plugins[pluginName].settings[key].defaultValue
})
}
return settings
}

export default getUserSettings
4 changes: 2 additions & 2 deletions app/lib/plugins/settings/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import get from './get'
import getUserSettings from './get'
import validate from './validate'

export default { get, validate }
export default { getUserSettings, validate }
14 changes: 2 additions & 12 deletions app/main/actions/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import plugins from 'plugins'
import config from 'lib/config'
import { shell, clipboard, remote } from 'electron'
import { settings as pluginSettings } from 'lib/plugins'

import store from '../store'

import {
Expand Down Expand Up @@ -31,17 +32,6 @@ const DEFAULT_SCOPE = {
}
}

const getSettings = (name) => {
const settings = pluginSettings.get(name) || {}
if (plugins[name].settings) {
// Provide default values if nothing is set by user
Object.keys(plugins[name].settings).forEach((key) => {
settings[key] = settings[key] || plugins[name].settings[key].defaultValue
})
}
return settings
}

/**
* Pass search term to all plugins and handle their results
* @param {String} term Search term
Expand All @@ -57,7 +47,7 @@ const eachPlugin = (term, display) => {
hide: id => store.dispatch(hideElement(`${name}-${id}`)),
update: (id, result) => store.dispatch(updateElement(`${name}-${id}`, result)),
display: payload => display(name, payload),
settings: getSettings(name)
settings: pluginSettings.getUserSettings(name)
})
} catch (error) {
// Do not fail on plugin errors, just log them to console
Expand Down
2 changes: 1 addition & 1 deletion test/actions/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const actions = searchInjector({
plugins: pluginsMock,
'lib/config': {},
'lib/plugins': {
get: () => undefined
getUserSettings: () => undefined
}
})

Expand Down

0 comments on commit 74dc73a

Please sign in to comment.