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
50a1d676
Commit
50a1d676
authored
Jun 20, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix reusable things
parent
11d99588
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
7 deletions
+60
-7
src/decorators.ts
src/decorators.ts
+2
-0
src/def/constants.ts
src/def/constants.ts
+1
-0
src/register.ts
src/register.ts
+2
-2
tests/fork.spec.ts
tests/fork.spec.ts
+55
-5
No files found.
src/decorators.ts
View file @
50a1d676
...
...
@@ -167,3 +167,5 @@ export const MixinModel = <K extends Keys<Tables>>(
export
const
Fork
=
(
forkPlugin
:
PluginClass
)
=>
Metadata
.
set
(
'
KoishiFork
'
,
forkPlugin
);
export
const
Reusable
=
(
value
=
true
)
=>
Metadata
.
set
(
'
KoishiReusable
'
,
value
);
src/def/constants.ts
View file @
50a1d676
...
...
@@ -32,6 +32,7 @@ export interface MetadataMap {
KoishiPredefineSchema
:
Schema
|
ClassType
<
any
>
;
KoishiPredefineName
:
string
;
KoishiFork
:
PluginClass
;
KoishiReusable
:
boolean
;
}
export
const
ThirdEyeSym
=
Symbol
(
'
ThirdEyeSym
'
);
src/register.ts
View file @
50a1d676
...
...
@@ -23,7 +23,7 @@ export interface KoishiPluginRegistrationOptions<T = any> {
schema
?:
Schema
<
T
>
|
Type
<
T
>
;
Config
?:
Schema
<
T
>
|
Type
<
T
>
;
using
?:
ServiceName
[];
//
reusable?: boolean;
reusable
?:
boolean
;
}
export
interface
PluginMeta
<
T
=
any
>
{
...
...
@@ -102,7 +102,7 @@ export function DefinePlugin<T>(
}
static
get
reusable
()
{
return
!!
getFork
(
newClass
);
return
reflector
.
get
(
'
KoishiReusable
'
,
newClass
);
}
__ctx
:
Context
;
...
...
tests/fork.spec.ts
View file @
50a1d676
import
{
RegisterSchema
,
SchemaProperty
}
from
'
schemastery-gen
'
;
import
{
DefinePlugin
}
from
'
../src/register
'
;
import
{
ParentPluginMap
,
StarterPlugin
}
from
'
../src/base-plugin
'
;
import
{
Fork
,
InjectParent
,
Provide
}
from
'
../src/decorators
'
;
import
{
UseCommand
}
from
'
koishi-decorators
'
;
import
{
Fork
,
InjectParent
,
Provide
,
Reusable
}
from
'
../src/decorators
'
;
import
{
Apply
,
UseCommand
}
from
'
koishi-decorators
'
;
import
{
App
}
from
'
koishi
'
;
import
{
Prop
}
from
'
../src/def
'
;
...
...
@@ -39,12 +39,33 @@ class ChildPlugin extends StarterPlugin(Config) {
async
onSelfCommand
()
{
return
this
.
config
.
getName
();
}
@
Apply
()
increase
()
{
this
.
parent
.
loadCount
++
;
// console.log('fork loaded: ', this.parent.loadCount);
}
}
@
Provide
(
'
forkTest
'
,
{
immediate
:
true
})
@
DefinePlugin
()
class
MyPlugin
extends
ParentPluginMap
(
ChildPlugin
,
(
p
)
=>
p
.
config
.
getName
())
{
loadCount
=
0
;
isParent
=
true
;
@
Apply
()
onLoad
()
{
// console.log('load', this.config);
}
}
@
Reusable
()
@
DefinePlugin
()
class
MyReusablePlugin
extends
StarterPlugin
(
Config
)
{
@
Apply
()
onLoad
()
{
this
.
ctx
.
app
[
'
count
'
]
++
;
}
}
describe
(
'
Fork
'
,
()
=>
{
...
...
@@ -52,18 +73,39 @@ describe('Fork', () => {
beforeEach
(
async
()
=>
{
app
=
new
App
();
await
app
.
start
();
app
[
'
count
'
]
=
0
;
});
it
(
'
should fork a plugin
'
,
async
()
=>
{
// console.log('before 1');
app
.
plugin
(
MyPlugin
,
{
name
:
'
a
'
});
// console.log('after 1: ' + app.forkTest.loadCount);
const
myPlugin
=
app
.
forkTest
;
expect
(
app
.
forkTest
.
config
.
getName
()).
toEqual
(
'
a
'
);
expect
(
app
.
forkTest
.
instances
.
get
(
'
a
'
).
config
.
getName
()).
toEqual
(
'
a
'
);
expect
(
app
.
forkTest
.
instances
.
get
(
'
a
'
).
parent
).
toEqual
(
myPlugin
);
// console.log(myPlugin.instances.get('a').parent);
// console.log(myPlugin);
expect
(
myPlugin
.
instances
.
get
(
'
a
'
).
parent
===
myPlugin
).
toBe
(
true
);
expect
(
app
.
forkTest
.
instances
.
get
(
'
b
'
)).
toBeUndefined
();
expect
(
app
.
forkTest
.
loadCount
).
toBe
(
1
);
// console.log('before 2: ' + app.forkTest.loadCount);
app
.
plugin
(
MyPlugin
,
{
name
:
'
b
'
});
// console.log('after 2: ' + app.forkTest.loadCount);
expect
(
app
.
forkTest
.
instances
.
get
(
'
b
'
).
config
.
getName
()).
toEqual
(
'
b
'
);
expect
(
myPlugin
.
instances
.
get
(
'
b
'
).
parent
).
toEqual
(
app
.
forkTest
);
// console.log(myPlugin.instances.get('b').parent);
// console.log(myPlugin);
expect
(
myPlugin
.
instances
.
get
(
'
b
'
).
parent
===
myPlugin
).
toBe
(
true
);
expect
(
app
.
forkTest
.
loadCount
).
toBe
(
2
);
// console.log('before 3: ' + app.forkTest.loadCount);
app
.
plugin
(
MyPlugin
,
{
name
:
'
c
'
});
// console.log('after 3: ' + app.forkTest.loadCount);
expect
(
app
.
forkTest
.
instances
.
get
(
'
c
'
).
config
.
getName
()).
toEqual
(
'
c
'
);
// console.log(myPlugin.instances.get('c').parent);
// console.log(myPlugin);
expect
(
myPlugin
.
instances
.
get
(
'
c
'
).
parent
===
myPlugin
).
toBe
(
true
);
expect
(
app
.
forkTest
.
loadCount
).
toBe
(
3
);
const
commandChildA
=
app
.
command
(
'
childa
'
);
const
commandChildB
=
app
.
command
(
'
childb
'
);
...
...
@@ -73,6 +115,14 @@ describe('Fork', () => {
expect
(
await
commandChildA
.
execute
({})).
toEqual
(
'
a
'
);
expect
(
await
commandChildB
.
execute
({})).
toEqual
(
'
b
'
);
expect
(
await
commandParentA
.
execute
({})).
toEqual
(
'
a
'
);
// expect(await commandParentB.execute({})).toEqual('a');
expect
(
await
commandParentB
.
execute
({})).
toEqual
(
'
a
'
);
});
it
(
'
it should work on reusable
'
,
async
()
=>
{
expect
(
app
[
'
count
'
]).
toBe
(
0
);
app
.
plugin
(
MyReusablePlugin
,
{
name
:
'
a
'
});
expect
(
app
[
'
count
'
]).
toBe
(
1
);
app
.
plugin
(
MyReusablePlugin
,
{
name
:
'
b
'
});
expect
(
app
[
'
count
'
]).
toBe
(
2
);
});
});
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