This module provides a function to create a lazy structure based on the provided function or class.
The created structure is a function with the pre-defined methods and properties from the passed scheme and/or class prototype.
All property or method actions will be intercepted and accumulated in a queue. After invoking the result function,
all accumulated actions will be executed.
// Declaring a scheme with the result of the original function invoking { // A string property with the default value name:'',
// A number property with the default value age:0,
// A method showInfo:Function } );
// Nothing happens at all lazyUser.age = 45; lazyUser.showInfo();
// Invoking the lazy function and passing necessary arguments. // After invoking we can see in a console the result of calling `showInfo`: // `Name: Bob; Age: 45` constuser = lazyUser('Bob', 10);
// Declaring only properties. // All methods are taken automatically from the class prototype. { config: { attr: {}, errorHandler:Function } } );
// Nothing happens at all LazyUser.showInfo(); LazyUser.config.attr = 'value'; LazyUser.config.errorHandler();
// Creating an instance of the lazy class and passing necessary arguments to its constructor. // After invoking we can see in a console the results of calling `showInfo` and `config.errorHandler()`: // `Name: Bob; Age: 23` // `Boom!` constuser = newLazyUser('Bob', 23);
// Because of `LazyUser.config.attr = 'value'` console.log(user.config.attr === 'value');
// After invoking we can see in a console the results of `showInfo` and `config.errorHandler()`: // `Name: Fred; Age: 56` // `Boom!` constuser2 = newLazyUser('Fred', 56);
There is possibility to provide hook handlers on of some structure actions,
like getting or setting a property value or method invoking. These handlers take an array of the already created Lazy instances.
Other arguments depend on the hook type.
default<T>(constructor: T, scheme?: ObjectScheme, hooks?: Hooks<T extends ClassConstructor<any[], any> ? InstanceType<T> : T extends (...args: A) => R ? R : object>): T extends ClassConstructor ? T & InstanceType<T> : T extends (...args: infer A) => infer R ? ((...args: A) => R) & R : never
Creates a new function based on the passed function or class and returns it.
The new function accumulates all method and properties actions into a queue.
The queue will drain after invoking the created function.
core/lazy
This module provides a function to create a lazy structure based on the provided function or class. The created structure is a function with the pre-defined methods and properties from the passed scheme and/or class prototype. All property or method actions will be intercepted and accumulated in a queue. After invoking the result function, all accumulated actions will be executed.
Usage
With a function that returns an object
With a class
Action hooks
There is possibility to provide hook handlers on of some structure actions, like getting or setting a property value or method invoking. These handlers take an array of the already created
Lazy
instances. Other arguments depend on the hook type.