Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Add a new "async-jquery" monad distinct from Task.

  • Loading branch information...
commit d6480d98d8b64db00252dc10c7301c5059b9d39d 1 parent e23bd06
唐鳳 authored
1  Makefile
View
@@ -8,5 +8,4 @@ demo ::
node example.js
test :: all demo
- node example.js
4 example.js
View
@@ -7,10 +7,10 @@
};
fetch = $.evalAsync(function(url){
var it, a, title, text;
- it = $await($.async($.get(url)));
+ it = $await($.get(url));
a = $(it).find('h2:first a');
title = a.text();
- it = $await($.async($.get(url + "/" + a.attr('href'))));
+ it = $await($.get(url + "/" + a.attr('href')));
text = $(it).find('.post p:last').text();
return {
title: title,
68 jscex-jquery.js
View
@@ -1,5 +1,5 @@
(function(){
- var Jscex, XMLHttpRequest, $;
+ var Jscex, XMLHttpRequest, $, AsyncBuilder;
Jscex = require('jscex');
require('jscex-jit').init(Jscex);
require('jscex-async').init(Jscex);
@@ -21,34 +21,64 @@
return t;
});
};
- /* Shorthand for fromPromise */
- $.async = Jscex.Async.Binding.fromPromise;
+ AsyncBuilder = (function(){
+ AsyncBuilder.displayName = 'AsyncBuilder';
+ var prototype = AsyncBuilder.prototype, constructor = AsyncBuilder;
+ prototype.Start = function(_this, task){
+ var __;
+ __ = $.Deferred();
+ task.next(_this, function(type, value, target){
+ switch (type) {
+ case 'normal':
+ case 'return':
+ return __.resolve(value);
+ case 'throw':
+ return __.reject(value);
+ default:
+ throw new Error("Unsupported type: " + type);
+ }
+ });
+ return __;
+ };
+ prototype.Bind = function(promise, generator){
+ return {
+ next: function(_this, cb){
+ return promise.then(function(result){
+ var nextTask;
+ try {
+ nextTask = generator.call(_this, result);
+ } catch (e) {
+ return cb('throw', e);
+ }
+ return nextTask.next(_this, cb);
+ }, function(error){
+ return cb('throw', error);
+ });
+ }
+ };
+ };
+ function AsyncBuilder(){}
+ return AsyncBuilder;
+ }());
+ __importAll(AsyncBuilder.prototype, Jscex.BuilderBase.prototype);
+ Jscex.binders['async-jquery'] = '$await';
+ Jscex.builders['async-jquery'] = new AsyncBuilder;
+ Jscex.modules['async-jquery'] = true;
/* Compile a function containing the special $await keyword.
Once invoked, we implicitly start the task, and return a
deferred Promise object representing its result.
*/
$.evalAsync = function(cb){
- var runner;
- runner = eval(Jscex.compile('async', cb));
- return function(){
- var __;
- __ = $.Deferred();
- (function(){
- this.addEventListener('success', function(){
- __.resolve(this.result);
- });
- this.addEventListener('failure', function(){
- __.reject(this.error);
- });
- this.start();
- }.call(runner.apply(null, arguments)));
- return __;
- };
+ return eval(Jscex.compile('async-jquery', cb));
};
/* Turn off Jscex logging by default */
Jscex.logger.level = 999;
/* Export the $ object extended with $.Jscex */
$.Jscex = Jscex;
module.exports = $;
+ function __importAll(obj, src){
+ for (var key in src) obj[key] = src[key];
+ return obj;
+ }
}).call(this);
4 src/example.ls
View
@@ -2,10 +2,10 @@ $ = require './jscex-jquery'
say = !-> console.log it; console.log ''
fetch = $.evalAsync (url) ->
- it = $await $.async $.get url
+ it = $await $.get url
a = $(it) .find 'h2:first a'
title = a.text!
- it = $await $.async $.get "#url/#{ a.attr \href }"
+ it = $await $.get "#url/#{ a.attr \href }"
text = $(it) .find '.post p:last' .text!
return { title, text }
38 src/jscex-jquery.ls
View
@@ -18,23 +18,39 @@ Jscex.Async.Binding.fromPromise = (p) ->
)
return t
-/* Shorthand for fromPromise */
-$.async = Jscex.Async.Binding.fromPromise
+class AsyncBuilder
+ Start: (_this, task) ->
+ __ = $.Deferred()
+ task.next _this, (type, value, target) ->
+ switch type
+ | \normal \return => __.resolve value
+ | \throw => __.reject value
+ | otherwise => throw new Error "Unsupported type: #type"
+ return __
+ Bind: (promise, generator) ->
+ return next: (_this, cb) -> promise.then(
+ (result) ->
+ try
+ nextTask = generator.call _this, result
+ catch
+ return cb \throw, e
+ nextTask.next _this, cb
+ (error) ->
+ cb \throw, error
+ )
+
+AsyncBuilder:: <<<< Jscex.BuilderBase::
George Zahariev
gkz added a note

You can do instead (at line 21) class AsyncBuilder extends Jscex.BuilderBase - or is there some functionality of inheritance that you do not want?

唐鳳 Owner
audreyt added a note

Thanks! Nice catch, it's now fixed as you said. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+Jscex.binders.\async-jquery = \$await
+Jscex.builders.\async-jquery = new AsyncBuilder
+Jscex.modules.\async-jquery = true
/* Compile a function containing the special $await keyword.
Once invoked, we implicitly start the task, and return a
deferred Promise object representing its result.
*/
-$.evalAsync = (cb) ->
- runner = eval Jscex.compile \async, cb
- return ->
- __ = $.Deferred!
- with runner ...arguments
- @addEventListener \success, !-> __.resolve @result
- @addEventListener \failure, !-> __.reject @error
- @start!
- __
+$.evalAsync = (cb) -> eval Jscex.compile \async-jquery, cb
/* Turn off Jscex logging by default */
Jscex.logger.level = 999
George Zahariev

You can do instead (at line 21) class AsyncBuilder extends Jscex.BuilderBase - or is there some functionality of inheritance that you do not want?

唐鳳

Thanks! Nice catch, it's now fixed as you said. :-)

Please sign in to comment.
Something went wrong with that request. Please try again.