Skip to content

Commit

Permalink
jscex-promise
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyZhao committed Jun 24, 2012
1 parent c592ec2 commit 6bc5e8b
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
52 changes: 52 additions & 0 deletions samples/promise/jquery-animation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!doctype html>
<html>
<head>
<title>jQuery Animation - Jscex Sample</title>

<script src="../../src/jscex.js"></script>
<script src="../../src/jscex-parser.js"></script>
<script src="../../src/jscex-jit.js"></script>
<script src="../../src/jscex-builderbase.js"></script>
<script src="../../src/jscex-promise.js"></script>

<script src="../../lib/jquery-1.7.2.js"></script>

<script>
/*
Jscex.Promise.create = function (fn) {
var dfd = new $.Deferred();
fn(dfd.resolve, dfd.reject);
return dfd.promise();
}*/

var oneRoundTripAsync = eval(Jscex.compile("promise", function () {
$await($("#block").animate({ left: "200px" }, 1000).promise());
$await($("#block").animate({ left: "0px" }, 1000).promise());
}));

var roundTripsAsync = eval(Jscex.compile("promise", function (n) {
for (var i = 0; i < n; i++) {
$await(oneRoundTripAsync());
}
}));
</script>

<style>
#block {
background-color: red;
width: 50px;
height: 50px;
position: relative;
top: 10px;
left: 0px;
}
</style>
</head>
<body>
<input type="button" value="One Round-trip" onclick="oneRoundTripAsync()" />
<input type="button" value="Three Round-trips" onclick="roundTripsAsync(3)" />

<div id="block"></div>
</body>
</html>

99 changes: 99 additions & 0 deletions src/jscex-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
(function () {
"use strict";

var Jscex;

var defaultCreate = function () {
throw new Error('Please set "Jscex.Promise.create" to provide a factory method for creating a promise object.');
}

var PromiseBuilder = function () { }
PromiseBuilder.prototype = {
Start: function (_this, task) {
return Jscex.Promise.create(function (complete, error) {
task.next(_this, function (type, value, target) {
if (type == "normal" || type == "return") {
complete(value);
} else if (type == "throw") {
error(value);
} else {
throw new Error("Unsupported type: " + type);
}
});
});
},

Bind: function (promise, generator) {
return {
next: function (_this, callback) {
promise.then(function (result) {
var nextTask;
try {
nextTask = generator.call(_this, result);
} catch (ex) {
return callback("throw", ex);
}

nextTask.next(_this, callback);
}, function (error) {
callback("throw", error);
});
}
};
}
}

// CommonJS
var isCommonJS = !!(typeof require === "function" && typeof module !== "undefined" && module.exports);
// CommonJS AMD
var isAmd = !!(typeof require === "function" && typeof define === "function" && define.amd);

var defineModule = function () {
Jscex.define({
name: "promise",
version: "0.6.5",
exports: isCommonJS && module.exports,
require: isCommonJS && require,
autoloads: [ "builderbase" ],
dependencies: { builderbase: "~0.6.5" },
init: function () {
Jscex._.each(Jscex.BuilderBase.prototype, function (m, fn) {
PromiseBuilder.prototype[m] = fn;
});

if (!Jscex.Promise) {
Jscex.Promise = {};
}

Jscex.Promise.create = defaultCreate;

Jscex.binders["promise"] = "$await";
Jscex.builders["promise"] = new PromiseBuilder();
}
});
}

if (isCommonJS) {
try {
Jscex = require("./jscex");
} catch (ex) {
Jscex = require("jscex");
}

defineModule();
} else if (isAmd) {
require("jscex", function (jscex) {
Jscex = jscex;
defineModule();
});
} else {
var Fn = Function, global = Fn('return this')();

if (!global.Jscex) {
throw new Error('Missing the root object, please load "jscex" component first.');
}

Jscex = global.Jscex;
defineModule();
}
})();

0 comments on commit 6bc5e8b

Please sign in to comment.