0.10.3 • Published 2 years ago

@logux/vuex v0.10.3

Weekly downloads
132
License
MIT
Repository
github
Last release
2 years ago

Logux Vuex

Logux is a new way to connect client and server. Instead of sending HTTP requests (e.g., AJAX and GraphQL) it synchronizes log of operations between client, server, and other clients.

This repository contains Vuex compatible API on top of the Logux Client.

The current version is for Vue 3 and Vuex 4. For Vue 2 support, we have 0.8 version from a separate branch.

Install

npm install @logux/core @logux/client @logux/vuex vuex@next

or

yarn add @logux/core @logux/client @logux/vuex vuex@next

Usage

See documentation for Logux API.

import { CrossTabClient } from '@logux/client'
import { createStoreCreator } from '@logux/vuex'

const client = new CrossTabClient({
  server: process.env.NODE_ENV === 'development'
    ? 'ws://localhost:31337'
    : 'wss://logux.example.com',
  subprotocol: '1.0.0',
  userId: 'anonymous',
  token: ''
})

const createStore = createStoreCreator(client)

const store = createStore({
  state: {},
  mutations: {},
  actions: {},
  modules: {}
})

store.client.start()

export default store

Subscription

useSubscription

Composable function that subscribes for channels during component initialization and unsubscribes on unmount.

<template>
  <h1 v-if="isSubscribing">Loading</h1>
  <h1 v-else>{{ user.name }}</h1>
</template>

<script>
import { toRefs } from 'vue'
import { useStore, useSubscription } from '@logux/vuex'

export default {
  props: ['userId'],
  setup (props) {
    let store = useStore()
    let { userId } = toRefs(props)
    let channels = computed(() => [`user/${userId.value}`])
    let isSubscribing = useSubscription(channels)

    let user = computed(() => store.state.users[userId.value])

    return {
      user,
      isSubscribing
    }
  }
})
</script>

Subscribe

Component-wrapper that subscribes for channels during component initialization and unsubscribes on unmount.

<template>
  <subscribe :channels="channels" v-slot="{ isSubscribing }">
    <h1 v-if="isSubscribing">Loading</h1>
    <h1 v-else>{{ user.name }}</h1>
  </subscribe>
</template>

<script>
import { toRefs, computed } from 'vue'
import { Subscribe, useStore } from '@logux/vuex'

export default {
  components: { Subscribe },
  props: ['userId'],
  setup (props) {
    let store = useStore()
    let { userId } = toRefs(props)

    let user = computed(() => store.state.users[userId.value])
    let channels = computed(() => [`users/${userId.value}`])

    return {
      user,
      channels
    }
  }
}
</script>

Using with Typescript

Place the following code in your project to allow this.$store to be typed correctly:

// shims-vuex.d.ts

import { LoguxVuexStore } from '@logux/vuex'

declare module '@vue/runtime-core' {
  // Declare your own store states.
  interface State {
    count: number
  }

  interface ComponentCustomProperties {
    $store: LoguxVuexStore<State>
  }
}
0.8.6

2 years ago

0.10.2

2 years ago

0.10.3

2 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.24

3 years ago

0.9.23

3 years ago

0.9.22

3 years ago

0.8.5

3 years ago

0.9.21

3 years ago

0.9.20

3 years ago

0.8.4

3 years ago

0.9.19

3 years ago

0.9.18

3 years ago

0.8.3

3 years ago

0.9.17

4 years ago

0.9.16

4 years ago

0.9.15

4 years ago

0.9.14

4 years ago

0.9.12

4 years ago

0.9.13

4 years ago

0.9.11

4 years ago

0.9.10

4 years ago

0.8.2

4 years ago

0.9.9

4 years ago

0.9.8

4 years ago

0.9.7

4 years ago

0.9.6

4 years ago

0.9.5

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.0

4 years ago

0.9.1

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.8.0-beta.1

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.7.0-beta.1

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.5.0-beta.2

4 years ago

0.5.0-beta.1

4 years ago

0.4.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago