Skip to content

Commit

Permalink
[stream] Build is based on ts
Browse files Browse the repository at this point in the history
  • Loading branch information
smialy committed Nov 9, 2018
1 parent 4694d6c commit a5f5f33
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 827 deletions.
5 changes: 5 additions & 0 deletions packages/sjs-stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# sjs-stream


## License
MIT
36 changes: 36 additions & 0 deletions packages/sjs-stream/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Karma configuration
const typescript = require('rollup-plugin-typescript');

module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['qunit'],
files: [
'tests/*.js',
'src/*.js'
],
preprocessors: {
'tests/*.js': ['rollup'],
'src/*.js': ['rollup']
},
rollupPreprocessor: {
plugins: [
typescript()
],
output: {
format: 'iife',
name: 'events',
sourcemap: 'inline'
}
},
reporters: ['progress'],
// web server port
port: 9876,
colors: true,

// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_WARN,
autoWatch: true,
browsers: ['Chrome', 'Firefox']
});
};
43 changes: 24 additions & 19 deletions packages/sjs-stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,44 @@
"version": "2.0.0",
"description": "Stream",
"license": "MIT",
"main": "src/index.js",
"main": "dist/node/index.js",
"module": "dist/esm/index.js",
"types": "types/index.d.ts",
"homepage": "https://github.com/smialy/sjs#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/smialy/sjs.git"
},
"scripts": {
"build": "tsc",
"lint": "eslint src"
"build": "npm run build:clean && npm run build:tsc && npm run build:exts",
"build:clean": "rm -rf dist/ && rm -rf types",
"build:tsc": "tsc && tsc -p tsconfig.esm.json",
"build:exts": "renamer --path-element ext --find js --replace mjs dist/esm/*",
"test": "qunit --require ts-node/register tests/*",
"karma": "karma start --single-run",
"lint": "eslint tests && tslint src/*"
},
"files": [
"README.md",
"src/*.js",
"dist/*.js",
"types/*.ts"
],
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@babel/register": "^7.0.0",
"eslint": "^5.0.0",
"eslint": "^5.7.0",
"karma": "^3.1.1",
"karma-chrome-launcher": "^2.1.1",
"karma-firefox-launcher": "^1.0.1",
"karma-qunit": "^2.0.1",
"karma-rollup-preprocessor": "^6.0.0",
"qunit": "^2.6.0",
"karma-chrome-launcher": "^2.2.0",
"karma-firefox-launcher": "^1.1.0",
"karma-qunit": "^2.1.0",
"karma-rollup-preprocessor": "^6.1.0",
"qunit": "^2.7.1",
"renamer": "^1.1.0",
"rollup": "^0.66.6",
"typescript": "^3.0.1"
},
"babel": {
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-typescript": "^1.0.0",
"ts-node": "^7.0.1",
"tslib": "^1.9.3",
"tslint": "^5.11.0",
"typescript": "^3.1.6"
}
}
111 changes: 0 additions & 111 deletions packages/sjs-stream/src/controller.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
import { IStream, IControlerSubscribe, IStreamSubscription, IControllerOptions, IObserverListener } from './interfaces';
import { IControlerSubscribe, IControllerOptions, IObserverListener, IStream, IStreamSubscription } from './interfaces';
import { BufferStreamSubscription, Stream } from './stream';
import { noop, runMethod } from './utils';
import { Stream, BufferStreamSubscription } from './stream';


const STATE_INIT = 1;
const STATE_CLOSED = 2;

const defaultOptions = {
onListen(){},
onCancel(){}
}
onListen() {/**/},
onCancel() {/**/},
};

