File tree Expand file tree Collapse file tree 7 files changed +93
-13
lines changed Expand file tree Collapse file tree 7 files changed +93
-13
lines changed Original file line number Diff line number Diff line change @@ -20,9 +20,9 @@ export function Inject(inject: Inject) {
20
20
value . inject = inject
21
21
} else if ( decorator . kind === 'method' ) {
22
22
decorator . addInitializer ( function ( ) {
23
- const property = this [ symbols . tracker ] ?. property
23
+ const property = this [ Symbol . for ( 'cordis. tracker' ) ] ?. property
24
24
if ( ! property ) throw new Error ( 'missing context tracker' )
25
- ; ( this [ symbols . initHooks ] ??= [ ] ) . push ( ( ) => {
25
+ ; ( this [ Symbol . for ( 'cordis. initHooks' ) ] ??= [ ] ) . push ( ( ) => {
26
26
( this [ property ] as Context ) . inject ( inject , ( ctx ) => {
27
27
return value . call ( withProps ( this , { [ property ] : ctx } ) )
28
28
} )
Original file line number Diff line number Diff line change 1
- import { Context , Inject , Service } from '../src'
1
+ import { Context , Service } from 'cordis'
2
+ import { Inject } from '../src'
2
3
import { expect } from 'chai'
3
4
import { mock } from 'node:test'
4
- import { sleep } from './utils'
5
5
6
6
describe ( 'Decorator' , ( ) => {
7
7
it ( '@Inject on class method' , async ( ) => {
@@ -31,7 +31,7 @@ describe('Decorator', () => {
31
31
expect ( callback . mock . calls ) . to . have . length ( 0 )
32
32
expect ( dispose . mock . calls ) . to . have . length ( 0 )
33
33
const scope = root . plugin ( Foo )
34
- await sleep ( )
34
+ await scope
35
35
expect ( callback . mock . calls ) . to . have . length ( 1 )
36
36
expect ( dispose . mock . calls ) . to . have . length ( 0 )
37
37
await scope . dispose ( )
Original file line number Diff line number Diff line change 5
5
"type" : " module" ,
6
6
"main" : " lib/index.js" ,
7
7
"types" : " lib/index.d.ts" ,
8
- "exports" : {
9
- "." : {
10
- "types" : " ./lib/index.d.ts" ,
11
- "default" : " ./lib/index.js"
12
- },
13
- "./src/*" : " ./src/*" ,
14
- "./package.json" : " ./package.json"
15
- },
16
8
"files" : [
17
9
" lib"
18
10
],
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " @cordisjs/utils" ,
3
+ "description" : " Utilities for cordis" ,
4
+ "version" : " 1.0.0" ,
5
+ "type" : " module" ,
6
+ "main" : " lib/index.js" ,
7
+ "types" : " lib/index.d.ts" ,
8
+ "files" : [
9
+ " lib"
10
+ ],
11
+ "author" :
" Shigma <[email protected] >" ,
12
+ "license" : " MIT" ,
13
+ "repository" : {
14
+ "type" : " git" ,
15
+ "url" : " git+https://github.com/cordiverse/cordis.git" ,
16
+ "directory" : " packages/utils"
17
+ },
18
+ "bugs" : {
19
+ "url" : " https://github.com/cordiverse/cordis/issues"
20
+ },
21
+ "homepage" : " https://github.com/cordiverse/cordis" ,
22
+ "keywords" : [
23
+ " cordis" ,
24
+ " utilties"
25
+ ],
26
+ "devDependencies" : {
27
+ "cordis" : " ^4.0.0-alpha.5"
28
+ },
29
+ "peerDependencies" : {
30
+ "cordis" : " ^4.0.0-alpha.5"
31
+ },
32
+ "dependencies" : {
33
+ "cosmokit" : " ^1.7.3"
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ # @cordisjs/utils
Original file line number Diff line number Diff line change
1
+ import { Context , Service } from 'cordis'
2
+ import { defineProperty } from 'cosmokit'
3
+
4
+ export class List < T > {
5
+ private sn = 0
6
+ private inner = new Map < number , T > ( )
7
+
8
+ constructor ( public ctx : Context , private trace : string ) {
9
+ defineProperty ( this , Service . tracker , { property : 'ctx' } )
10
+ }
11
+
12
+ get length ( ) {
13
+ return this . inner . size
14
+ }
15
+
16
+ push ( value : T ) {
17
+ this . ctx . effect ( ( ) => {
18
+ this . inner . set ( ++ this . sn , value )
19
+ return ( ) => this . inner . delete ( this . sn )
20
+ } , `${ this . trace } .push()` )
21
+ }
22
+
23
+ * filter ( predicate : ( value : T ) => boolean ) {
24
+ for ( const value of this . inner . values ( ) ) {
25
+ if ( predicate ( value ) ) yield value
26
+ }
27
+ }
28
+
29
+ * map < U > ( mapper : ( value : T ) => U ) {
30
+ for ( const value of this . inner . values ( ) ) {
31
+ yield mapper ( value )
32
+ }
33
+ }
34
+
35
+ [ Symbol . iterator ] ( ) {
36
+ return this . inner . values ( )
37
+ }
38
+
39
+ [ Symbol . for ( 'nodejs.util.inspect.custom' ) ] ( ) {
40
+ return [ ...this ]
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "extends" : " ../../tsconfig.base" ,
3
+ "compilerOptions" : {
4
+ "rootDir" : " src" ,
5
+ "outDir" : " lib" ,
6
+ },
7
+ "include" : [
8
+ " src" ,
9
+ ],
10
+ }
You can’t perform that action at this time.
0 commit comments