Code Monkey home page Code Monkey logo

load-entry's Introduction

load-entry

Package to offload the execution of a function, object or module.

By default, if there's a document (i.e. on browsers), it will wait for the 'DOMContentLoaded' event and then execute the given object. If not it will execute the object immediately.

Why I use it on the browser

To create modules that are testable in any scenario.

Why I use it on a NodeJs app

To facilitate module running and unit testing.

Basic Usage (browser)

// index.js -> app entry file
import loadEntry from 'load-entry';
import App from './App';

const myEntryPoint = () => {
  App.run(); // -> executed only after the document triggers 'ready'
};

loadEntry(myEntryPoint);

More examples

Also runs these examples seamlessly:

// Example 1: object with init function
const myEntryPoint = {
  init: () => {
    App.run();
  }
};
loadEntry(myEntryPoint);

// Example 2: object with custom init function
const myEntryPoint = {
  myInit: () => {
    App.run();
  }
};
loadEntry(myEntryPoint, 'myInit');
// OR
loadEntry(myEntryPoint, { init: 'myInit' });

// Example 3: module with default as export
// entry.js
export default () => {
  App.run();
};
// index.js
import Entry from './entry';
loadEntry(Entry);

// Example 4: module with named exports
// entry.js
export const myEntryPoint = () => {
  App.run();
};
export const mySecondEntryPoint = () => {
  console.log('This also runs!');
};
// index.js
import Entry from './entry';
loadEntry(Entry);

// Example 5: run after a custom event is triggered
const myEntryPoint = () => {
  App.run();
};
loadEntry(myEntryPoint, { event: 'MyCustomEvent' });

// Example 6: run immediately (in a browser environment)
const myEntryPoint = () => {
  App.run();
};
loadEntry(myEntryPoint, { event: null });

load-entry's People

Contributors

tiagomapmarques avatar

Stargazers

 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.