1.1.9 • Published 7 months ago

async-source v1.1.9

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

AsyncSource

With AsyncSource you can

  • Create stateful reactive data source for asynchronous requests
  • Handle errors
  • Configure throttling
  • Ignore inconsistent state
  • Control state of your requests (isLoading, isFetch, data)

In case of multiple calls - only last call will be processed. Connect AsyncSource to your dynamic selects with in build debounce

Install

npm install --save async-source

Usage

// javascript
import AsyncSource from 'async-source';

const source = new AsyncSource(request);

async function loadItems() {
    await source.update(...requestParams);
    const response = source.data;
}
// typescript
import AsyncSource from 'async-source';

const source = new AsyncSource<RespnonsType>(request);

async function loadItems() {
    await source.update(...requestParams);
    const response = source.data;
}

Parameters

ParameterIs requiredDescription
serviceMethodtrueMethod that returns promise
errorHandlerfalseFunction that will be called in case of service method rejected
delayfalsedelay in ms

Properties(reactive getters)

PropertyDescriptionType
dataReturns your method responseYour method response
isLoadingReturns true when request pendingBoolean
isFetchReturns true after first loadBoolean

Methods

MethodDescriptionParams
updateCalls be requestYour method request params
pushCalls be request and handles success responseSuccess handler, Your method request params
updateIfEmptyCalls be request in source if data is emptyYour method request params
updateOnceCalls be request in source if data is empty and waits till other request resolvedYour method request params
updateImmediateCalls be request in source ignoring debounceYour method request params
clearSet source data to initial state(null)---

Usage with vue3(composition api)

<template>
    <ul v-loading="isLoading">
        <li v-for="item in items" :key="item.id">
            {{ item.name }}
        </li>
    </ul>
</template>

<script>
import AsyncSource from 'async-source';
import { computed, reactive } from 'vue';

export default {
    name: 'MyComponent',
    setup() {
        function errorHandler(error) {
            console.error(error);
        }

        const source = reactive(new AsyncSource(request, errorHandler, 300));

        source.update();

        const items = computed(() => source.data || []);
        const isLoading = computed(() => source.isLoading);
        
        return {
            items,
            isLoading
        };
    }
}

</script>

Usage with vue2(option api)

<template>
    <ul v-loading="isLoading">
        <li v-for="item in items" :key="item.id">
            {{ item.name }}
        </li>
    </ul>
</template>

<script>
import AsyncSource from 'async-source';

export default {
    name: 'MyComponent',
    data() {
        return {
            source: new AsyncSource(request, this.errorHandler, 300)
        }
    },
    computed: {
        items() {
            return this.source.data || [];
        },
        isLoading() {
            return this.source.isLoading;
        }
    },
    created() {
        this.source.update();
    },
    methods: {
        errorHandler(error) {
            console.error(error);
        }
    }
}

</script>
1.1.9

7 months ago

1.1.8

9 months ago

1.1.7

10 months ago

1.1.6

12 months ago

1.1.5

1 year ago

1.1.4

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.1

3 years ago