forked from MetaMask/web3-provider-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
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 MetaMask#154 from MetaMask/InflightCache
Inflight cache
- Loading branch information
Showing
8 changed files
with
24,817 additions
and
17,960 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const cacheIdentifierForPayload = require('../util/rpc-cache-utils.js').cacheIdentifierForPayload | ||
const Subprovider = require('./subprovider.js') | ||
|
||
|
||
class InflightCacheSubprovider extends Subprovider { | ||
|
||
constructor (opts) { | ||
super() | ||
this.inflightRequests = {} | ||
} | ||
|
||
addEngine (engine) { | ||
this.engine = engine | ||
} | ||
|
||
handleRequest (req, next, end) { | ||
const cacheId = cacheIdentifierForPayload(req) | ||
|
||
// if not cacheable, skip | ||
if (!cacheId) return next() | ||
|
||
// check for matching requests | ||
let activeRequestHandlers = this.inflightRequests[cacheId] | ||
|
||
if (!activeRequestHandlers) { | ||
activeRequestHandlers = [] | ||
this.inflightRequests[cacheId] = activeRequestHandlers | ||
|
||
next((err, result, cb) => { | ||
delete this.inflightRequests[cacheId] | ||
activeRequestHandlers.forEach((handler) => handler(err, result)) | ||
cb(err, result) | ||
}) | ||
|
||
} else { | ||
// setup the response lister | ||
activeRequestHandlers.push(end) | ||
} | ||
|
||
} | ||
} | ||
|
||
module.exports = InflightCacheSubprovider | ||
|
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