Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
Koishi Thirdeye
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
Koishi Thirdeye
Commits
5551d867
Commit
5551d867
authored
May 03, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
plugin factory
parent
2eeb9e02
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
12 deletions
+70
-12
src/plugin-factory.ts
src/plugin-factory.ts
+15
-11
tests/multi-instance.spec.ts
tests/multi-instance.spec.ts
+1
-1
tests/plugin-factory.spec.ts
tests/plugin-factory.spec.ts
+54
-0
No files found.
src/plugin-factory.ts
View file @
5551d867
import
{
Context
}
from
'
koishi
'
;
import
{
ClassType
,
Mixin
,
SchemaProperty
}
from
'
schemastery-gen
'
;
import
{
AnyClass
,
ClassType
,
Mixin
}
from
'
schemastery-gen
'
;
import
{
PluginSchema
}
from
'
./decorators
'
;
import
{
PartialDeep
}
from
'
./base-plugin
'
;
export
function
CreatePluginFactory
<
C
,
P
>
(
export
function
CreatePluginFactory
<
C
,
IC
,
P
extends
{
config
:
IC
}
>
(
basePlugin
:
new
(
ctx
:
Context
,
config
:
C
)
=>
P
,
baseConfig
:
ClassType
<
C
>
,
):
<
S
>
(
specificConfig
?:
ClassType
<
S
>
)
=>
new
(
ctx
:
Context
,
config
:
S
&
C
)
=>
P
;
export
function
CreatePluginFactory
<
C
,
P
>
(
basePlugin
:
new
(
ctx
:
Context
,
config
:
C
)
=>
any
,
baseConfig
:
ClassType
<
C
>
,
):
<
S
>
(
specificConfig
?:
ClassType
<
S
>
,
)
=>
new
(
ctx
:
Context
,
config
:
S
&
C
)
=>
P
{
return
(
specificConfig
)
=>
{
baseConfig
:
ClassType
<
IC
>
,
):
<
S
>
(
specificConfig
?:
ClassType
<
S
>
)
=>
new
(
ctx
:
Context
,
config
:
PartialDeep
<
S
>
&
C
,
)
=>
Omit
<
C
,
'
config
'
>
&
{
config
:
IC
&
S
;
};
export
function
CreatePluginFactory
(
basePlugin
:
new
(
ctx
:
Context
,
config
:
any
)
=>
any
,
baseConfig
:
AnyClass
,
)
{
return
(
specificConfig
:
AnyClass
)
=>
{
const
plugin
=
class
specificPlugin
extends
basePlugin
{};
const
config
=
specificConfig
?
Mixin
(
specificConfig
,
baseConfig
)
...
...
tests/multi-instance.spec.ts
View file @
5551d867
...
...
@@ -62,7 +62,7 @@ class Outer2 extends MultiInstancePlugin(Inner2, OuterMessageConfig) {
}
}
describe
(
'
It should
register multi plugin instance
'
,
()
=>
{
describe
(
'
register multi plugin instance
'
,
()
=>
{
it
(
'
should work on schemastery-gen
'
,
async
()
=>
{
const
app
=
new
App
();
app
.
plugin
(
Outer
,
{
msg
:
'
hello
'
,
instances
:
[{
msg
:
'
world
'
}]
});
...
...
tests/plugin-factory.spec.ts
0 → 100644
View file @
5551d867
import
{
SchemaProperty
}
from
'
schemastery-gen
'
;
import
{
UseCommand
}
from
'
koishi-decorators
'
;
import
{
CreatePluginFactory
}
from
'
../src/plugin-factory
'
;
import
{
App
,
Context
}
from
'
koishi
'
;
import
{
DefinePlugin
}
from
'
../src/register
'
;
import
{
InjectConfig
}
from
'
../src/decorators
'
;
import
{
BasePlugin
}
from
'
../src/base-plugin
'
;
class
MessageConfig
{
@
SchemaProperty
()
msg
:
string
;
getMsg
()
{
return
this
.
msg
;
}
}
class
Base
extends
BasePlugin
<
MessageConfig
>
{
@
UseCommand
(
'
message
'
)
async
onMessage
()
{
return
this
.
config
.
getMsg
();
}
}
const
Factory
=
CreatePluginFactory
(
Base
,
MessageConfig
);
class
SpecificConfig
{
@
SchemaProperty
()
msg2
:
string
;
getMsg2
()
{
return
this
.
msg2
;
}
}
@
DefinePlugin
()
class
SpecificPlugin
extends
Factory
(
SpecificConfig
)
{
@
UseCommand
(
'
message2
'
)
async
onMessage2
()
{
return
this
.
config
.
getMsg2
();
}
}
describe
(
'
plugin factory
'
,
()
=>
{
it
(
'
should register SpecificPlugin
'
,
async
()
=>
{
const
app
=
new
App
();
app
.
plugin
(
SpecificPlugin
,
{
msg
:
'
hello
'
,
msg2
:
'
world
'
});
await
app
.
start
();
const
innerCommand
=
app
.
command
(
'
message
'
);
const
outerCommand
=
app
.
command
(
'
message2
'
);
expect
(
await
innerCommand
.
execute
({})).
toBe
(
'
hello
'
);
expect
(
await
outerCommand
.
execute
({})).
toBe
(
'
world
'
);
});
});
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