cocktailjs / cocktail Goto Github PK
View Code? Open in Web Editor NEWTraits, Talents & Annotations for NodeJS.
Home Page: cocktailjs.github.io
License: MIT License
Traits, Talents & Annotations for NodeJS.
Home Page: cocktailjs.github.io
License: MIT License
Got myself in some trouble when trying to extend a few classes.
And since the proof is in eating the pudding, here's some code:
cocktail.mix({
'@exports': module,
'@as': 'class',
'@traits': [AlgorithmInterface],
'@logger' : [console, "Algorithm Base:"],
constructor: function() {
//Should be initialized by some especification of this class.
this._victims = undefined;
this._requirements = undefined;
this._finalized = new Queue();
this._filters = [];
},
initialize: function(requirements) {
this._requirements = requirements;
this._finalized = new Queue();
}
});
cocktail.mix({
'@exports': module,
'@as': 'class',
'@extends': Algorithm,
'@traits': [AlgorithmInterface],
'@logger' : [console, "Algorithm Fifo:"],
constructor: function() {
this.callSuper("constructor");
this._victims = new Queue();
this.log("Created.");
},
initialize: function(requirements) {
this.callSuper("initialize", requirements);
this._victims = new Queue();
}
});
cocktail.mix({
'@exports': module,
'@as': 'class',
'@extends': Fifo,
'@traits': [AlgorithmInterface],
'@logger' : [console, "Algorithm Lru:"],
constructor: function() {
this.callSuper("constructor");
this._victims = new ReQueueQueue();
this.log("Created.");
},
initialize: function(requirements) {
this.callSuper("initialize", requirements);
this._victims = new ReQueueQueue();
}
});
As you can see, there are 2 chains of callSuper methods, one in the constructor call, the other in the initialize method call, but the call only starts looping when initialize is called.
The @exports annotation should accept the module as a parameter and export the current mix as module.exports.
Cocktail.mix(MyClass, {
'@exports': module,
//.....
someFunc: function(){}
});
This will avoid to do:
module.exports = MyClass;
When composing a TraitA with TraitB both having same method definition 'methA', when aliasing TraitA 'methA' on TraitB a same method definition error is thrown.
cocktail.mix({
//...
'@traits': [
{
trait: TraitA,
alias: {methA: 'methodA'}
}
],
methA: function(){/**/}
});
Right now istanbul runs manually from command line. Explore grunt plugin
https://github.com/taichi/grunt-istanbul
to add the coverage report as part of the process.
Hi,
just came across CoctailJS - nice job!
Right away, though, I thought - wouldn't it be nice if it also supported AOP. Anyway, it is also kind of a cross-cutting "trait" that you would like to maintain separately and not pollute your nice little classes with logging etc.
So, the question is - have you been considering adding AOP support using some nice annotations like:
@before : {
'get.*' : function (methodName, args) {
console.log("Method " + methodName + " going to be called with args:", args)
}
}
Similarly for @after and @around.
Any thoughts?
Vilem
Docs at cocktailjs.github.io/docs should be updated to reflect the new single argument class definition in mix()
Annotation processor is not working between modules. The method cocktail.use() has been added - see #16 - and Annotation processor should be refactored to add a name property to the Processor prototype adding the annotation symbol.
README should contain better information, short examples and explanation for Traits and Talents.
The '@static' annotation should be able to define properties and methods that will be attached to the class as class methods and properties:
var Cocktail = require('Cocktail'),
Class;
Class = Cocktail.mix({
'@static': {
value: 'someValue',
someStaticMethod: function(){return this.value;} // <- this should be referencing the Class
}
});
console.log(Class.value); // 'someValue'
console.log(Class.someStaticMethod()); // 'someValue'
On Google Chrome 51 an upwards 'annotation.name' returns 'ctor' which is inconsistent with the other browsers which return '@Evented'. This prevents the registration of the event emitter from 'cocktail-annotation-evented'.
The trait/talent definition could be defined as a module/object. The current impl only works with classes.
Since we are deprecating the Annotation processor ( See #15 ) we need a better and clear way to add custom annotation processors to current cocktail instance.
Methods defined in ES6 classes are NOT enumerable. This is is causing issues when a class is used as a trait/talent.
When defining a [], {} or a Date inside the @properties
annotation those should be cloned when read/assign into the class instance.
Since they are assigned to the Class.prototype, this causes all instances to use the same value from prototype if it was not assigned into constructor or via setter.
Even though it can be solved by setting default values in construction time, only for properties in @properties
we want to avoid this issue.
In Linux OS the filesystem is case sensitive. The module is declared as 'cocktail' and then having a require('Cocktail')
will fail.
Rename Cocktail in favor of cocktail in all docs, examples, guides, etc.
When defining a custom annotation, if the process methods calls Cocktail.mix then execution queue gets wrong information. A new list of processor is added and the current processor list is cleared.
The @talent
annotation should be applied differently when it is applied to a Class definition. The method defined in the talent should be part of the function as a property and not as a part of the prototype as it should be the case with @trait
Example:
cocktail.mix({
'@exports': module,
'@as' : 'class',
'@talents': [Create], // 1
'@traits' : [Configurable] // 2
});
In case (1) the methods defined in Create talent should be added as static methods of the class. In (2) the Configurable behaviour should be added to the class prototype.
Documentation site should be updated to add @exports annotation support.
The single parameter class/trait definition will work when the parameter subject has any of the following annotations:
constructor
methodCocktail.mix({
constructor: function(params) {/*body*/},
someAwesomeFun: function(){/*body*/}
});
pseudo-annotation @type should provide a way to specify the intent when single parameter definition is used as:
Move:
methods form Cocktail into a Test Helper Trait/Talent and refactor integration test to mix the Cocktail test instance to add the helper methods.
Explore ideas about having annotated functions such as:
'@annoation(someParam) funcName': function(){ .... }
The default value defined in properties annotation should not override the value if subject is an object and there is a property already defined with the same name:
var o1 = {anyProp: 'any'};
cocktail.mix(o1, {
'@properties': {
propName: 'default'
}
});
o1.getPropName(); // default
o1.propName; /// default
var o2 = {propName: 'defined'};
cocktail.mix(o2, {
'@properties': {
propName: 'default'
}
});
o2.getPropName(); // defined
o2.propName; /// defined
Hi,
I am having a hard time getting cocktail to work with requirejs. I thought I just wrap it up into the usual
if (typeof define === 'function' && define.amd) {
define(["require" ], function (require){
return factory(require('../sequence'));
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory(require('../sequence'));
}
But I always hit a wall with the relative path configurations. It is really hard to debug this problem. I thought I patch cocktail to submit a pull request but I can not get it to work. For now I will try to use browserify but it would be nice to have cocktailjs be compatible with the most common module loaders.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.