export class StreamController<T> implements IControlerSubscribe<T> {

private _state: number = STATE_INIT;
private _subscription: BufferStreamSubscription<T> | null = null;
private _options: IControllerOptions={};
private _options: IControllerOptions = {};

constructor(options: IControllerOptions={}) {
constructor(options: IControllerOptions= {}) {
this._options = Object.assign({}, options, defaultOptions);
}

add(value: T){
if(this._state & STATE_CLOSED){
public add(value: T) {
if (this._state & STATE_CLOSED) {
throw new Error('Cannot add() after close.');
}
if(this._subscription){
if (this._subscription) {
this._subscription._addData(value);
}
}

addError(error: any){
if(this._state & STATE_CLOSED){
public addError(error: any) {
if (this._state & STATE_CLOSED) {
throw new Error('Cannot addError() after close.');
}
if(this._subscription){
if (this._subscription) {
this._subscription._addError(error);
}
}

close(): void {
if(this._state & STATE_CLOSED){
public close(): void {
if (this._state & STATE_CLOSED) {
return;
}
if(this._subscription){
if (this._subscription) {
this._subscription._close();
}
}
Expand All @@ -52,8 +51,8 @@ export class StreamController<T> implements IControlerSubscribe<T> {
return new ControllerStream<T>(this);
}

_subscribe(listener: IObserverListener<T>): IStreamSubscription {
if(this._subscription !== null){
public _subscribe(listener: IObserverListener<T>): IStreamSubscription {
if (this._subscription !== null) {
throw new Error('Stream has already was listen.');
}
this._subscription = new BufferStreamSubscription(listener, () => {
Expand All @@ -69,34 +68,34 @@ export class EventController<T> implements IControlerSubscribe<T> {

private _state: number = STATE_INIT;
private _subscriptions: Set<BufferStreamSubscription<T>> = new Set();
private _options: IControllerOptions={}
private _options: IControllerOptions = {};

constructor(options: IControllerOptions) {
this._options = Object.assign({}, options, defaultOptions);
}

add(value: T){
if(this._state & STATE_CLOSED){
public add(value: T) {
if (this._state & STATE_CLOSED) {
throw new Error('Cannot add() after close.');
}
for(let subscription of this._subscriptions){
for (const subscription of this._subscriptions) {
subscription._addData(value);
}
}

addError(error: any){
if(this._state & STATE_CLOSED){
public addError(error: any) {
if (this._state & STATE_CLOSED) {
throw new Error('Cannot addError() after close.');
}
for(let subscription of this._subscriptions){
for (const subscription of this._subscriptions) {
subscription._addError(error);
}
}
close(): void {
if(this._state & STATE_CLOSED){
public close(): void {
if (this._state & STATE_CLOSED) {
throw new Error('Cannot close() after close.');
}
for(let subscription of this._subscriptions){
for (const subscription of this._subscriptions) {
subscription._close();
}
}
Expand All @@ -105,15 +104,15 @@ export class EventController<T> implements IControlerSubscribe<T> {
return new ControllerStream<T>(this);
}

_subscribe(listener: IObserverListener<T>): IStreamSubscription {
const subscription = new BufferStreamSubscription(listener, subscription => {
this._subscriptions.delete(subscription);
if(this._subscriptions.size === 0){
public _subscribe(listener: IObserverListener<T>): IStreamSubscription {
const subscription = new BufferStreamSubscription(listener, (sub) => {
this._subscriptions.delete(sub);
if (this._subscriptions.size === 0) {
runMethod(this._options.onCancel);
}
});
this._subscriptions.add(subscription);
if(this._subscriptions.size === 1){
if (this._subscriptions.size === 1) {
runMethod(this._options.onListen);
}
return subscription;
Expand All @@ -122,10 +121,10 @@ export class EventController<T> implements IControlerSubscribe<T> {

class ControllerStream<T> extends Stream<T> {

constructor(private _controller: IControlerSubscribe<T>){
constructor(private _controller: IControlerSubscribe<T>) {
super();
}
_createSubscription(listener: IObserverListener<T>): IStreamSubscription {
public _createSubscription(listener: IObserverListener<T>): IStreamSubscription {
return this._controller._subscribe(listener);
}
}
Loading

0 comments on commit a5f5f33

Please sign in to comment.