Skip to content

Commit

Permalink
add custom transport
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-jentzsch committed Aug 30, 2020
1 parent a6cab13 commit 9b4e86d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion wasm/src/in3.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
* All properties are optional and will be verified when sending the next request.
*/
export declare interface IN3Config {
/**
* sets the transport-function.
*
* @param fn the function to fetch the response for the given url
*/
transport?: (url: string, payload: string, timeout?: number) => Promise<string>

/**
* if true the nodelist will be automaticly updated if the lastBlock is newer.
*
Expand Down Expand Up @@ -462,7 +469,7 @@ export default class IN3Generic<BigIntType, BufferType> {


/**
* changes the transport-function.
* changes the default transport-function.
*
* @param fn the function to fetch the response for the given url
*/
Expand Down
7 changes: 6 additions & 1 deletion wasm/src/in3.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ class IN3 {
}
this.needsSetConfig = !this.ptr
if (this.ptr) {
if (this.config.transport) {
this.transport = this.config.transport
delete this.config.transport
}
const r = in3w.ccall('in3_config', 'number', ['number', 'string'], [this.ptr, JSON.stringify(this.config)]);
if (r) {
const ex = new Error(UTF8ToString(r))
Expand Down Expand Up @@ -361,7 +365,8 @@ function url_queue(req) {
let counter = 0
const promises = [], responses = []
if (req.in3.config.debug) console.log("send req (" + req.ctx + ") to " + req.urls.join() + ' : ', JSON.stringify(req.payload, null, 2))
req.urls.forEach((url, i) => in3w.transport(url, JSON.stringify(req.payload), req.timeout || 30000).then(
const transport = req.in3.transport || in3w.transport
req.urls.forEach((url, i) => transport(url, JSON.stringify(req.payload), req.timeout || 30000).then(
response => { responses.push({ i, url, response }); trigger() },
error => { responses.push({ i, url, error }); trigger() }
))
Expand Down

0 comments on commit 9b4e86d

Please sign in to comment.