Skip to content

Commit 1dc32df

Browse files
committed
Refactor code to use injected global store
1 parent 44639c7 commit 1dc32df

13 files changed

+57
-18
lines changed

npm-debug.log.1285439957

Whitespace-only changes.

npm-debug.log.2713944281

Whitespace-only changes.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"rollup-plugin-uglify": "^2.0.1",
2626
"rollup-watch": "^4.3.1",
2727
"serve": "^6.4.1",
28-
"svelte": "^1.41.2",
28+
"svelte": "file:../../svelte",
2929
"typescript": "^2.6.1"
3030
}
3131
}

public/js/bundle.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/counter/DecrementButton.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<button on:click="decrement()">-1</button>
22

33
<script>
4-
import { getActions } from 'redux-zero/svelte';
54
import store from "../../store/store";
65
import { actions } from "../../store/actions";
6+
import { getActions } from 'redux-zero/svelte';
77

88
const { decrement } = getActions(store, actions);
99

src/components/counter/IncrementButton.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<button on:click="increment()">+1</button>
22

33
<script>
4-
import { getActions } from 'redux-zero/svelte';
54
import store from "../../store/store";
65
import { actions } from "../../store/actions";
6+
import { getActions } from 'redux-zero/svelte';
77

88
const { increment } = getActions(store, actions);
99

src/components/counter/Value.html

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<h2>{{counter}}</h2>
2+
<button on:click="increment2()">+2</button>
3+
<button on:click="decrement2()">-2</button>
24

35
<script>
4-
import { connect } from 'redux-zero/svelte';
5-
import store from '../../store/store';
6+
import store from "../../store/store";
7+
import { mapActions } from "../../store/actions";
68

9+
const { increment2, decrement2 } = mapActions;
710
const mapToProps = ({ counter }) => ({ counter });
811

912
export default {
1013
oncreate() {
11-
connect(this, store, mapToProps);
14+
this.connect(mapToProps, mapActions);
15+
},
16+
methods: {
17+
decrement2,
18+
increment2
1219
}
13-
14-
// // Dummy placeholder data to suppress the warning message
15-
// data() {
16-
// return {
17-
// counter: null
18-
// }
19-
// }
2020
};
2121
</script>

src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import App from './components/App.html';
2+
import store from './store/store';
23

34
const app = new App({
45
target: document.querySelector('main'),
6+
store: store
57
});

src/store/actions.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const actions = store => ({
1+
export const stateActions = {
22
increment: state => ({ counter: state.counter + 1 }),
3-
decrement: state => ({ counter: state.counter - 1 })
4-
});
3+
decrement: state => ({ counter: state.counter - 1 }),
4+
increment2: state => ({ counter: state.counter + 2 }),
5+
decrement2: state => ({ counter: state.counter - 2 })
6+
};
7+
8+
export const actions = store => stateActions;

src/store/bindActions.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function bindActions(actions, store) {
2+
actions = typeof actions === "function" ? actions(store) : actions
3+
let bound = {}
4+
for (let name in actions) {
5+
bound[name] = (...args) => {
6+
let ret = actions[name](store.getState(), ...args)
7+
if (ret != null) store.setState(ret)
8+
}
9+
}
10+
return bound
11+
}

src/store/store.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
import createStore from 'redux-zero';
2+
import { connect, getActions } from 'redux-zero/svelte';
23

34
const initialState = {
45
counter: 0
56
};
7+
68
const store = createStore(initialState);
9+
const context: any = store;
10+
11+
const init = (component, options) => {
12+
options.data = options.data || {};
13+
component.connect = (mapToProps, mapActions) => {
14+
if (mapActions) {
15+
const actions = getActions(() => mapActions, store);
16+
const methods = {};
17+
Object.keys(actions).forEach(x => {
18+
if (component[x]) methods[x] = actions[x];
19+
});
20+
Object.assign(component, methods);
21+
}
22+
connect(component, store, mapToProps);
23+
}
24+
Object.assign(options.data, store.getState());
25+
}
26+
27+
context.init = init;
728

829
export default store;

src/typings/svelte.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare class Svelte {
2-
constructor(options: { target: Element, data?: any });
2+
constructor(options: { target: Element, data?: any, store?: any });
33

44
get(name: string);
55
set(data: any);

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"outDir": "build/js/",
1010
"noImplicitReturns": true,
1111
"allowSyntheticDefaultImports": true,
12-
"lib": [ "dom", "es2015" ],
12+
"lib": [ "dom", "es2015", "es2017.object" ],
1313
"typeRoots": [
1414
"./node_modules/@types"
1515
]

0 commit comments

Comments
 (0)