Skip to content

Commit

Permalink
🐎 Lazily load remaining dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Sep 1, 2016
1 parent cdbfac1 commit b5419c3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/adapters/legacy-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @access private
*/
export default class LegacyAdater {
export default class LegacyAdapter {
constructor (textEditor) { this.textEditor = textEditor }

enableCache () { this.useCache = true }
Expand Down
33 changes: 18 additions & 15 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ if (!atom.inSpecMode()) {
})
}

import {Emitter, CompositeDisposable} from 'atom'
import include from './decorators/include'
import PluginManagement from './mixins/plugin-management'

let Minimap, MinimapElement, MinimapPluginGeneratorElement
let Emitter, CompositeDisposable, Minimap, MinimapElement, MinimapPluginGeneratorElement

/**
* The `Minimap` package provides an eagle-eye view of text buffers.
Expand All @@ -33,6 +32,8 @@ class Main {
* @access private
*/
constructor () {
if (!Emitter) { ({Emitter, CompositeDisposable} = require('atom')) }

/**
* The activation state of the package.
*
Expand Down Expand Up @@ -69,13 +70,7 @@ class Main {
* @access private
*/
this.subscriptionsOfCommands = null
/**
* The package's config object.
*
* @type {Object}
* @access private
*/
this.config = require('./config-schema.json')

/**
* The package's events emitter.
*
Expand All @@ -92,11 +87,7 @@ class Main {
*/
activate () {
if (this.active) { return }

if (!Minimap) { Minimap = require('./minimap') }
if (!MinimapElement) { MinimapElement = require('./minimap-element') }

MinimapElement.registerViewProvider(Minimap)
if (!CompositeDisposable) { ({Emitter, CompositeDisposable} = require('atom')) }

this.subscriptionsOfCommands = atom.commands.add('atom-workspace', {
'minimap:toggle': () => {
Expand Down Expand Up @@ -162,6 +153,12 @@ class Main {
this.active = false
}

getConfigSchema () {
return this.config
? this.config
: atom.packages.getLoadedPackage('minimap').metadata.configSchema
}

/**
* Toggles the minimap display.
*/
Expand Down Expand Up @@ -290,7 +287,10 @@ class Main {
*
* @return {Function} the `Minimap` class constructor
*/
minimapClass () { return Minimap }
minimapClass () {
if (!Minimap) { Minimap = require('./minimap') }
return Minimap
}

/**
* Returns the `Minimap` object associated to the passed-in
Expand All @@ -317,6 +317,8 @@ class Main {
let minimap = this.editorsMinimaps.get(textEditor)

if (!minimap) {
if (!Minimap) { Minimap = require('./minimap') }

minimap = new Minimap({textEditor})
this.editorsMinimaps.set(textEditor, minimap)

Expand All @@ -339,6 +341,7 @@ class Main {
*/
standAloneMinimapForEditor (textEditor) {
if (!textEditor) { return }
if (!Minimap) { Minimap = require('./minimap') }

return new Minimap({
textEditor: textEditor,
Expand Down
27 changes: 22 additions & 5 deletions lib/minimap-element.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use babel'

import {CompositeDisposable, Disposable} from 'atom'
import {EventsDelegation, AncestorsMethods} from 'atom-utils'
import Main from './main'
import include from './decorators/include'
import element from './decorators/element'
import DOMStylesReader from './mixins/dom-styles-reader'
import CanvasDrawer from './mixins/canvas-drawer'
import MinimapQuickSettingsElement from './minimap-quick-settings-element'
import include from './decorators/include'
import element from './decorators/element'

let Main, MinimapQuickSettingsElement, CompositeDisposable, Disposable

const SPEC_MODE = atom.inSpecMode()

Expand Down Expand Up @@ -43,6 +42,10 @@ export default class MinimapElement {
* @access private
*/
createdCallback () {
if (!CompositeDisposable) {
({CompositeDisposable, Disposable} = require('atom'))
}

// Core properties

/**
Expand Down Expand Up @@ -540,6 +543,10 @@ export default class MinimapElement {

this.openQuickSettingSubscription = this.subscribeTo(this.openQuickSettings, {
'mousedown': (e) => {
if (!MinimapQuickSettingsElement) {
MinimapQuickSettingsElement = require('./minimap-quick-settings-element')
}

e.preventDefault()
e.stopPropagation()

Expand Down Expand Up @@ -656,6 +663,8 @@ export default class MinimapElement {
* @return {Minimap} this element's Minimap
*/
setModel (minimap) {
if (!Main) { Main = require('./main') }

this.minimap = minimap
this.subscriptions.add(this.minimap.onDidChangeScrollTop(() => {
this.requestUpdate()
Expand Down Expand Up @@ -1144,6 +1153,10 @@ export default class MinimapElement {
* @access private
*/
subscribeToMediaQuery () {
if (!Disposable) {
({CompositeDisposable, Disposable} = require('atom'))
}

const query = 'screen and (-webkit-min-device-pixel-ratio: 1.5)'
const mediaQuery = window.matchMedia(query)
const mediaListener = (e) => { this.requestForcedUpdate() }
Expand Down Expand Up @@ -1172,6 +1185,10 @@ export default class MinimapElement {
* @access private
*/
startDrag ({y, isLeftMouse, isMiddleMouse}) {
if (!Disposable) {
({CompositeDisposable, Disposable} = require('atom'))
}

if (!this.minimap) { return }
if (!isLeftMouse && !isMiddleMouse) { return }

Expand Down
17 changes: 13 additions & 4 deletions lib/minimap.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use babel'

import {Emitter, CompositeDisposable} from 'atom'
let Emitter, CompositeDisposable, LegacyAdapter, StableAdapter

import include from './decorators/include'
import DecorationManagement from './mixins/decoration-management'
import LegacyAdater from './adapters/legacy-adapter'
import StableAdapter from './adapters/stable-adapter'

let nextModelId = 1

Expand Down Expand Up @@ -35,6 +34,10 @@ export default class Minimap {
throw new Error('Cannot create a minimap without an editor')
}

if (!Emitter) {
({Emitter, CompositeDisposable} = require('atom'))
}

/**
* The Minimap's text editor.
*
Expand Down Expand Up @@ -177,9 +180,15 @@ export default class Minimap {
this.initializeDecorations()

if (atom.views.getView(this.textEditor).getScrollTop != null) {
if (!StableAdapter) {
StableAdapter = require('./adapters/stable-adapter')
}
this.adapter = new StableAdapter(this.textEditor)
} else {
this.adapter = new LegacyAdater(this.textEditor)
if (!LegacyAdapter) {
LegacyAdapter = require('./adapters/legacy-adapter')
}
this.adapter = new LegacyAdapter(this.textEditor)
}

/**
Expand Down
12 changes: 8 additions & 4 deletions lib/mixins/decoration-management.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use babel'

import _ from 'underscore-plus'
import path from 'path'
import Mixin from 'mixto'
import {Emitter} from 'atom'
import Decoration from '../decoration'
let _, path, Emitter, Decoration

/**
* The mixin that provides the decorations API to the minimap editor
Expand All @@ -20,6 +17,8 @@ export default class DecorationManagement extends Mixin {
*/
initializeDecorations () {
if (this.emitter == null) {
if (!Emitter) { Emitter = require('atom').Emitter }

/**
* The minimap emitter, lazily created if not created yet.
* @type {Emitter}
Expand Down Expand Up @@ -326,6 +325,8 @@ export default class DecorationManagement extends Mixin {
decorateMarker (marker, decorationParams) {
if (this.destroyed || marker == null) { return }

if (!Decoration) { Decoration = require('../decoration') }

let {id} = marker

if (decorationParams.type === 'highlight') {
Expand Down Expand Up @@ -425,6 +426,9 @@ export default class DecorationManagement extends Mixin {
}

getOriginatorPackageName () {
if (!_) { _ = require('underscore-plus') }
if (!path) { path = require('path') }

const line = new Error().stack.split('\n')[3]
const filePath = line.split('(')[1].replace(')', '')
const re = new RegExp(
Expand Down
15 changes: 11 additions & 4 deletions lib/mixins/plugin-management.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use babel'

import Mixin from 'mixto'
import { CompositeDisposable } from 'atom'

let CompositeDisposable

/**
* Provides methods to manage minimap plugins.
Expand Down Expand Up @@ -68,6 +69,10 @@ export default class PluginManagement extends Mixin {
* the registration.
*/
registerPlugin (name, plugin) {
if (!CompositeDisposable) {
CompositeDisposable = require('atom').CompositeDisposable
}

this.plugins[name] = plugin
this.pluginsSubscriptions[name] = new CompositeDisposable()

Expand Down Expand Up @@ -208,14 +213,16 @@ export default class PluginManagement extends Mixin {
const settingsKey = `minimap.plugins.${name}`
const orderSettingsKey = `minimap.plugins.${name}DecorationsZIndex`

this.config.plugins.properties[name] = {
const config = this.getConfigSchema()

config.plugins.properties[name] = {
type: 'boolean',
title: name,
description: `Whether the ${name} plugin is activated and displayed in the Minimap.`,
default: true
}

this.config.plugins.properties[`${name}DecorationsZIndex`] = {
config.plugins.properties[`${name}DecorationsZIndex`] = {
type: 'integer',
title: `${name} decorations order`,
description: `The relative order of the ${name} plugin's decorations in the layer into which they are drawn. Note that this order only apply inside a layer, so highlight-over decorations will always be displayed above line decorations as they are rendered in different layers.`,
Expand Down Expand Up @@ -279,6 +286,6 @@ export default class PluginManagement extends Mixin {
unregisterPluginControls (name) {
this.pluginsSubscriptions[name].dispose()
delete this.pluginsSubscriptions[name]
delete this.config.plugins.properties[name]
delete this.getConfigSchema().plugins.properties[name]
}
}

0 comments on commit b5419c3

Please sign in to comment.