Skip to content

Commit

Permalink
#000 okan switch awasome-typescript-loader to ts-loader and remove ty…
Browse files Browse the repository at this point in the history
…pings library
  • Loading branch information
cimdalli committed Dec 3, 2016
1 parent f4df075 commit 3078292
Show file tree
Hide file tree
Showing 12 changed files with 1,810 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "./node_modules/typescript/lib"
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-ts",
"version": "2.3.1",
"version": "2.4.0",
"description": "Utils to define redux reducer/action in typescript",
"main": "lib/index.js",
"typings": "lib/src/index.d.ts",
Expand All @@ -23,9 +23,8 @@
"build:test": "cross-env NODE_ENV=test webpack --output-filename lib/specs.js",
"clean": "rimraf dist lib",
"build": "npm run build:dev && npm run build:prod && npm run build:prod:min",
"postinstall": "typings install",
"prepublish": "npm run clean && npm run build && npm run test",
"test": "npm run build:test && npm run test:run",
"test": "npm run clean && npm run build:test && npm run test:run",
"test:run": "mocha --require source-map-support/register ./lib/specs.js"
},
"tags": [
Expand Down Expand Up @@ -55,15 +54,16 @@
"ts-helpers": "^1.1.1"
},
"devDependencies": {
"awesome-typescript-loader": "2.1.1",
"@types/chai": "^3.4.34",
"@types/mocha": "^2.2.33",
"chai": "^3.5.0",
"cross-env": "^2.0.1",
"glob": "^7.0.6",
"mocha": "^2.0.1",
"rimraf": "^2.5.4",
"source-map-support": "^0.4.2",
"typescript": "^1.8.10",
"typings": "^1.3.3",
"ts-loader": "^1.2.2",
"typescript": "^2.0.10",
"webpack": "^1.13.2",
"webpack-node-externals": "^1.3.3"
},
Expand Down
10 changes: 5 additions & 5 deletions src/utils/actionHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import 'ts-helpers'
import "./promiseHelpers"
import { Dispatch, Action } from 'redux'

export type NullableDispatch = Dispatch<any> | void;

export type NullableDispatch = Redux.Dispatch<any> | void;

