Cycle.js
repository·master·Indexed 27 days ago
https://github.com/cyclejs/cyclejsA functional and reactive JavaScript framework that promotes predictable code through a circular data flow. It consists of a core runner (@cycle/run) and specialized driver packages for side effects, including @cycle/dom for DOM manipulation, @cycle/http for HTTP requests, @cycle/history for browser history, @cycle/html for HTML generation, @cycle/isolate for component scoping, @cycle/state for state management, and @cycle/time for time-based operations. It supports reactive stream libraries such as xstream, RxJS, and most.
What's inside Cycle.js
- Cycle.js is a functional and reactive JavaScript framework designed for predictable code. It is composed of several specialized packages that handle different aspects of application development, such as DOM manipulation, HTTP requests, and routing. You can install these packages individually via npm to build your application.
Understand Dataflow Components in Cycle.js
masterIn Cycle.js, any application can be reused as a component within a larger application. These are called dataflow components.
A dataflow component is essentially a
main()function that takes sources (streams provided by a parent or drivers) and returns sinks (streams sent back to a parent or drivers).Key concepts:
- Sources: Input streams (e.g., user events, properties/props).
- Sinks: Output streams (e.g., virtual DOM, state values).
- Props: Input attributes passed from a parent to a component to customize behavior or appearance, typically provided as a stream via a source key.
Use @cycle/http for HTTP requests
masterThe@cycle/httppackage provides a Cycle.js driver for making HTTP requests. It allows your application to interact with remote servers by treating HTTP requests as sources and HTTP responses as sinks. For detailed API signatures, usage patterns, and configuration options, refer to the official documentation.Use @cycle/html for HTML generation
masterThe@cycle/htmlpackage provides a way to generate HTML structures within a Cycle.js application. For detailed API documentation, including specific functions and usage patterns, refer to the official documentation at https://cycle.js.org/api/html.html.Use @cycle/state for state management
masterThe@cycle/statepackage provides a way to manage state within a Cycle.js application. For detailed API references, usage patterns, and implementation guides, refer to the official documentation.Extend Cycle.js by replacing or adding drivers
masterCycle.js is designed to be highly extensible. Because the core framework is minimal, drivers like
@cycle/domare treated as optional plugins. You can replace any standard driver with a custom implementation to target different environments or interfaces, such as:- Network/Sockets: For real-time communication.
- Node.js: For server-side reactive logic.
- Alternative UIs: Targeting
<canvas>, native mobile UI, or other UI trees instead of the standard DOM.
Use @cycle/history for browser history management
master@cycle/history is a Cycle.js driver that provides access to the browser's history API. It allows your application to react to URL changes and trigger navigation events (like pushing new states or replacing current states) through streams.Understand the Cycle.js Core Abstraction
masterCycle.js treats your application as a pure function called
main().- Sources: Inputs read from the external world (e.g., DOM events, HTTP responses).
- Sinks: Outputs written to the external world (e.g., Virtual DOM updates, HTTP requests).
- Drivers: Plugins that manage the I/O effects between your
main()function and the external world (e.g.,@cycle/domhandles DOM effects).
Your application logic is built using Reactive Programming primitives, creating an explicit dataflow graph where dependencies are clearly visible in the code.
Understand the concept of Drivers in Cycle.js
masterDrivers are functions that encapsulate imperative side effects. They act as adapters between your Cycle.js application and the external world (theUse @cycle/run to start a Cycle.js application
masterThe@cycle/runpackage is the core runner used to start a Cycle.js application. It connects your application's logic (themainfunction) to the outside world (sources and sinks) using a driver. For detailed API signatures and advanced usage, refer to the official documentation.Use @cycle/isolate for component isolation
masterThe@cycle/isolatepackage provides a mechanism to isolate components in Cycle.js. This allows you to create components that have their own local scope for sources and sinks, preventing naming collisions and enabling modularity. For detailed API documentation and usage patterns, refer to the official Cycle.js documentation.Understand the Cycle.js Dialogue Abstraction
masterCycle.js models Human-Computer Interaction (HCI) as a continuous dialogue or message-passing architecture. Instead of focusing solely on DOM manipulation, Cycle.js treats the interaction as a two-way exchange where both the human and the computer are agents that listen and speak.
In this model:
- The Computer acts as a function that takes inputs (senses) and produces outputs (actuators).
- The Human can also be modeled as a function that takes senses and produces actuators.
- The interaction is symmetric: the actuators of one agent are connected to the senses of the other.
This architecture is heavily inspired by the concept of stream-based I/O (similar to Haskell 1.0), where interaction is modeled as a mapping between streams of requests and responses.