Skip to content

Commit

Permalink
fix: remove synchronous lazy loading
Browse files Browse the repository at this point in the history
In short, there are better ways to improve the startup time

- Loading built-in package has no effect the loading time of the pacakge
- Tree shaking by Parcel is more effective than synchronous lazy
loading.
- A lot of these loadins were duplicate. Once a package is loaded, Node
will cache it. No need to lazy load
  • Loading branch information
aminya committed Dec 25, 2020
1 parent f8d49f7 commit 927cc1e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/canvas-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = class CanvasLayer {
*/
this.canvas = document.createElement('canvas')

const desynchronized = process.platform !== "linux"
const desynchronized = process.platform !== 'linux'

/**
* The onscreen canvas context.
Expand Down
19 changes: 4 additions & 15 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ if (!atom.inSpecMode()) {
const include = require('./decorators/include')
const PluginManagement = require('./mixins/plugin-management')

let Emitter, CompositeDisposable, Minimap, MinimapElement, MinimapPluginGeneratorElement
const { Emitter, CompositeDisposable } = require('atom')
const MinimapElement = require('./minimap-element')
const Minimap = require('./minimap')
const MinimapPluginGeneratorElement = require('./minimap-plugin-generator-element')

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

/**
* The activation state of the package.
*
Expand Down Expand Up @@ -92,7 +93,6 @@ class Main {
*/
activate () {
if (this.active) { return }
if (!CompositeDisposable) { ({ Emitter, CompositeDisposable } = require('atom')) }

this.subscriptionsOfCommands = atom.commands.add('atom-workspace', {
'minimap:toggle': () => {
Expand Down Expand Up @@ -123,11 +123,7 @@ class Main {
* @return {MinimapElement}
*/
minimapViewProvider (model) {
if (!Minimap) { Minimap = require('./minimap') }

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

const element = new MinimapElement()
element.setModel(model)
return element
Expand Down Expand Up @@ -192,9 +188,6 @@ class Main {
* @param {string} template the name of the template to use
*/
generatePlugin (template) {
if (!MinimapPluginGeneratorElement) {
MinimapPluginGeneratorElement = require('./minimap-plugin-generator-element')
}
const view = new MinimapPluginGeneratorElement()
view.template = template
view.attach()
Expand Down Expand Up @@ -293,7 +286,6 @@ class Main {
* @return {Function} the `Minimap` class constructor
*/
minimapClass () {
if (!Minimap) { Minimap = require('./minimap') }
return Minimap
}

Expand Down Expand Up @@ -323,8 +315,6 @@ 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 @@ -347,7 +337,6 @@ class Main {
*/
standAloneMinimapForEditor (textEditor) {
if (!textEditor) { return }
if (!Minimap) { Minimap = require('./minimap') }

return new Minimap({
textEditor: textEditor,
Expand Down
23 changes: 4 additions & 19 deletions lib/minimap-element.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict'

const { CompositeDisposable, Disposable } = require('atom')
const { EventsDelegation, AncestorsMethods } = require('atom-utils-plus')
const DOMStylesReader = require('./mixins/dom-styles-reader')
const CanvasDrawer = require('./mixins/canvas-drawer')
const include = require('./decorators/include')
const element = require('./decorators/element')
const elementResizeDetector = require('element-resize-detector')({ strategy: 'scroll' })
const MinimapQuickSettingsElement = require('./minimap-quick-settings-element')
const Main = require('./main')

let Main, MinimapQuickSettingsElement, CompositeDisposable, Disposable, overlayStyle
let overlayStyle

const ensureOverlayStyle = () => {
if (!overlayStyle) {
Expand Down Expand Up @@ -71,10 +74,6 @@ class MinimapElement {
* @access private
*/
createdCallback () {
if (!CompositeDisposable) {
({ CompositeDisposable, Disposable } = require('atom'))
}

// Core properties

/**
Expand Down Expand Up @@ -601,10 +600,6 @@ 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 @@ -687,8 +682,6 @@ 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 @@ -1177,10 +1170,6 @@ 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 @@ -1209,10 +1198,6 @@ class MinimapElement {
* @access private
*/
startDrag ({ y, isLeftMouse, isMiddleMouse }) {
if (!Disposable) {
({ CompositeDisposable, Disposable } = require('atom'))
}

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

Expand Down
15 changes: 4 additions & 11 deletions lib/minimap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
const include = require('./decorators/include')
const DecorationManagement = require('./mixins/decoration-management')

let Emitter, CompositeDisposable, LegacyAdapter, StableAdapter
const { Emitter, CompositeDisposable } = require('atom')
const StableAdapter = require('./adapters/stable-adapter')
const LegacyAdapter = require('./adapters/legacy-adapter')

let nextModelId = 1

/**
Expand Down Expand Up @@ -37,10 +40,6 @@ 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 @@ -208,14 +207,8 @@ 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 {
if (!LegacyAdapter) {
LegacyAdapter = require('./adapters/legacy-adapter')
}
this.adapter = new LegacyAdapter(this.textEditor)
}

Expand Down
14 changes: 5 additions & 9 deletions lib/mixins/decoration-management.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'

let _, path, Emitter, Decoration
const { Emitter } = require('atom')
const { escapeRegExp } = require('underscore-plus')
const path = require('path')
const Decoration = require('../decoration')

/**
* The mixin that provides the decorations API to the minimap editor
Expand All @@ -15,8 +18,6 @@ module.exports = class DecorationManagement {
*/
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 @@ -323,8 +324,6 @@ module.exports = class DecorationManagement {
decorateMarker (marker, decorationParams) {
if (this.destroyed || marker == null) { return }

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

const { id } = marker

if (decorationParams.type === 'highlight') {
Expand Down Expand Up @@ -428,13 +427,10 @@ module.exports = class DecorationManagement {
}

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(
atom.packages.getPackageDirPaths().join('|') + _.escapeRegExp(path.sep)
atom.packages.getPackageDirPaths().join('|') + escapeRegExp(path.sep)
)
const plugin = filePath.replace(re, '').split(path.sep)[0].replace(/minimap-|-minimap/, '')
return plugin.indexOf(path.sep) < 0 ? plugin : undefined
Expand Down
7 changes: 1 addition & 6 deletions lib/mixins/plugin-management.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const Mixin = require('mixto')

let CompositeDisposable
const { CompositeDisposable } = require('atom')

/**
* Provides methods to manage minimap plugins.
Expand Down Expand Up @@ -69,10 +68,6 @@ module.exports = 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

0 comments on commit 927cc1e

Please sign in to comment.