Skip to content

Commit e01d031

Browse files
committed
👽 Make API more convinient
1 parent 0b37126 commit e01d031

9 files changed

+40
-40
lines changed

README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
A tiny connector for [Storeon] and [Svelte]. ([Demo])
66

7-
Size is only 185 bytes (minified and gzipped). It uses [Size Limit] to control size.
7+
Size is only 187 bytes (minified and gzipped). It uses [Size Limit] to control size.
88

99
Read more about Storeon [article].
1010

1111
## Why?
1212

1313
[Svelte] is the smallest JS framework, but even so, it contains many built-in features. One of them is a `svelte/store`. But why we need to use a third-party store? `@storeon/svelte` has several advantages compared with the built-in one.
1414

15-
- **Size**. 185 bytes instead of 426 bytes (minified and gzipped).
15+
- **Size**. 187 bytes instead of 426 bytes (minified and gzipped).
1616
- **Ecosystem**. Many additional [tools] can be combined with a store.
1717
- **Speed**. It tracks what parts of state were changed and re-renders only components based on the changes.
1818

@@ -75,29 +75,29 @@ export const store = createStoreon<State, Events>([counter])
7575

7676
#### `App.svelte`
7777

78-
Provide store to Svelte Context using `setStore` from `@storeon/svelte`
78+
Provide store to Svelte Context using `provideStoreon` from `@storeon/svelte`
7979

8080
```html
8181
<script>
82-
import { setStore } from '@storeon/svelte'
82+
import { provideStoreon } from '@storeon/svelte'
8383
import { store } from './store'
8484
import Counter from './Counter.svelte'
8585
86-
setStore(store)
86+
provideStoreon(store)
8787
</script>
8888

8989
<Counter />
9090
```
9191

92-
Import `getStore` function from our `@storeon/svelte` module and use it for getting state and dispatching new events:
92+
Import `useStoreon` function from our `@storeon/svelte` module and use it for getting state and dispatching new events:
9393

9494
#### `Child.svelte`
9595

9696
```html
9797
<script>
98-
import { getStore } from '@storeon/svelte';
98+
import { useStoreon } from '@storeon/svelte';
9999
100-
const { count, dispatch } = getStore('count');
100+
const { count, dispatch } = useStoreon('count');
101101
102102
function increment() {
103103
dispatch('inc');
@@ -108,13 +108,13 @@ Import `getStore` function from our `@storeon/svelte` module and use it for gett
108108

109109
<button on:click={increment}>+</button>
110110
```
111-
Using typescript you can pass `State` and `Events` interfaces to `getStore` function to be full type safe
111+
Using typescript you can pass `State` and `Events` interfaces to `useStoreon` function to be full type safe
112112
```html
113113
<script lang="typescript">
114-
import { getStore } from '@storeon/svelte';
114+
import { useStoreon } from '@storeon/svelte';
115115
import { State, Events } from './store'
116116
117-
const { count, dispatch } = getStore<State, Events>('count');
117+
const { count, dispatch } = useStoreon<State, Events>('count');
118118
119119
function increment() {
120120
dispatch('inc');
@@ -146,22 +146,22 @@ And use it like:
146146
#### `App.svelte`
147147
```html
148148
<script>
149-
import { setStore } from '@storeon/svelte'
149+
import { provideStoreon } from '@storeon/svelte'
150150
import { store } from './store'
151151
import Counter from './Child.svelte'
152152
153-
setStore(store)
153+
provideStoreon(store)
154154
</script>
155155

156156
<Counter />
157157
```
158158
#### `Child.svelte`
159159
```html
160160
<script>
161-
import { getStore } from '@storeon/svelte';
161+
import { useStoreon } from '@storeon/svelte';
162162
import router from '@storeon/router'
163163
164-
const { [router.key]: route } = getStore(router.key)
164+
const { [router.key]: route } = useStoreon(router.key)
165165
</script>
166166

167167
You can access the router like default svelte store via $:

