Skip to content

Commit 27f82f2

Browse files
author
Péter Hauszknecht
committed
prettifying
1 parent c827b91 commit 27f82f2

5 files changed

Lines changed: 137 additions & 172 deletions

File tree

src/store/index.ts

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,61 @@
1-
import {
2-
Store as IStore,
3-
Listener,
4-
Unsubscribe,
5-
Middleware,
6-
Reducer,
7-
Dispatch,
8-
GetState
9-
} from './types'
10-
import thunk, { ThunkMiddleware } from './middlewares/thunk'
1+
import { Store as IStore, Listener, Unsubscribe, Middleware, Reducer, Dispatch, GetState } from './types';
2+
import thunk, { ThunkMiddleware } from './middlewares/thunk';
113

12-
export * from './types'
13-
export * from './middlewares/thunk'
14-
export { thunk }
4+
export * from './types';
5+
export * from './middlewares/thunk';
6+
export { thunk };
157

16-
export default class Store<State, R = Reducer<State>>
17-
implements IStore<State, R> {
18-
static thunk = thunk
8+
export default class Store<State, R = Reducer<State>> implements IStore<State, R> {
9+
static thunk = thunk;
1910

20-
private state: State
21-
private listeners: Function[] = []
22-
private middlewares: Function[] = []
11+
private state: State;
12+
private listeners: Function[] = [];
13+
private middlewares: Function[] = [];
2314

2415
constructor(initialState: State) {
25-
this.state = initialState
16+
this.state = initialState;
2617
}
2718

28-
getState: GetState<State> = () => this.state
19+
getState: GetState<State> = () => this.state;
2920

3021
dispatch: Dispatch<R> = reducer => {
31-
assertReducer(reducer)
32-
const finalReducer = this.applyMiddlewares(reducer)
22+
assertReducer(reducer);
23+
const finalReducer = this.applyMiddlewares(reducer);
3324
if (typeof finalReducer === 'function') {
34-
this.state = finalReducer(this.state)
35-
this.listeners.forEach(listener => listener())
25+
this.state = finalReducer(this.state);
26+
this.listeners.forEach(listener => listener());
3627
}
37-
return <R>finalReducer
38-
}
28+
return <R>finalReducer;
29+
};
3930

4031
subscribe = (listener: Listener): Unsubscribe => {
41-
assertListener(listener)
42-
this.listeners = [ ...this.listeners, listener ]
43-
return () =>
44-
(this.listeners = this.listeners.filter(lis => lis !== listener))
45-
}
32+
assertListener(listener);
33+
this.listeners = [ ...this.listeners, listener ];
34+
return () => (this.listeners = this.listeners.filter(lis => lis !== listener));
35+
};
4636

47-
addMiddleware = <R2>(
48-
...middlewares: Middleware<State>[]
49-
): Store<State, R | R2> => {
50-
assertMiddlewares(middlewares)
51-
this.middlewares = [ ...this.middlewares, ...middlewares ]
52-
return this
53-
}
37+
addMiddleware = <R2>(...middlewares: Middleware<State>[]): Store<State, R | R2> => {
38+
assertMiddlewares(middlewares);
39+
this.middlewares = [ ...this.middlewares, ...middlewares ];
40+
return this;
41+
};
5442

5543
private applyMiddlewares = (reducer: R): R =>
56-
<R>this.middlewares.reduce(
57-
(prevReducer, middleware) => middleware(this, prevReducer),
58-
reducer
59-
)
44+
<R>this.middlewares.reduce((prevReducer, middleware) => middleware(this, prevReducer), reducer);
6045
}
6146

6247
function assertReducer(reducer) {
6348
if (typeof reducer !== 'function')
64-
throw new Error(
65-
'Reducer is not a function: dispatch takes only reducers as functions.'
66-
)
49+
throw new Error('Reducer is not a function: dispatch takes only reducers as functions.');
6750
}
6851

6952
function assertListener(listener) {
7053
if (typeof listener !== 'function')
71-
throw new Error(
72-
'Listener is not a function: subscribe takes only listeners as functions.'
73-
)
54+
throw new Error('Listener is not a function: subscribe takes only listeners as functions.');
7455
}
7556

