-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change dequeue logic with channels (#80)
* Update package.json * Improve logging * Improve logging * Update payment.ts * fix consumer flow with asyncWith wrapper, fix logs * Fix double spending information display * Update index.ts * Update index.ts * Update index.ts * update subnet to `community.3` in the examples * move logsummary to utils (#55) * move logsummary to utils * update js example * Update package.json * Clean dependencies with npm script * Move cancellationToken to Engine (#60) * More sgx runtime names + simplified code (#64) * update clean script with rimraf for cross-platform * version bump * version bump (#66) * More like yapapi * Fix * Fix * Fix * Fix * Fix * More fixes * Fix * Add timeout * Fix agreement.confirm * Fix * Port invoice.accept-related code from yapapi * Fix * Fix * Fix * Remove waiting for done workers * Fix timeout * Cancel worker, subscription when computation ends (#68) * remove unused variable * Fix tasks invoices handling (#70) * Add checking payments logging * Improve logging * Cancel worker_starter_task * Add PaymentsFinished event; print a nice table with computation results * Print table when checking payments * Show table only in debug mode * Address code review * Total cost in non-debug mode * Fix * Fix * Fix * version bump * Final fix for invoice event (#73) * Final fix for invoice event * Fix total cost printing (#74) * Fix total cost printing * Fix Co-authored-by: filipgolem <[email protected]> * version bump * version bump * Change dequeue logic with channels * Reject get method in case of closed channel * Remove local path Co-authored-by: filipgolem <[email protected]> Co-authored-by: Filip <[email protected]> Co-authored-by: shadeofblue <[email protected]> Co-authored-by: Marek Franciszkiewicz <[email protected]>
- Loading branch information
1 parent
01165ef
commit 55c7319
Showing
6 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
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
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,10 @@ | ||
export default function promisify(fn: Function): Function { | ||
return (...args) => | ||
new Promise((resolve, reject) => { | ||
try { | ||
fn(...args, resolve); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,36 +1,45 @@ | ||
import sleep from "./sleep"; | ||
import * as csp from "js-csp"; | ||
import promisify from "./promisify"; | ||
|
||
export default interface Queue<T> {} | ||
export default class Queue<T> { | ||
private _tasks; | ||
private _cancellationToken; | ||
private __new_items; | ||
|
||
constructor(list = [], cancellationToken) { | ||
constructor(list = []) { | ||
this._tasks = list; | ||
this._cancellationToken = cancellationToken; | ||
this.__new_items = csp.chan(); | ||
|
||
if (list.length > 0) { | ||
let first = this._tasks.shift(); | ||
first(); | ||
} | ||
} | ||
|
||
put(item: T) { | ||
if(item === undefined || item === null || this.__new_items.closed) return; | ||
this._tasks.push(item); | ||
csp.putAsync(this.__new_items, true); | ||
} | ||
|
||
async get(): Promise<T> { | ||
return new Promise(async (resolve, reject) => { | ||
let item; | ||
while (!item) { | ||
if (this._cancellationToken.cancelled) break; | ||
item = this._tasks.shift(); | ||
if (!item) await sleep(2); | ||
if(this.__new_items.closed) reject("new_items channel interrupted"); | ||
try { | ||
await promisify(csp.takeAsync)(this.__new_items); | ||
let item = this._tasks.shift(); | ||
resolve(item); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
if (this._cancellationToken.cancelled) reject(); | ||
resolve(item); | ||
}); | ||
} | ||
|
||
empty() { | ||
return this._tasks.length === 0; | ||
} | ||
|
||
close() { | ||
this.__new_items.close(); | ||
} | ||
} |