Skip to content

Commit

Permalink
working on splitting up tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Dec 12, 2018
1 parent a54cad9 commit fd3f852
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 85 deletions.
30 changes: 22 additions & 8 deletions src/internal/math.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ICategory} from '../model';
import {bisectLeft} from 'd3-array';
import {IForEachAble, isIndicesAble} from './interable';

export interface INumberBin {
x0: number;
Expand Down Expand Up @@ -100,7 +101,20 @@ function quantile(values: Float32Array, quantile: number) {
return v + (vAfter - v) * (target - index); // shift by change
}

export function boxplotBuilder(): {push: (v: number) => void, build: () => IAdvancedBoxPlotData} {
function pushAll<T>(push: (v: T) => void) {
return (vs: IForEachAble<T>) => {
if (!isIndicesAble(vs)) {
vs.forEach(push);
return;
}
// tslint:disable-next-line:prefer-for-of
for (let j = 0; j < vs.length; ++j) {
push(vs[j]);
}
};
}

export function boxplotBuilder(): {push: (v: number) => void, pushAll: (vs: IForEachAble<number>) => void, build: () => IAdvancedBoxPlotData} {
// filter out NaN
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
Expand Down Expand Up @@ -198,13 +212,13 @@ export function boxplotBuilder(): {push: (v: number) => void, build: () => IAdva
};
};

return {push, build};
return {push, build, pushAll: pushAll(push)};
}

/**
* @internal
*/
export function normalizedStatsBuilder(numberOfBins: number): {push: (v: number) => void, build: () => IStatistics} {
export function normalizedStatsBuilder(numberOfBins: number): {push: (v: number) => void, pushAll: (vs: IForEachAble<number>) => void, build: () => IStatistics} {

const hist: INumberBin[] = [];

Expand Down Expand Up @@ -280,7 +294,7 @@ export function normalizedStatsBuilder(numberOfBins: number): {push: (v: number)
};
};

return {push, build};
return {push, build, pushAll: pushAll(push)};
}

function computeGranularity(min: Date | null, max: Date | null) {
Expand Down Expand Up @@ -333,7 +347,7 @@ function computeGranularity(min: Date | null, max: Date | null) {
return {hist, histGranularity: EDateHistogramGranularity.MONTH};
}

export function dateStatsBuilder(template?: IDateStatistics): {push: (v: Date | null) => void, build: () => IDateStatistics} {
export function dateStatsBuilder(template?: IDateStatistics): {push: (v: Date | null) => void, pushAll: (vs: IForEachAble<Date | null>) => void, build: () => IDateStatistics} {
// filter out NaN
let min: Date | null = null;
let max: Date | null = null;
Expand Down Expand Up @@ -374,7 +388,7 @@ export function dateStatsBuilder(template?: IDateStatistics): {push: (v: Date |
};
};

return {push, build};
return {push, build, pushAll: pushAll(push)};
}

/**
Expand All @@ -384,7 +398,7 @@ export function dateStatsBuilder(template?: IDateStatistics): {push: (v: Date |
* @returns {{hist: {cat: string, y: number}[]}}
* @internal
*/
export function categoricalStatsBuilder(categories: ICategory[]): {push: (category: ICategory | null) => void, build: () => ICategoricalStatistics} {
export function categoricalStatsBuilder(categories: ICategory[]): {push: (category: ICategory | null) => void, pushAll: (vs: IForEachAble<ICategory | null>) => void, build: () => ICategoricalStatistics} {
const m = new Map<string, number>();
categories.forEach((cat) => m.set(cat.name, 0));

Expand Down Expand Up @@ -412,7 +426,7 @@ export function categoricalStatsBuilder(categories: ICategory[]): {push: (catego
};
};

return {push, build};
return {push, build, pushAll: pushAll(push)};
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/internal/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export interface ITask<T> {
result: PromiseLike<T | symbol>;
}

export const ANOTHER_ROUND = {
value: null,
done: false
};

export function oneShotIterator<T>(calc: () => T): Iterator<T> {
return {
next: () => ({done: true, value: calc()})
};
}


function thenFactory<T>(wrappee: PromiseLike<T>, abort: () => void) {
function then<TResult1 = T | symbol, TResult2 = never>(onfulfilled?: ((value: T | symbol) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): IAbortAblePromiseBase<TResult1 | TResult2> {
const r = wrappee.then(onfulfilled, onrejected);
Expand Down Expand Up @@ -107,15 +119,7 @@ export default class TaskScheduler {
}

push<T>(id: string, calc: () => T | PromiseLike<T>): IAbortAblePromise<T> {
const singleIt = {
next: () => {
return {
value: calc(),
done: true
};
}
};
return this.pushMulti(id, singleIt);
return this.pushMulti(id, oneShotIterator(calc));
}

abort(id: string) {
Expand Down
Loading

0 comments on commit fd3f852

Please sign in to comment.