Skip to content

Commit abe7060

Browse files
committed
add project to repo
0 parents  commit abe7060

11 files changed

+8950
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
npm-debug.log
2+
yarn-error.log
3+
4+
.cache/
5+
node_modules/
6+
dist/
7+
coverage/

example/App.svelte

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { connect } from './store.js';
3+
4+
const [dispatch, count] = connect('count');
5+
6+
function increment() {
7+
dispatch('inc');
8+
}
9+
10+
function decrement() {
11+
dispatch('dec');
12+
}
13+
</script>
14+
15+
<h1>The count is {$count}</h1>
16+
17+
<button on:click={decrement}>
18+
-
19+
</button>
20+
<button on:click={increment}>
21+
+
22+
</button>

example/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<meta charset='utf8'>
6+
<meta name='viewport'
7+
content='width=device-width'>
8+
9+
<title>Svelte app</title>
10+
</head>
11+
12+
<body>
13+
<script src='main.js'></script>
14+
</body>
15+
16+
</html>

example/main.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import App from './App.svelte';
2+
3+
const app = new App({
4+
target: document.body,
5+
});
6+
7+
export default app;

example/store.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import createStore from 'storeon';
2+
import { bindStoreon } from '..';
3+
4+
let counter = store => {
5+
store.on('@init', () => ({ count: 0 }));
6+
store.on('inc', ({ count }) => ({ count: count + 1 }));
7+
store.on('dec', ({ count }) => ({ count: count - 1 }));
8+
};
9+
10+
const store = createStore([counter]);
11+
12+
export const connect = bindStoreon(store);

index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Store,Dispatch } from 'storeon';
2+
3+
declare function connect(key: string): [Dispatch, any]
4+
5+
declare function bindStoreon<T>(store: Store<T>): connect;
6+
7+
export { bindStoreon }

index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
*
3+
* @param {*} store
4+
*
5+
* @return {}
6+
*/
7+
const bindStoreon = function (store) {
8+
/**
9+
*
10+
*/
11+
return function (key) {
12+
var state = store.get();
13+
var subscribers = [];
14+
15+
function subscribe(run) {
16+
subscribers.push(run);
17+
run(state[key])
18+
19+
return function () {
20+
var index = subscribers.indexOf(run);
21+
if (index !== -1) subscribers.splice(index, 1);
22+
}
23+
}
24+
25+
store.on("@changed", function (_, changed) {
26+
if (key in changed) {
27+
subscribers.forEach(s => s(changed[key]))
28+
}
29+
})
30+
31+
var changes = {
32+
subscribe: subscribe
33+
}
34+
35+
return [store.dispatch, changes];
36+
}
37+
}
38+
39+
module.exports = { bindStoreon }

index.spec.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const createStore = require('storeon');
2+
const { bindStoreon } = require('.');
3+
4+
function connectFactory() {
5+
let counter = store => {
6+
store.on('@init', () => ({ count: 0, foo: 'baz' }));
7+
store.on('inc', ({ count }) => ({ count: count + 1 }));
8+
};
9+
10+
const store = createStore([counter]);
11+
12+
return bindStoreon(store);
13+
}
14+
15+
it('should start with init value', () => {
16+
const connect = connectFactory()
17+
18+
const [_, count] = connect('count');
19+
20+
count.subscribe((value) => expect(value).toBe(0));
21+
});
22+
23+
it('should be reactive', () => {
24+
const connect = connectFactory()
25+
26+
let currentValue;
27+
const [dispatch, count] = connect('count');
28+
29+
count.subscribe((value) => currentValue = value);
30+
31+
expect(currentValue).toBe(0);
32+
33+
dispatch('inc');
34+
35+
expect(currentValue).toBe(1);
36+
});
37+
38+
it('should not emmit changes on other dispatches', () => {
39+
const connect = connectFactory()
40+
const spyCb = jest.fn();
41+
42+
const [dispatch, foo] = connect('foo');
43+
44+
foo.subscribe(spyCb);
45+
46+
dispatch('inc');
47+
48+
expect(spyCb).toBeCalledWith('baz');
49+
expect(spyCb).toBeCalledTimes(1);
50+
});
51+
52+
it('shoud to be unsubscribed', ()=> {
53+
const connect = connectFactory()
54+
55+
let currentValue;
56+
const [dispatch, count] = connect('count');
57+
58+
const unsubscribe = count.subscribe((value) => currentValue = value);
59+
60+
expect(currentValue).toBe(0);
61+
62+
dispatch('inc');
63+
64+
expect(currentValue).toBe(1);
65+
66+
unsubscribe();
67+
unsubscribe();
68+
dispatch('inc');
69+
70+
expect(currentValue).toBe(1);
71+
});
72+
73+
74+

package.json

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "storeon-svelte",
3+
"version": "0.0.1",
4+
"main": "index.js",
5+
"repository": "https://github.com/distolma/storeon-svelte.git",
6+
"author": "Dmytro Mostovyi <[email protected]>",
7+
"license": "MIT",
8+
"scripts": {
9+
"start": "parcel serve example/index.html --open",
10+
"test": "jest --coverage && size-limit"
11+
},
12+
"devDependencies": {
13+
"husky": "^2.3.0",
14+
"jest": "^24.8.0",
15+
"lint-staged": "^8.1.7",
16+
"parcel-bundler": "^1.12.3",
17+
"parcel-plugin-svelte": "^3.0.1",
18+
"prettier": "^1.17.1",
19+
"size-limit": "^1.3.2",
20+
"svelte": "^3.4.0"
21+
},
22+
"dependencies": {
23+
"storeon": "^0.8.3"
24+
},
25+
"lint-staged": {
26+
"*.js": [
27+
"prettier --write",
28+
"git add"
29+
]
30+
},
31+
"husky": {
32+
"hooks": {
33+
"pre-commit": "lint-staged"
34+
}
35+
},
36+
"browserslist": [
37+
"last 1 chrome versions"
38+
],
39+
"size-limit": [
40+
{
41+
"path": "index.js",
42+
"limit": "128 B"
43+
}
44+
],
45+
"prettier": {
46+
"trailingComma": "all",
47+
"tabWidth": 2,
48+
"singleQuote": true
49+
}
50+
}

0 commit comments

Comments
 (0)