Giter Site home page Giter Site logo

stream's Introduction

Stream Component

Build Status

Basic readable and writable stream interfaces that support piping.

In order to make the event loop easier to use, this component introduces the concept of streams. They are very similar to the streams found in PHP itself, but have an interface more suited for async I/O.

Mainly it provides interfaces for readable and writable streams, plus a file descriptor based implementation with an in-memory write buffer.

This component depends on événement, which is an implementation of the EventEmitter.

Readable Streams

EventEmitter Events

  • data: Emitted whenever data was read from the source.
  • end: Emitted when the source has reached the eof.
  • error: Emitted when an error occurs.
  • close: Emitted when the connection is closed.

Methods

  • isReadable(): Check if the stream is still in a state allowing it to be read from. It becomes unreadable when the connection ends, closes or an error occurs.
  • pause(): Remove the data source file descriptor from the event loop. This allows you to throttle incoming data.
  • resume(): Re-attach the data source after a pause().
  • pipe(WritableStreamInterface $dest, array $options = []): Pipe this readable stream into a writable stream. Automatically sends all incoming data to the destination. Automatically throttles based on what the destination can handle.

Writable Streams

EventEmitter Events

  • drain: Emitted if the write buffer became full previously and is now ready to accept more data.
  • error: Emitted whenever an error occurs.
  • close: Emitted whenever the connection is closed.
  • pipe: Emitted whenever a readable stream is pipe()d into this stream.

Methods

  • isWritable(): Check if the stream can still be written to. It cannot be written to after an error or when it is closed.
  • write($data): Write some data into the stream. If the stream cannot handle it, it should buffer the data or emit and error event. If the internal buffer is full after adding $data, write should return false, indicating that the caller should stop sending data until the buffer drains.
  • end($data = null): Optionally write some final data to the stream, empty the buffer, then close it.

Usage

    $loop = React\EventLoop\Factory::create();

    $source = new React\Stream\Stream(fopen('omg.txt', 'r'), $loop);
    $dest = new React\Stream\Stream(fopen('wtf.txt', 'w'), $loop);

    $source->pipe($dest);

    $loop->run();

stream's People

Contributors

igorw avatar cboden avatar clue avatar steverhoades avatar wyrihaximus avatar arnaud-lb avatar jmalloc avatar adamlc avatar daverandom avatar jsor avatar

Watchers

 avatar James Cloos avatar James Jackson avatar mattfenn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.