是
一个 Dart 包,它将一个字符串不断地写入 IOSink,类似于 UNIX 的 yes 实用程序。
用法
// Write to stdout for 5 seconds.
final controller = yes(stdout, message);
await Future<void>.delayed(const Duration(seconds: 5));
controller.cancel();
// Write to a process's stdin for 5 seconds, and kill it.
// Wait for the yes operation to complete (which occurs when stdin closes)
// before continuing.
final process = await Process.start('cat', const []);
process.stdout.pipe(stdout);
final controller = yes(process.stdin);
Future.delayed(const Duration(seconds: 5))
.then((_) => process.kill(ProcessSignal.sigint));
await controller.done;
print('Done!');
完整的 UNIX yes 实用程序克隆可以在示例项目中找到。
性能说明
为了不阻塞事件循环,每个循环最多执行一次写入。
虽然这在大多数情况下都可以,但输出速度会受到严重限制。
这可以通过重复给定的消息几次来缓解
yes(stdout, 'y\ny\ny\ny');
此外,默认情况下,给定的 IOSink 的 flush future 被认为可能
是同步的,因此会添加一个额外的事件循环循环以防止事件循环
阻塞。如果 flush future 被保证为异步的,则可以避免额外的
循环。
yes(stdout, 'y', false);
许可证
MIT License
Copyright (c) 2021 hacker1024
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.