Skip to content

Instantly share code, notes, and snippets.

@gavinmcfarland
Last active August 13, 2019 12:22
Show Gist options
  • Select an option

  • Save gavinmcfarland/d020d4c157231e7d318b575f2377af03 to your computer and use it in GitHub Desktop.

Select an option

Save gavinmcfarland/d020d4c157231e7d318b575f2377af03 to your computer and use it in GitHub Desktop.
Plugin architecture

Option 1: Using constructor

Adding plugin inline

// index.js
import { mole, Model } from 'mole'

mole.add(
	new Model('model-name', (data, theme) => {
		return // return object
	})
)

Adding plugin via import

// modelTest.js
import { Model } from 'mole'

export default new Model('model-name', (data, theme) => {
	return // return object
})

// index.js
import { mole } from 'mole'
import { modelTest } from './modelTest'

mole.add(modelTest)

Adding dynamically via config

// models/modelTest.js
import { Model } from 'mole'

export default new Model('model-name', (data, theme) => {
	return // return object
})

// mole.config.js
export default {
	theme: 'theme.js',
	model: './models/modelTest.js'
}

// index.js
import { mole } from 'mole'

// no need to add because being reference via config

Option 2: Using array literal

Adding plugin inline

// index.js
import { mole } from 'mole'

mole.add('model', 'model-name', (data, theme) => {
	return // return object
})

Adding plugin via import

// modelTest.js
export default ['model', 'model-name', (data, theme) => {
	return // return object
}]

// index.js
import { mole } from 'mole'
import { modelTest } from './modelTest'

mole.add(modelTest) // Automatically checks for array

Adding dynamically via config

// models/modelTest.js
export default ['model-name', (data, theme) => {
	return // return object
}]

// Doesn't really care about type 'plugin/model' type at this point because it gets it from the config

// mole.config.js
export default {
	theme: 'theme.js',
	model: './models/modelTest.js'
}

// index.js
import { mole } from 'mole'

// no need to add because being reference via config

Option 3: Smater/Cleaner?

Adding plugin inline

// index.js
import { mole } from 'mole'

// Shortcut to mole.add(instance)?
mole.addModel('model-name', (data, theme) => {
	return // return object
})

// Possibly cause some confusion with add()?

Adding plugin via import

// modelTest.js
import { mole } from 'mole'

export default new mole.Model('model-name', (data, theme) => {
	return // return object
})

// index.js
import { mole } from 'mole'
import { modelTest } from './modelTest'

mole.add(modelTest)
// could use mole.addModule() as well, probably could cause confusion though?

Adding dynamically via config

// models/modelTest.js
import { mole } from 'mole'

export default new mole.Model('model-name', (data, theme) => {
	return // return object
})

// mole.config.js
export default {
	theme: 'theme.js',
	model: './models/modelTest.js'
}

// index.js
import { mole } from 'mole'

// no need to add because being reference via config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment