Skip to content

Commit 893ac0d

Browse files
committed
added: node.js generators for routes
1 parent 3f95a88 commit 893ac0d

8 files changed

Lines changed: 52 additions & 99 deletions

File tree

changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ source-code: "tabs" instead of "spaces"
3939
- added: global.GET([group], [name]) for getting new schemas (more in docs)
4040
- added: global.FINISHED(res/stream, callback) --> real end of the stream
4141
- added: global.DESTROY(stream) --> destroys the stream
42+
- added: (IMPORTANT) node.js generators for the routes
4243

4344
- updated: (IMPORTANT): for evaluation multiple roles in routing (@role) framework validates only one role
4445
- updated: GZIP compression for static files (added .md, .json)

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ Framework.prototype.route = function(url, funcExecute, flags, length, middleware
791791
var isNOXHR = false;
792792
var method = '';
793793
var schema;
794+
var isGENERATOR = false;
794795

795796
if (flags) {
796797

@@ -826,6 +827,14 @@ Framework.prototype.route = function(url, funcExecute, flags, length, middleware
826827
var flag = flags[i].toString().toLowerCase();
827828

828829
switch (flag) {
830+
831+
case 'sync':
832+
case 'yield':
833+
case 'synchronize':
834+
isGENERATOR = true;
835+
count--;
836+
continue;
837+
829838
case 'noxhr':
830839
case '-xhr':
831840
isNOXHR = true;
@@ -969,6 +978,7 @@ Framework.prototype.route = function(url, funcExecute, flags, length, middleware
969978
isJSON: flags.indexOf('json') !== -1,
970979
isXML: flags.indexOf('xml') !== -1,
971980
isRAW: isRaw,
981+
isGENERATOR: isGENERATOR,
972982
isMEMBER: isMember,
973983
isXSS: flags.indexOf('xss') !== -1,
974984
isASTERIX: isASTERIX,
@@ -7461,7 +7471,11 @@ Subscribe.prototype.doExecute = function() {
74617471
if (controller.isCanceled)
74627472
return self;
74637473

7464-
self.route.execute.apply(controller, framework_internal.routeParam(self.route.param.length > 0 ? framework_internal.routeSplit(req.uri.pathname, true) : req.path, self.route));
7474+
if (self.route.isGENERATOR)
7475+
async.call(controller, self.route.execute, controller, framework_internal.routeParam(self.route.param.length > 0 ? framework_internal.routeSplit(req.uri.pathname, true) : req.path, self.route))(controller);
7476+
else
7477+
self.route.execute.apply(controller, framework_internal.routeParam(self.route.param.length > 0 ? framework_internal.routeSplit(req.uri.pathname, true) : req.path, self.route));
7478+
74657479
return self;
74667480

74677481
} catch (err) {

test/controllers/default.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports.install = function(framework) {
2424
flags: ['unauthorize']
2525
});
2626

27+
framework.route('/sync/', synchronize, ['sync']);
2728
framework.route('/package/', '@testpackage/test');
2829
framework.route('/precompile/', view_precomile);
2930
framework.route('/homepage/', view_homepage);
@@ -119,6 +120,12 @@ exports.install = function(framework) {
119120
framework.websocket('/', socket);
120121
};
121122

123+
function *synchronize() {
124+
var self = this;
125+
var content = (yield sync(require('fs').readFile)(self.path.public('file.txt'))).toString('utf8');
126+
self.plain(content);
127+
}
128+
122129
function plain_rest() {
123130
this.plain(this.req.method);
124131
}

test/test-framework-debug.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ function test_routing(next) {
111111
});
112112
});
113113

114+
async.await('sync', function(complete) {
115+
utils.request(url + 'sync/', 'GET', null, function(error, data, code, headers) {
116+
if (error)
117+
throw error;
118+
assert.ok(data === 'TEST', 'generator problem');
119+
complete();
120+
});
121+
});
122+
114123
async.await('a/b', function(complete) {
115124
utils.request(url + 'c/b/', 'GET', null, function(error, data, code, headers) {
116125
if (error)

test/test-framework-release.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ function test_routing(next) {
111111
});
112112
});
113113

114+
async.await('sync', function(complete) {
115+
utils.request(url + 'sync/', 'GET', null, function(error, data, code, headers) {
116+
if (error)
117+
throw error;
118+
assert.ok(data === 'TEST', 'generator problem');
119+
complete();
120+
});
121+
});
122+
114123
async.await('a/b', function(complete) {
115124
utils.request(url + 'c/b/', 'GET', null, function(error, data, code, headers) {
116125
if (error)
@@ -531,7 +540,6 @@ function run() {
531540
});
532541
});
533542
}
534-
535543
/*
536544
var mem = require('memwatch');
537545

test/test-tests.js

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,3 @@
1-
var framework = require('../index');
1+
var cube = x => x * x * x;
22

3-
var q = SCHEMA('test').create('q');
4-
var x = SCHEMA('test').create('x');
5-
var w = SCHEMA('test').create('w');
6-
7-
w.define('firstname', 'String(10)');
8-
9-
q.define('name', 'string(5)', true);
10-
q.define('arr', '[x]', true);
11-
q.define('dep', w, true);
12-
x.define('age', Number, false);
13-
x.define('note', String, false);
14-
15-
q.setValidation(function(name, value, index) {
16-
// console.log('–––> q', name, value);
17-
});
18-
19-
x.setValidation(function(name, value) {
20-
// console.log('–––> x', name, value);
21-
});
22-
23-
var qi = {};
24-
qi.dep = {};
25-
qi.arr = [{ age: 300, note: 'FET' }];
26-
qi.dep.lastname = 'Sirka';
27-
qi.dep.firstname = 'Peter';
28-
qi.name = 'ASDASKDSADSALDKASLDJLSJ';
29-
30-
console.log(q.prepare(qi));
31-
32-
/*
33-
var xi = x.create();
34-
xi.age = 30;
35-
xi.note = 'Peter';
36-
qi.arr.push(xi);
37-
38-
xi = x.create();
39-
xi.age = 23;
40-
xi.note = 'Jano';
41-
qi.arr.push(xi);
42-
*/
43-
//qi.$validate();
44-
45-
Array.prototype.compare = function(id, arr, keys, callback) {
46-
47-
var self = this;
48-
var cache = {};
49-
50-
for (var i = 0, length = arr.length; i < length; i++) {
51-
var key = arr[i][id];
52-
if (key === undefined)
53-
continue;
54-
cache[key] = i;
55-
}
56-
57-
var kl = keys.length;
58-
59-
for (var i = 0, length = self.length; i < length; i++) {
60-
var a = self[i];
61-
var key = a[id];
62-
63-
if (!key)
64-
continue;
65-
66-
var index = cache[key];
67-
68-
if (index === undefined) {
69-
callback(0, item);
70-
continue;
71-
}
72-
73-
var b = arr[index];
74-
75-
for (var j = 0; j < kl; j++) {
76-
77-
var k = keys[j];
78-
var av = a[k];
79-
var bv = b[k];
80-
81-
if (av === bv)
82-
continue;
83-
84-
85-
}
86-
87-
}
88-
};
89-
90-
var arr1 = [{ id: 1, name: 'Peter', age: 25 }, { id: 2, name: 'Lucia', age: 19 }, { id: 3, name: 'Jozef', age: 33 }];
91-
var arr2 = [{ id: 2, name: 'Lucka', age: 5 }, { id: 3, name: 'Peter', age: 50 }, { id: 1, name: 'Peter', age: 20 }, { id: 5, name: 'New', age: 33 }];
92-
93-
arr1.compare('id', arr2, ['name', 'age'], function(type, item) {
94-
console.log(type, item);
95-
});
3+
console.log(cube(23));

test/views/fromURL.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4173,11 +4173,18 @@ exports.sync = function(fn, owner) {
41734173
};
41744174

41754175
exports.async = function(fn) {
4176+
var context = this;
41764177
return function(complete) {
41774178

41784179
var self = this;
4179-
var generator = fn();
4180+
var argv = [];
41804181

4182+
if (arguments.length > 0) {
4183+
for (var i = 1; i < arguments.length; i++)
4184+
argv.push(arguments[i]);
4185+
}
4186+
4187+
var generator = fn.apply(context, argv);
41814188
next(null);
41824189

41834190
function next(err, result) {
@@ -4345,4 +4352,4 @@ exports.minifyHTML = function(value) {
43454352
};
43464353

43474354
global.async = exports.async;
4348-
global.sync = exports.sync;
4355+
global.sync = global.SYNCHRONIZE = exports.sync;

0 commit comments

Comments
 (0)