Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tdecaluwe committed Mar 16, 2016
1 parent 5ac5797 commit 2a349f1
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @author Tom De Caluwé
* @copyright 2016 Tom De Caluwé
* @license Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict'

var EventEmitter = require('events');

var Configuration = require('./configuration.js');
var Tokenizer = require('./tokenizer.js');

var push = function (encoder, chunk) {
if (encoder._paused) {
this._buffer.push(chunk);
} else {
encoder.emit('data', chunk);
}
}

var Encoder = function () {
EventEmitter.call(this);
this._buffer = [];
this._paused = true;
this._start = 0;
this._configuration = new Configuration();
this._tokenizer = new Tokenizer(this._configuration);
}

Encoder.prototype = Object.create(EventEmitter.prototype);

Encoder.prototype.opensegment = function (segment) {
this.push(segment);
}

Encoder.prototype.element = function (components) {
}

Encoder.prototype.pause = function () {
this._paused = true;
}

Encoder.prototype.pipe = function (destination) {
destination.setDefaultEncoding('utf8');
this.on('data', function (chunk) {
destination.write(chunk);
});
}

Encoder.prototype.resume = function () {
for (var i = 0; i < this._buffer.length; i++) {
this.emit('data', this._buffer[i]);
}
this._buffer.length = 0;
this._paused = false;
}

Encoder.prototype.read = function () {
return this._buffer[this._start++];
}

Encoder.prototype.unpipe = function (destination) {}

Encoder.prototype.unshift = function (chunk) {
if (this._paused) {
this._buffer.unshift(chunk);
} else {
this.emit('data', chunk);
}
}

Encoder.states = {
empty: 0,
segment: 1,
element: 2,
component: 3,
data: 4
};

0 comments on commit 2a349f1

Please sign in to comment.