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

nodecg-vue-composable

v1.1.0

Published

A set of Vue composables for interacting more predictably with NodeCG replicants.

Downloads

51

Readme

nodecg-vue-composable

A set of Vue composables for interacting more predictably with NodeCG replicants.

Works with Vue 2 & Vue 3 thanks to vue-demi.

useReplicant is a happy medium between manually syncing a vue reactive value with a nodecg replicant on every change and not using reactivity at all. It forces you to be explicit about when a replicant should be updated while still providing reactivity for easy v-model binding.

Usage

Note: use of composables requires the Vue Composition API, either importing it directly from Vue 3 or using the @vue/composition-api package with Vue 2. You are also required to create components using defineComponent.

useReplicant(name, namespace, opts)

A (sort-of) two-way binding that keeps a separate copy of the latest replicant value locally which you're free to modify. Doesn't update the replicant itself until you explicitly tell it to.

Returns an object with the following properties:

  • data: a ref that stores the current local value
  • changed: a ref bool indicating if the local value is different to the replicant value
  • upToDate: a ref bool indicating if the replicant has been updated since the local copy was first changed (i.e. if true, saving will overwrite a change from elsewhere)
  • save(): a function to commit the locally current value to the replicant
  • revert(): a function to set data to the replicant value (clearing local changes)
  • loadDefault(): a function to reset the local value to the default value of the replicant
  • oldData: a readonly ref of the latest replicant value

Update the value (data) programatically or in a template binding and commit it with save().

If the local value is out of sync when a new value comes in from the replicant, it won't update (indicated by upToDate). If it's unchanged however, data will be updated and propagate to your bindings.

Example

<template>
    <div>
        <input v-model="lowerThird.data" />
        <button :disabled="!lowerThird.changed" @click="lowerThird.save()">
            SAVE
        </button>
        <button :disabled="!lowerThird.changed" @click="lowerThird.revert()">
            REVERT CHANGES
        </button>
        <button @click="lowerThird.loadDefault()">
            CLEAR
        </button>
    </div>
</template>

<script>
import { defineComponent } from 'vue' // or '@vue/composition-api' with vue 2
import { useReplicant } from 'nodecg-vue-composable'

export default defineComponent({
    setup() {
        const lowerThird = useReplicant('lowerThird', { defaultValue: '' })

        return {
            lowerThird
        }
    }
})
</script>

<template>

useAssetReplicant(name)

A read-only binding to an asset replicant with the name assets:<name>.

Example

<template>
    <div>
        <select v-model="logo.data">
            <option disabled value="">Select a logo...</option>
            <option v-for="logoAsset in logos" :value="logoAsset.url">
                {{ logoAsset.name }}
            </option>
        </select>
        <button :disabled="!logo.changed" @click="logo.save()">
            SAVE
        </button>
        <button :disabled="!logo.changed" @click="logo.revert()">
            REVERT CHANGES
        </button>
        <button @click="logo.loadDefault()">
            CLEAR
        </button>
    </div>
</template>

<script>
import { defineComponent } from 'vue' // or '@vue/composition-api' with vue 2
import { useReplicant, useAssetReplicant } from 'nodecg-vue-composable'

export default defineComponent({
    setup() {
        const logo = useReplicant('logo', { defaultValue: null })
        const logos = useAssetReplicant('logos')

        return {
            logo,
            logos
        }
    }
})
</script>

<template>

useDynamicReplicant(name, namespace, opts)

If you want to use a name that is itself reactive use a useDynamicReplicant (name suggestions welcome).

Only usage difference with useReplicant is that it returns a ref that wraps the reactive, so if you want to use it inside the setup function or another composable you need to access the value before data. Usage inside the template is unaffected as the unwrapping will be done for you.

I.e. instead of:

const rep = useReplicant('repName')
rep.data = '....'

You need to do:

const repName = toRef(props, 'repName')  // example reactive name

const rep = useDynamicReplicant(repName)
rep.value.data = '....'

Installation

Vue 3

npm install -D nodecg-vue-composable

Vue 2

npm install -D @vue/composition-api nodecg-vue-composable

Setup @vue/composition-api by adding the following to your entry file:

import VueCompositionApi from '@vue/composition-api'

Vue.use(VueCompositionApi)

Todo

  • ~~Add TypeScript typings~~
  • Write some tests
  • ~~Come up with a better name for DynamicReactiveReplicant~~