mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
add node-fibers bindings.
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
///<reference path='./node-fibers.d.ts'/>
|
||||
// node-fibers tests
|
||||
// compile with --module=common
|
||||
|
||||
import Fiber = require("fibers");
|
||||
import Future = require("fibers/future");
|
||||
|
||||
function sleep(ms) {
|
||||
var fiber = Fiber.current;
|
||||
setTimeout(function() {
|
||||
fiber.run();
|
||||
}, ms);
|
||||
Fiber.yield();
|
||||
}
|
||||
|
||||
Fiber(function() {
|
||||
console.log('wait... ' + new Date);
|
||||
sleep(1000);
|
||||
console.log('ok... ' + new Date);
|
||||
}).run();
|
||||
console.log('back in main');
|
||||
|
||||
var inc = Fiber(function(start) {
|
||||
var total = start;
|
||||
while (true) {
|
||||
total += Fiber.yield(total);
|
||||
}
|
||||
});
|
||||
|
||||
for (var ii = inc.run(1); ii <= 10; ii = inc.run(1)) {
|
||||
console.log(ii);
|
||||
}
|
||||
|
||||
|
||||
// Generator function. Returns a function which returns incrementing
|
||||
// Fibonacci numbers with each call.
|
||||
function Fibonacci() {
|
||||
// Create a new fiber which yields sequential Fibonacci numbers
|
||||
var fiber = Fiber(function() {
|
||||
Fiber.yield(0); // F(0) -> 0
|
||||
var prev = 0, curr = 1;
|
||||
while (true) {
|
||||
Fiber.yield(curr);
|
||||
var tmp = prev + curr;
|
||||
prev = curr;
|
||||
curr = tmp;
|
||||
}
|
||||
});
|
||||
// Return a bound handle to `run` on this fiber
|
||||
return fiber.run.bind(fiber);
|
||||
}
|
||||
|
||||
// Initialize a new Fibonacci sequence and iterate up to 1597
|
||||
var seq = Fibonacci();
|
||||
for (var ii = seq(); ii <= 1597; ii = seq()) {
|
||||
console.log(ii);
|
||||
}
|
||||
|
||||
var fn = Fiber(function() {
|
||||
console.log('async work here...');
|
||||
Fiber.yield();
|
||||
console.log('still working...');
|
||||
Fiber.yield();
|
||||
console.log('just a little bit more...');
|
||||
Fiber.yield();
|
||||
throw new Error('oh crap!');
|
||||
});
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
fn.run();
|
||||
}
|
||||
} catch(e) {
|
||||
console.log('safely caught that error!');
|
||||
console.log(e.stack);
|
||||
}
|
||||
console.log('done!');
|
||||
|
||||
|
||||
|
||||
// This function returns a future which resolves after a timeout. This
|
||||
// demonstrates manually resolving futures.
|
||||
function sleep2(ms) {
|
||||
var future = new Future();
|
||||
setTimeout(function() {
|
||||
future.return();
|
||||
}, ms);
|
||||
return future;
|
||||
}
|
||||
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
// Type definitions for node-fibers
|
||||
// Project: https://github.com/laverdet/node-fibers
|
||||
// Definitions by: Cary Haynie <https://github.com/caryhaynie>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
interface Fiber {
|
||||
reset: () => any;
|
||||
run: (param?: any) => any;
|
||||
throwInto: (ex: any) => any;
|
||||
}
|
||||
|
||||
declare module "fibers" {
|
||||
|
||||
function Fiber(fn: Function): Fiber;
|
||||
|
||||
module Fiber {
|
||||
export var current: Fiber;
|
||||
export function yield(value?: any): any
|
||||
}
|
||||
|
||||
export = Fiber;
|
||||
}
|
||||
|
||||
declare module "fibers/future" {
|
||||
|
||||
class Future {
|
||||
constructor();
|
||||
detach(): void;
|
||||
get(): any;
|
||||
isResolved (): boolean;
|
||||
proxy(future: Future): void;
|
||||
proxyErrors(futureOrList: any): Future;
|
||||
resolver(): Function;
|
||||
resolve(fn: Function): void;
|
||||
resolveSuccess(fn: Function): void;
|
||||
return(result?: any): void;
|
||||
throw (error: any): void;
|
||||
wait (): void;
|
||||
static wait(future: Future);
|
||||
static wait(future_list: Future[]);
|
||||
static wrap(fn: Function): Future
|
||||
}
|
||||
|
||||
export = Future;
|
||||
}
|
||||
Reference in New Issue
Block a user