Browse files

Extend Function.prototype for Fibers

Allow to use Fibers in context...

Example
```javascript
(function() {
  console.log( (require("fs").readdir.toSync)(".") );
}).asFiber;
```

<b>asFiber</b> extension

before
```javascript
(require("fibers")(function() { /* function code */ })).run()
```
after
```javascript
(function() { /* function code */ }).asFiber 
```
<b>toSync</b> extension

before
```javascript
(require("fibers/future").wrap(asyncFunction))(asyncArguments).wait()
```
after
```javascript
(asyncFunction.toSync)(asyncArguments)
```
  • Loading branch information...
1 parent 5f3094d commit ae6ab64b6c2daa4c6c13db1cb404cdef0635b950 @qxfusion committed Dec 29, 2012
Showing with 29 additions and 0 deletions.
  1. +29 −0 fibers.js
View
29 fibers.js
@@ -16,3 +16,32 @@ try {
// Injects `Fiber` and `yield` in to global scope (for now)
require(modPath);
module.exports = Fiber;
+
+// Function.prototype.toSync - Wrap function as Sync
+Object.defineProperty(Function.prototype,"toSync", {
+ enumerable: false, configurable: false,
+ get: function() {
+ return (function(context){
+ return (function(/*...*/) {
+ var a = []; for(var i in arguments) { a.push(arguments[i]); }
+ return (require("fibers/future").wrap(context)).apply(context,a).wait();
+ });
+ })(this); },
+ set: function() { }
+
+});
+
+// Function.prototype.toSync toString lock
+Object.defineProperty(Function.prototype.toSync,"toString", {
+ enumerable: false, configurable: false, get: function() { return function() {} }, set: function() { }
+});
+
+// Function.prototype.toFiber - Run function as Fiber
+Object.defineProperty(Function.prototype,"asFiber", {
+ enumerable: false, configurable: false, get: function() { return require("fibers")(this).run(); }, set: function() { }
+});
+
+// Function.prototype.toFiber toString lock
+Object.defineProperty(Function.prototype.asFiber,"toString", {
+ enumerable: false, configurable: false, get: function() { return function() {} }, set: function() { }
+});

0 comments on commit ae6ab64

Please sign in to comment.