Skip to content

Commit

Permalink
refactor(general): update namespaces and clean up imports
Browse files Browse the repository at this point in the history
  • Loading branch information
protoman92 committed Sep 12, 2018
1 parent 0ccc5f6 commit b4befe4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as reduxstore from './store';
import * as ReduxStore from './store';
import {DispatchReducer, RxReducer} from './store';
export {DispatchReducer, RxReducer, reduxstore};
export {DispatchReducer, RxReducer, ReduxStore};
38 changes: 18 additions & 20 deletions src/store/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {BehaviorSubject, Observable, Scheduler, Subscription} from 'rxjs';

import {observeOn, scan} from 'rxjs/operators';
import {Never} from 'javascriptutilities';
import {Type as StoreType} from './types';

import {
IncompletableSubject,
MappableObserver,
mapNonNilOrEmpty,
MappableObserver,
} from 'rx-utilities-js';
import {BehaviorSubject, Observable, Scheduler, Subscription} from 'rxjs';
import {observeOn, scan} from 'rxjs/operators';
import {Type as StoreType} from './types';

export namespace action {
export namespace Action {
/**
* Represents an action to be dispatched to the global state.
* @template T Generics parameter.
Expand All @@ -22,18 +20,18 @@ export namespace action {
}
}

export type Reducer<State, T> = (state: State, action: action.Type<T>) => State;
export type Reducer<State, T> = (state: State, action: Action.Type<T>) => State;

/**
* Represents a dispatch store type.
* @extends {StoreType} Store type extension.
* @template State Generics parameter.
*/
export interface Type<State> extends StoreType<State> {
readonly actionTrigger: MappableObserver.Type<Never<action.Type<any>>>;
readonly actionStream: Observable<action.Type<any>>;
readonly actionTrigger: MappableObserver.Type<Never<Action.Type<any>>>;
readonly actionStream: Observable<Action.Type<any>>;
readonly lastState: State;
dispatch(action: action.Type<any>): void;
dispatch(action: Action.Type<any>): void;
}

/**
Expand All @@ -42,8 +40,8 @@ export interface Type<State> extends StoreType<State> {
* @implements {Type} Type implementation.
* @template State Generics parameter.
*/
export class Self<State> implements Type<State> {
private readonly action: IncompletableSubject<Never<action.Type<any>>>;
export class Impl<State> implements Type<State> {
private readonly action: IncompletableSubject<Never<Action.Type<any>>>;
private readonly state: BehaviorSubject<State>;
private readonly subscription: Subscription;

Expand All @@ -53,11 +51,11 @@ export class Self<State> implements Type<State> {
this.subscription = new Subscription();
}

public get actionTrigger(): MappableObserver.Type<Never<action.Type<any>>> {
public get actionTrigger(): MappableObserver.Type<Never<Action.Type<any>>> {
return this.action;
}

public get actionStream(): Observable<action.Type<any>> {
public get actionStream(): Observable<Action.Type<any>> {
return this.action.asObservable().pipe(mapNonNilOrEmpty(v => v));
}

Expand All @@ -75,7 +73,7 @@ export class Self<State> implements Type<State> {
.asObservable()
.pipe(
mapNonNilOrEmpty(v => v),
scan((acc: State, action: action.Type<any>): State => {
scan((acc: State, action: Action.Type<any>): State => {
return reducer(acc, action);
}, this.state.value),
observeOn(scheduler)
Expand All @@ -88,7 +86,7 @@ export class Self<State> implements Type<State> {
this.subscription.unsubscribe();
}

public dispatch(action: action.Type<any>): void {
public dispatch(action: Action.Type<any>): void {
this.actionTrigger.next(action);
}
}
Expand All @@ -99,14 +97,14 @@ export class Self<State> implements Type<State> {
* @param {State} initialState Initial state.
* @param {Reducer<State, any>} reducer A Reducer instance.
* @param {Scheduler} scheduler A Scheduler instance.
* @returns {Self} A dispatch store instance.
* @returns {Impl} A dispatch store instance.
*/
export function createDefault<State>(
initialState: State,
reducer: Reducer<State, any>,
scheduler: Scheduler
): Self<State> {
let store = new Self(initialState);
): Impl<State> {
let store = new Impl(initialState);
store.initialize(reducer, scheduler);
return store;
}
6 changes: 3 additions & 3 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dispatch from './dispatch';
import * as rx from './rx';
import * as Dispatch from './dispatch';
import * as Rx from './rx';
import {Reducer as DispatchReducer} from './dispatch';
import {RxReducer} from './rx';
import {Type} from './types';
export {dispatch, DispatchReducer, rx, RxReducer, Type};
export {Dispatch, DispatchReducer, Rx, RxReducer, Type};
30 changes: 15 additions & 15 deletions src/store/rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {map, scan, observeOn, startWith} from 'rxjs/operators';
import {Never, Types} from 'javascriptutilities';
import {Type as StoreType} from './types';

export namespace action {
export namespace Action {
/**
* Represents a dispatchable action.
* @template T Generics parameter.
Expand All @@ -14,29 +14,29 @@ export namespace action {
}
}

export namespace stateinfo {
export namespace StateInfo {
/**
* Represents necessary state information.
* @template T Generics parameter.
*/
export interface Type<State, T> {
readonly state: State;
readonly lastAction: Never<action.Type<T>>;
readonly lastAction: Never<Action.Type<T>>;
}
}

export type ActionType<T> = action.Type<T> | T;
export type Reducer<State, T> = (state: State, action: action.Type<T>) => State;
export type RxReducer<State, T> = (state: State) => stateinfo.Type<State, T>;
export type ActionType<T> = Action.Type<T> | T;
export type Reducer<State, T> = (state: State, action: Action.Type<T>) => State;
export type RxReducer<State, T> = (state: State) => StateInfo.Type<State, T>;

/**
* Convenience method to create an action with a default name.
* @template T Generics parameter.
* @param {ActionType<T>} action An ActionType instance.
* @returns {action.Type<T>} An Action type instance.
* @returns {Action.Type<T>} An Action type instance.
*/
function createAction<T>(action: ActionType<T>): action.Type<T> {
if (Types.isInstance<action.Type<T>>(action, 'name', 'value')) {
function createAction<T>(action: ActionType<T>): Action.Type<T> {
if (Types.isInstance<Action.Type<T>>(action, 'name', 'value')) {
return action;
} else {
return {name: 'DummyAction', value: action};
Expand Down Expand Up @@ -81,16 +81,16 @@ export function createReducer<State, T>(
* @param {State} initialState Initial state.
* @param {Scheduler} scheduler A Scheduler instance.
* @param {...Observable<RxReducer<State, T>>[]} reducers An Array of Observable.
* @returns {Observable<stateinfo.Type<State, T>>} An Observable instance.
* @returns {Observable<StateInfo.Type<State, T>>} An Observable instance.
*/
export function create<State, T>(
initialState: State,
scheduler: Scheduler,
...reducers: Observable<RxReducer<State, T>>[]
): Observable<stateinfo.Type<State, T>> {
): Observable<StateInfo.Type<State, T>> {
return merge(...reducers).pipe(
scan(
(v1: stateinfo.Type<State, T>, v2: RxReducer<State, T>) => {
(v1: StateInfo.Type<State, T>, v2: RxReducer<State, T>) => {
return v2(v1.state);
},
{state: initialState, lastAction: undefined}
Expand All @@ -113,8 +113,8 @@ export interface Type<State> extends StoreType<State> {}
* @implements {Type} Type implementation.
* @template State Generics paramter.
*/
export class Self<State> implements Type<State> {
private store: Observable<stateinfo.Type<State, any>>;
export class Impl<State> implements Type<State> {
private store: Observable<StateInfo.Type<State, any>>;

public constructor(
initialState: State,
Expand All @@ -124,7 +124,7 @@ export class Self<State> implements Type<State> {
this.store = create(initialState, scheduler, ...reducers);
}

public get stateInfoStream(): Observable<stateinfo.Type<State, any>> {
public get stateInfoStream(): Observable<StateInfo.Type<State, any>> {
return this.store;
}

Expand Down
30 changes: 15 additions & 15 deletions test/store.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {BehaviorSubject, queueScheduler as scheduler} from 'rxjs';
import {map} from 'rxjs/operators';
import {Collections, Never, Try} from 'javascriptutilities';
import {doOnNext} from 'rx-utilities-js';
import {BehaviorSubject, queueScheduler as scheduler} from 'rxjs';
import {map} from 'rxjs/operators';
import {State as S} from 'type-safe-state-js';
import {reduxstore as Store} from './../src';
import {ReduxStore as Store} from './../src';
import {Reducer as DispatchReducer} from './../src/store/dispatch';

type State = S.Type<any>;
type DispatchStore = Store.dispatch.Self<State>;
type RxStore = Store.rx.Self<State>;
type DispatchStore = Store.Dispatch.Impl<State>;
type RxStore = Store.Rx.Impl<State>;
type StoreType = Store.Type<State>;

let timeout = 100;
Expand Down Expand Up @@ -73,28 +73,28 @@ let testReduxStore = (store: StoreType, actionFn: () => void): void => {

describe('Rx store should be implemented correctly', () => {
var action1: BehaviorSubject<number>;
var action2: BehaviorSubject<Store.rx.action.Type<string>>;
var action3: BehaviorSubject<Store.rx.action.Type<boolean>>;
var action2: BehaviorSubject<Store.Rx.Action.Type<string>>;
var action3: BehaviorSubject<Store.Rx.Action.Type<boolean>>;
var stateStore: RxStore;

let createStore = (): RxStore => {
let reducer1 = Store.rx.createReducer(action1, (state: State, v) => {
let reducer1 = Store.Rx.createReducer(action1, (state: State, v) => {
return state.mappingValue(path1, v1 => {
return v1.map(v2 => v2 + v.value).successOrElse(Try.success(v.value));
});
});

let reducer2 = Store.rx.createReducer(action2, (state: State, v) => {
let reducer2 = Store.Rx.createReducer(action2, (state: State, v) => {
return state.mappingValue(path2, v1 => {
return v1.map(v2 => v2 + v.value).successOrElse(Try.success(v.value));
});
});

let reducer3 = Store.rx.createReducer(action3, (state: State, v) => {
let reducer3 = Store.Rx.createReducer(action3, (state: State, v) => {
return state.updatingValue(path3, v.value);
});

return new Store.rx.Self(
return new Store.Rx.Impl(
S.empty(),
scheduler,
reducer1,
Expand Down Expand Up @@ -126,19 +126,19 @@ describe('Rx store should be implemented correctly', () => {
describe('Dispatch store should be implemented correctly', () => {
var stateStore: DispatchStore;

let actionFn1 = (v: number): Store.dispatch.action.Type<number> => ({
let actionFn1 = (v: number): Store.Dispatch.Action.Type<number> => ({
id: actionKey1,
fullValuePath: path1,
payload: v,
});

let actionFn2 = (v: string): Store.dispatch.action.Type<string> => ({
let actionFn2 = (v: string): Store.Dispatch.Action.Type<string> => ({
id: actionKey2,
fullValuePath: path2,
payload: v,
});

let actionFn3 = (v: boolean): Store.dispatch.action.Type<boolean> => ({
let actionFn3 = (v: boolean): Store.Dispatch.Action.Type<boolean> => ({
id: actionKey3,
fullValuePath: path3,
payload: v,
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('Dispatch store should be implemented correctly', () => {
};

beforeEach(() => {
stateStore = Store.dispatch.createDefault(S.empty(), reducer, scheduler);
stateStore = Store.Dispatch.createDefault(S.empty(), reducer, scheduler);
});

it('Dispatch action with action creators - should work', () => {
Expand Down

0 comments on commit b4befe4

Please sign in to comment.