export abstract class SyncAction implements Redux.Action {
export abstract class SyncAction implements Action {
type: string;
}

export abstract class AsyncAction extends SyncAction implements Promise<NullableDispatch>{

private promise: Promise<Redux.Dispatch<any>>
private promise: Promise<Dispatch<any>>

then(onfulfilled?: (value: Redux.Dispatch<any>) => NullableDispatch | PromiseLike<NullableDispatch>, onrejected?: (reason: any) => void): Promise<NullableDispatch> {
then(onfulfilled?: (value: Dispatch<any>) => NullableDispatch | PromiseLike<NullableDispatch>, onrejected?: (reason: any) => void): Promise<NullableDispatch> {
return this.promise.then(onfulfilled, onrejected);
}

Expand Down
11 changes: 6 additions & 5 deletions src/utils/asyncMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Action, MiddlewareAPI, Dispatch } from 'redux'
import { SyncAction, AsyncAction } from './actionHelpers'


const isSyncAction = (action: Redux.Action): action is any => {
const isSyncAction = (action: Action): action is any => {
return action instanceof SyncAction;
}

const isAsyncAction = (action: Redux.Action): action is any => {
const isAsyncAction = (action: Action): action is any => {
return action instanceof AsyncAction;
}

Expand All @@ -19,12 +20,12 @@ const mergeObject = (action: any): any => {
return merged;
}

export const asyncMiddleware = <S>(store: Redux.MiddlewareAPI<S>) => (next: Redux.Dispatch<S>): Redux.Dispatch<S> => (action: Redux.Action) => {
export const asyncMiddleware = <S>(store: MiddlewareAPI<S>) => (next: Dispatch<S>): Dispatch<S> => (action: Action) => {
if (isSyncAction(action)) {
action.type = action.constructor.name;
action.type = (<any>action).constructor.name;

if (isAsyncAction(action)) {
action.promise = new Promise<Redux.Dispatch<any>>((resolve, reject) => {
(<any>action).promise = new Promise<Dispatch<any>>((resolve, reject) => {
//After original dispatch lifecycle, resolve dispatch in order to handle async operations
setTimeout(() => {
resolve(store.dispatch);
Expand Down
13 changes: 7 additions & 6 deletions src/utils/reducerBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'mocha'
import { expect } from 'chai'
import { StoreBuilder } from './storeBuilder'
import { ReducerBuilder } from './reducerBuilder'
import { Action, Store, Reducer } from 'redux'
import { SyncAction, AsyncAction } from './actionHelpers'
import { ReducerBuilder } from './reducerBuilder'
import { StoreBuilder } from './storeBuilder'
import { expect } from 'chai'
import 'mocha'


interface SampleState {
Expand Down Expand Up @@ -58,9 +59,9 @@ describe("Reducer", () => {

describe("with async action handler", () => {
var dispatchedEvents: any[] = [];
var store: Redux.Store<SampleStore>;
var store: Store<SampleStore>;

var hookReducer: Redux.Reducer<any> = (state: any = {}, action: Redux.Action) => {
var hookReducer: Reducer<any> = (state: any = {}, action: Action) => {
if (!action.type.startsWith("@@")) {
dispatchedEvents.push(action.type);
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/reducerBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Action } from 'redux'
import { SyncAction, AsyncAction } from '../utils/actionHelpers'


export interface IAction<T extends Redux.Action> {
export interface IAction<T extends Action> {
prototype: T;
}

Expand Down
13 changes: 7 additions & 6 deletions src/utils/storeBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'mocha'
import { expect } from 'chai'
import { Action, StoreCreator } from 'redux'
import { StoreBuilder } from './storeBuilder'
import { expect } from 'chai'
import 'mocha'


describe("Store", () => {

var TestAction = <Redux.Action>{ type: "test" };
var TestAction = <Action>{ type: "test" };
var reducer = (state: any = {}, action: any) => { return state };
var initState = { reducer: { test: true } };

Expand Down Expand Up @@ -40,7 +41,7 @@ describe("Store", () => {

describe("with reducer", () => {
var isSet = false;
var testReducer = (state = {}, action: Redux.Action) => {
var testReducer = (state = {}, action: Action) => {
if (action.type == TestAction.type) { isSet = true; }
return state;
};
Expand All @@ -58,7 +59,7 @@ describe("Store", () => {

describe("with reducer map", () => {
var isSet = false;
var testReducer = (state = {}, action: Redux.Action) => {
var testReducer = (state = {}, action: Action) => {
if (action.type == TestAction.type) { isSet = true; }
return state;
}
Expand All @@ -76,7 +77,7 @@ describe("Store", () => {

describe("with enhancer", () => {
var isSet = false;
var enhancer = (f: Redux.StoreCreator) => {
var enhancer = (f: StoreCreator) => {
isSet = true;
return f;
};
Expand Down
22 changes: 11 additions & 11 deletions src/utils/storeBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { combineReducers, createStore, applyMiddleware, compose } from 'redux';
import { combineReducers, createStore, applyMiddleware, compose, Middleware, ReducersMapObject, GenericStoreEnhancer, StoreCreator, Reducer, Store } from 'redux';
import { asyncMiddleware } from '../utils/asyncMiddleware'


export class StoreBuilder<StoreType> {

private middlewares: Redux.Middleware[];
private reducers: Redux.ReducersMapObject;
private middlewares: Middleware[];
private reducers: ReducersMapObject;
private initialState: StoreType;
private enhancer: Redux.GenericStoreEnhancer;
private enhancer: GenericStoreEnhancer;

constructor() {
this.middlewares = [asyncMiddleware];
this.reducers = {};
this.initialState = {} as StoreType;
this.enhancer = (f: Redux.StoreCreator) => f;
this.enhancer = (f: StoreCreator) => f;
}

public withMiddleware(middleware: Redux.Middleware) {
public withMiddleware(middleware: Middleware) {
this.middlewares.push(middleware);
return this;
}
Expand All @@ -26,21 +26,21 @@ export class StoreBuilder<StoreType> {
return this;
}

public withReducer<S>(name: string, reducer: Redux.Reducer<S>) {
public withReducer<S>(name: string, reducer: Reducer<S>) {
this.reducers[name] = reducer;
return this;
}

public withReducersMap(reducers: Redux.ReducersMapObject) {
public withReducersMap(reducers: ReducersMapObject) {
for (let reducer in reducers) {
this.reducers[reducer] = reducers[reducer];
}
return this;
}

public withEnhancer(enhancer: Redux.GenericStoreEnhancer) {
public withEnhancer(enhancer: GenericStoreEnhancer) {
let preEnhancer = this.enhancer;
this.enhancer = (f: Redux.StoreCreator) => enhancer(preEnhancer(f));
this.enhancer = (f: StoreCreator) => enhancer(preEnhancer(f));
return this;
}

Expand All @@ -51,6 +51,6 @@ export class StoreBuilder<StoreType> {
let composer = compose(middlewares, this.enhancer)(createStore);
let store = composer(reducers, this.initialState);

return store as Redux.Store<StoreType>;
return store as Store<StoreType>;
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"target": "es5",
"noImplicitAny": true,
"noEmitHelpers": true,
"experimentalDecorators": true
"experimentalDecorators": true,
"lib": ["dom", "es5", "es2015.promise", "es2015.core"]
},
"exclude": [
"node_modules",
Expand Down
14 changes: 0 additions & 14 deletions typings.json

This file was deleted.

14 changes: 7 additions & 7 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const config = {
loaders: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'awesome-typescript',
query: {
declaration: isDev,
sourceMap: true
}
loader: 'ts-loader'
}
]

},
ts: {
compilerOptions: {
declaration: isDev,
sourceMap: true
}
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
Expand Down
Loading

0 comments on commit 3078292

Please sign in to comment.