made mixin configure better + some tweaking + docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-19 06:13:11 +03:00
parent 30e8197e0f
commit e25658db0f
3 changed files with 42 additions and 14 deletions

View File

@ -130,7 +130,7 @@ class B extends A {
- [`<object>.__init__(..)`](#object__init__) - [`<object>.__init__(..)`](#object__init__)
- [`<object>.__call__(..)`](#object__call__) - [`<object>.__call__(..)`](#object__call__)
- [Components](#components) - [Components](#components)
- [`STOP`](#stop) - [`STOP` / `STOP(..)`](#stop--stop)
- [`sources(..)`](#sources) - [`sources(..)`](#sources)
- [`values(..)`](#values) - [`values(..)`](#values)
- [`parent(..)`](#parent) - [`parent(..)`](#parent)
@ -554,7 +554,7 @@ var l = object.RawInstance(null, Array, 'a', 'b', 'c')
``` ```
### `STOP` ### `STOP` / `STOP(..)`
Used in [`sources(..)`](#sources), [`values(..)`](#values) and Used in [`sources(..)`](#sources), [`values(..)`](#values) and
[`mixins(..)`](#mixins) to stop the search before it reaches the top of [`mixins(..)`](#mixins) to stop the search before it reaches the top of

View File

@ -1149,42 +1149,62 @@ function(base, ...objects){
// Mixin(name, data, ..) // Mixin(name, data, ..)
// -> mixin // -> mixin
// //
// Apply the mixin to an object... // Create a new mixin setting the default mode...
// Mixin(name, mode, data, ..)
// -> mixin
//
//
// Apply mixin in the prototype chain (default)...
// mixin(obj) // mixin(obj)
// mixin('proto', obj)
// -> obj // -> obj
// //
// mixin(mode, obj) // Copy date from mixin into obj directly...
// mixin(obj, mode) // mixin('flat', obj)
// -> obj // -> obj
// //
// //
//
// Example: // Example:
// //
// var BasicMixin = Mixin('BasicMixin', { // var BasicMixin = Mixin('BasicMixin', {
// ...
// })
//
// ... // ...
// })
// //
// var o = { // var o = {
// ... // ...
// } // }
// //
// BasicMixin(o) // BasicMixin(o)
// //
// //
// NOTE: the constructor will allways create a new .data object but will
// not do a deep copy into it -- see mixin(..) / mixinFlat(..)
var Mixin = var Mixin =
module.Mixin = module.Mixin =
Constructor('Mixin', { Constructor('Mixin', {
name: null, name: null,
// mixin data...
data: null, data: null,
// data "copy" mode...
//
// This can be:
// 'proto' - mix data into prototype chain (default)
// 'flat' - use mixinFlat(..) to copy data
mode: 'proto', mode: 'proto',
// base API...
//
isMixed: function(target){ isMixed: function(target){
return hasMixin(target, this.data) }, return hasMixin(target, this.data) },
mixout: function(target){ mixout: function(target){
return mixout(target, this.data) }, return mixout(target, this.data) },
// mixin to target... // mix into target...
__call__: function(_, target, mode=this.mode){ __call__: function(_, target, mode=this.mode){
typeof(target) == typeof('str') typeof(target) == typeof('str')
&& ([_, mode, target] = arguments) && ([_, mode, target] = arguments)
@ -1193,12 +1213,20 @@ Constructor('Mixin', {
: mixin(target, this.data) }, : mixin(target, this.data) },
__init__: function(name, ...data){ __init__: function(name, ...data){
// Mixin(name, mode, ...) -- handle default mode...
typeof(data[0]) == typeof('str')
&& (this.mode = data.shift())
// name...
// NOTE: .defineProperty(..) is used because this is a function // NOTE: .defineProperty(..) is used because this is a function
// and function's .name is not too configurable... // and function's .name is not too configurable...
// XXX do we need to configure this prop better??? // NOTE: we do not need to configure this any more, .defineProperty(..)
// merges the descriptor into the original keeping any values not
// explicitly overwritten...
Object.defineProperty(this, 'name', { value: name }) Object.defineProperty(this, 'name', { value: name })
// create/merge .data...
this.data = mixinFlat({}, this.data = mixinFlat({},
...data.map(function(e){ ...data.map(function(e){
// handle bare objects and mixins differently...
return e instanceof Mixin ? return e instanceof Mixin ?
e.data e.data
: e })) }, : e })) },

View File

@ -1,6 +1,6 @@
{ {
"name": "ig-object", "name": "ig-object",
"version": "5.4.3", "version": "5.4.4",
"description": "", "description": "",
"main": "object.js", "main": "object.js",
"scripts": { "scripts": {