Skip to content

Commit

Permalink
feat: fire component's events
Browse files Browse the repository at this point in the history
    Promised fires 'resolved' event when the promise is resolved,
    fires 'rejected' event when the promise is rejected and
    fires 'pending' event when pending delay time is elapsed.

close posva#311
  • Loading branch information
AbrahemAlhofe committed Jun 13, 2022
1 parent dd5db7f commit 7d84458
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Promised.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { watch } from 'vue'
import {
defineComponent,
isVue3,
Expand All @@ -24,13 +25,23 @@ export const PromisedImpl = /*#__PURE__*/ defineComponent({
default: 200,
},
},

setup(props, { slots }) {
emits: ['resolved', 'rejected', 'pending'],
setup(props, { slots, emit, attrs }) {
const propsAsRefs = toRefs(props)
const promiseState = reactive(
usePromise(propsAsRefs.promise, propsAsRefs.pendingDelay)
)

watch(promiseState, (promiseState) =>
promiseState.isRejected
? emit('rejected', promiseState.error)
: !promiseState.isPending
? emit('resolved', promiseState.data)
: promiseState.isDelayElapsed
? emit('pending', promiseState.data)
: null
)

return () => {
if ('combined' in slots) {
return slots.combined!(promiseState)
Expand Down

0 comments on commit 7d84458

Please sign in to comment.