Factory
Classes
- Factory
The Factory allows the lookup and construction of objects/results based on a type. A sample usage is:
const f = new Factory( ) f.add( 'myType', MyTypeMsg ) f.add( 'myOtherType', MyOtherTypeMsg ) const msg = f.create( { type : 'myOtherType', value : 5 } ) // msg is equivalent to new MyOtherMsg( { type : 'myOtherType', value : 5 } )
Strategies when create( obj, arg1, argN ) is called. Below, clzz equals get( obj ) ALL (default): new clzz( obj, arg1, argN ) // clzz is a class clzz( obj, arg1, argN ) // clzz is a function NONE: new clzz( ) clzz( ) ARGS: new clzz( arg1, argN ) clzz( arg1, argN ) MERGE: const alt1 = { model : obj, ...arg1 } new clzz( alt1, argN ) clzz( alt1, argN ) LOOKUP: clzz
: return strategy( this, clzz, obj, arg1, argN ) ALL is equivalent to ARGS is create( model, model, arg1, argN ) is called
Functions
- allStrategy(factory, clzz, typeOrObject) ⇒
A strategy where the args passed into the constructor/function are ( typeOrObject, arg1, arg2, ...etc... )
- noneStrategy(factory, clzz, typeOrObject) ⇒
A strategy where no args passed into the constructor/function
- argsStrategy(factory, clzz, typeOrObject) ⇒
A strategy where the args passed into the constructor/function are ( arg1, arg2, ...etc... )
- lookupStrategy(factory, clzz, typeOrObject) ⇒
A strategy where clzz is returned
- mergeStrategy(key) ⇒
Creates a strategy where the args passed into the constructor/function are ( MOD, arg2, ...etc... ), where MOD is { [key] : typeOrObject, ...args[0] }
- reactStrategy(React, propKey) ⇒
Creates a strategy for createing react elements. The clzz object given to the strategy is assumed to be a React.Component or React pure component/function. the typeOrObejct will be merge into the props at the specified propKey.
Factory
The Factory allows the lookup and construction of objects/results based on a type. A sample usage is:
const f = new Factory( ) f.add( 'myType', MyTypeMsg ) f.add( 'myOtherType', MyOtherTypeMsg ) const msg = f.create( { type : 'myOtherType', value : 5 } ) // msg is equivalent to new MyOtherMsg( { type : 'myOtherType', value : 5 } )
Strategies when create( obj, arg1, argN ) is called. Below, clzz equals get( obj )
ALL (default):
new clzz( obj, arg1, argN ) // clzz is a class
clzz( obj, arg1, argN ) // clzz is a function
NONE:
new clzz( )
clzz( )
ARGS:
new clzz( arg1, argN )
clzz( arg1, argN )
MERGE:
const alt1 = { model : obj, ...arg1 }
new clzz( alt1, argN )
clzz( alt1, argN )
LOOKUP:
clzz
ALL is equivalent to ARGS is create( model, model, arg1, argN ) is called
Kind: global class
- Factory
- new Factory(options, name, typeKey, [registry], defaultObject, strategy)
- .add(type, obj)
- .remove(type)
- .getType(typeOrObj) ⇒
- .has() ⇒
boolean
- .get(type) ⇒
any
- .getAll() ⇒
object
- .create(...args) ⇒
any
new Factory(options, name, typeKey, [registry], defaultObject, strategy)
Create a factory object.
Param | Type | Description | |
---|---|---|---|
options | object \ | function | the options for the factory. If this is a function, it is the strategy. |
name | string | the name of the factory | |
typeKey | string | the property to look at when determining the type of an object. Defaults to 'type' | |
[registry] | object | a type to obj map. Using this is the same as calling add for each key/value pair | |
defaultObject | any | If a lookup results in null/undefined, this object will be used | |
strategy | function | a function( factory, clzz, typeOrClass, ...args ) invoked to create the returned object. (Defaults to Factory.ALL) |
factory.add(type, obj)
Registers an object to support the type. If there is already an object registered for the type, this will throw an exception unless overwrite is set to true
Kind: instance method of Factory
Param | Type | Description |
---|---|---|
type | string | the type to register |
obj | any | the thing to register for the type |
factory.remove(type)
Removes a registered type.
Kind: instance method of Factory
Param | Type | Description |
---|---|---|
type | string | the type to remove |
factory.getType(typeOrObj) ⇒
If typeOrObj is an object, this will extract the type from the object usin the typeKey field. Otherwise, typeOrObject will be returned
Kind: instance method of Factory
Returns: the type
Param | Type | Description |
---|---|---|
typeOrObj | any | the type or an object containing the type |
boolean
factory.has() ⇒ Return true if an object is registered to suppor the type.
Kind: instance method of Factory
Returns: boolean
- true if there is a registered object
any
factory.get(type) ⇒ Returns the function/class registered to manager the object of the specified type.
Kind: instance method of Factory
Returns: any
- the thing registered at that that location
Param | Type | Description | |
---|---|---|---|
type | object \ | string | If this is a string, it is used as the type to lookup. If it is an object, the typeKey field of the object is used to do the lookup. |
object
factory.getAll() ⇒ Returns an object containing the mapping between the types and the things registered to suppor that type.
Kind: instance method of Factory
Returns: object
- the mapping
any
factory.create(...args) ⇒ Creates the thing used to support the typeOfObj.
Kind: instance method of Factory
Returns: any
- the constructed item
Oaram: object|string
typeOrObj usually the object we're creating
a handler for, but in some strategies, this might just be the type
Param | Type | Description |
---|---|---|
...args | array | the rest of the params |
allStrategy(factory, clzz, typeOrObject) ⇒
A strategy where the args passed into the constructor/function are ( typeOrObject, arg1, arg2, ...etc... )
Kind: global function
Returns: the new class, the result of the function, or clzz if
clzz is not a class or function.
Param | Type | Description | |
---|---|---|---|
factory | Factory | the invoking factory | |
clzz | Class \ | function | the registered |
typeOrObject | object \ | string | the object used to lookup the clzz |
...args | array | the rest of the args |
noneStrategy(factory, clzz, typeOrObject) ⇒
A strategy where no args passed into the constructor/function
Kind: global function
Returns: the new class, the result of the function, or clzz if
clzz is not a class or function.
Param | Type | Description | |
---|---|---|---|
factory | Factory | the invoking factory | |
clzz | Class \ | function | the registered |
typeOrObject | object \ | string | the object used to lookup the clzz |
...args | array | the rest of the args |
argsStrategy(factory, clzz, typeOrObject) ⇒
A strategy where the args passed into the constructor/function are ( arg1, arg2, ...etc... )
Kind: global function
Returns: the new class, the result of the function, or clzz if
clzz is not a class or function.
Param | Type | Description | |
---|---|---|---|
factory | Factory | the invoking factory | |
clzz | Class \ | function | the registered |
typeOrObject | object \ | string | the object used to lookup the clzz |
...args | array | the rest of the args |
lookupStrategy(factory, clzz, typeOrObject) ⇒
A strategy where clzz is returned
Kind: global function
Returns: the new class, the result of the function, or clzz if
clzz is not a class or function.
Param | Type | Description | |
---|---|---|---|
factory | Factory | the invoking factory | |
clzz | Class \ | function | the registered |
typeOrObject | object \ | string | the object used to lookup the clzz |
...args | array | the rest of the args |
mergeStrategy(key) ⇒
Creates a strategy where the args passed into the constructor/function are ( MOD, arg2, ...etc... ), where MOD is { [key] : typeOrObject, ...args[0] }
Kind: global function
Returns: the new merge strategy
Param | Type | Default | Description |
---|---|---|---|
key | string | "model" | the the merge key |
reactStrategy(React, propKey) ⇒
Creates a strategy for createing react elements. The clzz object given to the strategy is assumed to be a React.Component or React pure component/function. the typeOrObejct will be merge into the props at the specified propKey.
Kind: global function
Returns: the react element
Param | Type | Default | Description | |
---|---|---|---|---|
React | React | the React library | ||
propKey | string \ | false | "model" | if this is a string, the typeOrObject will be merged into the props at that key. Otherwise, it will not be supplied |