Permalink
Browse files

first commit

  • Loading branch information...
axkibe committed Oct 15, 2011
0 parents commit cc4a7d163fbbbd39cd5beb6ce21c2b5ea8bfd841
Showing with 126 additions and 0 deletions.
  1. +18 −0 LICENSE
  2. +57 −0 README.md
  3. +51 −0 green-light.js
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright 2011 Axel Kittenberger
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
@@ -0,0 +1,57 @@
+node-green-light --- A very simple stop and go interace to node-fibers
+======================================================================
+
+This wrapper to node-fiber aims to ease calling existing asynchronous
+from a synchronous(fiber) context. A green-light-fiber gets two functions,
+one for pausing ('red light', 'pause', 'stop', 'yield') and one for resuming
+('green light', 'resume' 'go', 'run').
+
+The resume() function is simply handed to asynchronous code as callback and
+the fiber then pauses() for it. This way all this pointers simply stay as
+they where. While using node-green-light produces a tad more code than other
+synchronization wrappers it is easy and transparent.
+
+The red function returns an array of all arguments passed to green (the callback).
+
+EXAMPLES
+--------
+A simple timeout:
+
+```javascript
+var greenlight = require('green-light');
+
+greenlight(function(pause, resume) {
+ console.log('starting timer');
+ setTimeout(resume, 2000, 'an argument');
+ pause();
+ console.log('two seconds passed');
+}
+console.log('the main context is not impressed');
+```
+
+Inserting an entry into a mongodb.
+
+```
+var mongodb = require('mongodb');
+var greenlight = require('./green-light');
+var server = new mongodb.Server('localhost', mongodb.Connection.DEFAULT_PORT, {});
+var connector = new mongodb.Db('test', server, {});
+
+greenlight(function(red, green) {
+ // comfort routine for the return values of almost all mongodb functions.
+ var myRed = function() {
+ var a = red();
+ if (a[0]) { throw new Error('dberror '+a[0].message); };
+ return a[1];
+ }
+ connector.open(green);
+ var client = myRed();
+ client.collection('test_collection', green);
+ var collection = myRed();
+ collection.insert({hello: 'world'}, {safe:true}, green);
+ myRed();
+ client.close(green);
+ myRed();
+ console.log('all finished');
+});
+```
@@ -0,0 +1,51 @@
+require('fibers');
+
+module.exports = function(fn) {
+ var red, green;
+ var light = false; // false .. red, true .. green
+ var fiber = Fiber(function() {
+ try {
+ fn(red, green);
+ } catch(e) {
+ process.nextTick(function() {
+ throw e;
+ });
+ }
+ });
+
+ var buffer = null;
+
+ red = function() {
+ if (!light) {
+ process.nextTick(function() {
+ throw new Error('red called on red light');
+ });
+ }
+ if (buffer) {
+ var b = buffer;
+ buffer = null;
+ return b;
+ }
+ light = false;
+ return yield();
+ };
+
+ green = function() {
+ if (light) {
+ // green called on green.
+ // an async functions might call its callback before red() was called.
+ // so buffer its answer for call of red.
+ if (buffer !== null) {
+ process.nextTick(function() {
+ throw new Error('green called twice on green light');
+ });
+ }
+ buffer = Array.prototype.slice.call(arguments);
+ } else {
+ light = true;
+ fiber.run(Array.prototype.slice.call(arguments));
+ }
+ };
+ light = true;
+ fiber.run();
+}

0 comments on commit cc4a7d1

Please sign in to comment.