BY Simmi Kava4 Aug 2021 Edit
WebAtoms: Load Decorator & Debouncing

What are Function/Method Decorators?

Function decorators allow us to add extra feature to your existing function i.e. it allows to change behavior of your existing function at compile time.

WebAtoms: Load Decorator

Load decorator makes loading of async data very easy. Let's assume that we want to load countries and then selected state. It reports exceptions and cancels previous operation.

export default class SignupViewModel extends AtomViewModel {

    /** This gets called on initialization */
    @Load({ init: true })
    public async loadCountries() {
        this.countries = 
            await this.restService.getCountries();
        this.selectedCountry = this.countries
            .find((x) => x.value === "IN").value;
    }

    /** This gets called when `this.selectedCountry` changes */
    @Load({ watch: true /* watch */ })
    public async loadStates(ct: CancelToken) {
        const country = this.selectedCountry;
        // pass cancel token
        // to automatically cancel previous
        // incomplete request
        this.states = 
            await this.restService.getStates(country, ct);
    }

}

If you look in the above logic, we have a decorator @Load({init: true}), which will be executed during the initialization (only once) of view model (we can use init on multiple methods) and another @Load({watch:true}), which will watch for changes in the country selected and fire the next operation i.e. getting the states based on country selected.

What is Debouncing?

It is a programming technique to limit the number of times a long-drawn-out function gets called, which stalls the performance of your web page.

How can we achieve Debouncing using WebAtoms?

Let's look at the example below.

export default class SignupViewModel extends AtomViewModel {

    @Load({
        /** watch for changes */
        watch: true,
        /** wait as user might still be typing */
        watchDelayInMS: 500
    })
    public async loadCities(ct: CancelToken) {
        const search = this.search;

        /** cancellation of previous request is tracked here */
        this.cities = await
            this.restService.searchCities(search, ct);
    }

}

As we look in the above logic, we are watching for any changes (user input) to the City. We have introduced a delay of 500ms as user might still be typing, ct is a cancel token which is tracked and only the last call result is returned i.e. if the debounced method/function is called only the latest call will result in the method call, the other ones will be discarded thereby limiting the number of times the method is invoked and improving the performance.

BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2024
2023
2022
2021
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository