Git Product home page Git Product logo

lazy-loader's Introduction

Lazy Initializer

logo GitHub issues GitHub license Node version NPM version NPM downloads

Lazy Initializer is a generic deferred object initializer, which will creates a wrapper which waits for your first time use, then it will triggers the initialize function you defined. The concept is similar to C#'s Lazy class, but more transparent implementation in ES6.

Usage

Simple usage for wrapping a property in a class:

import { LazyProperty } from 'lazy-initializer'; // or require(...) if your environment does not support import.

class Schrodinger {
  @LazyProperty
  get cat() { return Math.random() > 0.5; }
  // Setter will be called when the value has been assigned first time.
  // Setters can be not defined, but then the property will be read-only.
  set cat(value) {
    console.log(`It is ${value ? 'alive' : 'dead'} now!`);
    assert.strictEqual(value, this.cat);
  }
}

const isAlive = new Schrodinger().cat;

Alternatively, if your transpiler or environment does not support ES6 decorators:

import { LazyProperty } from 'lazy-initializer';

class Schrodinger {
  get cat() { return Math.random() > 0.5; }
}
LazyProperty.transform(Schrodinger, 'cat');

Also, you may manually craete a new lazy property without defining the getter/setter before:

import { LazyProperty } from 'lazy-initializer';

const someObject = {};
LazyProperty.define(someObject, 'somelazyField', () => 'boo!');
// Then, `someObject` has a `somelazyField` now!

// You may batch define more properties like this:
LazyProperty.define(someObject, {
  someOtherLazyField: () => 'another one!',
  someMoreComplicatedLazyField: {
    init: () => 'More controllable behaviour!',
    enumerable: false,
    configurable: false,
    writable: true,
  },
});

Another advanced usage is wrapping a whole object (which uses proxy):

import { LazyProxy } from 'lazy-initializer';

const somethingExpensive = LazyProxy.create(() => {
  // Some heavy stuffs...
  return someHeavyObject;
});

// You may treat the object is loosely equals to the initialized object itself.
const someValue = somethingExpensive.someValue();

If the lazy initialized object will be used as constructors:

import { LazyProxy } from 'lazy-initializer';

const SomeHeavyConstructor = LazyProxy.create(() => {
  // Some heavy stuffs...
  return Foo;
}, true);
// The true means this will use as constructor,
// the proxy internals will do some tweaks to make this to be supported.

const someValue = new SomeHeavyConstructor();

For more information, please see docs.

Installation

In your Node.js project path, run:

$ npm install --save lazy-initializer

or yarn

$ yarn add lazy-initializer

Requirements

This module make uses the new ES6 features, especially proxy, therefore it requires at least Node.js 6+ to works.

ECMAScript 6 compatibility table

License

MIT

lazy-loader's People

Contributors

dependabot[bot] avatar jlchntoz avatar reallinfo avatar

Watchers

 avatar  avatar  avatar

Forkers

reallinfo

lazy-loader's Issues

logo contribution

Hi @JLChnToZ I contribute to open source projects in my free time. I reviewed your project and i noticed this project did not have a logo. how about using this logo? Please let me know if you have any requests. I will wait for feedback. Have a nice day!

LAZYLOADER

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.