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

vue3-reactive-storage

v1.0.1

Published

Vue3 plugin to use reactivity with object that implements the Storage interface

Downloads

114

Readme

Vue3 plugin to use reactivity with object that implements the Storage interface

🏠 Homepage

A Vue3 plugin to use reactivity with object that implements the Storage interface, for example: localStorage, sessionStorage or customer object. Working with one or various app instances. Based on @cljimenez/vue-localstorage-reactive. Include the class ReactiveWebStorageError used by ReactiveWebStorage to throw Errors.

Index

Install

npm install vue3-reactive-storage

How to use?

  • Install the plugin.

    Using an app instance

    import { createApp, reactive, ref } from 'vue'
    import createReactiveWebStorageInstaller from 'vue3-reactive-storage'
    
    import { createApp } from 'vue'
    
    import App from './App.vue'
    
    const app = createApp(App)
    
    app.use(createReactiveWebStorageInstaller(), {
      webStorage: localStorage,
      reactiveStorage: ref({}),
    })
    
    const appReactiveStorage = app.config.globalProperties.$reactiveWebStorage
    
    app.provide('storage', appReactiveStorage)
    
    app.mount('#app')

    Using various app instances

    import { createApp, reactive, ref } from 'vue'
    import createReactiveWebStorageInstaller from 'vue3-reactive-storage'
    import App from './App.vue'
    import SubApp from './SubApp.vue'
    import OtherSubApp from './OtherSubApp.vue'
    
    const app = createApp(App)
    const subApp = createApp(SubApp)
    const otherSubApp = createApp(OtherSubApp)
    
    app.use(createReactiveWebStorageInstaller(), {
      webStorage: localStorage,
      reactiveStorage: ref({}),
    })
    
    subApp.use(createReactiveWebStorageInstaller(), {
      prefix: 'new_prefix',
      webStorage: sessionStorage,
      reactiveStorage: ref({}),
    })
    
    otherSubApp.use(createReactiveWebStorageInstaller(), {
      prefix: 'some_prefix',
      webStorage: localStorage,
      reactiveStorage: reactive({}),
    })
    
    const appReactiveStorage = app.config.globalProperties.$reactiveWebStorage
    const subAppReactiveStorage =
      subApp.config.globalProperties.$reactiveWebStorage
    const otherSubAppReactiveStorage =
      otherSubApp.config.globalProperties.$reactiveWebStorage
    
    app.provide('storage', appReactiveStorage)
    subApp.provide('storage', subAppReactiveStorage)
    otherSubApp.provide('storage', otherSubAppReactiveStorage)
    
    app.mount('#app')
    subApp.mount('#subapp')
    otherSubApp.mount('#othersubapp')
  • Install options.

    When you installs this plugin using you can use options:

    app.use(createReactiveWebStorageInstaller(), options)

    The options object can contain the following attributes:

    • webStorage: Required value. localStorage, sessionStorage or other object that implements the Storage interface.

    • reactiveStorage: Required value. ref or reactive object.

    • prefix: Optional value. Used to segment the Storage object, the prefix is added to key (using '-') in Storage object. Generally used when using multiple app instances. For example:

      import { createApp, reactive, ref } from 'vue'
      import createReactiveWebStorageInstaller from 'vue3-reactive-storage'
      
      import { createApp } from 'vue'
      
      import App from './App.vue'
      
      const app = createApp(App)
      
      app.use(createReactiveWebStorageInstaller(), {
        prefix: 'hello_world'
        webStorage: localStorage,
        reactiveStorage: ref({}),
      })
      
      const appReactiveStorage = app.config.globalProperties.$reactiveWebStorage
      
      // Adds in Storage object
      // key: hello_world-my_key
      // value: data
      // Adds in reactive object
      // key: my_key
      // value: data
      appReactiveStorage.setItem('my_key', 'data')
      
      app.provide('storage', appReactiveStorage)
      
      app.mount('#app')

      by default, prefix is ''.

    • loadDataFromWebStorage: Optional value. By default is true. Loads the keys/values in Storage object to reactive object when the load event is fired by window object. Useful when closing and opening the browser window.

  • About the ReactiveWebStorage methods

    The ReactiveWebStorage object provides an interface similar to the Storage interface, this methods are:

    • (getter) length: Obtains the number of elements saved in reactiveWebStorage.
    • (method) key(index): Returns the key in nth position into reactiveWebStorage.
    • (method) getItem(key): Returns the parsed key's value saved into reactiveWebStorage.
    • (method) setItem(key, item): Saves the pair key/value into reactiveWebStorage.
    • (method) removeItem(key): Removes the pair key/value from reactiveWebStorage.
    • (method) clear(): Removes all pairs key/value into reactiveWebStorage.

    And include others methods:

    • (getter) reactiveStorageAdapter: Returns the reactiveStorageAdapter (object that wraps the reactiveStorage using an insterface similar to Storage) object used by reactiveWebStorage instance.
    • (getter) reactiveStorage: Returns the reactiveStorage object used by reactiveWebStorage instance.
  • Use the composition API:

    You can use the provide / inject functions.

    import { createApp, reactive, ref } from 'vue'
    import createReactiveWebStorageInstaller from 'vue3-reactive-storage'
    
    import { createApp } from 'vue'
    
    import App from './App.vue'
    
    const app = createApp(App)
    
    app.use(createReactiveWebStorageInstaller(), {
      webStorage: localStorage,
      reactiveStorage: ref({}),
    })
    
    const appReactiveStorage = app.config.globalProperties.$reactiveWebStorage
    
    app.provide('storage', appReactiveStorage)
    
    app.mount('#app')
    // you can use the inject function to access to the reactiveWebStorage.
    <template>
      <h2>{{ getUsername }}</h2>
      <button @click="storage.setItem('username', 'an username')">
        Add username
      </button>
      <button @click="storage.removeItem('username')">Delete username</button>
    </template>
    
    <script setup>
    import { inject, computed } from 'vue'
    
    const storage = inject('storage')
    
    const getUsername = computed(() => {
      return storage.getItem('username')
    })
    </script>

Author

👤 Cristopher Jiménez Meza

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 Cristopher Jiménez Meza. This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator