@ayonli/jsext
Advanced tools
Comparing version 0.5.6 to 0.5.7
42
chan.ts
@@ -5,4 +5,4 @@ export class Channel<T> implements AsyncIterable<T> { | ||
private buffer: T[] = []; | ||
private pub?: () => T; | ||
private sub?: (err: Error | null, data?: T) => void; | ||
private producers: (() => T)[] = []; | ||
private consumers: ((err: Error | null, data?: T) => void)[] = []; | ||
private error?: Error | null; | ||
@@ -35,4 +35,5 @@ private state: 0 | 1 | 2 = 1; | ||
throw new Error("the channel is closed"); | ||
} else if (this.sub) { | ||
return Promise.resolve(this.sub(null, data)); | ||
} else if (this.consumers.length) { | ||
const consume = this.consumers.shift() as (err: Error | null, data?: T) => void; | ||
return Promise.resolve(consume(null, data)); | ||
} else if (this.capacity && this.buffer.length < this.capacity) { | ||
@@ -43,15 +44,13 @@ this.buffer.push(data); | ||
return new Promise<void>(resolve => { | ||
this.pub = () => { | ||
this.producers.push(() => { | ||
if (this.capacity) { | ||
const _data = this.buffer.shift(); | ||
this.buffer.push(data); | ||
this.pub = undefined; | ||
resolve(); | ||
return _data as T; | ||
} else { | ||
this.pub = undefined; | ||
resolve(); | ||
return data; | ||
} | ||
}; | ||
}); | ||
}); | ||
@@ -81,5 +80,10 @@ } | ||
return Promise.resolve(data); | ||
} else if (this.pub) { | ||
this.state === 2 && (this.state = 0); | ||
return Promise.resolve(this.pub()); | ||
} else if (this.producers.length) { | ||
const produce = this.producers.shift() as () => T; | ||
if (this.state === 2 && !this.producers.length) { | ||
this.state = 0; | ||
} | ||
return Promise.resolve(produce()); | ||
} else if (this.state === 0) { | ||
@@ -98,7 +102,9 @@ return Promise.resolve(undefined); | ||
return new Promise<T>((resolve, reject) => { | ||
this.sub = (err: unknown, data?: T) => { | ||
this.state === 2 && (this.state = 0); | ||
this.sub = undefined; | ||
this.consumers.push((err: unknown, data?: T) => { | ||
if (this.state === 2 && !this.consumers.length) { | ||
this.state = 0; | ||
} | ||
err ? reject(err) : resolve(data as T); | ||
}; | ||
}); | ||
}); | ||
@@ -120,3 +126,7 @@ } | ||
this.error = err; | ||
this.sub?.(err, undefined); | ||
let consume: ((err: Error | null, data?: T) => void) | undefined; | ||
while (consume = this.consumers.shift()) { | ||
consume(err, undefined); | ||
} | ||
} | ||
@@ -123,0 +133,0 @@ |
@@ -8,2 +8,4 @@ 'use strict'; | ||
this.buffer = []; | ||
this.producers = []; | ||
this.consumers = []; | ||
this.state = 1; | ||
@@ -32,4 +34,5 @@ if (capacity < 0) { | ||
} | ||
else if (this.sub) { | ||
return Promise.resolve(this.sub(null, data)); | ||
else if (this.consumers.length) { | ||
const consume = this.consumers.shift(); | ||
return Promise.resolve(consume(null, data)); | ||
} | ||
@@ -42,7 +45,6 @@ else if (this.capacity && this.buffer.length < this.capacity) { | ||
return new Promise(resolve => { | ||
this.pub = () => { | ||
this.producers.push(() => { | ||
if (this.capacity) { | ||
const _data = this.buffer.shift(); | ||
this.buffer.push(data); | ||
this.pub = undefined; | ||
resolve(); | ||
@@ -52,7 +54,6 @@ return _data; | ||
else { | ||
this.pub = undefined; | ||
resolve(); | ||
return data; | ||
} | ||
}; | ||
}); | ||
}); | ||
@@ -80,5 +81,8 @@ } | ||
} | ||
else if (this.pub) { | ||
this.state === 2 && (this.state = 0); | ||
return Promise.resolve(this.pub()); | ||
else if (this.producers.length) { | ||
const produce = this.producers.shift(); | ||
if (this.state === 2 && !this.producers.length) { | ||
this.state = 0; | ||
} | ||
return Promise.resolve(produce()); | ||
} | ||
@@ -101,7 +105,8 @@ else if (this.state === 0) { | ||
return new Promise((resolve, reject) => { | ||
this.sub = (err, data) => { | ||
this.state === 2 && (this.state = 0); | ||
this.sub = undefined; | ||
this.consumers.push((err, data) => { | ||
if (this.state === 2 && !this.consumers.length) { | ||
this.state = 0; | ||
} | ||
err ? reject(err) : resolve(data); | ||
}; | ||
}); | ||
}); | ||
@@ -120,6 +125,8 @@ } | ||
close(err = null) { | ||
var _a; | ||
this.state = 2; | ||
this.error = err; | ||
(_a = this.sub) === null || _a === void 0 ? void 0 : _a.call(this, err, undefined); | ||
let consume; | ||
while (consume = this.consumers.shift()) { | ||
consume(err, undefined); | ||
} | ||
} | ||
@@ -126,0 +133,0 @@ [Symbol.asyncIterator]() { |
@@ -9,3 +9,3 @@ 'use strict'; | ||
function avg(...values) { | ||
return Math.sum(...values) / values.length; | ||
return sum(...values) / values.length; | ||
} | ||
@@ -12,0 +12,0 @@ /** Returns a the product value multiplied by the given values. */ |
class Channel { | ||
constructor(capacity = 0) { | ||
this.buffer = []; | ||
this.producers = []; | ||
this.consumers = []; | ||
this.state = 1; | ||
@@ -27,4 +29,5 @@ if (capacity < 0) { | ||
} | ||
else if (this.sub) { | ||
return Promise.resolve(this.sub(null, data)); | ||
else if (this.consumers.length) { | ||
const consume = this.consumers.shift(); | ||
return Promise.resolve(consume(null, data)); | ||
} | ||
@@ -37,7 +40,6 @@ else if (this.capacity && this.buffer.length < this.capacity) { | ||
return new Promise(resolve => { | ||
this.pub = () => { | ||
this.producers.push(() => { | ||
if (this.capacity) { | ||
const _data = this.buffer.shift(); | ||
this.buffer.push(data); | ||
this.pub = undefined; | ||
resolve(); | ||
@@ -47,7 +49,6 @@ return _data; | ||
else { | ||
this.pub = undefined; | ||
resolve(); | ||
return data; | ||
} | ||
}; | ||
}); | ||
}); | ||
@@ -75,5 +76,8 @@ } | ||
} | ||
else if (this.pub) { | ||
this.state === 2 && (this.state = 0); | ||
return Promise.resolve(this.pub()); | ||
else if (this.producers.length) { | ||
const produce = this.producers.shift(); | ||
if (this.state === 2 && !this.producers.length) { | ||
this.state = 0; | ||
} | ||
return Promise.resolve(produce()); | ||
} | ||
@@ -96,7 +100,8 @@ else if (this.state === 0) { | ||
return new Promise((resolve, reject) => { | ||
this.sub = (err, data) => { | ||
this.state === 2 && (this.state = 0); | ||
this.sub = undefined; | ||
this.consumers.push((err, data) => { | ||
if (this.state === 2 && !this.consumers.length) { | ||
this.state = 0; | ||
} | ||
err ? reject(err) : resolve(data); | ||
}; | ||
}); | ||
}); | ||
@@ -115,6 +120,8 @@ } | ||
close(err = null) { | ||
var _a; | ||
this.state = 2; | ||
this.error = err; | ||
(_a = this.sub) === null || _a === void 0 ? void 0 : _a.call(this, err, undefined); | ||
let consume; | ||
while (consume = this.consumers.shift()) { | ||
consume(err, undefined); | ||
} | ||
} | ||
@@ -121,0 +128,0 @@ [Symbol.asyncIterator]() { |
@@ -7,3 +7,3 @@ /** Returns the sum value of the given values. */ | ||
function avg(...values) { | ||
return Math.sum(...values) / values.length; | ||
return sum(...values) / values.length; | ||
} | ||
@@ -10,0 +10,0 @@ /** Returns a the product value multiplied by the given values. */ |
@@ -8,3 +8,3 @@ /** Returns the sum value of the given values. */ | ||
export function avg(...values: number[]): number { | ||
return Math.sum(...values) / values.length; | ||
return sum(...values) / values.length; | ||
}; | ||
@@ -11,0 +11,0 @@ |
{ | ||
"name": "@ayonli/jsext", | ||
"version": "0.5.6", | ||
"version": "0.5.7", | ||
"description": "Additional functions for JavaScript programming in practice.", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
@@ -5,4 +5,4 @@ export declare class Channel<T> implements AsyncIterable<T> { | ||
private buffer; | ||
private pub?; | ||
private sub?; | ||
private producers; | ||
private consumers; | ||
private error?; | ||
@@ -9,0 +9,0 @@ private state; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
659044
9078