Skip to content
Snippets Groups Projects
Commit 85d5e9b9 authored by David Sehnal's avatar David Sehnal
Browse files

Computation API cleanup

parent 99e829a1
No related branches found
No related tags found
No related merge requests found
...@@ -456,7 +456,6 @@ interface LoopReadState { ...@@ -456,7 +456,6 @@ interface LoopReadState {
} }
function readLoopChunk(state: LoopReadState, chunkSize: number) { function readLoopChunk(state: LoopReadState, chunkSize: number) {
//console.log(chunkSize);
const { tokenizer, tokens, fieldCount } = state; const { tokenizer, tokens, fieldCount } = state;
let tokenCount = state.tokenCount; let tokenCount = state.tokenCount;
let counter = 0; let counter = 0;
...@@ -466,20 +465,13 @@ function readLoopChunk(state: LoopReadState, chunkSize: number) { ...@@ -466,20 +465,13 @@ function readLoopChunk(state: LoopReadState, chunkSize: number) {
counter++; counter++;
} }
state.tokenCount = tokenCount; state.tokenCount = tokenCount;
return counter; //tokenizer.currentTokenType === CifTokenType.Value; return counter;
} }
function readLoopChunks(state: LoopReadState) { function readLoopChunks(state: LoopReadState) {
const { chunker } = state.tokenizer; return state.tokenizer.chunker.process(
// while (readLoopChunk(state, computation.chunkSize)) {
// if (computation.requiresUpdate) {
// await computation.updateProgress('Parsing...', void 0, state.tokenizer.position, state.tokenizer.data.length);
// }
// }
return chunker.process(
chunkSize => readLoopChunk(state, chunkSize), chunkSize => readLoopChunk(state, chunkSize),
update => update('Parsing...', void 0, state.tokenizer.position, state.tokenizer.data.length)); update => update({ message: 'Parsing...', current: state.tokenizer.position, max: state.tokenizer.data.length }));
} }
/** /**
...@@ -509,12 +501,6 @@ async function handleLoop(tokenizer: TokenizerState, categories: { [name: string ...@@ -509,12 +501,6 @@ async function handleLoop(tokenizer: TokenizerState, categories: { [name: string
tokens tokens
}; };
// let tokenCount = 0;
// while (tokenizer.currentTokenType === CifTokenType.Value) {
// TokenBuilder.add(tokens[(tokenCount++) % fieldCount], tokenizer.currentTokenStart, tokenizer.currentTokenEnd);
// moveNext(tokenizer);
// }
await readLoopChunks(state); await readLoopChunks(state);
if (state.tokenCount % fieldCount !== 0) { if (state.tokenCount % fieldCount !== 0) {
...@@ -569,7 +555,7 @@ async function parseInternal(data: string, ctx: Computation.Context) { ...@@ -569,7 +555,7 @@ async function parseInternal(data: string, ctx: Computation.Context) {
//inSaveFrame = false, //inSaveFrame = false,
//blockSaveFrames: any; //blockSaveFrames: any;
ctx.updateProgress('Parsing...'); ctx.updateProgress({ message: 'Parsing...' });
moveNext(tokenizer); moveNext(tokenizer);
while (tokenizer.currentTokenType !== CifTokenType.End) { while (tokenizer.currentTokenType !== CifTokenType.End) {
......
...@@ -112,7 +112,7 @@ export namespace Tokenizer { ...@@ -112,7 +112,7 @@ export namespace Tokenizer {
readLinesChunk(state, linesToRead, lineTokens); readLinesChunk(state, linesToRead, lineTokens);
linesAlreadyRead += linesToRead; linesAlreadyRead += linesToRead;
return linesToRead; return linesToRead;
}, update => update('Parsing...', void 0, state.position, length)); }, update => update({ message: 'Parsing...', current: state.position, max: length }));
return lineTokens; return lineTokens;
} }
......
...@@ -19,7 +19,7 @@ const file = 'md_1u19_trj.gro' ...@@ -19,7 +19,7 @@ const file = 'md_1u19_trj.gro'
async function runGro(input: string) { async function runGro(input: string) {
console.time('parseGro'); console.time('parseGro');
const comp = Gro(input); const comp = Gro(input);
const running = comp.runObservable(Computation.observableContext({ updateRateMs: 150 })); const running = comp.runObservable(Computation.observable({ updateRateMs: 150 }));
running.subscribe(p => console.log(`[Gro] ${(p.current / p.max * 100).toFixed(2)} (${p.elapsedMs | 0}ms)`)); running.subscribe(p => console.log(`[Gro] ${(p.current / p.max * 100).toFixed(2)} (${p.elapsedMs | 0}ms)`));
const parsed = await running.result; const parsed = await running.result;
console.timeEnd('parseGro'); console.timeEnd('parseGro');
...@@ -85,8 +85,8 @@ async function runCIF(input: string | Uint8Array) { ...@@ -85,8 +85,8 @@ async function runCIF(input: string | Uint8Array) {
console.time('parseCIF'); console.time('parseCIF');
const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input); const comp = typeof input === 'string' ? CIF.parseText(input) : CIF.parseBinary(input);
const running = comp.runObservable(Computation.observableContext({ updateRateMs: 250 })); const running = comp.runObservable(Computation.observable({ updateRateMs: 250 })); // Computation.synchronous
running.subscribe(p => console.log(`[CIF] ${(p.current / p.max * 100).toFixed(2)} (${p.elapsedMs | 0}ms)`)); running.subscribe(p => console.log(`[CIF] ${p.message} ${(p.current / p.max * 100).toFixed(2)}% (${p.elapsedMs | 0}ms)`));
const parsed = await running.result; const parsed = await running.result;
console.timeEnd('parseCIF'); console.timeEnd('parseCIF');
if (parsed.isError) { if (parsed.isError) {
...@@ -137,7 +137,7 @@ import Computation from './utils/computation' ...@@ -137,7 +137,7 @@ import Computation from './utils/computation'
const comp = Computation.create(async ctx => { const comp = Computation.create(async ctx => {
for (let i = 0; i < 0; i++) { for (let i = 0; i < 0; i++) {
await new Promise(res => setTimeout(res, 500)); await new Promise(res => setTimeout(res, 500));
if (ctx.requiresUpdate) await ctx.updateProgress('working', void 0, i, 2); if (ctx.requiresUpdate) await ctx.updateProgress({ message: 'working', current: i, max: 2 });
} }
return 42; return 42;
}); });
......
...@@ -42,13 +42,20 @@ namespace Computation { ...@@ -42,13 +42,20 @@ namespace Computation {
requestAbort?: () => void requestAbort?: () => void
} }
export interface ProgressUpdate {
message?: string,
abort?: boolean | (() => void),
current?: number,
max?: number
}
export interface Context { export interface Context {
readonly isSynchronous: boolean, readonly isSynchronous: boolean,
/** Also checks if the computation was aborted. If so, throws. */ /** Also checks if the computation was aborted. If so, throws. */
readonly requiresUpdate: boolean, readonly requiresUpdate: boolean,
requestAbort(): void, requestAbort(): void,
/** Also checks if the computation was aborted. If so, throws. */ /** Also checks if the computation was aborted. If so, throws. */
updateProgress(msg: string, abort?: boolean | (() => void), current?: number, max?: number): Promise<void> | void updateProgress(info: ProgressUpdate): Promise<void> | void
} }
export type ProgressObserver = (progress: Readonly<Progress>) => void; export type ProgressObserver = (progress: Readonly<Progress>) => void;
...@@ -59,14 +66,14 @@ namespace Computation { ...@@ -59,14 +66,14 @@ namespace Computation {
} }
/** A context without updates. */ /** A context without updates. */
export const synchronousContext: Context = { export const synchronous: Context = {
isSynchronous: true, isSynchronous: true,
requiresUpdate: false, requiresUpdate: false,
requestAbort() { }, requestAbort() { },
updateProgress(msg, abort, current, max) { } updateProgress(info) { }
} }
export function observableContext(params?: Partial<Params>) { export function observable(params?: Partial<Params>) {
return new ObservableContext(params); return new ObservableContext(params);
} }
...@@ -163,7 +170,7 @@ class ObservableContext implements Computation.Context { ...@@ -163,7 +170,7 @@ class ObservableContext implements Computation.Context {
} catch (e) { } } catch (e) { }
} }
updateProgress(msg: string, abort?: boolean | (() => void), current?: number, max?: number): Promise<void> | void { updateProgress({ message, abort, current, max }: Computation.ProgressUpdate): Promise<void> | void {
this.checkAborted(); this.checkAborted();
const time = Computation.now(); const time = Computation.now();
...@@ -175,14 +182,14 @@ class ObservableContext implements Computation.Context { ...@@ -175,14 +182,14 @@ class ObservableContext implements Computation.Context {
this.progress.requestAbort = abort ? this.abortRequester : void 0; this.progress.requestAbort = abort ? this.abortRequester : void 0;
} }
this.progress.message = msg; if (typeof message !== 'undefined') this.progress.message = message;
this.progress.elapsedMs = time - this.startedTime; this.progress.elapsedMs = time - this.startedTime;
if (isNaN(current!) || isNaN(max!)) { if (isNaN(current!)) {
this.progress.isIndeterminate = true; this.progress.isIndeterminate = true;
} else { } else {
this.progress.isIndeterminate = false; this.progress.isIndeterminate = false;
this.progress.current = current!; this.progress.current = current!;
this.progress.max = max!; if (!isNaN(max!)) this.progress.max = max!;
} }
if (this.observers) { if (this.observers) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment