data.future
Advanced tools
Comparing version 1.0.0 to 2.0.0
// Generated by LiveScript 1.2.0 | ||
/** ^ | ||
* Copyright (c) 2013 Quildreen Motta | ||
* Copyright (c) 2013-2014 Quildreen Motta | ||
* | ||
@@ -25,3 +25,3 @@ * Permission is hereby granted, free of charge, to any person | ||
(function(){ | ||
var memoisedFork, id, RejectedFutureExtractionError, Future; | ||
var memoisedFork, Future; | ||
memoisedFork = function(f, future){ | ||
@@ -49,4 +49,3 @@ var pending, started, resolved, rejected, fold; | ||
return f(function(a){ | ||
future.isPending = false; | ||
future.isRejected = rejected = true; | ||
rejected = true; | ||
future.value = a; | ||
@@ -56,4 +55,3 @@ invokePending('rejected', a); | ||
}, function(b){ | ||
future.isPending = false; | ||
future.isResolved = resolved = true; | ||
resolved = true; | ||
future.value = b; | ||
@@ -78,12 +76,2 @@ invokePending('resolved', b); | ||
}; | ||
id = function(a){ | ||
return a; | ||
}; | ||
RejectedFutureExtractionError = (function(superclass){ | ||
var prototype = extend$((import$(RejectedFutureExtractionError, superclass).displayName = 'RejectedFutureExtractionError', RejectedFutureExtractionError), superclass).prototype, constructor = RejectedFutureExtractionError; | ||
function RejectedFutureExtractionError(){ | ||
RejectedFutureExtractionError.superclass.call(this, "Can't extract the value of a rejected future."); | ||
} | ||
return RejectedFutureExtractionError; | ||
}(TypeError)); | ||
Future = (function(){ | ||
@@ -93,7 +81,13 @@ Future.displayName = 'Future'; | ||
function Future(f){ | ||
this.fork = memoisedFork(f, this); | ||
this.fork = f; | ||
} | ||
prototype.isPending = true; | ||
prototype.isResolved = false; | ||
prototype.isRejected = false; | ||
Future.memoise = function(f){ | ||
var future; | ||
future = new Future; | ||
future.fork = memoisedFork(f, future); | ||
return future; | ||
}; | ||
prototype.memoise = function(f){ | ||
return Future.memoise(f); | ||
}; | ||
Future.of = function(b){ | ||
@@ -128,35 +122,4 @@ return new Future(function(reject, resolve){ | ||
prototype.toString = function(){ | ||
switch (false) { | ||
case !this.isPending: | ||
return "Future.Pending"; | ||
case !this.isResolved: | ||
return "Future.Resolved(" + this.value + ")"; | ||
case !this.isRejected: | ||
return "Future.Rejected(" + this.value + ")"; | ||
} | ||
return "Future"; | ||
}; | ||
prototype.isEqual = function(a){ | ||
return Boolean((function(){ | ||
switch (false) { | ||
case !this.isResolved: | ||
return a.isResolved && a.value === this.value; | ||
case !this.isRejected: | ||
return a.isRejected && a.value === this.value; | ||
case !this.isPending: | ||
return this.fork(function(e){ | ||
return a.fork(function(e2){ | ||
return e === e2; | ||
}, function(_){ | ||
return false; | ||
}); | ||
}, function(s){ | ||
return a.fork(function(_){ | ||
return false; | ||
}, function(s2){ | ||
return s === s2; | ||
}); | ||
}); | ||
} | ||
}.call(this))); | ||
}; | ||
prototype.orElse = function(f){ | ||
@@ -215,13 +178,2 @@ var this$ = this; | ||
module.exports = Future; | ||
function extend$(sub, sup){ | ||
function fun(){} fun.prototype = (sub.superclass = sup).prototype; | ||
(sub.prototype = new fun).constructor = sub; | ||
if (typeof sup.extended == 'function') sup.extended(sub); | ||
return sub; | ||
} | ||
function import$(obj, src){ | ||
var own = {}.hasOwnProperty; | ||
for (var key in src) if (own.call(src, key)) obj[key] = src[key]; | ||
return obj; | ||
} | ||
function curry$(f, bound){ | ||
@@ -228,0 +180,0 @@ var context, |
{ | ||
"name": "data.future", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A monad for time-dependant values, providing explicit effects for delayed computations, latency, etc.", | ||
@@ -28,8 +28,7 @@ "main": "lib/index.js", | ||
}, | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"claire": "~0.4.1", | ||
"browserify": "git://github.com/robotlolita/node-browserify", | ||
"groc": "git://github.com/robotlolita/groc#patch-livescript", | ||
"groc": "git://github.com/robotlolita/groc", | ||
"LiveScript": "~1.2.0", | ||
@@ -36,0 +35,0 @@ "hifive-tap": "~0.1.0", |
@@ -25,27 +25,45 @@ The Future monad | ||
var Future = require('data.future') | ||
var fs = require('fs') | ||
function get(url) { | ||
return new Future(function(resolve, reject) { | ||
var request = new XMLHttpRequest() | ||
request.onload = function() { | ||
if (!/2\d\d/.test(this.status)) reject(this.responseText) | ||
else resolve(this.responseText) | ||
} | ||
request.open("get", url, true) | ||
request.send() | ||
// read : String -> Future(Error, Buffer) | ||
function read(path) { | ||
return new Future(reject, resolve) { | ||
fs.readFile(path, function(error, data) { | ||
if (error) reject(error) | ||
else resolve(data) | ||
}) | ||
} | ||
} | ||
// decode : Future(Error, Buffer) -> Future(Error, String) | ||
function decode(buffer) { | ||
return buffer.map(function(a) { | ||
return a.toString('utf-8') | ||
}) | ||
} | ||
var t1 = get('/something') | ||
var t2 = get('/other') | ||
var intro = decode(read('intro.txt')) | ||
var outro = decode(read('outro.txt')) | ||
var t3 = t1.map(function(a) { | ||
t2.map(function(b) { | ||
return a + b | ||
}) | ||
}) | ||
// You can use `.chain` to sequence two asynchronous actions, and | ||
// `.map` to perform a synchronous computation with the eventual | ||
// value of the Future. | ||
var concatenated = intro.chain(function(a) { | ||
return outro.map(function(b) { | ||
return a + b | ||
}) | ||
}) | ||
t3.chain(function(a) { | ||
console.log(a) | ||
}) | ||
// But the implementation of Future is pure, which means that you'll | ||
// never observe the effects by using `chain` or `map` or any other | ||
// method. The Future just records the sequence of actions that you | ||
// wish to observe, and defers the playing of that sequence of actions | ||
// for your application's entry-point to call. | ||
// | ||
// To observe the effects, you have to call the `fork` method, which | ||
// takes a callback for the rejection, and a callback for the success. | ||
concatenated.fork( | ||
function(error) { throw error } | ||
, function(data) { console.log(data) } | ||
) | ||
``` | ||
@@ -128,3 +146,3 @@ | ||
Copyright (c) 2013 Quildreen Motta. | ||
Copyright (c) 2013-2014 Quildreen Motta. | ||
@@ -143,4 +161,4 @@ Released under the [MIT licence](https://github.com/folktale/data.future/blob/master/LICENCE). | ||
<!-- [release: https://github.com/folktale/data.future/releases/download/v$VERSION/data.future-$VERSION.tar.gz] --> | ||
[release]: https://github.com/folktale/data.future/releases/download/v1.0.0/data.future-1.0.0.tar.gz | ||
[release]: https://github.com/folktale/data.future/releases/download/v2.0.0/data.future-2.0.0.tar.gz | ||
<!-- [/release] --> | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
262727
26
1835
162
1