deThunk - dwCore/dwCore-Wiki GitHub Wiki

Cached results via a deferred computation of parameter-less async functions. Used in dBrowser and with dDrive.

Table of Contents

Preview

dWebShield

Installation

npm i --save dethunk
yarn add dethunk
dpack add dethunk

Terms

  • dfeed - dFeeds are just what they sound like - data feeds. Feeds are permanent data structures that can be shared on the dat network.
  • dwstream - dWeb Streams are similar to Node.js streams, a tool in the code for reading or writing data. Streams are temporary and almost always returned by functions within modular libraries within the dWeb ecosystem.
  • channel - dWeb Streams and regular streams, tend to either be readable (giving data) or writable (receiving data). Channeling is when you connected a Readable dWeb Stream to a Writeable dWeb Stream.
  • duplicating dwstream - A stream returned by the duplicate() function can be piped to a peer on the dWeb network. We use this to sync a peer's dFeeds on the dWeb network.
  • flocking - Flocking is when a peer, like yourself, adds themselves to the dWeb network, look for other peers and can also refer to peers sharing data with other peers. Channeling a Duplication dFeed is when one peer shares a data feed with another single peer. In essence, that data feed is now distributed and then Seeded by those two peers. Subsequent peers who Flock to that data, become seeds either while viewing or streaming that seed or if they prefer, can seed that data for any period of time.

Usage

The follow are several usage examples for the dethunk library.

Cached Result

Below, we will create a call "dethunkTest", which will generate a random number and cache the result. Then we will call the same function again and will show how it will only display the same result via the first "dethunkTest" call, since the result was cached - thanks to dethunked.

var dethunk = require('dethunk')

var dethunkTest = dethunk(function (callback) { // We should only accept one inner function - which has to be a callback here.
  console.log('Waiting one second and then returning a random number.')
  setTimeout(function () {
    callback(Math.random())
  }, 1000)
})

dethunkTest(function (num) {  // The first time we call dethunkTest, we call an inner function to generate a random number.
  console.log(num) // We then print that number to the console.
})

dethunkTest(function (num) {  // If dethunkTest is called again, first it waits for the original call to finish and then runs.
  console.log(num) // In this console.log result, is that it's simply a cached version of the original call.
})

Deferred Computation

var connDb = dethunk(function (callback) {
  db.open(databaseConnStr, callback)
})

var queryDatabase = function (query, callback) {
  connDb(function (err, db) {
    if (err) return callback(err)
    db.query(query, callback)
  })
}

queryDatabase('1st query', function (err, result) { ... } )

queryDatabase('2nd query', function (err, result) { ... } )

The first time connDb is called and executed, it attempts to connect to and open a specific database. Any subsequent calls will wait for the first call to complete and then will execute.

The key piece of dethunk, is the fact we can pass any error via connDb to the queryDatabase inner-function callback.

Example Of How Errors Don't Cache

If the first call ran results in an error, it won't cache.

var dethunkBad = dethunk(function (callback) {
  console.log('returning an error')
  callback(new Error('ultimate fail'))
})

dethunkBad(function (err) { // `dethunkBad` is called the first time
  console.log(err)
});

dethunkBad(function (err) { // `dethunkBad` is called again since the first one failed. 
  console.log(err)
})