Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
cordis-decorators
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
cordis-decorators
Commits
6e4df9a2
Commit
6e4df9a2
authored
Jul 20, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rework typing system
parent
d2b7c7cb
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
207 additions
and
194 deletions
+207
-194
package-lock.json
package-lock.json
+2
-2
src/def/interfaces.ts
src/def/interfaces.ts
+12
-19
src/plugin-def/index.ts
src/plugin-def/index.ts
+1
-2
src/registrar-aspect.ts
src/registrar-aspect.ts
+27
-15
src/registrar.ts
src/registrar.ts
+143
-144
src/utility/register-meta.ts
src/utility/register-meta.ts
+10
-0
tests/service-activate.spec.ts
tests/service-activate.spec.ts
+12
-12
No files found.
package-lock.json
View file @
6e4df9a2
{
"name"
:
"
myproject
"
,
"name"
:
"
cordis-decorators
"
,
"version"
:
"1.0.7"
,
"lockfileVersion"
:
2
,
"requires"
:
true
,
"packages"
:
{
""
:
{
"name"
:
"
myproject
"
,
"name"
:
"
cordis-decorators
"
,
"version"
:
"1.0.7"
,
"license"
:
"MIT"
,
"dependencies"
:
{
...
...
src/def/interfaces.ts
View file @
6e4df9a2
import
{
PluginRegistrar
}
from
'
../plugin-def
'
;
import
PluginClass
=
PluginRegistrar
.
PluginClass
;
import
{
Context
}
from
'
cordis
'
;
export
type
Awaitable
<
T
>
=
[
T
]
extends
[
Promise
<
unknown
>
]
?
T
:
T
|
Promise
<
T
>
;
export
type
TypedMethodDecorator
<
F
extends
(...
args
:
any
[])
=>
any
>
=
<
...
...
@@ -13,23 +11,6 @@ export type TypedMethodDecorator<F extends (...args: any[]) => any> = <
descriptor
:
TypedPropertyDescriptor
<
T
>
,
)
=>
void
;
export
type
FunctionParam
<
F
extends
(...
args
:
any
[])
=>
any
>
=
F
extends
(
...
args
:
infer
R
)
=>
any
?
R
:
never
;
export
type
FunctionReturn
<
F
extends
(...
args
:
any
[])
=>
any
>
=
F
extends
(
...
args
:
any
[]
)
=>
infer
R
?
R
:
never
;
export
type
PickEventFunction
<
M
,
K
extends
keyof
M
=
keyof
M
>
=
M
[
K
]
extends
(
...
args
:
any
[]
)
=>
any
?
(...
args
:
FunctionParam
<
M
[
K
]
>
)
=>
Awaitable
<
FunctionReturn
<
M
[
K
]
>>
:
M
[
K
];
export
type
ParamRenderer
=
<
T
>
(
v
:
T
)
=>
T
;
export
interface
ControlTypeMap
{
...
...
@@ -57,6 +38,18 @@ export type ParamsFromClass<T> = T extends { new (...args: infer U): any }
?
U
:
never
;
export
type
FunctionParams
<
F
extends
(...
args
:
any
[])
=>
any
>
=
F
extends
(
...
args
:
infer
U
)
=>
any
?
U
:
never
;
export
type
FunctionReturn
<
F
extends
(...
args
:
any
[])
=>
any
>
=
F
extends
(
...
args
:
any
)
=>
infer
U
?
U
:
never
;
export
type
Prop
<
T
>
=
T
;
export
type
PartialDeep
<
T
>
=
T
extends
...
...
src/plugin-def/index.ts
View file @
6e4df9a2
import
{
Context
,
Plugin
}
from
'
cordis
'
;
import
Schema
from
'
schemastery
'
;
import
{
ClassType
}
from
'
schemastery-gen
'
;
import
type
{
Registrar
}
from
'
../registrar
'
;
// eslint-disable-next-line @typescript-eslint/no-namespace
export
namespace
PluginRegistrar
{
...
...
@@ -20,7 +19,7 @@ export namespace PluginRegistrar {
name
?:
string
;
schema
?:
Schema
<
T
>
|
ClassType
<
T
>
;
Config
?:
Schema
<
T
>
|
ClassType
<
T
>
;
using
?:
Registrar
.
ServiceName
<
Ctx
>
[];
using
?:
string
[];
reusable
?:
boolean
;
}
...
...
src/registrar-aspect.ts
View file @
6e4df9a2
import
{
Context
}
from
'
cordis
'
;
import
{
Registrar
}
from
'
./registrar
'
;
import
{
generateRenderer
,
renderObject
}
from
'
./utility/render-object
'
;
import
{
generateRenderer
}
from
'
./utility/render-object
'
;
import
{
extractObjectMethod
}
from
'
./utility/utility
'
;
import
{
ControlType
}
from
'
./def
'
;
import
{
...
...
@@ -9,13 +9,10 @@ import {
Observable
,
ObservableInput
,
ObservedValueOf
,
of
,
}
from
'
rxjs
'
;
import
_
from
'
lodash
'
;
type
RecursiveUnwrapObservable
<
T
>
=
T
extends
ObservableInput
<
any
>
?
RecursiveUnwrapObservable
<
ObservedValueOf
<
T
>>
:
T
;
import
ContextTransformer
=
Registrar
.
ContextTransformer
;
import
{
RegisterMeta
}
from
'
./utility/register-meta
'
;
export
class
RegistrarAspect
<
Ctx
extends
Context
,
T
=
any
>
{
constructor
(
...
...
@@ -45,8 +42,11 @@ export class RegistrarAspect<Ctx extends Context, T = any> {
this
.
obj
,
key
,
);
const
r
=
generateRenderer
({
...
this
.
view
,
...
extraView
});
return
this
.
registrar
.
transformContext
(
ctx
,
contextFilters
,
r
);
return
this
.
registrar
.
transformContext
(
ctx
,
contextFilters
as
RegisterMeta
<
ContextTransformer
<
Ctx
,
any
[]
>>
[],
{
...
this
.
view
,
...
extraView
},
);
}
registerMethod
(
...
...
@@ -57,12 +57,18 @@ export class RegistrarAspect<Ctx extends Context, T = any> {
const
data
=
this
.
registrar
.
reflector
.
get
(
'
CordisRegister
'
,
this
.
obj
,
key
);
if
(
!
data
)
return
;
const
specificCtx
=
this
.
getScopeContext
(
ctx
,
key
,
extraView
,
false
);
const
result
=
data
.
action
(
const
result
=
data
.
run
(
{
...
this
.
view
,
...
extraView
},
specificCtx
,
extractObjectMethod
(
this
.
obj
,
key
),
...
renderObject
(
data
.
args
,
{
...
this
.
view
,
...
extraView
}),
);
return
{
...
data
,
key
:
key
,
result
,
ctx
:
specificCtx
};
return
{
...
data
,
type
:
data
.
info
.
type
,
key
:
key
,
result
,
ctx
:
specificCtx
,
};
}
private
registerWithLoopControl
(
...
...
@@ -98,7 +104,7 @@ export class RegistrarAspect<Ctx extends Context, T = any> {
private
runLayersWith
<
R
extends
ObservableInput
<
any
>>
(
ctx
:
Ctx
,
cb
:
Registrar
.
ContextFunction
<
Ctx
,
R
>
,
layers
:
Regist
rar
.
ContextCallbackLayer
<
Ctx
>
[],
layers
:
Regist
erMeta
<
Registrar
.
ContextCallbackLayer
<
Ctx
>
>
[],
):
Observable
<
ObservedValueOf
<
R
>>
{
const
rest
=
[...
layers
];
const
layer
=
rest
.
pop
();
...
...
@@ -106,7 +112,7 @@ export class RegistrarAspect<Ctx extends Context, T = any> {
return
from
(
cb
(
ctx
));
}
return
new
Observable
((
subscriber
)
=>
{
layer
(
ctx
,
async
(
nextCtx
)
=>
{
layer
.
run
(
this
.
view
,
ctx
,
async
(
nextCtx
)
=>
{
if
(
!
rest
.
length
)
{
const
result
=
cb
(
nextCtx
);
if
(
result
)
{
...
...
@@ -134,7 +140,11 @@ export class RegistrarAspect<Ctx extends Context, T = any> {
this
.
obj
,
key
as
string
,
);
return
this
.
runLayersWith
(
ctx
,
cb
,
layers
);
return
this
.
runLayersWith
(
ctx
,
cb
,
layers
as
RegisterMeta
<
Registrar
.
ContextCallbackLayer
<
Ctx
>>
[],
);
}
registerFor
(
ctx
:
Ctx
,
key
:
keyof
T
&
string
)
{
...
...
@@ -169,6 +179,8 @@ export class RegistrarAspect<Ctx extends Context, T = any> {
this
.
registrar
.
reflector
.
getArray
(
'
CordisTopLevelAction
'
,
this
.
obj
),
);
const
renderer
=
generateRenderer
({
...
this
.
view
,
...
extraView
});
actions
.
forEach
((
action
)
=>
action
(
ctx
,
this
.
obj
,
renderer
));
actions
.
forEach
((
action
)
=>
action
.
run
({
...
this
.
view
,
...
extraView
},
ctx
,
this
.
obj
,
renderer
),
);
}
}
src/registrar.ts
View file @
6e4df9a2
import
'
reflect-metadata
'
;
import
{
Context
,
Events
,
Fork
}
from
'
cordis
'
;
import
{
Context
,
GetEvents
}
from
'
cordis
'
;
import
{
ClassType
,
SchemaClass
}
from
'
schemastery-gen
'
;
import
{
MetadataSetter
,
Reflector
}
from
'
typed-reflector
'
;
import
type
{
Awaitable
,
Condition
,
ControlType
,
FunctionParam
,
FunctionReturn
,
ParamRenderer
,
PartialDeep
,
PickEventFunction
,
TypedMethodDecorator
,
}
from
'
./def
'
;
import
{
RegistrarAspect
}
from
'
./registrar-aspect
'
;
import
Schema
from
'
schemastery
'
;
import
{
PluginRegistrar
}
from
'
./plugin-def
'
;
import
_
from
'
lodash
'
;
import
{
RegisterMeta
}
from
'
./utility/register-meta
'
;
declare
module
'
cordis
'
{
interface
Context
{
...
...
@@ -26,119 +23,100 @@ declare module 'cordis' {
// eslint-disable-next-line @typescript-eslint/no-namespace
export
namespace
Registrar
{
export
interface
Methods
<
Ctx
extends
Context
>
{
on
(
event
:
keyof
Events
<
Ctx
>
,
prepend
?:
boolean
):
()
=>
boolean
;
plugin
():
Awaitable
<
Fork
<
Ctx
>>
;
apply
():
Awaitable
<
any
>
;
export
interface
MetadataArrayMap
{
CordisRegisterKeys
:
string
;
CordisContextTransformer
:
RegisterMeta
<
ContextTransformer
<
Context
>>
;
CordisTopLevelAction
:
RegisterMeta
<
TopLevelActionDef
<
Context
>>
;
CordisContextLayers
:
RegisterMeta
<
ContextCallbackLayer
<
Context
>>
;
CordisControl
:
ControlType
;
CordisPluginUsing
:
string
;
CordisPluginProvide
:
ProvideDefinition
<
Context
>
;
CordisPluginInjectKeys
:
string
;
CordisPluginSystemKeys
:
string
;
}
export
interface
MethodLimitations
<
Ctx
extends
Context
>
{
on
:
PickEventFunction
<
Events
<
Ctx
>>
;
plugin
(
...
args
:
any
[]
):
Awaitable
<
PluginRegistrar
.
PluginDefinition
<
Ctx
>
|
undefined
>
;
export
interface
MetadataMap
{
CordisRegister
:
MethodMeta
<
Context
>
;
CordisPluginInject
:
string
;
CordisPluginSystem
:
PluginRegistrar
.
SystemInjectFun
<
Context
>
;
CordisPluginPredefineSchema
:
Schema
|
ClassType
<
any
>
;
CordisPluginPredefineName
:
string
;
CordisPluginFork
:
PluginRegistrar
.
PluginClass
<
Context
>
;
CordisPluginReusable
:
boolean
;
}
export
type
MethodType
<
Ctx
extends
Context
>
=
keyof
Methods
<
Ctx
>
;
export
type
MethodBody
<
C
extends
Context
,
K
extends
keyof
Methods
<
C
>
=
keyof
Methods
<
C
>
,
>
=
Methods
<
C
>
[
K
];
export
type
MethodParams
<
C
extends
Context
,
K
extends
keyof
Methods
<
C
>
=
keyof
Methods
<
C
>
,
>
=
FunctionParam
<
MethodBody
<
C
,
K
>>
;
export
type
MethodReturn
<
C
extends
Context
,
K
extends
keyof
Methods
<
C
>
=
keyof
Methods
<
C
>
,
>
=
FunctionReturn
<
MethodBody
<
C
,
K
>>
;
export
type
MethodClassMethodFunction
<
C
extends
Context
,
K
extends
keyof
Methods
<
C
>
=
keyof
Methods
<
C
>
,
>
=
K
extends
keyof
MethodLimitations
<
C
>
?
MethodLimitations
<
C
>
[
K
]
:
(...
args
:
any
[])
=>
any
;
export
type
MethodResolver
<
C
extends
Context
,
K
extends
keyof
Methods
<
C
>
=
keyof
Methods
<
C
>
,
>
=
(
ctx
:
C
,
fun
:
MethodClassMethodFunction
<
C
,
K
>
,
...
args
:
MethodParams
<
C
,
K
>
)
=>
MethodReturn
<
C
,
K
>
;
export
interface
RegisterInfo
<
C
extends
Context
>
{
type
:
MethodType
<
C
>
;
args
:
MethodParams
<
C
>
;
action
:
MethodResolver
<
C
>
;
}
export
type
DecorateFunctionParam
<
Ctx
extends
Context
,
A
extends
any
[]
=
any
[],
R
=
any
,
>
=
(
ctx
:
Ctx
,
...
args
:
A
)
=>
R
;
export
interface
RegisterResult
<
C
extends
Context
,
T
,
K
extends
keyof
Methods
<
C
>
=
keyof
Methods
<
C
>
,
>
extends
RegisterInfo
<
C
>
{
export
type
DecorateFunctionParamSingle
<
Ctx
extends
Context
,
P
,
A
extends
any
[]
=
any
[],
R
=
any
,
>
=
(
ctx
:
Ctx
,
param
:
P
,
...
args
:
A
)
=>
R
;
export
type
MethodResolver
<
Ctx
extends
Context
,
A
extends
any
[]
=
any
[],
F
extends
(...
args
:
any
[])
=>
any
=
(...
args
:
any
[])
=>
any
,
>
=
DecorateFunctionParamSingle
<
Ctx
,
F
,
A
>
;
export
type
MethodMeta
<
Ctx
extends
Context
>
=
RegisterMeta
<
MethodResolver
<
Ctx
>
,
{
type
:
string
}
>
;
export
type
RegisterResult
<
Ctx
extends
Context
,
T
>
=
Omit
<
MethodMeta
<
Ctx
>
,
'
run
'
>
&
{
type
:
string
;
key
:
keyof
T
&
string
;
result
:
MethodReturn
<
C
,
K
>
;
ctx
:
C
;
}
result
:
any
;
ctx
:
C
tx
;
}
;
export
interface
ProvideOptions
extends
Context
.
ServiceOptions
{
immediate
?:
boolean
;
}
export
interface
ProvideDefinition
<
C
extends
Context
>
extends
ProvideOptions
{
serviceName
:
ServiceName
<
C
>
;
serviceName
:
string
;
}
export
type
ContextFunction
<
C
extends
Context
,
T
>
=
(
ctx
:
C
)
=>
T
;
export
type
RenderedContextTransformer
<
C
extends
Context
>
=
(
ctx
:
C
,
r
:
ParamRenderer
,
)
=>
C
;
export
type
ContextCallbackLayer
<
C
extends
Context
,
T
=
any
>
=
(
ctx
:
C
,
cb
:
ContextFunction
<
C
,
void
>
,
)
=>
T
;
export
type
ServiceName
<
C
extends
Context
>
=
string
;
export
type
TopLevelActionDef
<
C
extends
Context
>
=
(
ctx
:
C
,
obj
:
any
,
renderer
:
ParamRenderer
,
)
=>
void
;
export
interface
MetadataArrayMap
<
C
extends
Context
>
{
CordisRegisterKeys
:
string
;
CordisContextTransformer
:
RenderedContextTransformer
<
C
>
;
CordisTopLevelAction
:
TopLevelActionDef
<
C
>
;
CordisContextLayers
:
ContextCallbackLayer
<
C
>
;
CordisControl
:
ControlType
;
CordisPluginUsing
:
ServiceName
<
C
>
;
CordisPluginProvide
:
ProvideDefinition
<
C
>
;
CordisPluginInjectKeys
:
string
;
CordisPluginSystemKeys
:
string
;
}
export
interface
MetadataMap
<
C
extends
Context
>
{
CordisRegister
:
RegisterInfo
<
C
>
;
CordisPluginInject
:
ServiceName
<
C
>
;
CordisPluginSystem
:
PluginRegistrar
.
SystemInjectFun
<
C
>
;
CordisPluginPredefineSchema
:
Schema
|
ClassType
<
any
>
;
CordisPluginPredefineName
:
string
;
CordisPluginFork
:
PluginRegistrar
.
PluginClass
<
C
>
;
CordisPluginReusable
:
boolean
;
}
export
type
ContextFunction
<
Ctx
extends
Context
,
T
>
=
DecorateFunctionParam
<
Ctx
,
[],
T
>
;
export
type
ContextTransformer
<
Ctx
extends
Context
,
A
extends
any
[]
=
any
[],
>
=
DecorateFunctionParam
<
Ctx
,
A
,
Ctx
>
;
export
type
ContextCallbackLayer
<
Ctx
extends
Context
,
A
extends
any
[]
=
any
[],
>
=
DecorateFunctionParamSingle
<
Ctx
,
ContextFunction
<
Ctx
,
void
>
,
A
>
;
export
type
TopLevelActionDef
<
Ctx
extends
Context
,
A
extends
any
[]
=
any
[],
>
=
DecorateFunctionParamSingle
<
Ctx
,
any
,
A
>
;
}
const
ThirdEyeSym
=
Symbol
(
'
ThirdEyeSym
'
);
export
class
Registrar
<
Ctx
extends
Context
>
{
metadata
=
new
MetadataSetter
<
Registrar
.
MetadataMap
<
Ctx
>
,
Registrar
.
MetadataArrayMap
<
Ctx
>
Registrar
.
MetadataMap
,
Registrar
.
MetadataArrayMap
>
();
reflector
=
new
Reflector
<
Registrar
.
MetadataMap
<
Ctx
>
,
Registrar
.
MetadataArrayMap
<
Ctx
>
Registrar
.
MetadataMap
,
Registrar
.
MetadataArrayMap
>
();
constructor
(
public
contextClass
:
ClassType
<
Ctx
>
)
{}
...
...
@@ -146,34 +124,46 @@ export class Registrar<Ctx extends Context> {
return
new
RegistrarAspect
(
this
,
obj
,
view
);
}
decorateMethod
<
K
extends
keyof
Registrar
.
Methods
<
Ctx
>
>
(
type
:
K
,
action
:
Registrar
.
MethodResolver
<
Ctx
,
K
>
,
decorateMethod
<
A
extends
any
[],
F
extends
(...
args
:
any
[])
=>
any
>
(
type
:
string
,
action
:
Registrar
.
MethodResolver
<
Ctx
,
A
,
F
>
,
)
{
return
(...
args
:
Registrar
.
MethodParams
<
Ctx
,
K
>
)
=>
return
(...
args
:
A
):
TypedMethodDecorator
<
F
>
=>
this
.
metadata
.
set
(
'
CordisRegister
'
,
{
type
,
args
,
action
,
},
new
RegisterMeta
(
action
,
args
,
{
type
}),
'
CordisRegisterKeys
'
,
)
as
TypedMethodDecorator
<
Registrar
.
MethodClassMethodFunction
<
Ctx
,
K
>>
;
);
}
decorateTransformer
(
transformer
:
Registrar
.
RenderedContextTransformer
<
Ctx
>
,
):
ClassDecorator
&
MethodDecorator
{
return
this
.
metadata
.
append
(
'
CordisContextTransformer
'
,
transformer
);
decorateTransformer
<
A
extends
any
[]
>
(
transformer
:
Registrar
.
ContextTransformer
<
Ctx
,
A
>
,
)
{
return
(...
args
:
A
):
ClassDecorator
&
MethodDecorator
=>
this
.
metadata
.
append
(
'
CordisContextTransformer
'
,
new
RegisterMeta
(
transformer
,
args
),
);
}
decorateTopLevelAction
(
action
:
Registrar
.
TopLevelActionDef
<
Ctx
>
)
{
return
this
.
metadata
.
append
(
'
CordisTopLevelAction
'
,
action
);
decorateTopLevelAction
<
A
extends
any
[]
>
(
action
:
Registrar
.
TopLevelActionDef
<
Ctx
,
A
>
,
)
{
return
(...
args
:
A
):
ClassDecorator
=>
this
.
metadata
.
append
(
'
CordisTopLevelAction
'
,
new
RegisterMeta
(
action
,
args
),
);
}
decorateContextLayer
(
layer
:
Registrar
.
ContextCallbackLayer
<
Ctx
>
)
{
return
this
.
metadata
.
append
(
'
CordisContextLayers
'
,
layer
);
decorateContextLayer
<
A
extends
any
[]
>
(
action
:
Registrar
.
ContextCallbackLayer
<
Ctx
,
A
>
,
)
{
return
(...
args
:
A
):
ClassDecorator
&
MethodDecorator
=>
this
.
metadata
.
append
(
'
CordisContextLayers
'
,
new
RegisterMeta
(
action
,
args
),
);
}
private
getFork
(
obj
:
any
)
{
...
...
@@ -184,9 +174,7 @@ export class Registrar<Ctx extends Context> {
return
this
.
plugin
()(
fork
);
}
afterPluginMethodRegistration
(
result
:
Registrar
.
RegisterResult
<
Ctx
,
any
,
keyof
Registrar
.
Methods
<
Ctx
>>
,
)
{
afterPluginMethodRegistration
(
result
:
Registrar
.
RegisterResult
<
Ctx
,
any
>
)
{
// for override
}
...
...
@@ -434,34 +422,41 @@ export class Registrar<Ctx extends Context> {
return
{
UseEvent
:
this
.
decorateMethod
(
'
on
'
,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(
ctx
,
fun
,
event
,
prepend
)
=>
ctx
.
on
(
event
,
fun
,
prepend
),
(
ctx
,
fun
,
event
:
keyof
GetEvents
<
Ctx
>
,
prepend
?:
boolean
)
=>
ctx
.
on
(
event
as
any
,
fun
,
prepend
),
),
UsePlugin
:
this
.
decorateMethod
(
'
plugin
'
,
(
ctx
,
fun
)
=>
{
const
result
=
fun
();
const
register
=
(
def
:
PluginRegistrar
.
PluginDefinition
<
Ctx
,
any
>
)
=>
{
if
(
!
def
)
{
return
;
UsePlugin
:
this
.
decorateMethod
(
'
plugin
'
,
(
ctx
,
fun
:
(
...
args
:
any
[]
)
=>
Awaitable
<
PluginRegistrar
.
PluginDefinition
<
Ctx
>>
,
)
=>
{
const
result
=
fun
();
const
register
=
(
def
:
PluginRegistrar
.
PluginDefinition
<
Ctx
>
)
=>
{
if
(
!
def
)
{
return
;
}
return
ctx
.
plugin
(
def
.
plugin
,
def
.
options
);
};
if
(
result
instanceof
Promise
)
{
return
result
.
then
(
register
);
}
else
{
return
register
(
result
);
}
return
ctx
.
plugin
(
def
.
plugin
,
def
.
options
);
};
if
(
result
instanceof
Promise
)
{
return
result
.
then
(
register
);
}
else
{
return
register
(
result
);
}
}),
},
),
Apply
:
this
.
decorateMethod
(
'
apply
'
,
(
ctx
,
fun
)
=>
fun
()),
};
}
scopeDecorators
()
{
return
{
Isolate
:
(...
services
:
Registrar
.
ServiceName
<
Ctx
>
[])
=>
Isolate
:
(...
services
:
string
[])
=>
this
.
decorateTransformer
((
ctx
,
r
)
=>
ctx
.
isolate
(
r
(
services
))),
UsingService
:
(
...
services
:
Registrar
.
ServiceName
<
Ctx
>
[]
...
services
:
string
[]
):
ClassDecorator
&
MethodDecorator
=>
{
return
(
obj
,
key
?)
=>
{
if
(
!
key
)
{
...
...
@@ -472,7 +467,9 @@ export class Registrar<Ctx extends Context> {
const
dec
=
this
.
decorateContextLayer
((
ctx
,
cb
)
=>
ctx
.
using
(
services
,
cb
),
);
dec
(
obj
,
key
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
dec
()(
obj
,
key
);
}
};
},
...
...
@@ -510,7 +507,7 @@ export class Registrar<Ctx extends Context> {
condition
:
func
,
}),
Provide
:
(
name
:
Registrar
.
ServiceName
<
Ctx
>
,
name
:
string
,
options
?:
Registrar
.
ProvideOptions
,
):
ClassDecorator
=>
{
Context
.
service
(
name
,
options
);
...
...
@@ -563,14 +560,16 @@ export class Registrar<Ctx extends Context> {
};
}
transformContext
(
ctx
:
Ctx
,
filters
:
Regist
rar
.
RenderedContextTransformer
<
Ctx
>
[],
r
:
ParamRenderer
=
(
v
)
=>
v
,
transformContext
<
_Ctx
extends
Ctx
>
(
ctx
:
_
Ctx
,
filters
:
Regist
erMeta
<
Registrar
.
ContextTransformer
<
Ctx
>
>
[],
view
:
any
,
)
{
let
targetCtx
=
ctx
;
for
(
const
fun
of
filters
)
{
targetCtx
=
fun
(
targetCtx
,
r
)
||
targetCtx
;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
targetCtx
=
fun
.
run
(
view
,
ctx
)
||
targetCtx
;
}
return
targetCtx
;
}
...
...
src/utility/register-meta.ts
0 → 100644
View file @
6e4df9a2
import
{
renderObject
}
from
'
./render-object
'
;
import
{
FunctionParams
,
FunctionReturn
}
from
'
../def
'
;
export
class
RegisterMeta
<
F
extends
(...
args
:
any
[])
=>
any
,
I
=
any
>
{
constructor
(
public
action
:
F
,
public
args
:
any
[],
public
info
?:
I
)
{}
run
(
view
:
any
,
...
extras
:
FunctionParams
<
F
>
):
FunctionReturn
<
F
>
{
return
this
.
action
(...
extras
,
...
renderObject
(
this
.
args
,
view
));
}
}
tests/service-activate.spec.ts
View file @
6e4df9a2
...
...
@@ -14,8 +14,8 @@ declare module 'cordis' {
}
interface
Events
{
'
pang
'
(
message
:
string
):
Promise
<
string
>
;
'
pong
'
(
message
:
string
):
Promise
<
string
>
;
'
pang
'
(
message
:
string
):
string
;
'
pong
'
(
message
:
string
):
string
;
}
}
...
...
@@ -101,14 +101,14 @@ class MyPartialConsumer {
@
UsingService
(
'
dummyProvider
'
)
@
UseEvent
(
'
pang
'
)
async
onPang
(
content
:
string
)
{
onPang
(
content
:
string
)
{
const
msg
=
`pang:
${
content
}
`
;
console
.
log
(
msg
);
return
msg
;
}
@
UseEvent
(
'
pong
'
)
async
onPong
(
content
:
string
)
{
onPong
(
content
:
string
)
{
const
msg
=
`pong:
${
content
}
`
;
console
.
log
(
msg
);
return
msg
;
...
...
@@ -157,16 +157,16 @@ describe('On service', () => {
await
app
.
start
();
app
.
plugin
(
MyPartialConsumer
);
expect
(
app
.
myPartialConsumer
).
toBeDefined
();
expect
(
a
wait
app
.
parallel
(
'
pang
'
,
'
hello
'
)).
toEqual
(
'
hello
'
);
expect
(
a
wait
app
.
paralle
l
(
'
pong
'
,
'
hello
'
)).
toEqual
(
'
pong: hello
'
);
expect
(
a
pp
.
bail
(
'
pang
'
,
'
hello
'
)).
toBeUndefined
(
);
expect
(
a
pp
.
bai
l
(
'
pong
'
,
'
hello
'
)).
toEqual
(
'
pong: hello
'
);
app
.
dummyProvider
=
{
foo
:
'
bar
'
};
expect
(
a
wait
app
.
paralle
l
(
'
pang
'
,
'
hello
'
)).
toEqual
(
'
pang: hello
'
);
expect
(
a
wait
app
.
paralle
l
(
'
pong
'
,
'
hello
'
)).
toEqual
(
'
pong: hello
'
);
expect
(
a
pp
.
bai
l
(
'
pang
'
,
'
hello
'
)).
toEqual
(
'
pang: hello
'
);
expect
(
a
pp
.
bai
l
(
'
pong
'
,
'
hello
'
)).
toEqual
(
'
pong: hello
'
);
app
.
dummyProvider
=
undefined
;
expect
(
a
wait
app
.
parallel
(
'
pang
'
,
'
hi
'
)).
toEqual
(
'
hi
'
);
expect
(
a
wait
app
.
paralle
l
(
'
pong
'
,
'
hi
'
)).
toEqual
(
'
pong: hi
'
);
expect
(
a
pp
.
bail
(
'
pang
'
,
'
hi
'
)).
toBeUndefined
(
);
expect
(
a
pp
.
bai
l
(
'
pong
'
,
'
hi
'
)).
toEqual
(
'
pong: hi
'
);
app
.
dummyProvider
=
{
foo
:
'
baz
'
};
expect
(
a
wait
app
.
paralle
l
(
'
pang
'
,
'
hi
'
)).
toEqual
(
'
pang: hi
'
);
expect
(
a
wait
app
.
paralle
l
(
'
pong
'
,
'
hi
'
)).
toEqual
(
'
pong: hi
'
);
expect
(
a
pp
.
bai
l
(
'
pang
'
,
'
hi
'
)).
toEqual
(
'
pang: hi
'
);
expect
(
a
pp
.
bai
l
(
'
pong
'
,
'
hi
'
)).
toEqual
(
'
pong: hi
'
);
});
});
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