Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hyperapp-loadable

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hyperapp-loadable

Experimental package to provide react-loadable like behavior to hyperapp

  • 0.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

hyperapp-loadable

WIP This package is inspired be react-loadable. The main difference between the two packages is that this package targets hyperapp instead of react and thus does not have stateful components.

To work around this difference we use a mixin to provide a { loadable: {} } in the state as well as add actions: { loadable: { loaded: () => any }} for triggering an update/re-render after the lazy-loaded module is available.

/* src/Test.js */
import { h } from "hyperapp";

export function Test(props) {
  return (
    <div>{"Testing lazy-loaded component!"}</div>
  );
}
import { h, app } from "hyperapp";
import { loadable, Loadable, load } from "hyperapp-loadable";

const Loading = (props) => <div>{`Loading... look at my ${props}`}</div>;

app({
  view: (state, actions) => {
    return (
      <Loadable 
        loaded={actions.loadable.loaded} // *REQUIRED
        loadable={state.loadable} // *REQUIRED
        name={"/Test"} // *REQUIRED unique key for the result of loader to be stored under state.loadable[name]
        loader={() => import("./Test")} // *REQUIRED thunk which returns a promise that resolves to a component
        loaderProps={({ state: state, actions: actions })} // Optional, but useful...
        loading={Loading} // *REQUIRED component to display while loading the above loader thunk...
        loadingProps={({ foo: "bar" })} // Optional, will be passed into your Loading component for render
        defaultTime={200} // Optional default 200ms time spent displaying loading
        terminalTime={3000} // Optional default 3 second error timeout
        errorHandler={ // Optional could be used to clear cache and retry on failures etc...
          ({ name, result }) => console.error(`Loadable: ${name}, error: ${result}`)
        }
      />
    );
  },
  mixins: [loadable]
});
  • NOTE on SSR
/* SSR needs a few things special for this to all work from within nodejs...
   - you will need to add babel-plugin-dynamic-import-node to your babel config for your server bundle
   - const defaultTime = isServer() ? -1 : 200;
     const terminalTime = isServer() ? -1 : 3000;
     set default timeouts to -1 so that setTimeout's do not trigger in your server side renders...
   - be sure to load() all your components needed for a route prior to calling app({...}) then it will   all just work, YMMV

   // example of isServer used in above SSR setup...
   const isServer() => {
     return (
      typeof process !== "undefined" &&
      process.release != null &&
      (process.release.name.search(/node|io.js/) !== -1 ||
        typeof process.versions.node !== "undefined")
    );
   }
*/

License

hyperapp-loadable is MIT licensed. See LICENSE.

FAQs

Package last updated on 28 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc