Vendor Cordis framework packages as source
cordis 4.0.0-rc.6, plugin-loader, -include, -group, -timer, -hmr, -logger-console, cosmokit 1.8.1, schemastery 3.18.0 — copied from the cordis-workspace checkout, flattened under vendor/, original npm names, private: true. vendor/README.md is the manifest: upstream repos + commit SHAs, local-modification log, sync procedure. Local modification: hmr's locale YAML imports and .i18n() call removed (avoids a runtime YAML import hook we don't vendor).
This commit is contained in:
21
vendor/timer/LICENSE
vendored
Normal file
21
vendor/timer/LICENSE
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-present Shigma
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
36
vendor/timer/README.md
vendored
Normal file
36
vendor/timer/README.md
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
# @cordisjs/plugin-timer
|
||||
|
||||
Disposal-aware timer service for Cordis.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { Context } from 'cordis'
|
||||
import Timer from '@cordisjs/plugin-timer'
|
||||
|
||||
const root = new Context()
|
||||
await root.plugin(Timer)
|
||||
|
||||
const dispose = root.timeout(() => {
|
||||
root.logger.info('done')
|
||||
}, 1000)
|
||||
|
||||
dispose()
|
||||
```
|
||||
|
||||
Timer handles are registered on the current fiber, so they are cleared
|
||||
automatically when the plugin that created them is disposed.
|
||||
|
||||
## API
|
||||
|
||||
| API | Description |
|
||||
| --- | --- |
|
||||
| `ctx.timeout(callback, delay)` | Run once and return a disposer. |
|
||||
| `ctx.timeout(delay)` | Return a promise that resolves after `delay`. |
|
||||
| `ctx.interval(callback, delay)` | Run repeatedly and return a disposer. |
|
||||
| `ctx.interval(delay)` | Return an async iterator that yields on each interval. |
|
||||
| `ctx.throttle(callback, delay, noTrailing?)` | Return a throttled function with `.dispose()`. |
|
||||
| `ctx.debounce(callback, delay)` | Return a debounced function with `.dispose()`. |
|
||||
|
||||
`ctx.setTimeout()` and `ctx.setInterval()` are kept as deprecated aliases for
|
||||
`ctx.timeout()` and `ctx.interval()`.
|
||||
29
vendor/timer/package.json
vendored
Normal file
29
vendor/timer/package.json
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@cordisjs/plugin-timer",
|
||||
"description": "Timer service for cordis",
|
||||
"version": "1.1.2",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./lib/index.d.ts",
|
||||
"default": "./lib/index.js"
|
||||
},
|
||||
"./src/*": "./src/*",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"files": [
|
||||
"lib",
|
||||
"src"
|
||||
],
|
||||
"author": "Shigma <shigma10826@gmail.com>",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"cordis": "^4.0.0-rc.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"cosmokit": "^1.8.1"
|
||||
}
|
||||
}
|
||||
147
vendor/timer/src/index.ts
vendored
Normal file
147
vendor/timer/src/index.ts
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
import { Context, Service } from 'cordis'
|
||||
|
||||
declare module 'cordis' {
|
||||
interface Context extends Pick<TimerService, 'interval' | 'timeout' | 'throttle' | 'debounce' | 'setTimeout' | 'setInterval'> {
|
||||
timer: TimerService
|
||||
}
|
||||
}
|
||||
|
||||
type WithDispose<T> = T & { dispose: () => void }
|
||||
|
||||
/** Disposable timer helpers mixed into Cordis contexts. */
|
||||
export class TimerService extends Service {
|
||||
constructor(ctx: Context) {
|
||||
super(ctx, 'timer')
|
||||
ctx.mixin('timer', ['timeout', 'interval', 'throttle', 'debounce', 'setTimeout', 'setInterval'])
|
||||
}
|
||||
|
||||
/** @deprecated use `ctx.timeout()` instead */
|
||||
setTimeout(callback: () => void, delay: number) {
|
||||
return this.timeout(callback, delay)
|
||||
}
|
||||
|
||||
/** @deprecated use `ctx.interval()` instead */
|
||||
setInterval(callback: () => void, delay: number) {
|
||||
return this.interval(callback, delay)
|
||||
}
|
||||
|
||||
/** Run a callback once, or return a promise that resolves after `delay`. */
|
||||
timeout(callback: () => void, delay: number): () => void
|
||||
timeout(delay: number): Promise<void>
|
||||
timeout(...args: any[]): any {
|
||||
const callback = typeof args[0] === 'function' ? args.shift() : undefined
|
||||
const delay = args[0] as number
|
||||
if (callback) {
|
||||
const dispose = this.ctx.effect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
dispose()
|
||||
callback()
|
||||
}, delay)
|
||||
return () => clearTimeout(timer)
|
||||
}, 'ctx.timeout()')
|
||||
return dispose
|
||||
} else {
|
||||
const { promise, resolve, reject } = Promise.withResolvers<void>()
|
||||
const dispose = this.ctx.effect(() => {
|
||||
const timer = setTimeout(resolve, delay)
|
||||
return () => {
|
||||
clearTimeout(timer)
|
||||
reject(new Error('Context has been disposed'))
|
||||
}
|
||||
}, 'ctx.timeout()')
|
||||
return promise.finally(dispose)
|
||||
}
|
||||
}
|
||||
|
||||
/** Run a callback repeatedly, or return an async iterator of ticks. */
|
||||
interval(callback: () => void, delay: number): () => void
|
||||
interval<R = any>(delay: number): AsyncIterableIterator<void, R, void>
|
||||
interval(...args: any[]): any {
|
||||
const callback = typeof args[0] === 'function' ? args.shift() : undefined
|
||||
const delay = args[0] as number
|
||||
if (callback) {
|
||||
return this.ctx.effect(() => {
|
||||
const timer = setInterval(callback, delay)
|
||||
return () => clearInterval(timer)
|
||||
}, 'ctx.interval()')
|
||||
} else {
|
||||
let done: { kind: 'return'; value: any } | { kind: 'throw'; reason: any } | undefined
|
||||
let nextTask: PromiseWithResolvers<IteratorResult<void>> | undefined
|
||||
const dispose = this.ctx.effect(() => {
|
||||
const timer = setInterval(() => {
|
||||
nextTask?.resolve({ done: false, value: undefined })
|
||||
}, delay)
|
||||
return () => {
|
||||
clearInterval(timer)
|
||||
if (done) return
|
||||
done = { kind: 'throw', reason: new Error('Context has been disposed') }
|
||||
nextTask?.reject(done.reason)
|
||||
}
|
||||
}, 'ctx.interval()')
|
||||
return {
|
||||
next: () => {
|
||||
if (!done) return (nextTask = Promise.withResolvers()).promise
|
||||
if (done.kind === 'return') return Promise.resolve({ done: true, value: done.value })
|
||||
return Promise.reject(done.reason)
|
||||
},
|
||||
return: (value) => {
|
||||
if (!done) done = { kind: 'return', value }
|
||||
nextTask?.resolve({ done: true, value })
|
||||
dispose()
|
||||
return Promise.resolve({ done: true, value })
|
||||
},
|
||||
throw: (reason) => {
|
||||
if (!done) done = { kind: 'throw', reason }
|
||||
nextTask?.reject(reason)
|
||||
dispose()
|
||||
return Promise.resolve({ done: true, value: undefined })
|
||||
},
|
||||
[Symbol.asyncIterator]() {
|
||||
return this
|
||||
},
|
||||
} satisfies AsyncIterableIterator<void>
|
||||
}
|
||||
}
|
||||
|
||||
private _schedule(label: string, trigger: (args: any[], isDisposed: boolean) => any, isDisposed = false) {
|
||||
let timer: number | NodeJS.Timeout | undefined
|
||||
const dispose = this.ctx.effect(() => () => {
|
||||
isDisposed = true
|
||||
clearTimeout(timer)
|
||||
}, label)
|
||||
const wrapper: any = (...args: any[]) => {
|
||||
clearTimeout(timer)
|
||||
timer = trigger(args, isDisposed)
|
||||
}
|
||||
wrapper.dispose = dispose
|
||||
return wrapper
|
||||
}
|
||||
|
||||
/** Return a throttled function whose timer is disposed with the current fiber. */
|
||||
throttle<F extends (...args: any[]) => void>(callback: F, delay: number, noTrailing?: boolean): WithDispose<F> {
|
||||
let lastCall = -Infinity
|
||||
const execute = (...args: any[]) => {
|
||||
lastCall = Date.now()
|
||||
callback(...args)
|
||||
}
|
||||
return this._schedule('ctx.throttle()', (args, isDisposed) => {
|
||||
const now = Date.now()
|
||||
const remaining = delay - now + lastCall
|
||||
if (remaining <= 0) {
|
||||
execute(...args)
|
||||
} else if (!isDisposed) {
|
||||
return setTimeout(execute, remaining, ...args)
|
||||
}
|
||||
}, noTrailing)
|
||||
}
|
||||
|
||||
/** Return a debounced function whose timer is disposed with the current fiber. */
|
||||
debounce<F extends (...args: any[]) => void>(callback: F, delay: number): WithDispose<F> {
|
||||
return this._schedule('ctx.debounce()', (args, isDisposed) => {
|
||||
if (isDisposed) return
|
||||
return setTimeout(callback, delay, ...args)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default TimerService
|
||||
12
vendor/timer/tsconfig.json
vendored
Normal file
12
vendor/timer/tsconfig.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "src",
|
||||
"outDir": "lib"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [
|
||||
{ "path": "../cosmokit" },
|
||||
{ "path": "../cordis" }
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user