npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@digitalbazaar/vue-extendable-event

v4.2.0

Published

ExtendableEvent for Vue

Downloads

161

Readme

vue-extendable-event

ExtendableEvents for Vue

Overview

This library provides a helper to Vue to enable "extendable events", modeled after the Web's "ExtendableEvent" interface.

It is often useful to be able to perform asynchronous actions when handling and event. It is also often useful for the emitter of the event to know when this event handling has completed. It allows the emitter to provide visual cues to the user, such as loading animations, etc. while the event is still being processed.

The idea behind an "ExtendableEvent" is to enable the lifetime of an event to become "extendable" in a way that the event emitter can know about. This approach keeps a clean separation of concerns:

The event emitter can perform its duties without having to know the implementation details of how its events are handled but it can be informed of when processing has finished. It can also be informed of any error that occurred during processing.

To accomplish this, an event that is emitted via the extendable event API is augmented with a waitUntil(Promise) method. The caller of this method passes it a Promise such that the event emitter can wait for it to settle before proceeding.

Additionally, some emitters may desire a specific response from an event handler. One motivation for this use case comes from this thread:

https://github.com/vuejs/vue/issues/5443

This library also augments the event with a respondWith(Promise) that controls the return value of the aforementioned Promise that the emitter awaits. This function call can only be called by a single event emitter, otherwise an error is thrown.

Installation

import ExtendableEvent from 'vue-extendable-event';
Vue.use(ExtendableEvent);

Examples

With this library installed, a Vue component can emit an extendable event:

await this.$emitExtendable(name, event);

This method returns a Promise that will settle once all promises passed via calls to event.waitUntil have settled and once the Promise that was passed to event.respondWith (if used) has settled. The Promise will be rejected if any of those promises reject, indicating a failure to process the event. Note that not every emitter will have a need for a specific return value via event.respondWith, often event.waitUntil will suffice.

Here is an example of emitting an "ExtendableEvent":

export default {
  data() {
    return {
      loading: false
    }
  },
  methods: {
    async click() {
      this.loading = true;
      try {
        const event = {foo: 1};
        await this.$emitExtendable('foo', event);
      } catch(e) {
        // handle/display error somehow
      } finally {
        this.loading = false;
      }
    }
  }
}

Another component could handle the event like this:

export default {
  methods: {
    foo(event) {
      event.waitUntil(this.doSomethingAsync());
    },
    async doSomethingAsync() {
      // do something async
    }
  }
}

Similarly, another component could respond to an event with a specific value like this:

// responding to the event with a value, if advertised as supported by the
// emitter's contract as useful
export default {
  methods: {
    foo(event) {
      event.respondWith(this.doSomethingAsync());
    },
    async doSomethingAsync() {
      // do something async and return something for the emitter
    }
  }
}

Example for Vue 3 Composition API, pass emit into createEmitExtendable:

<script>
import {createEmitExtendable} from '@digitalbazaar/vue-extendable-event';
import {ref} from 'vue';

export default {
  name: 'MyComponent',
  props: {
    bar: {
      default: 'bar',
      type: String,
    }
  },
  emits: ['foo'],
  setup(props, {emit}) {
    // pass in the emit function to createEmitExtendable
    const emitExtendable = createEmitExtendable({emit});

    const loading = ref(false);

    async function click() {
      loading.value = true;
      try {
        const event = {foo: props.bar};
        await emitExtendable('foo', event);
      } catch(e) {
        // handle/display error somehow
      } finally {
        loading.value = false;
      }
    }

    return {click};
  }
};
</script>