Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
efec897f
Commit
efec897f
authored
Feb 01, 2019
by
Hugo Häggmark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed unused factory and fixed index based mapper lookup
parent
7e64ee82
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
100 deletions
+8
-100
public/app/core/redux/reducerFactory.ts
+8
-100
No files found.
public/app/core/redux/reducerFactory.ts
View file @
efec897f
import
{
ActionOf
,
ActionCreator
,
actionCreatorFactory
}
from
'./actionCreatorFactory'
;
import
{
ActionOf
,
ActionCreator
}
from
'./actionCreatorFactory'
;
import
{
Reducer
}
from
'redux'
;
import
{
Reducer
}
from
'redux'
;
export
type
Mapper
<
State
,
Payload
>
=
(
state
:
State
,
action
:
ActionOf
<
Payload
>
)
=>
State
;
export
type
Mapper
<
State
,
Payload
>
=
(
state
:
State
,
action
:
ActionOf
<
Payload
>
)
=>
State
;
...
@@ -17,23 +17,23 @@ export interface CreateReducer<State> extends AddMapper<State> {
...
@@ -17,23 +17,23 @@ export interface CreateReducer<State> extends AddMapper<State> {
}
}
export
const
reducerFactory
=
<
State
>
(
initialState
:
State
):
AddMapper
<
State
>
=>
{
export
const
reducerFactory
=
<
State
>
(
initialState
:
State
):
AddMapper
<
State
>
=>
{
const
allMapper
Configs
:
Array
<
MapperConfig
<
State
,
any
>>
=
[]
;
const
allMapper
s
:
{
[
key
:
string
]:
Mapper
<
State
,
any
>
}
=
{}
;
const
addMapper
=
<
Payload
>
(
config
:
MapperConfig
<
State
,
Payload
>
):
CreateReducer
<
State
>
=>
{
const
addMapper
=
<
Payload
>
(
config
:
MapperConfig
<
State
,
Payload
>
):
CreateReducer
<
State
>
=>
{
if
(
allMapper
Configs
.
some
(
c
=>
c
.
filter
.
type
===
config
.
filter
.
type
)
)
{
if
(
allMapper
s
[
config
.
filter
.
type
]
)
{
throw
new
Error
(
`There is already a
Mappers
defined with the type
${
config
.
filter
.
type
}
`
);
throw
new
Error
(
`There is already a
mapper
defined with the type
${
config
.
filter
.
type
}
`
);
}
}
allMapper
Configs
.
push
(
config
)
;
allMapper
s
[
config
.
filter
.
type
]
=
config
.
mapper
;
return
instance
;
return
instance
;
};
};
const
create
=
():
Reducer
<
State
,
ActionOf
<
any
>>
=>
(
state
:
State
=
initialState
,
action
:
ActionOf
<
any
>
):
State
=>
{
const
create
=
():
Reducer
<
State
,
ActionOf
<
any
>>
=>
(
state
:
State
=
initialState
,
action
:
ActionOf
<
any
>
):
State
=>
{
const
mapper
Config
=
allMapperConfigs
.
filter
(
config
=>
config
.
filter
.
type
===
action
.
type
)[
0
];
const
mapper
=
allMappers
[
action
.
type
];
if
(
mapper
Config
)
{
if
(
mapper
)
{
return
mapper
Config
.
mapper
(
state
,
action
);
return
mapper
(
state
,
action
);
}
}
return
state
;
return
state
;
...
@@ -43,95 +43,3 @@ export const reducerFactory = <State>(initialState: State): AddMapper<State> =>
...
@@ -43,95 +43,3 @@ export const reducerFactory = <State>(initialState: State): AddMapper<State> =>
return
instance
;
return
instance
;
};
};
/** Another type of fluent reducerFactory */
export
interface
FilterWith
<
State
>
{
filterWith
:
<
Payload
>
(
actionCreator
:
ActionCreator
<
Payload
>
)
=>
MapTo
<
State
,
Payload
>
;
}
export
interface
OrFilterWith
<
State
>
{
orFilterWith
:
<
Payload
>
(
actionCreator
:
ActionCreator
<
Payload
>
)
=>
MapTo
<
State
,
Payload
>
;
}
export
interface
MapTo
<
State
,
Payload
>
{
mapTo
:
(
mapper
:
Mapper
<
State
,
Payload
>
)
=>
CreateReducerEx
<
State
>
;
}
export
interface
CreateReducerEx
<
State
>
extends
OrFilterWith
<
State
>
{
create
:
()
=>
Reducer
<
State
,
ActionOf
<
any
>>
;
}
export
const
reducerFactoryEx
=
<
State
>
(
initialState
:
State
):
FilterWith
<
State
>
=>
{
const
allMapperConfigs
:
Array
<
MapperConfig
<
State
,
any
>>
=
[];
const
innerMapTo
=
(
actionCreator
:
ActionCreator
<
any
>
,
mapper
:
Mapper
<
State
,
any
>
):
CreateReducerEx
<
State
>
=>
{
allMapperConfigs
.
filter
(
config
=>
config
.
filter
.
type
===
actionCreator
.
type
)[
0
].
mapper
=
mapper
;
return
instance
;
};
const
filterWith
=
<
Payload
>
(
actionCreator
:
ActionCreator
<
Payload
>
):
MapTo
<
State
,
Payload
>
=>
{
if
(
allMapperConfigs
.
some
(
c
=>
c
.
filter
.
type
===
actionCreator
.
type
))
{
throw
new
Error
(
`There is already a mapper defined with the type
${
actionCreator
.
type
}
`
);
}
allMapperConfigs
.
push
({
filter
:
actionCreator
,
mapper
:
null
});
const
mapTo
=
<
Payload
>
(
mapper
:
Mapper
<
State
,
Payload
>
):
CreateReducerEx
<
State
>
=>
{
innerMapTo
(
actionCreator
,
mapper
);
return
instance
;
};
return
{
mapTo
};
};
const
orFilterWith
=
<
Payload
>
(
actionCreator
:
ActionCreator
<
Payload
>
):
MapTo
<
State
,
Payload
>
=>
{
if
(
allMapperConfigs
.
some
(
c
=>
c
.
filter
.
type
===
actionCreator
.
type
))
{
throw
new
Error
(
`There is already a mapper defined with the type
${
actionCreator
.
type
}
`
);
}
allMapperConfigs
.
push
({
filter
:
actionCreator
,
mapper
:
null
});
const
mapTo
=
<
Payload
>
(
mapper
:
Mapper
<
State
,
Payload
>
):
CreateReducerEx
<
State
>
=>
{
innerMapTo
(
actionCreator
,
mapper
);
return
instance
;
};
return
{
mapTo
};
};
const
create
=
():
Reducer
<
State
,
ActionOf
<
any
>>
=>
(
state
:
State
=
initialState
,
action
:
ActionOf
<
any
>
):
State
=>
{
const
mapperConfig
=
allMapperConfigs
.
filter
(
config
=>
config
.
filter
.
type
===
action
.
type
)[
0
];
if
(
mapperConfig
)
{
return
mapperConfig
.
mapper
(
state
,
action
);
}
return
state
;
};
const
instance
=
{
filterWith
,
orFilterWith
,
create
};
return
instance
;
};
interface
TestState
{
data
:
string
[];
}
const
initialState
:
TestState
=
{
data
:
[],
};
const
dummyActionCreator
=
actionCreatorFactory
<
string
>
(
'dummyActionCreator'
).
create
();
const
dummyActionCreator2
=
actionCreatorFactory
<
number
>
(
'dummyActionCreator2'
).
create
();
export
const
reducerFactoryExReducer
=
reducerFactoryEx
<
TestState
>
(
initialState
)
.
filterWith
(
dummyActionCreator
)
.
mapTo
((
state
,
action
)
=>
({
...
state
,
data
:
state
.
data
.
concat
(
action
.
payload
)
}))
.
orFilterWith
(
dummyActionCreator2
)
.
mapTo
((
state
,
action
)
=>
({
...
state
,
data
:
state
.
data
.
concat
(
`
${
action
.
payload
}
`
)
}))
.
create
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment