Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Interface improvements

improved when, then, do chaining options
  • Loading branch information...
commit c863db7c716eb044648df6295bb0a2607c3f3378 1 parent f069324
Steven Thurlow authored
Showing with 94 additions and 34 deletions.
  1. +21 −17 README.md
  2. +13 −15 YJunction.js
  3. +2 −2 demo-server.js
  4. +58 −0 demo.html
38 README.md
View
@@ -42,23 +42,24 @@ requests, then compose them into some business entity your app needs:
// Data consumption
- YJ.when(['workplaces', 'date'])
- .then(function(workplaces, date) {
- var start = new Date(date);
- var h1 = document.createElement('h1');
- h1.innerText = "Workplaces on " + date.toDateString();
- document.body.appendChild(h1);
-
- workplaces.forEach(function(workplace) {
- var p = document.createElement('p');
- p.innerText = "You can work here: " + workplace.address;
- document.body.appendChild(p);
- });
- this.next(new Date().valueOf() - start.valueOf());
- })
- .then(function(workplaces, date, speed) {
- console.log("rendering took " + speed + "ms");
- });
+ YJ.when(['workplaces', 'date']).then(
+ function(workplaces, date) {
+ var start = new Date(date);
+ var h1 = document.createElement('h1');
+ h1.innerText = "Workplaces on " + date.toDateString();
+ document.body.appendChild(h1);
+
+ workplaces.forEach(function(workplace) {
+ var p = document.createElement('p');
+ p.innerText = "You can work here: " + workplace.address;
+ document.body.appendChild(p);
+ });
+ this.next(new Date().valueOf() - start.valueOf());
+ },
+ function(workplaces, date, speed) {
+ console.log("rendering took " + speed + "ms");
+ }
+ );
YJ.listen(window, 'load', 'windowload');
@@ -133,12 +134,14 @@ The documentation that exists is as follows.
Start your stack with these.
### when(dependencies:Array):Stack
+### when(dependency, dependency, dependency, ...):Stack
- dependencies: string names of the dependencies that
will trigger the stack when satisfied
- returns: Stack
### do():Stack
+### do(func, func, func, ...):Stack
- returns: Stack
@@ -177,6 +180,7 @@ object value whenever the event is triggered. Helpful for when waiting on DOM
events. Alias defaults to `event`.
## Stack.then(func:function):Stack
+## Stack.then(func, func, func, ...):Stack
Adds `func` to the stack, to be called when triggered. If the stack has already
been triggered, then `func` gets called immediately. Parameters passed to `func`
28 YJunction.js
View
@@ -54,8 +54,6 @@
once: function() {
this.once = true;
return this;
- //var self = this;
- //return { then: self.then, nowait: self.nowait, stack: self.stack, depends: self.depends, exec: self.exec, once: self.once };
},
nowait: function() {
// trigger if all dependencies already supplied
@@ -64,8 +62,6 @@
this.exec(this.depends.map(function(curr){ return dependencies[curr] }));
}
return this;
- //var self = this;
- //return { then: self.then, once: self.once, stack: self.stack, depends: self.depends };
},
any: function() {
// trigger if any value is emitted, even if some aren't yet
@@ -79,17 +75,16 @@
this.all = true;
return this;
},
- then: function(func) {
- this.stack.push(func);
+ then: function() {
+ // Allow building stack as a sequence of arguments instead of
+ // just a chain of thens
+ Array.prototype.push.apply(this.stack, arguments);
// block modifiers, replace with their setting
if (typeof(this.once) == "function") this.once = false;
if (typeof(this.nowait) == "function") this.nowait = false;
if (typeof(this.any) == "function") this.any = false;
if (typeof(this.any) == "function") this.any = false;
return this;
- //var self = this;
- // cannot call anything but chained `then` hereafter
- //return { then: self.then, stack: self.stack, depends: self.depends };
},
exec: function(depends, pos) {
pos = pos || 0;
@@ -121,12 +116,12 @@
// start executing up the stack
if (this.stack[pos]) this.stack[pos].apply(hook, depends);
// once stack exhausted, override `then` to exec immediately with accrued depends
- else this.then = function(func) {
+ else this.then = function() {
+ Array.prototype.push.apply(this.stack, arguments);
if (typeof(this.once) == "function") this.once = false;
if (typeof(this.nowait) == "function") this.nowait = false;
if (typeof(this.any) == "function") this.any = false;
- this.stack.push(func);
- func.apply(hook, depends);
+ arguments[0].apply(hook, depends);
return self;
}
}
@@ -149,14 +144,17 @@
}
var ret = {
- do: function(func) {
+ do: function() {
// run immediately
var stack = new Stack();
- stack.then(func);
+ // push all passed functions to stack
+ Stack.prototype.then.apply(stack, arguments);
stack.exec();
return stack;
},
when: function(depends) {
+ // pass array, or extract arguments array
+ if (!Array.isArray(depends)) depends = Array.prototype.slice.call(arguments);
// run when dependencies men
return new Stack(depends);
},
@@ -199,7 +197,7 @@
return ret;
})();
- // module hook-innicked from underscore.js
+ // module hook-in nicked from underscore.js
// https://github.com/documentcloud/underscore/blob/master/underscore.js#L54
// MIT licenced. Thanks!
4 demo-server.js
View
@@ -62,8 +62,8 @@ YJ.when(['windowload'])
console.log("You can work here: " + workplace.address);
});
this.next(new Date().valueOf() - start.valueOf());
- })
- .then(function(workplaces, date, speed) {
+ },
+ function(workplaces, date, speed) {
console.log("rendering took " + speed + "ms");
});
58 demo.html
View
@@ -116,6 +116,64 @@
console.log(val);
});
+ ////////////////////////
+ /// ALTERNATE METHOD ///
+ ////////////////////////
+
+ YJ.do(
+ function() {
+ console.log('do1');
+ this.next('do2');
+ },
+ function(val1) {
+ console.log(val1);
+ this.next('do3');
+ },
+ function(val1, val2) {
+ console.log(val2);
+ }
+ );
+
+ YJ.when('load', 'someprop').then(
+ function(load, someprop) {
+ console.log('newwhen1');
+ this.next('newwhen2');
+ },
+ function(load, someprop, val1) {
+ console.log(val1);
+ this.next('newwhen3');
+ },
+ function(load, someprop, val1, val2) {
+ console.log(val2);
+ }
+ );
+
+ var ch = YJ.when('emitsometime')
+ .then(
+ function(es) {
+ console.log('es1');
+ this.next();
+ },
+ function(es) {
+ console.log('es2');
+ this.next();
+ }
+ );
+
+ YJ.emit('emitsometime');
+
+ ch.then(
+ function() {
+ console.log('es3');
+ this.next();
+ },
+ function() {
+ console.log('es4');
+ }
+ );
+
+
+
////////////////////////////
/// USING SYNC FUNCTIONS ///
////////////////////////////
Please sign in to comment.
Something went wrong with that request. Please try again.