Skip to content

Commit d560982

Browse files
author
Sascha Braun
committed
FIX default state value is not set
1 parent 7044e1b commit d560982

File tree

5 files changed

+71
-27
lines changed

5 files changed

+71
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuex-simple",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A simpler way to write your Vuex store in Typescript",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

src/decorators.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Container, Token } from 'typedi';
22
import { Action, Mutation } from 'vuex';
33

4-
import { applyDecorators } from './metadata';
4+
import { applyDecorators, getInitialState } from './metadata';
55
import { getStoreBuilder, StoreBuilder } from './store-builder';
66
import { DecoratorType, InjectType, ModuleOptions } from './types';
77
import { decoratorUtil, injectUtil } from './utils';
@@ -28,8 +28,9 @@ export function Module(pOptions: ModuleOptions | string) {
2828

2929
const options = handleOptions(pOptions);
3030
const storeBuilder: StoreBuilder<any> = getStoreBuilder();
31+
const initialState = getInitialState(instance);
3132

32-
const moduleBuilder = storeBuilder.module(options.namespace, {});
33+
const moduleBuilder = storeBuilder.module(options.namespace, initialState);
3334

3435
applyDecorators<any>(moduleBuilder, instance);
3536

src/example/test-module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export class TestModule {
77
private testApi!: TestApi;
88

99
@State()
10-
public counter: number = 0;
10+
public counter: number = 10;
11+
12+
@State()
13+
public name: string = 'Will';
1114

1215
@Getter()
1316
public get cachedGetter() {

src/metadata.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
import Vue from 'vue';
21
import { Action as Act, Getter, Mutation as Mut } from 'vuex';
32

43
import { ModuleBuilder } from './module-builder';
54
import { DecoratorType } from './types';
65
import { decoratorUtil } from './utils';
76

8-
function getMethodDescriptor(prototype: any, propertyName: string) {
9-
const descriptor = Object.getOwnPropertyDescriptor(prototype, propertyName);
10-
if (descriptor && typeof descriptor.value === 'function') {
11-
return descriptor;
12-
}
13-
return undefined;
14-
}
15-
167
export function applyDecorators<State>(moduleBuilder: ModuleBuilder<State, any>, instance: any) {
178
const constructor = instance.constructor;
189
const decorators = decoratorUtil.getDecorators(constructor);
1910
const proto = constructor.prototype;
2011

21-
applyStates(moduleBuilder, instance);
12+
bindStateToStore(moduleBuilder, instance);
2213

2314
const properties = Object.getOwnPropertyNames(proto);
2415

@@ -53,25 +44,51 @@ export function applyDecorators<State>(moduleBuilder: ModuleBuilder<State, any>,
5344
});
5445
}
5546

56-
function applyStates<State>(moduleBuilder: ModuleBuilder<State, any>, instance: any) {
47+
export function getInitialState(instance: any) {
48+
const state: object = {};
49+
const properties = findStateProperties(instance);
50+
properties.forEach(propertyName => {
51+
state[propertyName] = instance[propertyName];
52+
});
53+
return state;
54+
}
55+
56+
function getMethodDescriptor(prototype: any, propertyName: string) {
57+
const descriptor = Object.getOwnPropertyDescriptor(prototype, propertyName);
58+
if (descriptor && typeof descriptor.value === 'function') {
59+
return descriptor;
60+
}
61+
return undefined;
62+
}
63+
64+
function findStateProperties(instance: any) {
65+
const properties: string[] = [];
66+
5767
const constructor = instance.constructor;
5868
const decorators = decoratorUtil.getDecorators(constructor);
5969
const keys = Object.keys(instance);
6070

6171
keys.forEach(propertyName => {
6272
const decorator = decorators.get(propertyName);
6373
if (decorator && decorator === DecoratorType.STATE) {
64-
Object.defineProperty(instance, propertyName, {
65-
get() {
66-
return moduleBuilder.state()[propertyName];
67-
},
68-
set(val: any) {
69-
moduleBuilder.state()[propertyName] = val;
70-
}
71-
});
72-
Vue.set(instance, propertyName, instance[propertyName]);
74+
properties.push(propertyName);
7375
}
7476
});
77+
return properties;
78+
}
79+
80+
function bindStateToStore<State>(moduleBuilder: ModuleBuilder<State, any>, instance: any) {
81+
const properties = findStateProperties(instance);
82+
properties.forEach(propertyName => {
83+
Object.defineProperty(instance, propertyName, {
84+
get() {
85+
return moduleBuilder.state()[propertyName];
86+
},
87+
set(val: any) {
88+
moduleBuilder.state()[propertyName] = val;
89+
}
90+
});
91+
});
7592
}
7693

7794
function applyMutation<State>(

src/tests/simple.spec.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,43 @@ Vue.use(VuexSimple);
1313
test.before(t => {
1414
const storeBuilder = getStoreBuilder();
1515
storeBuilder.loadModules([TestModule]);
16-
storeBuilder.create();
16+
const store = storeBuilder.create();
17+
18+
// create snapshot of initial state
19+
(t as any).context.snapshot = JSON.stringify(store.state);
1720
});
1821

1922
test.beforeEach(t => {
23+
const storeBuilder = getStoreBuilder();
24+
const store = storeBuilder.create();
25+
26+
// reset store
27+
store.replaceState(JSON.parse((t as any).context.snapshot));
28+
});
29+
30+
test.serial('initial state', t => {
2031
const testModule = Container.get(TestModule);
21-
testModule.setCounter(0);
32+
33+
t.is(testModule.counter, 10);
34+
t.is(testModule.name, 'Will');
2235
});
2336

2437
test.serial('simple increment', t => {
2538
const testModule = Container.get(TestModule);
39+
40+
t.is(testModule.counter, 10);
41+
42+
testModule.setCounter(0);
43+
2644
t.is(testModule.counter, 0);
2745
t.notThrows(() => testModule.increment());
2846
t.is(testModule.counter, 1);
2947
});
3048

3149
test.serial('simple computed getters', t => {
3250
const testModule = Container.get(TestModule);
51+
testModule.setCounter(0);
52+
3353
t.not(testModule.normalGetter, testModule.normalGetter);
3454
t.is(testModule.cachedGetter, testModule.cachedGetter);
3555
t.is(testModule.cachedGetter.item, 100);
@@ -41,6 +61,7 @@ test.serial('simple computed getters', t => {
4161

4262
test.serial('simple incrementAsync', async t => {
4363
const testModule = Container.get(TestModule);
64+
testModule.setCounter(0);
4465

4566
t.is(testModule.counter, 0);
4667

@@ -54,6 +75,7 @@ test.serial('simple incrementAsync', async t => {
5475

5576
test.serial('store replaceState', async t => {
5677
const testModule = Container.get(TestModule);
78+
testModule.setCounter(0);
5779

5880
t.is(testModule.counter, 0);
5981
testModule.increment();
@@ -79,6 +101,7 @@ test.serial('store replaceState', async t => {
79101

80102
test.serial('injection in module', async t => {
81103
const testModule = Container.get(TestModule);
104+
testModule.setCounter(0);
82105

83106
t.is(testModule.counter, 0);
84107

@@ -88,7 +111,7 @@ test.serial('injection in module', async t => {
88111
t.is(testModule.counter, 42);
89112
});
90113

91-
test.serial('injection in component', async t => {
114+
test.serial('injection in non vue component', async t => {
92115
const testModule = Container.get(TestModule);
93116
const myComponent = new MyComponent();
94117

0 commit comments

Comments
 (0)