Comparing version 0.2.0-2 to 0.2.0-3
## 0.2.x | ||
### 0.2.0 | ||
* bug fix - inject and resolve across multiple containers now works correctly | ||
* functions that can't be resolved in static or scoped lifecycles immediately will be once all dependencies become available | ||
* don't backfill dependencies from NPM for non-default containers | ||
* improvement - functions that can't be resolved in static or scoped lifecycles immediately will be once all dependencies become available | ||
* feature - add a way to register function as value without a wrapper | ||
* NPM modules | ||
* don't auto-backfill dependencies from NPM anymore, it's kinda gross | ||
* provide ability to grab an "ambient" module from the require cache | ||
@@ -9,0 +11,0 @@ ## 0.1.X |
{ | ||
"name": "fount", | ||
"version": "0.2.0-2", | ||
"version": "0.2.0-3", | ||
"description": "A source from which dependencies flow", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -116,2 +116,10 @@ # Fount | ||
__OR__ | ||
```javascript | ||
// this really is just wrapping the value in a function like above, but it's easier to read | ||
// and hopefully less frustrating | ||
fount.registerAsValue( 'calculator', function( x, y ) { return x + y; } ); | ||
``` | ||
### promise | ||
@@ -126,2 +134,19 @@ Registering a promise looks almost identical to registering a function. From a consuming perspective, they're functionally equivalent since fount will wrap raw function execution in a promise anyway. | ||
### NPM modules | ||
Fount will allow you to plug in an NPM module. If the module was previously loaded, it will grab it from the require cache, otherwise, it will attempt to load it from the modules folder: | ||
> Note: this method will register modules that return a function as a factory which will be invoked anytime the module is resolved as a dependency | ||
```javascript | ||
fount.registerModule( "when" ); | ||
``` | ||
In the example above, where `when` is regsitered, fount will see that it is a function and register it as a factory. During resolve time, because fount cannot resolve the argument list for `when`'s function, it will simply provide the `when` function as the value. | ||
```javascript | ||
fount.inject( function( when ) { | ||
return when( "this works as you'd expect" ); | ||
} ); | ||
``` | ||
## Resolving | ||
@@ -208,2 +233,28 @@ Resolving is pretty simple - fount will always return a promise to any request for a dependency. | ||
## Purge | ||
Fount provides three different ways to clean up: | ||
* ejecting all keys and resolved scope values from all containers | ||
* ejecting all keys and resolved scope values from one container | ||
* removing all resolved scoped values from one container | ||
> Note: purge doesn't support the `_` or `.` delimited syntax for containers. When purging non-default containers, select the container first like in the examples below: | ||
```javascript | ||
// this is like starting from scratch: | ||
fount.purgeAll(); | ||
// remove all values for the default container's custom scope | ||
// this does not remove the keys, just their resolved values | ||
fount.purgeScope( "custom" ); | ||
// eject all keys from the default container | ||
fount.purge(); | ||
// remove all values for the myContainer's custom scope | ||
fount( "myContainer" ).purgeScope( "custom" ); | ||
// eject all keys from myContainer | ||
fount( "myContainer" ).purge(); | ||
``` | ||
## Diagnostic | ||
@@ -210,0 +261,0 @@ Right now this is pretty weak, but if you call `log`, Fount will dump the containers and scopes out so you can see what keys are present. Got ideas for more useful ways to troubleshoot? I'd love a PR :smile:! |
@@ -13,13 +13,2 @@ var _ = require( 'lodash' ); | ||
function backfillMissingDependency( name, containerName ) { | ||
var mod = getLoadedModule( name ) || ( containerName === "default" ? getModuleFromInstalls( name ) : null ); | ||
if ( mod ) { | ||
var lifecycle = _.isFunction( mod ) ? 'factory' : 'static'; | ||
register( containerName, name, mod, lifecycle ); | ||
} else { | ||
debug( 'Could not backfill dependency %s in in container %s', name, containerName ); | ||
} | ||
return mod; | ||
} | ||
function canResolve( containerName, dependencies, scopeName ) { | ||
@@ -144,3 +133,3 @@ return getMissingDependencies( containerName, dependencies, scopeName ).length === 0; | ||
} | ||
if ( !ctr[ k ] && !backfillMissingDependency( key, ctrName ) ) { | ||
if ( !ctr[ k ] ) { | ||
acc.push( originalKey ); | ||
@@ -156,3 +145,3 @@ } | ||
} | ||
if ( !container( containerName )[ key ] && !backfillMissingDependency( key, containerName ) ) { | ||
if ( !container( containerName )[ key ] ) { | ||
acc.push( originalKey ); | ||
@@ -230,2 +219,17 @@ } | ||
function registerModule( containerName, name ) { | ||
var mod = getLoadedModule( name ) || getModuleFromInstalls( name ); | ||
if ( mod ) { | ||
var lifecycle = _.isFunction( mod ) ? 'factory' : 'static'; | ||
register( containerName, name, mod, lifecycle ); | ||
} else { | ||
debug( 'Fount could not find NPM module %s', name ); | ||
} | ||
return mod; | ||
} | ||
function registerAsValue( containerName, key, val ) { | ||
return register( containerName, key, function() { return val; } ); | ||
} | ||
function resolve( containerName, key, scopeName ) { | ||
@@ -395,4 +399,6 @@ scopeName = scopeName || 'default'; | ||
register: register.bind( undefined, containerName ), | ||
registerModule: registerModule.bind( undefined, containerName ), | ||
registerAsValue: registerAsValue.bind( undefined, containerName ), | ||
resolve: resolve.bind( undefined, containerName ), | ||
purge: purgeScope.bind( undefined, containerName ), | ||
purge: purge.bind( undefined, containerName ), | ||
purgeScope: purgeScope.bind( undefined, containerName ) | ||
@@ -406,2 +412,4 @@ }; | ||
fount.register = register.bind( undefined, 'default' ); | ||
fount.registerModule = registerModule.bind( undefined, 'default' ); | ||
fount.registerAsValue = registerAsValue.bind( undefined, 'default' ); | ||
fount.resolve = resolve.bind( undefined, 'default' ); | ||
@@ -408,0 +416,0 @@ fount.purge = purge.bind( undefined ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25052
407
265