All Projects → pomber → Hitchcock

pomber / Hitchcock

Licence: mit
The Master of Suspense 🍿

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Hitchcock

Vue Api Request
Control your API calls by using an amazing component which supports axios and vue-resource
Stars: ✭ 116 (-30.54%)
Mutual labels:  component, loader
Gdb Frontend
☕ GDBFrontend is an easy, flexible and extensionable gui debugger.
Stars: ✭ 2,104 (+1159.88%)
Mutual labels:  debugger, debug
React Native Vdebug
React-Native 调试工具,支持Console终端、Network导出cURL,可视化Response,Retry cURL。
Stars: ✭ 124 (-25.75%)
Mutual labels:  debugger, debug
Reason Loadable
🔥 Suspense/Lazy for ReasonReact.
Stars: ✭ 88 (-47.31%)
Mutual labels:  lazy, component
Investigator
Interactive and asynchronous logging tool for Node.js. An easier way to log & debug complex requests directly from the command line (experimental).
Stars: ✭ 155 (-7.19%)
Mutual labels:  async, debug
React Native Skeleton Content Nonexpo
A customizable skeleton-like loading placeholder for react native projects not using expo.
Stars: ✭ 92 (-44.91%)
Mutual labels:  component, loader
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (-20.36%)
Mutual labels:  async, concurrent
Ios Sdk
AppSpector is a debugging service for mobile apps
Stars: ✭ 56 (-66.47%)
Mutual labels:  debugger, debug
Tascalate Concurrent
Implementation of blocking (IO-Bound) cancellable java.util.concurrent.CompletionStage and related extensions to java.util.concurrent.ExecutorService-s
Stars: ✭ 144 (-13.77%)
Mutual labels:  async, concurrent
React Native Wormhole
⚛️ 🌌 Inter-dimensional Portals for React Native. 👽 🖖
Stars: ✭ 133 (-20.36%)
Mutual labels:  component, loader
Magpie
🐦 Successor of my monkey Interpreter(support for class, linq, sql, net, http, fmt, json and A realtime syntax highlighting REPL).
Stars: ✭ 88 (-47.31%)
Mutual labels:  async, debugger
Scyllahide
Advanced usermode anti-anti-debugger. Forked from https://bitbucket.org/NtQuery/scyllahide
Stars: ✭ 2,211 (+1223.95%)
Mutual labels:  debugger, debug
Strongod
StrongOD(anti anti-debug plugin) driver source code.
Stars: ✭ 76 (-54.49%)
Mutual labels:  debugger, debug
Redis
Async Redis Client for PHP based on Amp.
Stars: ✭ 107 (-35.93%)
Mutual labels:  async, cache
Keshi
A better in-memory cache for Node and the browser
Stars: ✭ 75 (-55.09%)
Mutual labels:  async, cache
Vue Loaders
Vue + loaders.css
Stars: ✭ 127 (-23.95%)
Mutual labels:  component, loader
React Async Fetcher
React component for asynchronous loading/fetch online data
Stars: ✭ 50 (-70.06%)
Mutual labels:  component, loader
Recloser
A concurrent circuit breaker implemented with ring buffers
Stars: ✭ 51 (-69.46%)
Mutual labels:  async, concurrent
Angular Async Loader
Load modules and components asynchronously for angular 1.x application.
Stars: ✭ 137 (-17.96%)
Mutual labels:  async, loader
Smartdebug.js
Next-generation debugging for javascript!
Stars: ✭ 157 (-5.99%)
Mutual labels:  debugger, debug

“There is no terror in the bang, only in the anticipation of it.”

— Alfred Hitchcock

Hitchcock npm version

Hitchcock is a debugging tool for React Suspense. It wraps your calls to React.lazy(), provides a simple cache (based on react-cache) and let you pause, delay or invalidate your promises.

🚨 EXPERIMENTAL 🚨

Use this only for experimenting with the new React Concurrent Mode. Hitchcock is inefficient and unstable. Also, I have no idea what I'm doing.

Demos

The code is in the examples folder.

Usage

Try it on CodeSandbox

Add the dependency:

$ yarn add hitchcock

Director

Import the Director component and add it somewhere in your app:

import { Director } from "hitchcock";

function YourApp() {
  return (
    <Director>
      <YourStuff />
    </Director>
  );
}

Lazy

Instead of using React.lazy import lazy from hitchcock:

import { lazy } from "hitchcock";

const HomePage = lazy(() => import("./components/HomePage"));

// Hitchcock's lazy accepts a second parameter with the name of the component:
const ArtistPage = lazy(() => import("./components/ArtistPage"), "ArtistPage");
// it's optional, but recommended, it isn't always easy to guess the name from the import

createResource

import { createResource } from "hitchcock";

const BeerResource = createResource(
  id =>
    fetch(`https://api.punkapi.com/v2/beers/${id}`)
      .then(r => r.json())
      .then(d => d[0]),
  id => `beer-${id}`
);

function Beer({ beerId }) {
  const beer = BeerResource.read(beerId);
  return (
    <>
      <h1>{beer.name}</h1>
      <p>{beer.description}</p>
    </>
  );
}

createResource has two parameters. The first one is a function that returns a promise. The second one is a function that returns an id, that id is used as the key in the cache and also is used as the name of the resource in the debugger.

It returns a resource with a read method that will suspend a component until the resource is ready (when the promise resolves).

Waterfalls

React docs warn about using Suspense as a way to start fetching data when a component renders. The recommended approach is to start fetching before rendering, for example, in an event handler. Hitchcock doesn't solve this problem, but it provides a preload method if you want to try:

import React, { Suspense } from "react";
import ReactDOM from "react-dom";
import { createResource, Director } from "hitchcock";

const BeerResource = createResource(
  id =>
    fetch(`https://api.punkapi.com/v2/beers/${id}`)
      .then(r => r.json())
      .then(d => d[0]),
  id => `beer-${id}`
);

function App() {
  const [beerId, setBeerId] = React.useState(0);
  const deferredBeerId = React.useDeferredValue(beerId, { timeoutMs: 1000 });

  const showBeer = deferredBeerId > 0;

  const handleChange = e => {
    const newBeerId = +e.target.value;
    BeerResource.preload(newBeerId);
    setBeerId(newBeerId);
  };

  return (
    <Director>
      Beer # <input type="number" value={beerId} onChange={handleChange} />
      <Suspense fallback={<div>{`Loading beer #${beerId}...`}</div>}>
        {showBeer && <Beer beerId={deferredBeerId} />}
      </Suspense>
    </Director>
  );
}

function Beer({ beerId }) {
  const beer = BeerResource.read(beerId);
  return (
    <>
      <h1>{beer.name}</h1>
      <p>{beer.description}</p>
    </>
  );
}

const container = document.getElementById("root");
ReactDOM.createRoot(container).render(<App />);

Contributing

$ git clone [email protected]:pomber/hitchcock.git
$ cd hitchcock
$ npx lerna bootstrap

Run the examples:

$ yarn start:example movies
$ yarn start:example suspensify

Publish new version:

$ yarn build:packages
$ npx lerna publish

License

Released under MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].