-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrote out more scaffolding code for usage and api
- Loading branch information
Thomas Chen
committed
May 18, 2017
1 parent
bfb5b79
commit c9a7a2f
Showing
10 changed files
with
224 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Place your settings in this file to overwrite default and user settings. | ||
{ | ||
"[typescript]": { | ||
"editor.formatOnSave": true, | ||
"editor.formatOnPaste": true | ||
}, | ||
"[markdown]": { | ||
"editor.formatOnSave": true, | ||
"editor.wordwrap": "on", | ||
"editor.renderWhitespace": "all", | ||
"editor.acceptSuggestionOnEnter": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
import ReducerArrow from './reducer-arrow'; | ||
import normalizeArrows from './normalize-arrows'; | ||
import { isBlank } from './utils'; | ||
|
||
export function redArrow(...args) { | ||
const [...keys] = args.slice(0, -1) | ||
const [redFn] = args.slice(-1) | ||
|
||
class ReadingFunction extends ExtensibleFunction { | ||
constructor(fn, depKeys=[]) { | ||
super(fn); | ||
this.depKeys = depKeys; | ||
} | ||
return ReducerArrow.create(redFn, keys) | ||
} | ||
|
||
export function reads(...args) { | ||
const depKeys = args.slice(0, -1); | ||
const fn = args[args.length - 1]; | ||
export function combineArrows(arrowsReducersHash) { | ||
const arrowsHash = normalizeArrows(arrowsReducersHash) | ||
|
||
return new ReadingFunction(depKeys, fn); | ||
return ReducerArrow.create((state, action) => { | ||
const keys = Object.keys(arrowsHash) | ||
const actionState = { state, action } | ||
const nextState = {} | ||
let hasChanged = false | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i] | ||
const arrow = arrowsHash[key] | ||
const { | ||
state: nextStateForKey, | ||
meta: { changedKeys } | ||
} = ReducerArrow.runKleisli(arrow, actionState) | ||
|
||
if (isBlank(nextStateForKey)) { | ||
throw 'undefined state error' | ||
} | ||
nextState[key] = nextStateForKey | ||
hasChanged = changedKeys.length > 0 | ||
} | ||
return hasChanged ? nextState : state | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import ReduxArrow from './redux-arrow'; | ||
|
||
const enum CombinableType { | ||
reducer, | ||
arrow, | ||
unknown | ||
} | ||
|
||
function typeOf(x: any): CombinableType { | ||
if (ReduxArrow.isArrow(x)) { | ||
return CombinableType.arrow; | ||
} | ||
|
||
if (typeof x === 'function') { | ||
return CombinableType.reducer; | ||
} | ||
|
||
return CombinableType.unknown; | ||
} | ||
|
||
const NORMALIZERS_BY_TYPE: TypedNormalizers = { | ||
[CombinableType.reducer](reducer) { | ||
return ReduxArrow.arr(reducer) | ||
}, | ||
[CombinableType.arrow](arrow) { | ||
return arrow | ||
}, | ||
[CombinableType.unknown]() { | ||
return ReduxArrow.id | ||
} | ||
} | ||
|
||
interface ArrowMaker { | ||
(x: any): ReduxArrow | ||
} | ||
|
||
interface TypedNormalizers { | ||
[propName: number]: ArrowMaker | ||
} | ||
|
||
interface ArrowsHash { | ||
[propName: string]: ReduxArrow | ||
} | ||
|
||
interface UserInputHash { | ||
[propName: string]: any | ||
} | ||
|
||
export default function normalizeArrows(arrowsReducersHash: UserInputHash): ArrowsHash { | ||
const keys = Object.keys(arrowsReducersHash); | ||
const arrows = {}; | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const thing = arrowsReducersHash[key]; | ||
|
||
arrows[key] = NORMALIZERS_BY_TYPE[typeOf(thing)](thing); | ||
} | ||
return arrows; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Tuple } from './utils'; | ||
import { Action, ReduxState } from './redux'; | ||
|
||
export type Keys = string[] | ||
export const enum ReducerStatusCode { | ||
ok, unused | ||
} | ||
|
||
export interface ReducerMeta { | ||
changedKeys: Keys | ||
statusCode: ReducerStatusCode | ||
} | ||
|
||
export interface ActionState { | ||
action: Action; | ||
state: ReduxState; | ||
} | ||
|
||
export interface ReducerExt { | ||
(actionState: ActionState): Tuple<ReduxState, ReducerMeta> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
export type Action = any; | ||
export type ReduxState = any; | ||
|
||
export interface ActionState { | ||
action: Action; | ||
state: ReduxState; | ||
} | ||
|
||
export interface Reducer { | ||
(state: ReduxState, action: Action): ReduxState | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters