Skip to content

Commit

Permalink
fix: export directly from PluginManagement to remove race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Dec 26, 2020
1 parent e354143 commit 4d9e234
Showing 1 changed file with 57 additions and 54 deletions.
111 changes: 57 additions & 54 deletions lib/mixins/plugin-management.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

import Mixin from 'mixto'
import { CompositeDisposable } from 'atom'
import { emitter, getConfigSchema } from '../main'

/**
* Provides methods to manage minimap plugins.
Expand All @@ -18,42 +18,46 @@ import { CompositeDisposable } from 'atom'
*
* @access public
*/
export default class PluginManagement extends Mixin {

export let plugins
export let pluginsSubscriptions
export let pluginsOrderMap

/**
* Returns the Minimap main module instance.
*
* @return {Main} The Minimap main module instance.
*/
provideMinimapServiceV1 () { return this }
export function provideMinimapServiceV1 () { return exports }

/**
* Initializes the properties for plugins' management.
*
* @access private
*/
initializePlugins () {
export function initializePlugins () {
/**
* The registered Minimap plugins stored using their name as key.
*
* @type {Object}
* @access private
*/
this.plugins = {}
plugins = {}
/**
* The plugins' subscriptions stored using the plugin names as keys.
*
* @type {Object}
* @access private
*/
this.pluginsSubscriptions = {}
pluginsSubscriptions = {}

/**
* A map that stores the display order for each plugin
*
* @type {Object}
* @access private
*/
this.pluginsOrderMap = {}
pluginsOrderMap = {}
}

/**
Expand All @@ -67,18 +71,18 @@ export default class PluginManagement extends Mixin {
* @emits {did-activate-plugin} if the plugin was activated during
* the registration.
*/
registerPlugin (name, plugin) {
this.plugins[name] = plugin
this.pluginsSubscriptions[name] = new CompositeDisposable()
export function registerPlugin (name, plugin) {
plugins[name] = plugin
pluginsSubscriptions[name] = new CompositeDisposable()

const event = { name: name, plugin: plugin }
this.emitter.emit('did-add-plugin', event)
emitter.emit('did-add-plugin', event)

if (atom.config.get('minimap.displayPluginsControls')) {
this.registerPluginControls(name, plugin)
registerPluginControls(name, plugin)
}

this.updatesPluginActivationState(name)
updatesPluginActivationState(name)
}

/**
Expand All @@ -88,17 +92,17 @@ export default class PluginManagement extends Mixin {
* @emits {did-remove-plugin} with the name and a reference
* to the added plugin.
*/
unregisterPlugin (name) {
const plugin = this.plugins[name]
export function unregisterPlugin (name) {
const plugin = plugins[name]

if (atom.config.get('minimap.displayPluginsControls')) {
this.unregisterPluginControls(name)
unregisterPluginControls(name)
}

delete this.plugins[name]
delete plugins[name]

const event = { name: name, plugin: plugin }
this.emitter.emit('did-remove-plugin', event)
emitter.emit('did-remove-plugin', event)
}

/**
Expand All @@ -112,7 +116,7 @@ export default class PluginManagement extends Mixin {
* @emits {did-activate-plugin} if the plugin was activated by the call.
* @emits {did-deactivate-plugin} if the plugin was deactivated by the call.
*/
togglePluginActivation (name, boolean) {
export function togglePluginActivation (name, boolean) {
const settingsKey = `minimap.plugins.${name}`

if (boolean !== undefined && boolean !== null) {
Expand All @@ -121,18 +125,18 @@ export default class PluginManagement extends Mixin {
atom.config.set(settingsKey, !atom.config.get(settingsKey))
}

this.updatesPluginActivationState(name)
updatesPluginActivationState(name)
}

/**
* Deactivates all the plugins registered in the minimap package so far.
*
* @emits {did-deactivate-plugin} for each plugin deactivated by the call.
*/
deactivateAllPlugins () {
for (const [name, plugin] of this.eachPlugin()) {
export function deactivateAllPlugins () {
for (const [name, plugin] of eachPlugin()) {
plugin.deactivatePlugin()
this.emitter.emit('did-deactivate-plugin', { name: name, plugin: plugin })
emitter.emit('did-deactivate-plugin', { name: name, plugin: plugin })
}
}

Expand All @@ -142,9 +146,9 @@ export default class PluginManagement extends Mixin {
* @return An iterable that yield the name and reference to every plugin
* as an array in each iteration.
*/
* eachPlugin () {
for (const name in this.plugins) {
yield [name, this.plugins[name]]
export function * eachPlugin () {
for (const name in plugins) {
yield [name, plugins[name]]
}
}

Expand All @@ -156,38 +160,38 @@ export default class PluginManagement extends Mixin {
* @emits {did-deactivate-plugin} if the plugin was deactivated by the call.
* @access private
*/
updatesPluginActivationState (name) {
const plugin = this.plugins[name]
export function updatesPluginActivationState (name) {
const plugin = plugins[name]
const pluginActive = plugin.isActive()
const settingActive = atom.config.get(`minimap.plugins.${name}`)

if (atom.config.get('minimap.displayPluginsControls')) {
if (settingActive && !pluginActive) {
this.activatePlugin(name, plugin)
activatePlugin(name, plugin)
} else if (pluginActive && !settingActive) {
this.deactivatePlugin(name, plugin)
deactivatePlugin(name, plugin)
}
} else {
if (!pluginActive) {
this.activatePlugin(name, plugin)
activatePlugin(name, plugin)
} else if (pluginActive) {
this.deactivatePlugin(name, plugin)
deactivatePlugin(name, plugin)
}
}
}

activatePlugin (name, plugin) {
export function activatePlugin (name, plugin) {
const event = { name: name, plugin: plugin }

plugin.activatePlugin()
this.emitter.emit('did-activate-plugin', event)
emitter.emit('did-activate-plugin', event)
}

deactivatePlugin (name, plugin) {
export function deactivatePlugin (name, plugin) {
const event = { name: name, plugin: plugin }

plugin.deactivatePlugin()
this.emitter.emit('did-deactivate-plugin', event)
emitter.emit('did-deactivate-plugin', event)
}

/**
Expand All @@ -204,11 +208,11 @@ export default class PluginManagement extends Mixin {
* to toggle the plugin state.
* @access private
*/
registerPluginControls (name, plugin) {
export function registerPluginControls (name, plugin) {
const settingsKey = `minimap.plugins.${name}`
const orderSettingsKey = `minimap.plugins.${name}DecorationsZIndex`

const config = this.getConfigSchema()
const config = getConfigSchema()

config.plugins.properties[name] = {
type: 'boolean',
Expand All @@ -232,23 +236,23 @@ export default class PluginManagement extends Mixin {
atom.config.set(orderSettingsKey, 0)
}

this.pluginsSubscriptions[name].add(atom.config.observe(settingsKey, () => {
this.updatesPluginActivationState(name)
pluginsSubscriptions[name].add(atom.config.observe(settingsKey, () => {
updatesPluginActivationState(name)
}))

this.pluginsSubscriptions[name].add(atom.config.observe(orderSettingsKey, (order) => {
this.updatePluginsOrderMap(name)
pluginsSubscriptions[name].add(atom.config.observe(orderSettingsKey, (order) => {
updatePluginsOrderMap(name)
const event = { name: name, plugin: plugin, order: order }
this.emitter.emit('did-change-plugin-order', event)
emitter.emit('did-change-plugin-order', event)
}))

this.pluginsSubscriptions[name].add(atom.commands.add('atom-workspace', {
pluginsSubscriptions[name].add(atom.commands.add('atom-workspace', {
[`minimap:toggle-${name}`]: () => {
this.togglePluginActivation(name)
togglePluginActivation(name)
}
}))

this.updatePluginsOrderMap(name)
updatePluginsOrderMap(name)
}

/**
Expand All @@ -257,18 +261,18 @@ export default class PluginManagement extends Mixin {
* @param {string} name the name of the plugin to update
* @access private
*/
updatePluginsOrderMap (name) {
export function updatePluginsOrderMap (name) {
const orderSettingsKey = `minimap.plugins.${name}DecorationsZIndex`

this.pluginsOrderMap[name] = atom.config.get(orderSettingsKey)
pluginsOrderMap[name] = atom.config.get(orderSettingsKey)
}

/**
* Returns the plugins display order mapped by name.
*
* @return {Object} The plugins order by name
*/
getPluginsOrder () { return this.pluginsOrderMap }
export function getPluginsOrder () { return pluginsOrderMap }

/**
* When the `minimap.displayPluginsControls` setting is toggled,
Expand All @@ -278,9 +282,8 @@ export default class PluginManagement extends Mixin {
* @param {string} name The identifying name of the plugin.
* @access private
*/
unregisterPluginControls (name) {
this.pluginsSubscriptions[name].dispose()
delete this.pluginsSubscriptions[name]
delete this.getConfigSchema().plugins.properties[name]
export function unregisterPluginControls (name) {
pluginsSubscriptions[name].dispose()
delete pluginsSubscriptions[name]
delete getConfigSchema().plugins.properties[name]
}
}

0 comments on commit 4d9e234

Please sign in to comment.