7657
function assertMiddlewares(middlewares) {
7758
if (middlewares.some(middleware => typeof middleware !== 'function')) {
78-
throw new Error(
79-
'Middleware is not a function: addMiddleware takes only middlewares as functions.'
80-
)
59+
throw new Error('Middleware is not a function: addMiddleware takes only middlewares as functions.');
8160
}
8261
}

src/store/middlewares/thunk.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1-
import { Middleware, GetState, Dispatch, Reducer, Store } from '../types'
1+
import { Middleware, GetState, Dispatch, Reducer, Store } from '../types';
22

3-
export interface ThunkMiddleware<State, ExtraArgument>
4-
extends Middleware<State> {
5-
withExtraArgument: (
6-
extraArgument: ExtraArgument
7-
) => ThunkMiddleware<State, ExtraArgument>
3+
export interface ThunkMiddleware<State, ExtraArgument> extends Middleware<State> {
4+
withExtraArgument: (extraArgument: ExtraArgument) => ThunkMiddleware<State, ExtraArgument>;
85
}
96

107
export interface Delegate<State, ExtraArgument, T> {
11-
(
12-
dispatch: ThunkDispatch<State, ExtraArgument>,
13-
getState: GetState<State>,
14-
extraArgument: ExtraArgument
15-
): T
8+
(dispatch: ThunkDispatch<State, ExtraArgument>, getState: GetState<State>, extraArgument: ExtraArgument): T;
169
}
1710

18-
export interface ThunkDispatch<State, ExtraArgument>
19-
extends Dispatch<Reducer<State>> {
20-
<T>(reducer: Thunk<State, ExtraArgument, T>): T
11+
export interface ThunkDispatch<State, ExtraArgument> extends Dispatch<Reducer<State>> {
12+
<T>(reducer: Thunk<State, ExtraArgument, T>): T;
2113
}
2214

2315
export interface Thunk<State, ExtraArgument, T> {
24-
(state: State): Delegate<State, ExtraArgument, T>
16+
(state: State): Delegate<State, ExtraArgument, T>;
2517
}
2618

2719
function thunkFactory<T>(extraArgument?: T): ThunkMiddleware<any, T> {
2820
const thunk = ((store, reducer) => {
29-
const state = store.getState()
30-
const result = reducer(state)
31-
return typeof result === 'function'
32-
? result(store.dispatch, store.getState, extraArgument)
33-
: reducer
34-
}) as ThunkMiddleware<any, T>
35-
thunk.withExtraArgument = thunkFactory
36-
return thunk
21+
const state = store.getState();
22+
const result = reducer(state);
23+
return typeof result === 'function' ? result(store.dispatch, store.getState, extraArgument) : reducer;
24+
}) as ThunkMiddleware<any, T>;
25+
thunk.withExtraArgument = thunkFactory;
26+
return thunk;
3727
}
3828

39-
export default thunkFactory()
29+
export default thunkFactory();

src/store/types.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
export interface GetState<State> {
2-
(): State
2+
(): State;
33
}
44

55
export interface Reducer<State> {
6-
(state: State): State
6+
(state: State): State;
77
}
88

99
export interface Dispatch<Reducer> {
10-
(reducer: Reducer): Reducer
10+
(reducer: Reducer): Reducer;
1111
}
1212

1313
export interface Listener {
14-
(): void
14+
(): void;
1515
}
1616

1717
export interface Unsubscribe {
18-
(): void
18+
(): void;
1919
}
2020

2121
export interface Middleware<State> {
22-
(store: Store<State>, reducer: Reducer<State>): Reducer<State>
22+
(store: Store<State>, reducer: Reducer<State>): Reducer<State>;
2323
}
2424

2525
export interface Store<State, R = Reducer<State>> {
26-
getState: GetState<State>
27-
dispatch: Dispatch<R>
28-
subscribe(listener: Listener): Unsubscribe
29-
addMiddleware<R2>(...middlewares: Middleware<State>[]): Store<State, R | R2>
26+
getState: GetState<State>;
27+
dispatch: Dispatch<R>;
28+
subscribe(listener: Listener): Unsubscribe;
29+
addMiddleware<R2>(...middlewares: Middleware<State>[]): Store<State, R | R2>;
3030
}

