Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ var _cache = null

module.exports = box

function box (name, key) {
function box(name, opts) {
assert.ok(typeof name === 'string' || typeof name === 'number', 'component-box: name should be type string or number')
assert.ok(components[name], 'component-box: no component handler found for [' + name + ']')

var key
var constructorArgs
var shouldCache
if (opts) {
key = opts.key
constructorArgs = opts.constructorArgs
shouldCache = opts.cache
}

if (!_cache) _cache = require('./lib/cache')()

if (key && _cache.get(name + '-' + key)) {
return _cache.get(name + '-' + key)
} else if (key) {
var value = components[name]()
var value = components[name].apply(null, constructorArgs)
_cache.set(name + '-' + key, value)
return value
} else if (key === false) {
} else if (shouldCache === false) {
return components[name]()
} else if (name && _cache.get(name)) {
return _cache.get(name)
Expand All @@ -38,7 +47,7 @@ box.use = function (newcomponents) {
components = x(components, newcomponents)
}

box.cache = function(cache) {
box.cache = function (cache) {
assert.equal(typeof cache.get, 'function', 'component-box.cache: cache should have get property of type function')
assert.equal(typeof cache.set, 'function', 'component-box.cache: cache should have set property of type function')
assert.equal(typeof cache.remove, 'function', 'component-box.cache: cache should have remove property of type function')
Expand Down
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('c(\'component\')', function (t) {
})

test('c(\'component\', \'custom\')', function (t) {
c('component', 'custom')
c('component', { key: 'custom' })

var box = c._inspect()

Expand All @@ -40,7 +40,7 @@ test('c.delete(\'component\')', function (t) {
})

test('c(\'component\', false)', function (t) {
c('component', false)
c('component', { cache: false })

var box = c._inspect()

Expand Down