Files
wren/doc/site/core/fiber.markdown
T

89 lines
1.9 KiB
Markdown
Raw Normal View History

2015-01-18 15:36:36 -08:00
^title Fiber Class
^category core
A lightweight coroutine. [Here](../fibers.html) is a gentle introduction.
### new **Fiber**(function)
Creates a new fiber that executes `function` in a separate coroutine when the
fiber is run. Does not immediately start running the fiber.
:::dart
var fiber = new Fiber {
IO.print("I won't get printed")
}
2015-02-26 23:08:36 -08:00
### Fiber.**yield**()
2015-01-18 15:36:36 -08:00
Pauses the current fiber and transfers control to the parent fiber. "Parent"
here means the last fiber that was started using `call` and not `run`.
:::dart
var fiber = new Fiber {
IO.print("Before yield")
2015-02-26 23:08:36 -08:00
Fiber.yield()
2015-01-18 15:36:36 -08:00
IO.print("After yield")
}
2015-02-26 23:08:36 -08:00
fiber.call() // "Before yield"
2015-01-18 15:36:36 -08:00
IO.print("After call") // "After call"
2015-02-26 23:08:36 -08:00
fiber.call() // "After yield"
2015-01-18 15:36:36 -08:00
2015-02-26 23:08:36 -08:00
When resumed, the parent fiber's `call()` method returns `null`.
2015-01-18 15:36:36 -08:00
If a yielded fiber is resumed by calling `call()` or `run()` with an argument,
2015-02-26 23:08:36 -08:00
`yield()` returns that value.
2015-01-18 15:36:36 -08:00
:::dart
var fiber = new Fiber {
2015-02-26 23:08:36 -08:00
IO.print(Fiber.yield()) // "value"
2015-01-18 15:36:36 -08:00
}
2015-02-26 23:08:36 -08:00
fiber.call() // Run until the first yield.
2015-01-18 15:36:36 -08:00
fiber.call("value") // Resume the fiber.
2015-02-26 23:08:36 -08:00
If it was resumed by calling `call()` or `run()` with no argument, it returns
`null`.
2015-01-18 15:36:36 -08:00
It is a runtime error to call this when there is no parent fiber to return to.
:::dart
2015-02-26 23:08:36 -08:00
Fiber.yield() // ERROR
2015-01-18 15:36:36 -08:00
new Fiber {
2015-02-26 23:08:36 -08:00
Fiber.yield() // ERROR
}.run()
2015-01-18 15:36:36 -08:00
### Fiber.**yield**(value)
Similar to `Fiber.yield` but provides a value to return to the parent fiber's
`call`.
:::dart
var fiber = new Fiber {
Fiber.yield("value")
}
2015-02-26 23:08:36 -08:00
IO.print(fiber.call()) // "value"
2015-01-18 15:36:36 -08:00
2015-02-26 23:08:36 -08:00
### **call**()
2015-01-18 15:36:36 -08:00
**TODO**
### **call**(value)
**TODO**
### **isDone**
Whether the fiber's main function has completed and the fiber can no longer be
run. This returns `false` if the fiber is currently running or has yielded.
2015-02-26 23:08:36 -08:00
### **run**()
2015-01-18 15:36:36 -08:00
**TODO**
### **run**(value)
**TODO**