src/test/store.spec.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
1-
import 'mocha'
2-
import * as assert from 'assert'
3-
import Store from '../store'
1+
import 'mocha';
2+
import * as assert from 'assert';
3+
import Store from '../store';
44

5-
process.env.NODE_ENV = 'test'
5+
process.env.NODE_ENV = 'test';
66

77
describe('store', () => {
88
describe('instantiation', () => {
99
it('Store instantiation', () => {
10-
const store = new Store(1)
11-
assert(store)
12-
assert(store.getState)
13-
assert(store.dispatch)
14-
assert(store.subscribe)
15-
assert(store.addMiddleware)
16-
})
17-
})
10+
const store = new Store(1);
11+
assert(store);
12+
assert(store.getState);
13+
assert(store.dispatch);
14+
assert(store.subscribe);
15+
assert(store.addMiddleware);
16+
});
17+
});
1818

1919
describe('getState', () => {
2020
it('getState returns the state', () => {
21-
const store = new Store(1)
22-
assert.strictEqual(store.getState(), 1)
23-
})
24-
})
21+
const store = new Store(1);
22+
assert.strictEqual(store.getState(), 1);
23+
});
24+
});
2525

2626
describe('dispatch', () => {
2727
it('dispatch throws if its argument is not a function', () => {
28-
const store = new Store(1)
29-
assert.throws(() => store.dispatch(undefined))
30-
})
28+
const store = new Store(1);
29+
assert.throws(() => store.dispatch(undefined));
30+
});
3131

3232
it('dispatch reduces the state', () => {
33-
const store = new Store(1)
34-
assert.strictEqual(store.getState(), 1)
35-
store.dispatch(state => state + 1)
36-
assert.strictEqual(store.getState(), 2)
37-
})
33+
const store = new Store(1);
34+
assert.strictEqual(store.getState(), 1);
35+
store.dispatch(state => state + 1);
36+
assert.strictEqual(store.getState(), 2);
37+
});
3838

3939
it('dispatch returns the reducer', () => {
40-
const store = new Store(1)
41-
const reducer = state => state + 1
42-
assert.strictEqual(store.dispatch(reducer), reducer)
43-
})
44-
})
40+
const store = new Store(1);
41+
const reducer = state => state + 1;
42+
assert.strictEqual(store.dispatch(reducer), reducer);
43+
});
44+
});
4545

4646
describe('subscribe', () => {
4747
it('subscribe throws if its argument is not a function', () => {
48-
const store = new Store(1)
49-
assert.throws(() => store.subscribe(undefined))
50-
})
48+
const store = new Store(1);
49+
assert.throws(() => store.subscribe(undefined));
50+
});
5151

5252
it('subscribe returns a subscriber', () => {
53-
const store = new Store(1)
54-
const subscriber = store.subscribe(() => {})
55-
assert.strictEqual(typeof subscriber, 'function')
56-
})
53+
const store = new Store(1);
54+
const subscriber = store.subscribe(() => {});
55+
assert.strictEqual(typeof subscriber, 'function');
56+
});
5757

5858
it('subscribe performs a subscribtion', done => {
59-
const store = new Store(1)
59+
const store = new Store(1);
6060
store.subscribe(() => {
61-
done()
62-
})
63-
store.dispatch(state => state + 1)
64-
})
61+
done();
62+
});
63+
store.dispatch(state => state + 1);
64+
});
6565

6666
it('subscribtion reads the new state', done => {
67-
const store = new Store(1)
67+
const store = new Store(1);
6868
store.subscribe(() => {
69-
assert.strictEqual(store.getState(), 2)
70-
done()
71-
})
72-
store.dispatch(state => state + 1)
73-
})
69+
assert.strictEqual(store.getState(), 2);
70+
done();
71+
});
72+
store.dispatch(state => state + 1);
73+
});
7474

7575
it('unsubscribe', done => {
76-
const store = new Store(1)
76+
const store = new Store(1);
7777
const unsubscribe = store.subscribe(() => {
78-
done(new Error('Unsuccessful subscribtion'))
79-
})
80-
unsubscribe()
81-
store.dispatch(state => state + 1)
82-
done()
83-
})
84-
})
85-
})
78+
done(new Error('Unsuccessful subscribtion'));
79+
});
80+
unsubscribe();
81+
store.dispatch(state => state + 1);
82+
done();
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)