example/App.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import Main from './components/Main.svelte'
44
import { store } from './store'
55
import { SHOW_ACTIVE, SHOW_COMPLETED, SHOW_ALL } from './constants/TodoFilters'
6-
import { setStore } from '..'
6+
import { provideStoreon } from '..'
77
8-
setStore(store)
8+
provideStoreon(store)
99
1010
function updateFilter() {
1111
if (window.location.hash === '#active') {

example/components/Footer.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script >
2-
import { getStore } from '../..'
2+
import { useStoreon } from '../..'
33
import { SHOW_ACTIVE, SHOW_ALL, SHOW_COMPLETED } from '../constants/TodoFilters'
44
5-
const { dispatch, todos, filter } = getStore('todos', 'filter')
5+
const { dispatch, todos, filter } = useStoreon('todos', 'filter')
66
77
function handleClearCompleted() {
88
dispatch('todo/clear_completed')

example/components/Header.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script >
22
import { ENTER } from '../constants/keyCodes'
3-
import { getStore } from '../..'
3+
import { useStoreon } from '../..'
44
5-
const { dispatch } = getStore()
5+
const { dispatch } = useStoreon()
66
77
function handleKeydown(event) {
88
if (event.which === ENTER && event.target.value.trim()) {

example/components/Main.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import Footer from './Footer.svelte'
33
import TodoItem from './TodoItem.svelte'
44
import { SHOW_ACTIVE, SHOW_ALL, SHOW_COMPLETED } from '../constants/TodoFilters'
5-
import { getStore } from '../..'
5+
import { useStoreon } from '../..'
66
7-
const { dispatch, todos, filter } = getStore('todos', 'filter')
7+
const { dispatch, todos, filter } = useStoreon('todos', 'filter')
88
99
function handleCompleteAll() {
1010
dispatch('todo/complete_all')

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Subscribable<State> = {
1010
};
1111
}
1212

13-
export declare function setStore(store: StoreonStore): void
14-
export declare function getStore<State, Events = any>(...keys: (keyof State)[]): Subscribable<State> & {
13+
export declare function provideStoreon(store: StoreonStore): void
14+
export declare function useStoreon<State, Events = any>(...keys: (keyof State)[]): Subscribable<State> & {
1515
dispatch: StoreonDispatch<Events & createStoreon.DispatchableEvents<State>>
1616
}

index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
let { getContext, setContext } = require('svelte')
1+
const { getContext, setContext } = require('svelte')
22

33
const STORE = typeof Symbol !== 'undefined' ? Symbol('storeon') : '@@storeon'
44

5-
function setStore (store) {
5+
function provideStoreon (store) {
66
setContext(STORE, store)
77
}
88

9-
function getStore (...keys) {
9+
function useStoreon (...keys) {
1010
let store = getContext(STORE)
1111
if (process.env.NODE_ENV !== 'production' && !store) {
1212
throw new Error(
1313
'Could not find storeon context value.' +
14-
'Please ensure you provide store using "setStore" function'
14+
'Please ensure you provide store using "provideStoreon" function'
1515
)
1616
}
1717

@@ -48,4 +48,4 @@ function getStore (...keys) {
4848
return data
4949
}
5050

51-
module.exports = { setStore, getStore }
51+
module.exports = { provideStoreon, useStoreon }

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@storeon/svelte",
33
"version": "0.5.2",
4-
"description": "A tiny (185 bytes) connector for Storeon and Svelte",
4+
"description": "A tiny (187 bytes) connector for Storeon and Svelte",
55
"main": "index.js",
66
"repository": "https://github.com/distolma/storeon-svelte.git",
77
"author": "Dmytro Mostovyi <[email protected]>",
@@ -58,7 +58,7 @@
5858
"size-limit": [
5959
{
6060
"path": "index.js",
61-
"limit": "185 B",
61+
"limit": "187 B",
6262
"ignore": [
6363
"svelte"
6464
]

test/index.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let { getContext, setContext } = require('svelte')
22
let { createStoreon } = require('storeon')
33

4-
let { getStore, setStore } = require('..')
4+
let { useStoreon, provideStoreon } = require('..')
55

66
jest.mock('svelte')
77

@@ -34,14 +34,14 @@ afterEach(() => {
3434
it('set store to svelte context', () => {
3535
let store = setupStore()
3636
setContext.mockImplementationOnce(() => {})
37-
setStore(store)
37+
provideStoreon(store)
3838

3939
expect(setContext).toHaveBeenCalledWith(expect.anything(), store)
4040
expect(setContext).toHaveBeenCalledTimes(1)
4141
})
4242

4343
it('get store from svelte context', () => {
44-
let store = getStore()
44+
let store = useStoreon()
4545

4646
expect(store).toBeDefined()
4747
expect(store.dispatch).toBeDefined()
@@ -50,18 +50,18 @@ it('get store from svelte context', () => {
5050
it('get error if store context not provided', () => {
5151
getContext.mockRestore()
5252

53-
expect(() => getStore()).toThrow(Error)
53+
expect(() => useStoreon()).toThrow(Error)
5454
})
5555

5656
it('should start with init value', () => {
57-
let { count } = getStore('count')
57+
let { count } = useStoreon('count')
5858

5959
count.subscribe(value => expect(value).toBe(0))
6060
})
6161

6262
it('should be reactive', () => {
6363
let currentValue
64-
let { count, dispatch } = getStore('count')
64+
let { count, dispatch } = useStoreon('count')
6565

6666
count.subscribe(value => { currentValue = value })
6767

@@ -76,7 +76,7 @@ it('should not emit changes on other dispatches', () => {
7676
let fooSpyCb = jest.fn()
7777
let countSpyCb = jest.fn()
7878

79-
let { foo, count, dispatch } = getStore('foo', 'count')
79+
let { foo, count, dispatch } = useStoreon('foo', 'count')
8080

8181
foo.subscribe(fooSpyCb)
8282
count.subscribe(countSpyCb)
@@ -95,7 +95,7 @@ it('should not emit changes on other dispatches', () => {
9595

9696
it('shoud to be unsubscribed', () => {
9797
let currentValue
98-
let { count, dispatch } = getStore('count')
98+
let { count, dispatch } = useStoreon('count')
9999

100100
let unsubscribe = count.subscribe(value => { currentValue = value })
101101

0 commit comments

Comments
 (0)