Skip to content

Commit b8224d2

Browse files
Move TypedRedux out into a new NPM package called 'redux-typed'
1 parent 047d14a commit b8224d2

File tree

12 files changed

+158
-67
lines changed

12 files changed

+158
-67
lines changed

samples/react/MusicStore/ReactApp/fx/TypedRedux.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/typings/
2+
/node_modules/
3+
/*.js
4+
/*.d.ts
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!/*.js
2+
!/*.d.ts
3+
/typings/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright (c) .NET Foundation. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4+
these files except in compliance with the License. You may obtain a copy of the
5+
License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed
10+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
specific language governing permissions and limitations under the License.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "redux-typed",
3+
"version": "1.0.0",
4+
"description": "Helpers for building React+Redux apps with strong TypeScript type checking everywhere",
5+
"main": "main.js",
6+
"typings": "main.d.ts",
7+
"scripts": {
8+
"prepublish": "tsd update && tsc && echo 'Finished building NPM package \"redux-typed\"'",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"author": "Microsoft",
12+
"license": "Apache-2.0"
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
2+
if (typeof (<any>Object).assign != 'function') {
3+
(function () {
4+
(<any>Object).assign = function (target) {
5+
'use strict';
6+
if (target === undefined || target === null) {
7+
throw new TypeError('Cannot convert undefined or null to object');
8+
}
9+
10+
var output = Object(target);
11+
for (var index = 1; index < arguments.length; index++) {
12+
var source = arguments[index];
13+
if (source !== undefined && source !== null) {
14+
for (var nextKey in source) {
15+
if (source.hasOwnProperty(nextKey)) {
16+
output[nextKey] = source[nextKey];
17+
}
18+
}
19+
}
20+
}
21+
return output;
22+
};
23+
})();
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Credits for the type detection technique: http://www.bluewire-technologies.com/2015/redux-actions-for-typescript/
2+
import * as React from 'react';
3+
import { Dispatch } from 'redux';
4+
import './ObjectAssignPolyfill';
5+
6+
export interface ActionClass<T extends Action> {
7+
prototype: T;
8+
}
9+
10+
export function typeName(name: string) {
11+
return function<T extends Action>(actionClass: ActionClass<T>) {
12+
// Although we could determine the type name using actionClass.prototype.constructor.name,
13+
// it's dangerous to do that because minifiers may interfere with it, and then serialized state
14+
// might not have the expected meaning after a recompile. So we explicitly ask for a name string.
15+
actionClass.prototype.type = name;
16+
}
17+
}
18+
19+
export function isActionType<T extends Action>(action: Action, actionClass: ActionClass<T>): action is T {
20+
return action.type == actionClass.prototype.type;
21+
}
22+
23+
// Middleware for transforming Typed Actions into plain actions
24+
export const typedToPlain = (store: any) => (next: any) => (action: any) => {
25+
next((<any>Object).assign({}, action));
26+
};
27+
28+
export abstract class Action {
29+
type: string;
30+
constructor() {
31+
// Make it an own-property (not a prototype property) so that it's included when JSON-serializing
32+
this.type = this.type;
33+
}
34+
}
35+
36+
export interface Reducer<TState> extends Function {
37+
(state: TState, action: Action): TState;
38+
}
39+
40+
export interface ActionCreatorGeneric<TState> extends Function {
41+
(dispatch: Dispatch, getState: () => TState): any;
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
import { connect as nativeConnect, ElementClass } from 'react-redux';
3+
4+
interface ClassDecoratorWithProps<TProps> extends Function {
5+
<T extends (typeof ElementClass)>(component: T): T;
6+
props: TProps;
7+
}
8+
9+
export type ReactComponentClass<T, S> = new(props: T) => React.Component<T, S>;
10+
export class ComponentBuilder<TOwnProps, TActions, TExternalProps> {
11+
constructor(private stateToProps: (appState: any) => TOwnProps, private actionCreators: TActions) {
12+
}
13+
14+
public withExternalProps<TAddExternalProps>() {
15+
return this as any as ComponentBuilder<TOwnProps, TActions, TAddExternalProps>;
16+
}
17+
18+
public get allProps(): TOwnProps & TActions & TExternalProps { return null; }
19+
20+
public connect<TState>(componentClass: ReactComponentClass<TOwnProps & TActions & TExternalProps, TState>): ReactComponentClass<TExternalProps, TState> {
21+
return nativeConnect(this.stateToProps, this.actionCreators as any)(componentClass);
22+
}
23+
}
24+
25+
export function provide<TOwnProps, TActions>(stateToProps: (appState: any) => TOwnProps, actionCreators: TActions) {
26+
return new ComponentBuilder<TOwnProps, TActions, {}>(stateToProps, actionCreators);
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './StrongActions';
2+
export * from './StrongProvide';

0 commit comments

Comments
 (0)