-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@cordisjs/timer", | ||
"description": "Timer plugin for cordis", | ||
"version": "0.1.0", | ||
"main": "lib/index.js", | ||
"module": "lib/index.mjs", | ||
"types": "lib/index.d.ts", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"author": "Shigma <shigma10826@gmail.com>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cordisjs/std.git", | ||
"directory": "packages/timer" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/cordisjs/std/issues" | ||
}, | ||
"homepage": "https://github.com/cordisjs/std", | ||
"keywords": [ | ||
"cordis", | ||
"timer", | ||
"service", | ||
"plugin" | ||
], | ||
"devDependencies": { | ||
"cordis": "^3.4.0" | ||
}, | ||
"peerDependencies": { | ||
"cordis": "^3.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @cordisjs/logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Context, Service } from 'cordis' | ||
import { defineProperty, remove } from 'cosmokit' | ||
import Logger from 'reggol' | ||
|
||
export { Logger } | ||
|
||
declare module 'cordis' { | ||
interface Context { | ||
timer: TimerService | ||
} | ||
} | ||
|
||
class TimerService extends Service { | ||
constructor(ctx: Context) { | ||
super(ctx, 'timer', true) | ||
defineProperty(this, Context.current, ctx) | ||
} | ||
|
||
createTimerDispose(timer: number | NodeJS.Timeout) { | ||
const dispose = () => { | ||
clearTimeout(timer) | ||
if (!this[Context.current].scope) return | ||
return remove(this[Context.current].scope.disposables, dispose) | ||
} | ||
this[Context.current].scope.disposables.push(dispose) | ||
return dispose | ||
} | ||
|
||
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]) { | ||
const dispose = this.createTimerDispose(setTimeout(() => { | ||
dispose() | ||
callback() | ||
}, ms, ...args)) | ||
return dispose | ||
} | ||
|
||
setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]) { | ||
return this.createTimerDispose(setInterval(callback, ms, ...args)) | ||
} | ||
|
||
sleep(ms: number) { | ||
return new Promise<void>((resolve, reject) => { | ||
const dispose1 = this.setTimeout(() => { | ||
dispose1() | ||
dispose2() | ||
resolve() | ||
}, ms) | ||
const dispose2 = this[Context.current].on('dispose', () => { | ||
dispose1() | ||
dispose2() | ||
reject(new Error('Context disposed')) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
export default TimerService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../tsconfig.base", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "lib", | ||
"strict": true, | ||
"noImplicitAny": false, | ||
}, | ||
"include": [ | ||
"src", | ||
], | ||
} |