Socket
Book a DemoInstallSign in
Socket

svore

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svore

stare management by composition-api

0.3.1
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

svore

This library makes it easy to manage the stores created by composition-api because you can easily describe the process of linking between stores.
This store is type safe.

It is useful if

  • you use realtime-update service such as firestore.
  • make stores decoupled.

Installation

npm i svore

Usage

<template>
  <router-view></router-view>
</template>

<script lang="ts">
  import { defineStore } from 'svore'
  import { defineComponent, onUnmounted } from 'vue'
  import { createStore } from './store'

  export default defineComponent({
    name: 'App',
    setup() {
      const store = createStore()

      store
        .on(({ modules }) => modules.userStore.userId)
        .filter((it) => it !== null && it !== '')  // option
        .trigger((modules) => modules.todoStore.subscribe)
        // or
        // .trigger((modules) => (newId, oldId, cleanUp) => {
        //  if (!newId) return
        //  const unsubscribe = modules.todoStore.subscribe(newId)
        //  cleanUp(unsubscribe)
        // })

      store
        .on(({ getters }) => getters.value.userIdAndTodos)
        .watch((newOne, oldOne) => console.log('[INFO]', newOne oldOne))

      onUnmounted(store.modules.unwatchAll)
      provide('key', store)
    }
  })
</script>
<template>
  <div>...</div>
</template>

<script lang="ts">
  import { inject } from 'svore'
  import { State, TodoStore } from './store'

  export default defineComponent({
    name: 'Page',
    setup() {
      // inject is in this library
      const store = inject<Action<TodoStore>>('key', 'module', 'todoStore')
      const add = (todo: Todo) => store.add(todo)

      return {
        add,
      }
    },
  })
</script>
import { defineStore } from 'svore'
import { signInWithEmailAndPassword, signOut } from './services/auth.service'
import { add, subscribe } from './services/todo.service'
import { reactive, computed } from 'vue'

export const createStore = () =>
  defineStore(
    {
      userStore: userStore(),
      todoStore: todoStore(),
    },
    (modules) => ({
      userIdAndTodos: [modules.userStore.userId.value, modules.todoStore.todos.value],
    })
  )

export type UserStore = ReturnType<typeof userStore>
export type TodoStore = ReturnType<typeof todoStore>

function userStore() {
  const state = reactive<{ user: User | null }>({
    user: null,
  })

  const userId = computed(() => user?.id ?? '')

  const signIn = async (email: string, password: string) => {
    const user = await signInWithEmailAndPassword(email, password)

    state.user = user
  }

  const so = async () => {
    await signOut()

    state.user = null
  }

  return {
    state,
    userId,
    signIn,
    signOut: so,
  }
}

function todoStore() {
  const state = reactive<{ todos: ToDo[] }>({
    todos: [],
  })

  const todos = computed(() => state.todos)

  const addNewTodo = async (todo: Todo) => {
    await add(todo)
  }

  const subscribeTodos = (userId: UserId) => {
    subscribe(userId, (todo) => (state.todos = [...state.todos, todo]))
  }

  return {
    state,
    todos,
    addNewTodo,
    subscribeTodos,
  }
}

Type Safe

example

TODO

  • Test
  • Description of merit
  • esm support

Keywords

vue

FAQs

Package last updated on 04 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.