Comparing version 6.1.0 to 6.2.0
CHANGELOG | ||
=== | ||
6.2.0 | ||
-- | ||
New Features | ||
- | ||
Defined CursorType for supporting of functional cursors. | ||
CursorType<TState extends IState> = (() => ICursor<TState>) | ICursor<TState> | ||
5.0.0 | ||
@@ -4,0 +13,0 @@ -- |
{ | ||
"name": "fun-model", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "fun-model is pure functional implementation of FLUX architecture.", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -11,5 +11,5 @@ import * as s from './store'; | ||
export declare type IParamLessActionHandler<TState> = (state: TState) => TState; | ||
export declare const createAction: <TState, TParams>(cursor: s.ICursor<TState> | s.ICursorFactory<TState, TParams>, handler: IActionHandler<TState, TParams>) => IAction<TParams>; | ||
export declare const createReplaceAction: <TState>(cursor: s.ICursor<TState> | s.ICursorFactory<TState, TState>) => IAction<TState>; | ||
export declare const createParamLessAction: <TState>(cursor: s.ICursor<TState>, handler: IParamLessActionHandler<TState>) => IParamLessAction; | ||
export declare const createAction: <TState, TParams>(cursor: s.ICursor<TState> | (() => s.ICursor<TState>) | s.ICursorFactory<TState, TParams>, handler: IActionHandler<TState, TParams>) => IAction<TParams>; | ||
export declare const createReplaceAction: <TState>(cursor: s.ICursor<TState> | (() => s.ICursor<TState>) | s.ICursorFactory<TState, TState>) => IAction<TState>; | ||
export declare const createParamLessAction: <TState>(cursor: s.ICursor<TState> | (() => s.ICursor<TState>) | s.ICursorFactory<TState, TState>, handler: IParamLessActionHandler<TState>) => IParamLessAction; | ||
export interface IPair<TState, TParam> { | ||
@@ -16,0 +16,0 @@ cursor: s.ICursor<TState>; |
@@ -33,3 +33,3 @@ "use strict"; | ||
throw renderCallbackMustBeSetBefore; | ||
if (changeStateWithQueue(cursor, handler)) { | ||
if (changeStateWithQueue(unifyCursor(cursor, null), handler)) { | ||
stateChanged(); | ||
@@ -41,3 +41,5 @@ d.log('Rendering invoked...'); | ||
function unifyCursor(cursor, params) { | ||
return cursor.create instanceof Function ? cursor.create(params) : cursor; | ||
return cursor.create instanceof Function | ||
? cursor.create(params) | ||
: (s.isCursorFunction(cursor) ? cursor() : cursor); | ||
} | ||
@@ -44,0 +46,0 @@ exports.createActions = function () { |
@@ -31,3 +31,3 @@ import * as s from './store'; | ||
export const createAction = <TState, TParams>(cursor: s.ICursor<TState> | s.ICursorFactory<TState, TParams>, handler: IActionHandler<TState, TParams>) | ||
export const createAction = <TState, TParams>(cursor: s.CursorType<TState> | s.ICursorFactory<TState, TParams>, handler: IActionHandler<TState, TParams>) | ||
: IAction<TParams> => { | ||
@@ -37,4 +37,4 @@ return (params: TParams): void => { | ||
throw renderCallbackMustBeSetBefore; | ||
if (changeStateWithQueue(unifyCursor<TState, TParams>(cursor, params), (state) => handler(state, params))) { | ||
if (changeStateWithQueue(unifyCursor(cursor, params), (state) => handler(state, params))) { | ||
stateChanged(); | ||
@@ -46,3 +46,3 @@ d.log('Rendering invoked...'); | ||
export const createReplaceAction = <TState>(cursor: s.ICursor<TState> | s.ICursorFactory<TState, TState>) | ||
export const createReplaceAction = <TState>(cursor: s.CursorType<TState> | s.ICursorFactory<TState, TState>) | ||
: IAction<TState> => { | ||
@@ -52,3 +52,3 @@ return createAction(cursor, (_state, params) => params); | ||
export const createParamLessAction = <TState>(cursor: s.ICursor<TState>, handler: IParamLessActionHandler<TState>) | ||
export const createParamLessAction = <TState>(cursor: s.CursorType<TState> | s.ICursorFactory<TState, TState>, handler: IParamLessActionHandler<TState>) | ||
: IParamLessAction => { | ||
@@ -59,3 +59,3 @@ return (): void => { | ||
if (changeStateWithQueue(cursor, handler)) { | ||
if (changeStateWithQueue(unifyCursor(cursor, null), handler)) { | ||
stateChanged(); | ||
@@ -67,4 +67,6 @@ d.log('Rendering invoked...'); | ||
function unifyCursor<TState, TParams>(cursor: s.ICursor<TState> | s.ICursorFactory<TState, TParams>, params: TParams): s.ICursor<TState> { | ||
return (<s.ICursorFactory<TState, TParams>>cursor).create instanceof Function ? (<s.ICursorFactory<TState, TParams>>cursor).create(params) : <s.ICursor<TState>>cursor; | ||
function unifyCursor<TState, TParams>(cursor: s.CursorType<TState> | s.ICursorFactory<TState, TParams>, params: TParams): s.ICursor<TState> { | ||
return (<any>cursor).create instanceof Function | ||
? <s.ICursor<TState>>(<any>cursor).create(params) | ||
: <s.ICursor<TState>>(s.isCursorFunction(<s.CursorType<TState>>cursor) ? (<() => s.ICursor<TState>>cursor)() : cursor); | ||
} | ||
@@ -71,0 +73,0 @@ |
export interface IState { | ||
} | ||
export declare type CursorType<TState extends IState> = (() => ICursor<TState>) | ICursor<TState>; | ||
export declare function createNestedCursor<TState extends IState, TNestedState extends IState>(cursor: CursorType<TState>, nestedStateKey: string): CursorType<TNestedState>; | ||
export declare function isCursorFunction<TState extends IState>(cursor: CursorType<TState>): cursor is () => ICursor<TState>; | ||
export interface ICursor<TState> { | ||
@@ -13,4 +16,4 @@ key: string; | ||
export declare const bootstrap: (defaultState: IState | null, withStateFreezing?: boolean | (() => boolean), subStateSeparator?: string) => void; | ||
export declare const isExistingCursor: <TState>(cursor: ICursor<TState>) => boolean; | ||
export declare const getState: <TState>(cursor: ICursor<TState>) => TState; | ||
export declare const setState: <TState>(cursor: ICursor<TState>, updatedState: TState, canCreateObjectsOnPath?: boolean) => void; | ||
export declare const isExistingCursor: <TState>(cursor: CursorType<TState>) => boolean; | ||
export declare const getState: <TState>(cursor: CursorType<TState>) => TState; | ||
export declare const setState: <TState>(cursor: CursorType<TState>, updatedState: TState, canCreateObjectsOnPath?: boolean) => void; |
@@ -5,2 +5,13 @@ "use strict"; | ||
var d = require("./debug"); | ||
function createNestedCursor(cursor, nestedStateKey) { | ||
if (isCursorFunction(cursor)) | ||
return function () { return { key: cursor().key + stateSeparator + nestedStateKey }; }; | ||
else | ||
return { key: cursor.key + stateSeparator + nestedStateKey }; | ||
} | ||
exports.createNestedCursor = createNestedCursor; | ||
function isCursorFunction(cursor) { | ||
return typeof cursor == "function"; | ||
} | ||
exports.isCursorFunction = isCursorFunction; | ||
var state = null; | ||
@@ -21,2 +32,3 @@ var stateSeparator = '.'; | ||
exports.isExistingCursor = function (cursor) { | ||
cursor = isCursorFunction(cursor) ? cursor() : cursor; | ||
var hasExistingInnerStateInArray = function (innerState, path) { | ||
@@ -48,2 +60,3 @@ var index = Number(path.shift()); | ||
exports.getState = function (cursor) { | ||
var cursorValue = isCursorFunction(cursor) ? cursor() : cursor; | ||
var getInnerState = function (innerState, path) { | ||
@@ -55,3 +68,3 @@ if (path.length === 0) | ||
return innerState; | ||
checkSubstate(innerState, subPath, path, cursor); | ||
checkSubstate(innerState, subPath, path, cursorValue); | ||
var prop = innerState[subPath]; | ||
@@ -62,10 +75,11 @@ return Array.isArray(prop) && path.length > 0 | ||
}; | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursor)) | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursorValue)) | ||
throw 'Invalid operation.'; | ||
return (cursor.key === rootStateKey | ||
return (cursorValue.key === rootStateKey | ||
? state | ||
: getInnerState(state, cursor.key.split(stateSeparator))); | ||
: getInnerState(state, cursorValue.key.split(stateSeparator))); | ||
}; | ||
exports.setState = function (cursor, updatedState, canCreateObjectsOnPath) { | ||
if (canCreateObjectsOnPath === void 0) { canCreateObjectsOnPath = false; } | ||
var cursorValue = isCursorFunction(cursor) ? cursor() : cursor; | ||
var setInnerState = function (innerState, path) { | ||
@@ -80,3 +94,3 @@ if (path.length === 0) | ||
else | ||
checkSubstate(innerState, subPath, path, cursor); | ||
checkSubstate(innerState, subPath, path, cursorValue); | ||
var prop = innerState[subPath]; | ||
@@ -97,8 +111,8 @@ var newSubState = null; | ||
}; | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursor)) | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursorValue)) | ||
throw 'Invalid operation.'; | ||
state = | ||
cursor.key === rootStateKey | ||
cursorValue.key === rootStateKey | ||
? updatedState | ||
: setInnerState(state, cursor.key.split(stateSeparator)); | ||
: setInnerState(state, cursorValue.key.split(stateSeparator)); | ||
if (h.isFunction(freezing) ? freezing() : freezing) | ||
@@ -105,0 +119,0 @@ h.deepFreeze(state); |
@@ -7,2 +7,14 @@ import * as h from './helpers'; | ||
export type CursorType<TState extends IState> = (() => ICursor<TState>) | ICursor<TState>; | ||
export function createNestedCursor<TState extends IState, TNestedState extends IState>(cursor: CursorType<TState>, nestedStateKey: string): CursorType<TNestedState> { | ||
if (isCursorFunction(cursor)) | ||
return () => { return { key: cursor().key + stateSeparator + nestedStateKey } } | ||
else | ||
return { key: cursor.key + stateSeparator + nestedStateKey }; | ||
} | ||
export function isCursorFunction<TState extends IState>(cursor: CursorType<TState>): cursor is () => ICursor<TState> { | ||
return typeof cursor == "function"; | ||
} | ||
export interface ICursor<TState> { | ||
@@ -33,3 +45,4 @@ key: string; | ||
export const isExistingCursor = <TState>(cursor: ICursor<TState>): boolean => { | ||
export const isExistingCursor = <TState>(cursor: CursorType<TState>): boolean => { | ||
cursor = isCursorFunction(cursor) ? cursor() : cursor; | ||
const hasExistingInnerStateInArray = (innerState: IState[], path: string[]): boolean => { | ||
@@ -63,3 +76,4 @@ const index = Number(path.shift()); | ||
export const getState = <TState>(cursor: ICursor<TState>): TState => { | ||
export const getState = <TState>(cursor: CursorType<TState>): TState => { | ||
const cursorValue = isCursorFunction(cursor) ? cursor() : cursor; | ||
const getInnerState = (innerState: IState, path: string[]): IState => { | ||
@@ -71,3 +85,3 @@ if (path.length === 0) | ||
return innerState; | ||
checkSubstate(innerState, subPath, path, cursor); | ||
checkSubstate(innerState, subPath, path, cursorValue); | ||
const prop = (<any>innerState)[subPath]; | ||
@@ -79,11 +93,12 @@ return Array.isArray(prop) && path.length > 0 | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursor)) | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursorValue)) | ||
throw 'Invalid operation.'; | ||
return <TState>(cursor.key === rootStateKey | ||
return <TState>(cursorValue.key === rootStateKey | ||
? state | ||
: getInnerState(state, cursor.key.split(stateSeparator))); | ||
: getInnerState(state, cursorValue.key.split(stateSeparator))); | ||
}; | ||
export const setState = <TState>(cursor: ICursor<TState>, updatedState: TState, canCreateObjectsOnPath = false) => { | ||
export const setState = <TState>(cursor: CursorType<TState>, updatedState: TState, canCreateObjectsOnPath = false) => { | ||
const cursorValue = isCursorFunction(cursor) ? cursor() : cursor; | ||
const setInnerState = <TInnerState>(innerState: TInnerState, path: string[]): TInnerState => { | ||
@@ -99,3 +114,3 @@ if (path.length === 0) | ||
else | ||
checkSubstate(innerState, subPath, path, cursor); | ||
checkSubstate(innerState, subPath, path, cursorValue); | ||
const prop = (<any>innerState)[subPath]; | ||
@@ -119,9 +134,9 @@ let newSubState: Object | Array<IState> | null = null; | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursor)) | ||
if (!isSetDefaultState(state) || !isValidCursorKey(cursorValue)) | ||
throw 'Invalid operation.'; | ||
state = | ||
cursor.key === rootStateKey | ||
cursorValue.key === rootStateKey | ||
? updatedState | ||
: setInnerState(state, cursor.key.split(stateSeparator)); | ||
: setInnerState(state, cursorValue.key.split(stateSeparator)); | ||
if (h.isFunction(freezing) ? freezing() : freezing) | ||
@@ -128,0 +143,0 @@ h.deepFreeze(state); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
248310
1973