Skip to content

Commit

Permalink
Merge pull request #8 from pedronasser/promise
Browse files Browse the repository at this point in the history
v0.0.9
  • Loading branch information
Pedro Nasser committed Oct 2, 2013
2 parents 7cb82b8 + aec4eea commit 8e66233
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![WNS](http://dl.dropbox.com/u/21773527/WNS-Logo.png)](http://wns.yept.net)

## Welcome to WNS Middleware (v0.0.8) [![Build Status](https://travis-ci.org/yeptlabs/wns.png?branch=master)](https://travis-ci.org/yeptlabs/wns)
## Welcome to WNS Middleware (v0.0.9) [![Build Status](https://travis-ci.org/yeptlabs/wns.png?branch=master)](https://travis-ci.org/yeptlabs/wns)

##### What's the idea?
To build your application/service, just installing packages, configuring as you like and extending **COMPONENTS** and **PACKAGES** (if you want).
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wnserver",
"description": "A high-performance, scalable and component-based middleware",
"version": "0.0.8",
"version": "0.0.9",
"author": "Pedro Nasser <pedro@yept.net>",
"devDependencies": {
"chai": "*",
Expand All @@ -16,7 +16,8 @@
"querystring": "~0.2.0",
"underscore": "~1.5.1",
"node-uuid": "~1.4.1",
"npm": "~1.3.11"
"npm": "~1.3.11",
"q": "0.9.7"
},
"keywords": [
"web",
Expand Down
26 changes: 26 additions & 0 deletions src/core/base/wnComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,32 @@ module.exports = {
return (file ? (binary===true) ? file : file.toString() : false);
},

/**
* Promise: Get a file.
* The file's path is relative to the module's path.
* @param string $filePath file's path
* @param boolean $buffer return as buffer?
* @return promise
*/
$getFile: function (filePath,buffer)
{
var realPath = done.promise.filePath = this.instanceOf('wnModule')?this.modulePath+filePath:filePath;
done.promise.returnAsBuffer=buffer=buffer || true;

fs.readFile(realPath,function (err,text) {
if (err)
done.reject(err);
else
{
if (!buffer)
text=text.toString('utf8');
done.resolve(text);
}
});

return done.promise;
},

/**
* Get a file statistic.
* The file's path is relative to the module's path.
Expand Down
33 changes: 25 additions & 8 deletions src/wnBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,32 @@ wnBuild.prototype.load = function (className)

for (c in loadList)
{
module = { exports: {} };
source = this.classesCode[c];
this.classesBuild[c] = { extend: [], public: {}, private: {}, methods:{}, dependencies: {}};

// Eval class source or a list of sources.
if (typeof source == 'string')
{
eval(source);
this.extend(this.classesBuild[c],module.exports,c);
var ctx = { module: { exports: {} } };
try {
vm.runInNewContext(source,ctx,c+'.js');
} catch (e)
{
throw new Error(e.message+' - building `'+c+'` class');
}
this.extend(this.classesBuild[c],ctx.module.exports,c);
}
else if (source instanceof Array)
for (s in source)
{
eval(source[s]);
this.extend(this.classesBuild[c],module.exports,c);
var ctx = { module: { exports: {} } };
try {
vm.runInNewContext(source[s],ctx,c+'.js');
} catch (e)
{
throw new Error(e.message+' - building `'+c+'` class');
}
this.extend(this.classesBuild[c],ctx.module.exports,c);
}
}

Expand Down Expand Up @@ -223,7 +234,6 @@ wnBuild.prototype.buildClass = function (className)

// Create a function to create a new instance from the prototype when called.
var build = self.classesBuild[className];
// var ctx = vm.createContext(global);
// ctx.builder = this;
var evalBuilder = "var classBuilder = function () {\n";
evalBuilder += ' this.builder.vmScript[className].runInThisContext();\n';
Expand Down Expand Up @@ -354,10 +364,17 @@ wnBuild.prototype.compileClass = function (targetClass)
}

// Declare privileged methods
var promiseRegExp = new RegExp('^\\\$');
classSource += '\n// Declaring privileged methods\n';
for (m in build.methods)
{
classSource += "proto['"+m+"'] = "+build.methods[m].toString()+";\n";
var methodName=m;
var fn = build.methods[m].toString();
if (promiseRegExp.test(m))
{
classSource += "proto['"+methodName+"'] = function () { var done=q.defer(); var self = this; return ("+fn+").apply(self,arguments); };\n";
} else
classSource += "proto['"+methodName+"'] = "+fn+";\n";
}

// Declare the constructor.
Expand All @@ -379,7 +396,7 @@ wnBuild.prototype.compileClass = function (targetClass)
this.compiledSource[targetClass] = classLoader;

// Create a VM.Script from the classLoader;
this.vmScript[targetClass] = vm.createScript(classLoader);
this.vmScript[targetClass] = vm.createScript(classLoader,targetClass+'.js');
} catch (e)
{
console.log('\nError on compiling `'+targetClass+'`: '+e.message);
Expand Down

0 comments on commit 8e66233

Please sign in to comment.