Giter VIP home page Giter VIP logo

redoodle's Introduction

Redoodle

Redoodle is an addon library for Redux that enhances its integration with TypeScript.

Redoodle includes a few major categories of addons that can be used individually and play well with each other:

Circle CI npm

Motivation

Redux brought sanity to state management, and has found itself a seat in many a React application's architecture. TypeScript is also gaining a large amount of traction, and has impressed the community with its month-over-month high quality releases. Unfortunately, there were a few places where default Redux and TypeScript failed to play well with each other:

  1. Redux Actions have magic string types and opaque payloads, with no obvious mechanism to track the correlation between the two.
  2. Redux Reducers have no easy way to infer correct typings for different Action branches.
  3. The TypeScript/JavaScript STL lacks good immutable state update functions. While this has gotten better with Object and Array spread and their reasonable typings, there is still a dearth of exact behavior for certain common Redux update workflows.

Redoodle attempts to solve these integration pains, and takes a stab at a few more common points of developer frustration when working with Redux and Typescript.

Features

Typed Actions

Typed Actions do the most legwork to bridge the Redux-TypeScript divide, by correlating Action magic type strings with Action magic payloads. With Redoodle, you can create an Action Definition, and then use that Definition in all places you were previously guessing, casting, or crying.

import { TypedAction, TypeReducer, setWith } from "redoodle";

// FlipTable is an action Definition for actions of type "app::flip_table",
// and associates the given payload type with the action.
const FlipTable = TypedAction.define("app::flip_table")<{
  tableId: string;
  face: "happy" | "angry";
}>();

// We can use a Definition to create a matching Action. This is all type-safe.
// At runtime, this `action` has the value
//
//    { type: "app::flip_table",
//      payload: { tableId: "2567f216-59b7-4bfe-b46f-909c6711fea4", face: "happy" }
//    }
//
const action = FlipTable.create({
  tableId: "2567f216-59b7-4bfe-b46f-909c6711fea4",
  face: "happy"
});

// We can also use Definitions to create Reducers that offer slick type inferencing.
// No more manual casts!
interface BanquetHall {
  [tableId: string]: {
    isFlipped: boolean;
  }
}

// banquetHallReducer is a standard Redux Reducer<BanquetHall>. Nothing fancy.
const banquetHallReducer = TypedReducer.builder<BanquetHall>()
  // FlipTable.TYPE is the plain string "app::flip_table", but it's branded with
  // rich compile-time information about its associated payload type {tableId, face}.
  .withHandler(FlipTable.TYPE, (state, {tableId, face}) => {
    // All of the operations here are 100% type-safe. If I misspelled `isFlipped` below
    // in either place, or fail to copy the correct subset of state when applying my
    // immutable copies, the compiler will save me!
    return setWith(state, {
      [tableId]: setWith(state[tableId], {
        isFlipped: !state[tableId].isFlipped
      });
    });
  })
  .build();

For more on Typed Actions, probably the most simple and significant utility Redoodle offers, check out the Redoodle docs.

Compound Actions

The only builtin Action Definition that Redoodle ships with is the CompoundAction, which is a simple higher-level Action that wraps another set of Actions. Its alternatives in stock Redux is either to

  1. Create more complex DoFooAndBar actions, which have the drawback of making every Reducer that cares about DoFoo or DoBar also care about DoFooAndBar.
  2. Dispatch DoFoo and DoBar individually, which has the drawback of causing multiple Store subscription events, which often results in e.g. multiple React renders. It also has the drawback of publishing a possibly inconsistent intermediate state when DoFoo was applied but not DoBar.
import { CompoundAction } from "redoodle";

const doFoo: Action = {...};
const doBar: Action = {...};

store.dispatch(CompoundAction.create([doFoo, doBar]));

To use CompoundActions, your store must be configured to correctly unwrap and reduce them; thankfully Redoodle comes with a compoundActionsEnhancer() (or the simpler reduceCompoundActions decorator) for exactly that.

For more on CompoundActions, check out the Redoodle docs.

Utilities

Redoodle packages a number of utility functions, some explicitly for clean TypeScript immutable state manipulation, some for completeness in developer experience.

On Initial State Management

Redoodle firmly believes that specific benefits the TypeScript compiler provides should change the way we think about Redux State initialization. This shifts the behavior of some Redoodle utilities slightly to be more ergonomic for developers, and is discussed in greater detail here.

Documentation

Getting Involved

Coming Soon!

License

Apache 2.0

redoodle's People

Contributors

crazytoucan avatar

Watchers

 avatar  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.