Skip to content

Commit

Permalink
perf(rstream): State => const enum
Browse files Browse the repository at this point in the history
- export `__State` for reverse lookups
  • Loading branch information
postspectacular committed Sep 24, 2018
1 parent a7b9a42 commit 7ac83c6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/rstream/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import { Transducer } from "@thi.ng/transducers/api";
import { Stream } from "./stream";
import { Subscription } from "./subscription";

export enum State {
export const enum State {
IDLE,
ACTIVE,
DONE,
ERROR,
DISABLED, // TODO currently unused
}

/**
* Reverse lookup for `State` enums
*/
export const __State = (<any>exports).State;

export type Fn<T> = (x: T) => void;

export interface ISubscriber<T> {
Expand Down
4 changes: 2 additions & 2 deletions packages/rstream/src/subs/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IID } from "@thi.ng/api/api";

import { DEBUG, State } from "../api";
import { __State, DEBUG, State } from "../api";
import { Subscription } from "../subscription";

export interface ResolverOpts extends IID<string> {
Expand Down Expand Up @@ -54,7 +54,7 @@ export class Resolver<T> extends Subscription<Promise<T>, T> {
this.done();
}
} else {
DEBUG && console.log(`resolved value in ${State[this.state]} state (${x})`);
DEBUG && console.log(`resolved value in ${__State[this.state]} state (${x})`);
}
},
(e) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/rstream/src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isReduced, unreduced } from "@thi.ng/transducers/reduced";
import { push } from "@thi.ng/transducers/rfn/push";

import {
__State,
DEBUG,
ISubscribable,
ISubscriber,
Expand Down Expand Up @@ -295,7 +296,7 @@ export class Subscription<A, B> implements

protected ensureState() {
if (this.state >= State.DONE) {
illegalState(`operation not allowed in ${State[this.state]} state`);
illegalState(`operation not allowed in ${__State[this.state]} state`);
}
}

Expand Down

0 comments on commit 7ac83c6

Please sign in to comment.