This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from vimaec/sroberge/0_3_16
Sroberge/0 3 16
- Loading branch information
Showing
10 changed files
with
451 additions
and
177 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as VIM from 'vim-webgl-viewer/' | ||
import { DeferredPromise } from './deferredPromise' | ||
|
||
type RequestCallbacks = { | ||
onProgress: (p: VIM.IProgressLogs) => void | ||
onError: (e: string) => void | ||
onDone: () => void | ||
} | ||
|
||
/** | ||
* Class to handle loading a request. | ||
*/ | ||
export class LoadRequest { | ||
private _callbacks : RequestCallbacks | ||
private _request: VIM.VimRequest | ||
|
||
private _progress: VIM.IProgressLogs = { loaded: 0, total: 0, all: new Map() } | ||
private _progressPromise = new DeferredPromise<void>() | ||
|
||
private _isDone: boolean = false | ||
private _completionPromise = new DeferredPromise<void>() | ||
|
||
constructor (callbacks: RequestCallbacks, source: VIM.RequestSource, settings: VIM.VimPartialSettings) { | ||
this._callbacks = callbacks | ||
|
||
this.startRequest(source, settings) | ||
} | ||
|
||
private async startRequest (source: VIM.RequestSource, settings: VIM.VimPartialSettings) { | ||
this._request = await VIM.request(source, settings) | ||
for await (const progress of this._request.getProgress()) { | ||
this.onProgress(progress) | ||
} | ||
const result = await this._request.getResult() | ||
if (result.isError()) { | ||
this.onError(result.error) | ||
} else { | ||
this.onSuccess() | ||
} | ||
} | ||
|
||
private onProgress (progress: VIM.IProgressLogs) { | ||
this._callbacks.onProgress(progress) | ||
this._progress = progress | ||
this._progressPromise.resolve() | ||
this._progressPromise = new DeferredPromise<void>() | ||
} | ||
|
||
private onSuccess () { | ||
this._callbacks.onDone() | ||
this.end() | ||
} | ||
|
||
private onError (error: string) { | ||
this._callbacks.onError(error) | ||
this.end() | ||
} | ||
|
||
private end () { | ||
this._isDone = true | ||
this._progressPromise.resolve() | ||
this._completionPromise.resolve() | ||
} | ||
|
||
async * getProgress () : AsyncGenerator<VIM.IProgressLogs, void, void> { | ||
while (!this._isDone) { | ||
await this._progressPromise | ||
yield this._progress | ||
} | ||
} | ||
|
||
async getResult () { | ||
await this._completionPromise | ||
return this._request.getResult() | ||
} | ||
|
||
abort () { | ||
this._request.abort() | ||
this.onError('Request aborted') | ||
} | ||
} |
Oops, something went wrong.