Skip to content

Commit

Permalink
feat(csp): enable TS strict compiler flags (refactor)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 4, 2019
1 parent 8ea894a commit 3d7fba2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/csp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface IBuffer<T> extends ILength, IRelease {

export interface IChannel<T> extends IID<string> {
channel(): Channel<T>;
close(flush?: boolean);
close(flush?: boolean): Promise<void> | undefined;
}

export interface IReadableChannel<T> extends IChannel<T> {
Expand Down
39 changes: 25 additions & 14 deletions packages/csp/src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Fn, Fn0, Predicate } from "@thi.ng/api";
import {
Fn,
Fn0,
Fn2,
FnAny,
Predicate
} from "@thi.ng/api";
import { shuffle } from "@thi.ng/arrays";
import { isFunction } from "@thi.ng/checks";
import { DCons } from "@thi.ng/dcons";
Expand Down Expand Up @@ -48,7 +54,7 @@ export class Channel<T> implements IReadWriteableChannel<T> {
step: number,
delay: number
): Channel<number>;
static range(...args: any[]) {
static range(...args: any[]): Channel<number> {
const [from, to, step, delay] = args;
return Channel.from(
range(from, to, step),
Expand Down Expand Up @@ -94,10 +100,14 @@ export class Channel<T> implements IReadWriteableChannel<T> {
return chan;
}

static from<T>(src: Iterable<any>);
static from<T>(src: Iterable<any>, close: boolean);
static from<T>(src: Iterable<any>, tx: Transducer<any, T>);
static from<T>(src: Iterable<any>, tx: Transducer<any, T>, close: boolean);
static from<T>(src: Iterable<any>): Channel<T>;
static from<T>(src: Iterable<any>, close: boolean): Channel<T>;
static from<T>(src: Iterable<any>, tx: Transducer<any, T>): Channel<T>;
static from<T>(
src: Iterable<any>,
tx: Transducer<any, T>,
close: boolean
): Channel<T>;
static from<T>(...args: any[]) {
let close, tx;
switch (args.length) {
Expand Down Expand Up @@ -135,7 +145,7 @@ export class Channel<T> implements IReadWriteableChannel<T> {
const _select = () => {
for (let c of shuffle(chans)) {
if (c.isReadable() || c.isClosed()) {
c.read().then((x) => resolve([x, c]));
c.read().then((x: any) => resolve([x, c]));
return;
}
}
Expand Down Expand Up @@ -256,7 +266,7 @@ export class Channel<T> implements IReadWriteableChannel<T> {
static MAX_WRITES = 1024;
static NEXT_ID = 0;

static SCHEDULE =
static SCHEDULE: Fn2<FnAny<void>, number, void> =
typeof setImmediate === "function" ? setImmediate : setTimeout;

private static RFN: Reducer<DCons<any>, any> = [
Expand Down Expand Up @@ -417,9 +427,9 @@ export class Channel<T> implements IReadWriteableChannel<T> {
);
}

consume<T>(fn: Fn<T, any> = (x) => console.log(this.id, ":", x)) {
consume(fn: Fn<T, any> = (x) => console.log(this.id, ":", x)) {
return (async () => {
let x;
let x: T;
while (((x = null), (x = await this.read())) !== undefined) {
await fn(x);
}
Expand Down Expand Up @@ -454,11 +464,11 @@ export class Channel<T> implements IReadWriteableChannel<T> {
})();
}

reduce<A, B>(rfn: Reducer<A, B>, acc?: A): Promise<A> {
reduce<A>(rfn: Reducer<A, T>, acc?: A): Promise<A> {
return (async () => {
const [init, complete, reduce] = rfn;
acc = acc != null ? acc : init();
let x;
let x: T;
while (((x = null), (x = await this.read())) !== undefined) {
acc = <any>reduce(acc, x);
if (isReduced(acc)) {
Expand Down Expand Up @@ -594,7 +604,7 @@ export class Channel<T> implements IReadWriteableChannel<T> {
}

protected flush() {
let op;
let op: any;
while ((op = this.reads.drop())) {
op();
}
Expand All @@ -613,4 +623,5 @@ const defaultErrorHandler = (e: Error, chan: Channel<any>, val?: any) =>
val !== undefined ? val : ""
);

const maybeBuffer = (x) => x instanceof FixedBuffer || typeof x === "number";
const maybeBuffer = (x: any) =>
x instanceof FixedBuffer || typeof x === "number";
2 changes: 1 addition & 1 deletion packages/csp/src/mult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Mult<T> implements IWriteableChannel<T> {
}

close(flush = false) {
this.src && this.src.close(flush);
return this.src ? this.src.close(flush) : undefined;
}

tap<R>(ch?: Channel<R> | Transducer<T, R>) {
Expand Down
2 changes: 1 addition & 1 deletion packages/csp/src/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PubSub<T> implements IWriteableChannel<T> {
}

close(flush = false) {
this.src && this.src.close(flush);
return this.src ? this.src.close(flush) : undefined;
}

/**
Expand Down

0 comments on commit 3d7fba2

Please sign in to comment.