Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove obsolete files

  • Loading branch information...
commit 4a0f1c12d33400373cdda4c5d9020e80a7cf6c4e 1 parent d6989fc
@akaspin authored
Showing with 9 additions and 472 deletions.
  1. +9 −0 ChangeLog
  2. +0 −241 compiler_old.js
  3. +0 −61 hstream.md
  4. +0 −170 hstream_old.js
View
9 ChangeLog
@@ -0,0 +1,9 @@
+In work, Version 0.1.11
+
+* Serious performance boost - faster three times
+
+* Remove obsolete tests and files
+
+2010.06.15, Version 0.1.10
+
+* Stable version
View
241 compiler_old.js
@@ -1,241 +0,0 @@
-var sys = require("sys");
-var Script = process.binding('evals').Script;
-var HStream = require("./hstream").HStream;
-
-var bundle = {
- HStream : HStream,
- section : section,
- sectionNormal : sectionNormal,
- sectionInverted : sectionInverted,
- lookup : lookup,
- unescaped : unescaped,
- process : process
-};
-
-/**
- * Compile parsed template to native JS function.
- * @param stream Parsed template. Represented as Array of
- * {op:string, value: string}
- * @param callback Function that takes compiled function.
- */
-function compile(stream, callback) {
- var codeStart =
- '__fn=function(context) {\n' +
- 'context = context || {};\n' +
- 'var HS = new HStream();\n' +
- 'var target = context;\n' +
- 'var prefix = "";\n' +
- 'process.nextTick(function() { var __a = [\n';
- var codeEnd =
- 'function() {HS.end("");}];\n' +
- 'for(var i=0;i<__a.length;i++){__a[i]()}\n' +
- '});\n' +
- 'return HS; }';
-
- var code = codeStart;
-
- var resolveTarget = function(name) {
- return (name.charAt(0) === '.' ?
- "context" + name : "target." + name);
- };
- stream.forEach(function(chunk) {
- if (chunk.op === 'raw') {
- code += 'function() {HS.map(prefix+i); HS.write(prefix+i,"' +
- chunk.value.replace(/\n/g, "\\n")
- + '");},\n';
- } else if (chunk.op === 'lookup' || chunk.op === 'unescaped') {
- code += 'function(){HS.map(prefix+i);' +
- chunk.op +
- '(HS,prefix+i,' +
- resolveTarget(chunk.value) +
- ',target);},\n';
- } else if (chunk.op === 'section') {
- code += 'function() {section(HS,sectionNormal,prefix+i,' +
- resolveTarget(chunk.value) +
- ',target,function(prefix,target){var __a = [\n';
- } else if (chunk.op === 'inverted') {
- code += 'function() {section(HS,sectionInverted,prefix+i,' +
- resolveTarget(chunk.value) +
- ',target,function(prefix,target){var __a = [\n';
- } else if (chunk.op === 'end') {
- code += ']; for(var i=0;i<__a.length;i++){__a[i]()} }); },\n';
- }
- });
-
- //sys.debug(code+ codeEnd);
- callback(Script.runInNewContext(code + codeEnd, bundle));
-}
-exports.compile = compile;
-
-// private
-/**
- * Section evaluation. Behaviour depends type of target parameter.
- *
- * @param hStream HStream instance
- * @param manner Normal or inverted section: sectionNormal or sectionInverted.
- * This calls if context is not function or if function is not Lambda.
- * @param id Root ID
- * @param target Lookup for in context
- * @param context Local context
- * @param action Bundle of actions
- */
-function section(hStream, manner, id, target, context, action) {
- hStream.map(id); // mapping root
-
- if (typeof target === 'function') { // Function
- var target = target(context);
- if (typeof target === 'function') { // Async function
- target(context, function(err, target) {
- if (typeof target === 'function') {// lambda
- hStream.lambda(id, function(data, callback) {
- target(data, context, function(data) {
- callback(typeof data === 'undefined' ? ''
- : data.toString());
- });
- });
- action(id + "/", context);
- hStream.end(id);
- } else { // No lambda
- manner(hStream, id, target, context, action);
- }
- });
- } else { // Sync function
- manner(hStream, id, target, context, action);
- }
- } else { // Just context
-
- manner(hStream, id, target, context, action);
- }
-}
-
-/**
- * Section evaluation with data.
- *
- * @param hStream HStream instance
- * @param id Root ID
- * @param target Lookup for in context
- * @param context Local context
- * @param action Bundle of actions
- */
-function sectionNormal(hStream, id, target, context, action) {
- if (target instanceof Array && target.length) {
- var streamId = id;
- for (var i = 0; i < target.length; i++) {
- var subid = streamId + "/" + i;
- hStream.map(subid);
- action(subid, target[i]);
- hStream.end(subid);
- }
- hStream.end(id);
- } else if (
- target !== undefined &&
- target != "" &&
- target != 0 &&
- target != false &&
- target != null &&
- typeof target !== 'object') {
- action(id + "/", context);
- hStream.end(id);
- } else if (typeof target === 'object' && target != null) {
- if (!Object.keys(target).length){
- hStream.write(id, "");
- } else {
- action(id + "/", target);
- hStream.end(id);
- }
- } else {
- hStream.write(id, "");
- }
-};
-
-/**
- * Inverted section evaluation with data.
- *
- * @param hStream HStream instance
- * @param id Root ID
- * @param target Lookup for in context
- * @param context Local context
- * @param action Bundle of actions
- */
-function sectionInverted(hStream, id, target, context, action) {
- if (target == undefined ||
- target == "" ||
- target == false ||
- target == null ||
- target == 0 ||
- (target instanceof Array && !target.length) ||
- (typeof target === 'object' && !Object.keys(target).length)
- ) {
- action(id + "/", target);
- hStream.end(id);
- } else {
- hStream.write(id, "");
- }
-};
-
-/**
- * Lookup for variable.
- *
- * @param hStream HStream instance
- * @param id Root ID
- * @param target Lookup for
- * @param context Context
- */
-function lookup(hStream, id, target, context) {
- function escapeReplace (char) {
- switch (char) {
- case '<': return '&lt;';
- case '>': return '&gt;';
- case '&': return '&amp;';
- case '"': return '&quot;';
- default: return char;
- }
- };
- function out(string) {
- return typeof string === 'undefined' ? '' : string.toString()
- .replace(/[&<>"]/g, escapeReplace);
- };
- if (typeof target === 'function'){ // Lambda
- var target = target(context);
- if (typeof target === 'function') { // Async
- hStream.lambda(id, function(data, callback) {
- target(context, function(err, data) {
- callback(out(data));
- }); });
- hStream.write(id, "");
- } else {
- hStream.write(id, out(target));
- }
- } else {
- hStream.write(id, out(target));
- }
-}
-
-/**
- * Some as lookup. But not escape HTML characters.
- *
- * @param hStream HStream instance
- * @param id Root ID
- * @param target Lookup for
- * @param context Context
- */
-function unescaped(hStream, id, target, context) {
- function out (string) {
- return typeof string === 'undefined' ? '' : string.toString();
- };
- if (typeof target === 'function'){ // Lambda
- var target = target(context);
- if (typeof target === 'function') { // Async
- hStream.lambda(id, function(data, callback) {
- target(context, function(err, data) {
- callback(out(data));
- }); });
- hStream.write(id, "");
- } else {
- hStream.write(id, out(target));
- }
- } else {
- hStream.write(id, out(target));
- }
-}
-
View
61 hstream.md
@@ -1,61 +0,0 @@
-# HStream
-
-Implementation of hierarchial mapped stream.
-
-## Usage
-
-Unlike standart stream, HStream writed to handle hierarchial structures that
-creates in async manner. So. Following code...
-
- var sys = require("sys");
- var hs = require("hstream").HStream;
-
- var lambda = function(data, callback) {
- setTimeout(function() {
- callback("[" + data + "]");
- },300);
- };
-
- var hs = new HStream();
- var buffer = '';
- var hs = new HStream()
- .addListener('data', function(data) {
- buffer += data + ":";
- })
- .addListener('end', function() {
- sys.puts(buffer);
- });
-
- hs.map("0"); hs.map('0');
- hs.map('1');
- hs.map('2');
- hs.map('3');
- hs.map('4');
- hs.write('2', '<2>');
- hs.end('');
-
- hs.map('1/1');
- hs.map('1/2');
- hs.end('1');
-
- hs.map('1/2/1');
- hs.map('1/2/2');
- hs.end('1/2');
-
- hs.lambda('1', lambda); // lambda to substructure
- hs.lambda('3', lambda); // lambda to single chunk
-
- hs.write('1/2/1', '<1/2/1>');
- hs.write('1/2/2', '<1/2/2>');
-
- hs.write('0', '<1>');
- hs.write('3', '<3>');
- hs.write('4', '<4>');
- hs.write('1/1', '<1/1>');
-
-... will produce this output:
-
- <1>:[<1/1><1/2/1><1/2/2>]:<2>:[<3>]:<4>:
-
-As we see, data emits as soon as possible.
-
View
170 hstream_old.js
@@ -1,170 +0,0 @@
-var sys = require("sys");
-var path = require("path");
-var events = require('events');
-
-/**
- * Hierarchial Stream. Heart of Nun.
- * @returns {HStream}
- * @constructor
- */
-function HStream() {
- events.EventEmitter.call(this);
- this.writes = [];
- this.stream = [];
- this.closed = false;
-}
-sys.inherits(HStream, events.EventEmitter);
-exports.HStream = HStream;
-
-/**
- * Map stream node.
- *
- * @param id String identifier in form "[parent/[parent.../]]id"
- */
-HStream.prototype.map = function (id) {
- var id = id.split("/");
-
- var target = this;
- if (id.length > 1) {
- var target = this.__getChunk(id.slice(0,-1));
- if (target.stream === undefined) target.stream = [];
- if (target.data === undefined) target.data = '';
- }
-
- target.stream.push({
- id:id[id.length-1],
- data: undefined,
- proc: undefined,
- stream: undefined
- });
-};
-
-/**
- * Set lambda to chunk.
- *
- * @param id String identifier in form "[parent/[parent.../]]id"
- * @param lambda Function that takes two arguments: data to transform and
- * callback. Callback takes one argument - warped data.
- */
-HStream.prototype.lambda = function(id, lambda) {
- var id = id.split("/");
- var target =this.__getChunk(id);
- target.proc = lambda;
- this.__process(id);
-};
-
-
-HStream.prototype.write = function(id, data) {
- var id = id.split("/");
- this.__write(id, data);
-};
-HStream.prototype.__write = function(id, data){
- var chunk =this.__getChunk(id);
-
- if (typeof chunk.proc === 'function') { // it's lambda
- var lambda = chunk.proc;
- chunk.proc = undefined;
- var self = this;
- lambda(data, function(data) {
- self.__write(id, data);
- });
- } else {
- chunk.data = data;
- chunk.stream = true;
- this.__process(id);
- }
-};
-
-HStream.prototype.__process = function(id) {
- if (id.length == 1) {
- this.__poll();
- } else {
- var parent = this.__getChunk(id.slice(0,-1));
- var stream = parent.stream;
- var buffer = parent.data;
- var ended = false;
- while (stream.length) {
- if (stream[0].stream === true) {
- var chunk = stream.shift();
- buffer += chunk.data;
- } else if (stream[0].id === '_') {
- stream = [];
- ended = true;
- } else {
- break;
- }
- }
- if (!ended) {
- parent.data = buffer;
- this.__poll();
- } else {
- parent.data = undefined;
- parent.stream = undefined;
- this.write(id.slice(0,-1).join("/"), buffer);
- //this.__poll();
- }
- }
-};
-HStream.prototype.end = function(id) {
- if (id == '' ) {
- this.map("_");
- this.__poll();
- } else {
- var streamId = id.split("/");
- var id = id + "/_";
- this.map(id);
- this.__process(id.split("/"));
- }
-};
-HStream.prototype.__poll = function() {
- if (this.closed) return;
- var stream = this.stream;
- while (stream.length) {
- if ((stream[0].data != undefined) &&
- (stream[0].proc === undefined )) {
-
- if (stream[0].stream === true) {
- if (stream[0].data.length > 0) {
- this.emit('data', stream[0].data);
- }
- stream.shift();
- } else if (stream[0].stream instanceof Array &&
- stream[0].data !== '') {
- this.emit('data', stream[0].data);
- stream[0].data = '';
- break;
- } else {
- break;
- }
- } else if (this.stream[0].id == '_') {
- this.emit('end');
- stream = [];
- this.closed = true;
- break;
- } else {
- break;
- }
- }
-};
-/**
- * returns chunk for id
- * @param id Id
- * @returns
- */
-HStream.prototype.__getChunk = function(id) {
- var stream = this.stream;
- for (var i=0; i<id.length; i++) {
- for (var j=0; j < stream.length; j++) {
- if (stream[j].id == id[i]) {
- if (i == id.length-1) {
- // found
- return stream[j];
- } else {
- stream = stream[j].stream;
- break;
- }
- }
- }
- }
- return undefined;
-};
Please sign in to comment.
Something went wrong with that request. Please try again.