From b8224d2befcfa6d0745b4f56238f81a5ee95e069 Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Tue, 9 Feb 2016 11:17:41 -0800 Subject: [PATCH] Move TypedRedux out into a new NPM package called 'redux-typed' --- .../MusicStore/ReactApp/fx/TypedRedux.ts | 67 ------------------- .../npm/redux-typed/.gitignore | 4 ++ .../npm/redux-typed/.npmignore | 3 + .../npm/redux-typed/LICENSE.txt | 12 ++++ .../npm/redux-typed/README.md | 1 + .../npm/redux-typed/package.json | 13 ++++ .../redux-typed/src/ObjectAssignPolyfill.ts | 24 +++++++ .../npm/redux-typed/src/StrongActions.ts | 42 ++++++++++++ .../npm/redux-typed/src/StrongProvide.ts | 27 ++++++++ .../npm/redux-typed/src/main.ts | 2 + .../npm/redux-typed/tsconfig.json | 12 ++++ .../npm/redux-typed/tsd.json | 18 +++++ 12 files changed, 158 insertions(+), 67 deletions(-) delete mode 100644 samples/react/MusicStore/ReactApp/fx/TypedRedux.ts create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/.gitignore create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/.npmignore create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/LICENSE.txt create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/README.md create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/package.json create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/ObjectAssignPolyfill.ts create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongActions.ts create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongProvide.ts create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/main.ts create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsconfig.json create mode 100644 src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsd.json diff --git a/samples/react/MusicStore/ReactApp/fx/TypedRedux.ts b/samples/react/MusicStore/ReactApp/fx/TypedRedux.ts deleted file mode 100644 index 4afa5d9..0000000 --- a/samples/react/MusicStore/ReactApp/fx/TypedRedux.ts +++ /dev/null @@ -1,67 +0,0 @@ -// Credits for the type detection trick: http://www.bluewire-technologies.com/2015/redux-actions-for-typescript/ -import * as React from 'react'; -import { Dispatch } from 'redux'; -import { connect as nativeConnect, ElementClass } from 'react-redux'; - -interface ActionClass { - prototype: T; -} - -export function typeName(name: string) { - return function(actionClass: ActionClass) { - // Although we could determine the type name using actionClass.prototype.constructor.name, - // it's dangerous to do that because minifiers may interfere with it, and then serialized state - // might not have the expected meaning after a recompile. So we explicitly ask for a name string. - actionClass.prototype.type = name; - } -} - -export function isActionType(action: Action, actionClass: ActionClass): action is T { - return action.type == actionClass.prototype.type; -} - -// Middleware for transforming Typed Actions into plain actions -export const typedToPlain = (store: any) => (next: any) => (action: any) => { - next(Object.assign({}, action)); -}; - -export abstract class Action { - type: string; - constructor() { - // Make it an own-property (not a prototype property) so that it's included when JSON-serializing - this.type = this.type; - } -} - -export interface Reducer extends Function { - (state: TState, action: Action): TState; -} - -export interface ActionCreatorGeneric extends Function { - (dispatch: Dispatch, getState: () => TState): any; -} - -interface ClassDecoratorWithProps extends Function { - (component: T): T; - props: TProps; -} - -type ReactComponentClass = new(props: T) => React.Component; -class ComponentBuilder { - constructor(private stateToProps: (appState: any) => TOwnProps, private actionCreators: TActions) { - } - - public withExternalProps() { - return this as any as ComponentBuilder; - } - - public get allProps(): TOwnProps & TActions & TExternalProps { return null; } - - public connect(componentClass: ReactComponentClass): ReactComponentClass { - return nativeConnect(this.stateToProps, this.actionCreators as any)(componentClass); - } -} - -export function provide(stateToProps: (appState: any) => TOwnProps, actionCreators: TActions) { - return new ComponentBuilder(stateToProps, actionCreators); -} diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/.gitignore b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/.gitignore new file mode 100644 index 0000000..a1df9a5 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/.gitignore @@ -0,0 +1,4 @@ +/typings/ +/node_modules/ +/*.js +/*.d.ts diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/.npmignore b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/.npmignore new file mode 100644 index 0000000..858cdc4 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/.npmignore @@ -0,0 +1,3 @@ +!/*.js +!/*.d.ts +/typings/ diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/LICENSE.txt b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/LICENSE.txt new file mode 100644 index 0000000..0bdc196 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/LICENSE.txt @@ -0,0 +1,12 @@ +Copyright (c) .NET Foundation. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +these files except in compliance with the License. You may obtain a copy of the +License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed +under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/README.md b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/README.md new file mode 100644 index 0000000..1333ed7 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/README.md @@ -0,0 +1 @@ +TODO diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/package.json b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/package.json new file mode 100644 index 0000000..9348d82 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/package.json @@ -0,0 +1,13 @@ +{ + "name": "redux-typed", + "version": "1.0.0", + "description": "Helpers for building React+Redux apps with strong TypeScript type checking everywhere", + "main": "main.js", + "typings": "main.d.ts", + "scripts": { + "prepublish": "tsd update && tsc && echo 'Finished building NPM package \"redux-typed\"'", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Microsoft", + "license": "Apache-2.0" +} diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/ObjectAssignPolyfill.ts b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/ObjectAssignPolyfill.ts new file mode 100644 index 0000000..a28f96e --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/ObjectAssignPolyfill.ts @@ -0,0 +1,24 @@ +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign +if (typeof (Object).assign != 'function') { + (function () { + (Object).assign = function (target) { + 'use strict'; + if (target === undefined || target === null) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + var output = Object(target); + for (var index = 1; index < arguments.length; index++) { + var source = arguments[index]; + if (source !== undefined && source !== null) { + for (var nextKey in source) { + if (source.hasOwnProperty(nextKey)) { + output[nextKey] = source[nextKey]; + } + } + } + } + return output; + }; + })(); +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongActions.ts b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongActions.ts new file mode 100644 index 0000000..55893bf --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongActions.ts @@ -0,0 +1,42 @@ +// Credits for the type detection technique: http://www.bluewire-technologies.com/2015/redux-actions-for-typescript/ +import * as React from 'react'; +import { Dispatch } from 'redux'; +import './ObjectAssignPolyfill'; + +export interface ActionClass { + prototype: T; +} + +export function typeName(name: string) { + return function(actionClass: ActionClass) { + // Although we could determine the type name using actionClass.prototype.constructor.name, + // it's dangerous to do that because minifiers may interfere with it, and then serialized state + // might not have the expected meaning after a recompile. So we explicitly ask for a name string. + actionClass.prototype.type = name; + } +} + +export function isActionType(action: Action, actionClass: ActionClass): action is T { + return action.type == actionClass.prototype.type; +} + +// Middleware for transforming Typed Actions into plain actions +export const typedToPlain = (store: any) => (next: any) => (action: any) => { + next((Object).assign({}, action)); +}; + +export abstract class Action { + type: string; + constructor() { + // Make it an own-property (not a prototype property) so that it's included when JSON-serializing + this.type = this.type; + } +} + +export interface Reducer extends Function { + (state: TState, action: Action): TState; +} + +export interface ActionCreatorGeneric extends Function { + (dispatch: Dispatch, getState: () => TState): any; +} diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongProvide.ts b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongProvide.ts new file mode 100644 index 0000000..bc20845 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/StrongProvide.ts @@ -0,0 +1,27 @@ +import * as React from 'react'; +import { connect as nativeConnect, ElementClass } from 'react-redux'; + +interface ClassDecoratorWithProps extends Function { + (component: T): T; + props: TProps; +} + +export type ReactComponentClass = new(props: T) => React.Component; +export class ComponentBuilder { + constructor(private stateToProps: (appState: any) => TOwnProps, private actionCreators: TActions) { + } + + public withExternalProps() { + return this as any as ComponentBuilder; + } + + public get allProps(): TOwnProps & TActions & TExternalProps { return null; } + + public connect(componentClass: ReactComponentClass): ReactComponentClass { + return nativeConnect(this.stateToProps, this.actionCreators as any)(componentClass); + } +} + +export function provide(stateToProps: (appState: any) => TOwnProps, actionCreators: TActions) { + return new ComponentBuilder(stateToProps, actionCreators); +} diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/main.ts b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/main.ts new file mode 100644 index 0000000..fde5f8f --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/src/main.ts @@ -0,0 +1,2 @@ +export * from './StrongActions'; +export * from './StrongProvide'; diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsconfig.json b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsconfig.json new file mode 100644 index 0000000..de676e9 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "module": "commonjs", + "target": "es5", + "declaration": true, + "outDir": "." + }, + "exclude": [ + "node_modules" + ] +} diff --git a/src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsd.json b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsd.json new file mode 100644 index 0000000..f37a7f8 --- /dev/null +++ b/src/Microsoft.AspNet.ReactServices/npm/redux-typed/tsd.json @@ -0,0 +1,18 @@ +{ + "version": "v4", + "repo": "borisyankov/DefinitelyTyped", + "ref": "master", + "path": "typings", + "bundle": "typings/tsd.d.ts", + "installed": { + "react/react.d.ts": { + "commit": "f4b1797c1201b6c575668f5d7ea12d9b1ab21846" + }, + "react-redux/react-redux.d.ts": { + "commit": "f4b1797c1201b6c575668f5d7ea12d9b1ab21846" + }, + "redux/redux.d.ts": { + "commit": "f4b1797c1201b6c575668f5d7ea12d9b1ab21846" + } + } +}