Skip to content

Commit 27f3238

Browse files
authored
Merge pull request #10 from sascha245/feature/store_module
Feature/store module
2 parents d560982 + cc4dd3f commit 27f3238

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1187
-529
lines changed

.gitignore

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
node_modules
2-
build
3-
test
4-
src/**.js
5-
.idea/*
2+
/build
63

7-
coverage
8-
.nyc_output
9-
*.log
4+
# local env files
5+
.env.local
6+
.env.*.local
7+
8+
# Log files
9+
npm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
13+
# Editor directories and files
14+
.idea
15+
.vscode
16+
*.suo
17+
*.ntvs*
18+
*.njsproj
19+
*.sln
20+
*.sw*
1021

11-
package-lock.json
1222
yarn.lock
23+
package-lock.json

.npmignore

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
src
2-
test
3-
tsconfig.json
4-
tsconfig.module.json
5-
tslint.json
6-
.travis.yml
1+
.vscode
72
.github
3+
/node_modules
4+
/public
5+
/samples
6+
/build/tests
7+
/build/samples
8+
/src
9+
/tests
10+
811
.prettierignore
9-
.vscode
10-
build/docs
11-
example
12-
tests
13-
**/*.spec.*
14-
coverage
15-
.nyc_output
16-
*.log
12+
.prettierrc
1713
.editorconfig
14+
.postcssrc.js
15+
babel.config.js
16+
jest.config.js
17+
tsconfig.json
18+
tsconfig.prod.json
19+
tslint.json
20+
21+
# Log files
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*
1825

19-
package-lock.json
2026
yarn.lock
27+
package-lock.json

.postcssrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
plugins: {
3+
autoprefixer: {}
4+
}
5+
}

.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
22
"typescript.tsdk": "node_modules/typescript/lib",
3-
"editor.formatOnSave": true,
3+
4+
"[javascript]": {
5+
"editor.formatOnSave": true
6+
},
7+
"[typescript]": {
8+
"editor.formatOnSave": true
9+
},
10+
411
"editor.rulers": [100],
512
"importSorter.generalConfiguration.sortOnBeforeSave": true
13+
// "importSorter.importStringConfiguration.quoteMark": "double"
614
// "typescript.implementationsCodeLens.enabled": true
715
// "typescript.referencesCodeLens.enabled": true
816
}

README.md

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,61 @@ So for now `@Action` is still included, but it may happen that it is removed in
169169
#### How to setup your store
170170

171171
1. Use `Vue.use(VuexSimple)`
172-
2. Use the StoreBuilder to load your modules
172+
173+
2. Get a StoreBuilder instance.
174+
175+
```ts
176+
// 1. Use getStoreBuilder()
177+
const storeBuilder = getStoreBuilder();
178+
179+
// 2. Create a new instance yourself
180+
const storeBuilder = new StoreBuilder();
181+
182+
```
183+
184+
**Note:** `getStoreBuilder()` always returns the same instance
185+
186+
3. (optional) Initialize your store options.
187+
188+
```ts
189+
190+
// 1. When using getStoreBuilder()
191+
const storeBuilder = getStoreBuilder();
192+
storeBuilder.initialize({
193+
modules: {},
194+
state: {},
195+
strict: false
196+
})
197+
198+
// 2. Pass directly in the StoreBuilder constructor
199+
const storeBuilder = new StoreBuilder({
200+
modules: {},
201+
state: {},
202+
strict: false
203+
});
204+
205+
```
206+
207+
4. (optional) You can also use a custom container instance.
208+
209+
```ts
210+
211+
const myContainer = Container.of('myContainer');
212+
const storeBuilder = getStoreBuilder();
213+
214+
// 1. Using the initialize function or in the StoreBuilder constructor
215+
storeBuilder.initialize({
216+
container: myContainer
217+
})
218+
219+
// 2. Using the 'useContainer' function
220+
storeBuilder.useContainer(myContainer);
221+
222+
```
223+
224+
**Note:** Be careful to set the container before you load your modules!
225+
226+
5. Use the StoreBuilder to load your modules
173227

174228
```ts
175229
const storeBuilder = getStoreBuilder();
@@ -178,23 +232,70 @@ storeBuilder.loadModules([
178232
]);
179233
```
180234

181-
3. (optional) Add your existing vuex modules: they will still work normally
235+
6. We finish by creating the store with `storeBuilder.create()`
236+
237+
238+
## FAQ
239+
240+
**How to split up your modules:**</br>
241+
There are different ways to split up your modules:
242+
1. Do all the heavy lifting (like API requests and such) in other files or services and inject them in your module
243+
2. Split up your modules into smaller modules. If necessary, you can also inject other modules into your main module.
244+
245+
In the case the 2 solutions above don't work out for you, you can also use inheritance to split up your files, though it isn't the most recommended way:
182246

183247
```ts
184-
const storeBuilder = getStoreBuilder();
185-
storeBuilder.addModule(namespace: string, module: Vuex.Module);
186-
```
248+
class CounterState {
249+
@State()
250+
public counter: number = 10;
251+
}
187252

188-
4. We finish by creating the store with `storeBuilder.create()`
253+
class CounterGetters extends CounterState {
254+
@Getter()
255+
public get counterPlusHundred() {
256+
return this.counter + 100;
257+
}
258+
}
189259

260+
class CounterMutations extends CounterGetters {
261+
@Mutation()
262+
public increment() {
263+
this.counter++;
264+
}
265+
}
190266

191-
**Note:** We can't configure the root of the store **for now**. The store is also set to use strict mode by default.
267+
class CounterActions extends CounterMutations {
268+
public async incrementAsync() {
269+
await new Promise(r => setTimeout(r, 500));
270+
this.increment();
271+
}
272+
}
192273

274+
@Module('counter')
275+
class CounterModule extends CounterActions {}
276+
```
193277

194278
## Contributors
195279

196280
If you are interested and want to help out, don't hesitate to contact me or to create a pull request with your fixes / features.
197281

282+
The project now also contains samples that you can use to directly test out your features during development.
283+
284+
1. Clone the repository
285+
286+
2. Install dependencies
287+
288+
`npm install`
289+
290+
3. Launch samples
291+
292+
`npm run serve`
293+
294+
4. Launch unit tests situated in *./tests*. The unit tests are written in Jest.
295+
296+
`npm run test:unit`
297+
298+
198299
## Bugs
199300

200301
The core features presented should be stable.

babel.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
presets: [
3+
'@vue/app'
4+
]
5+
}

jest.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
moduleFileExtensions: ["js", "jsx", "json", "vue", "ts", "tsx"],
3+
transform: {
4+
"^.+\\.vue$": "vue-jest",
5+
".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$":
6+
"jest-transform-stub",
7+
"^.+\\.tsx?$": "ts-jest"
8+
},
9+
moduleNameMapper: {
10+
"^@/(.*)$": "<rootDir>/src/$1"
11+
},
12+
snapshotSerializers: ["jest-serializer-vue"],
13+
testMatch: [
14+
"**/tests/unit/**/*.spec.(ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
15+
],
16+
testURL: "http://localhost/"
17+
};

0 commit comments

Comments
 (0)