simplitech / vue-await

Easy-to-use loading indicator
MIT License
0 stars 0 forks source link

refactoring: a pure TS proj that simply organizes the loading state, using a reactive model, promises and callbacks #3

Open melanke opened 4 years ago

melanke commented 4 years ago

It should be a library usable not only by vue, but something any library could use as a comunication pattern

melanke commented 4 years ago
import {AwaitC, AwaitState} from '@simpli/await-controller'

// any axios GET request with the endpoint ending with 'user/principal' 
// will trigger this controller
AwaitC.axios('loadingPrincipal', 'GET:user/principal')

// you can get the state of this request at any time, using the same name,
// possible states: ACTIVE, INITIAL, SUCCESS, ERROR, <customs>
const loadingPrincipalState: AwaitState = AwaitC.get('loadingPrincipal')

// or simply check if it's active
const isActive: boolean = AwaitC.isActive('loadingPrincipal')

// you can manually change a controller state, you can put any value 
AwaitC.set('loadingPrincipal', AwaitState.ACTIVE)

// you can create any process with a promise and associate it with a controller
const process = AwaitC.process('loadingPrincipal', async () => {

})

// then you can start the process
const result = await process.start()

// debounce with the state active even when not actually processing,
// but just waiting in the debounce
const process = AwaitC.debounce('loadingPrincipal', 500, 
{/*lodash debounce options, not mandatory*/}, 
async () => {

})

// there is a shortcut for process/start
const result = await AwaitC.run('loadingPrincipal', async () => {

})

using with vue-await

oh and by the way, I think we should use directives

<div v-await.noError="loadingPrincipal" v-await.class="loading-circle">
    <div v-await.success v-await.class="placeholder-text">{{ principal.title }}</div>
</div>
<div v-await.error="loadingPrincipal">Error on request</div>

Directives:

We don't need vue spinner integration anymore because we can have a vue-spinner with the v-await.active directive on it.

And with this kind of configuration we can have unobstrusive loading indicators and placeholders

melanke commented 4 years ago

The only problem is to know if the AwaitState will be reactive, we will find out testing, but if it doesn't work we can provide listener functions:

AwaitC.on("loadingPrincipal", (state) => {

})