项目作者: endenwer

项目描述 :
Immutable store for Svelte with full Typescript support and Redux Devtools integration.
高级语言: TypeScript
项目地址: git://github.com/endenwer/svelte-restate.git
创建时间: 2021-03-31T11:31:07Z
项目社区:https://github.com/endenwer/svelte-restate

开源协议:MIT License

下载


Svelte Restate

Immutable store for Svelte with full Typescript support and Redux Devtools integration. It is highly inspired by re-frame subscriptions(read more about signal graph and subscription layers here). Immer is used to work with immutable state.

Install

  1. npm i svelte-restate --save

Usage [Demo]

Create store with initial state.

  1. import { createStore } from 'svelte-restate'
  2. export interface State {
  3. isLoading
  4. todos: {
  5. id: number
  6. completed: boolean
  7. description: string
  8. }[]
  9. }
  10. const initState: State = {
  11. isLoading: true,
  12. todos: []
  13. }
  14. export default createStore(initState)

Create subscriptions. See more examples in documentation for RegRootsub and RegSub.

  1. import store from './store'
  2. // register root subs
  3. const isLoading = store.regRootSub(
  4. 'isLoading',
  5. ($root) => $root.count
  6. )
  7. const todos = store.regRootSub(
  8. 'todos',
  9. ($root) => $root.todos
  10. )
  11. // use root subs to register derived subs
  12. const completedTodos = store.regSub(
  13. 'completedTodos',
  14. () => todos()
  15. ($todos) => $todos.filter(todo => todo.completed)
  16. )
  17. // register sub with arguments
  18. const todo = store.regSub(
  19. 'todo',
  20. () => todos()
  21. ($todos, [id]: [number]) => $todos.find(todo => todo.id === id)
  22. )
  23. export default { count }

Create mutations. See more examples in documentation for RegMut.

  1. import store from './store'
  2. const setLoading = store.regMut<boolean>(
  3. 'setLoading',
  4. (draft, value) => draft.isLoading = value
  5. )
  6. const insertTodos = store.RegMut<Todo[]>(
  7. 'insertTodos',
  8. (draft, todos) => draft.todos = todos
  9. )
  10. export default { setLoading, insertTodos }

Use in svelte component.

  1. <script>
  2. import muts from './muts'
  3. import subs from './subs'
  4. import store from './store'
  5. import { onMount } 'svelete'
  6. import { fetchTodos } './api'
  7. // subscription without arguments.
  8. // To get the value use '$isLoading'
  9. const isLoading = subs.isLoading()
  10. // subscription with arguments
  11. export let id: number
  12. $: todo = subs.todo(id)
  13. // use mutations
  14. onMount(async () => {
  15. muts.setLoading(true)
  16. const todos = await fetchTodos()
  17. // call multiple mutations within transaction
  18. store.transaction(tx => [
  19. muts.setIsLoading(false, tx),
  20. muts.insertTodos(todos, tx)
  21. ])
  22. })
  23. </script>
  24. {# if $isLoading}
  25. <div>Loading...</div>
  26. {:else}
  27. <div>{$todo.description}</div>
  28. {/if}

Connect to Redux devtools. You can see main state, state of all active subscriptions and dispatch mutations directly from the devtools. Time travel also works. See documentation for connectToDevtools.

  1. import muts from './muts'
  2. import store from './store'
  3. connectToDevTools(store, muts)