Skip to content

Commit b0f9b9e

Browse files
committed
chore: setup utils package
1 parent ccb9e35 commit b0f9b9e

File tree

7 files changed

+93
-13
lines changed

7 files changed

+93
-13
lines changed

packages/core/src/registry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export function Inject(inject: Inject) {
2020
value.inject = inject
2121
} else if (decorator.kind === 'method') {
2222
decorator.addInitializer(function () {
23-
const property = this[symbols.tracker]?.property
23+
const property = this[Symbol.for('cordis.tracker')]?.property
2424
if (!property) throw new Error('missing context tracker')
25-
;(this[symbols.initHooks] ??= []).push(() => {
25+
;(this[Symbol.for('cordis.initHooks')] ??= []).push(() => {
2626
(this[property] as Context).inject(inject, (ctx) => {
2727
return value.call(withProps(this, { [property]: ctx }))
2828
})

packages/core/tests/decorator.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Context, Inject, Service } from '../src'
1+
import { Context, Service } from 'cordis'
2+
import { Inject } from '../src'
23
import { expect } from 'chai'
34
import { mock } from 'node:test'
4-
import { sleep } from './utils'
55

66
describe('Decorator', () => {
77
it('@Inject on class method', async () => {
@@ -31,7 +31,7 @@ describe('Decorator', () => {
3131
expect(callback.mock.calls).to.have.length(0)
3232
expect(dispose.mock.calls).to.have.length(0)
3333
const scope = root.plugin(Foo)
34-
await sleep()
34+
await scope
3535
expect(callback.mock.calls).to.have.length(1)
3636
expect(dispose.mock.calls).to.have.length(0)
3737
await scope.dispose()

packages/logger/package.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
"type": "module",
66
"main": "lib/index.js",
77
"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-
},
168
"files": [
179
"lib"
1810
],

packages/utils/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

packages/utils/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @cordisjs/utils

packages/utils/src/index.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

packages/utils/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.base",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "lib",
6+
},
7+
"include": [
8+
"src",
9+
],
10+
}

0 commit comments

Comments
 (0)