fix(types): [PKG-FACTORY-01] preserve published factory compatibility

This commit is contained in:
Harvey Zhao committed 2026-09-13 16:53:27 +08:00
1 parent 1117107032
commit c4c7491e16
35 files changed
+670 -229

No files matched your search

+48 -11
View File
@@ -1,23 +1,59 @@
import type { Factory, Option, Result } from 'artplayer-plugin-ambilight'
import type { Factory, Option, Result, RuntimeFactory } from 'artplayer-plugin-ambilight'
import type { RuntimeFactory as EntryFactory, Option as EntryOption, Result as EntryResult } from 'artplayer-plugin-ambilight/runtime'
import Artplayer from 'artplayer'
import ambilight from 'artplayer-plugin-ambilight'
import legacy from 'artplayer-plugin-ambilight/legacy'
import runtime from 'artplayer-plugin-ambilight/runtime'
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
type Assert<T extends true> = T
interface PublishedOption {
blur?: string
opacity?: number
frequency?: number
zIndex?: number
duration?: number
}
interface PublishedResult {
name: 'artplayerPluginAmbilight'
start: () => void
stop: () => void
}
type PublishedFactory = (option: PublishedOption) => (art: Artplayer) => PublishedResult
type RootFactory = Assert<Equal<typeof ambilight, PublishedFactory>>
type RootArguments = Assert<Equal<Parameters<typeof ambilight>, [option: PublishedOption]>>
type RootResult = Assert<Equal<ReturnType<ReturnType<typeof ambilight>>, PublishedResult>>
type RuntimeArguments = Assert<Equal<Parameters<typeof runtime>, [option?: Option]>>
type RuntimeTypes = Assert<Equal<[EntryOption, EntryResult, EntryFactory], [Option, Result, RuntimeFactory]>>
const replacement: PublishedFactory = (_option: PublishedOption) => (_art: Artplayer) => ({ name: 'artplayerPluginAmbilight', start() {}, stop() {} })
const assignToOld: PublishedFactory = ambilight
const assignFromOld: typeof ambilight = replacement
const option: Option = { blur: '40px', opacity: 0.6, frequency: 10, duration: 0.2, zIndex: 3 }
const art = new Artplayer({ container: '#player', url: 'video.mp4', plugins: [ambilight(), ambilight(option), legacy({})] })
ambilight(undefined)
ambilight.default(option)
legacy.default()
const art = new Artplayer({ container: '#player', url: 'video.mp4', plugins: [ambilight(option), legacy({}), runtime()] })
runtime(undefined)
runtime.default(option)
runtime.default.default()
declare const optional: Option | undefined
ambilight(optional)
runtime(optional)
const inferred: Parameters<typeof ambilight>[0] = option
const blur: string | undefined = inferred.blur
const factory: Factory = ambilight
const factory: Factory = replacement
const result: Result = factory(option)(art)
const name: 'artplayerPluginAmbilight' = result.name
const calls: void[] = [result.start(), result.stop()]
const registration: (art: Artplayer) => Result = ambilight()
// @ts-expect-error Keep required last overload for existing Parameters extraction.
const registration: (art: Artplayer) => Result = runtime()
// @ts-expect-error Published 1.1.0 requires the options argument.
ambilight()
// @ts-expect-error Published 1.1.0 does not permit undefined as its argument.
ambilight(undefined)
// @ts-expect-error Optional input belongs to the accurate runtime entry.
ambilight(optional)
// @ts-expect-error Historical replacement factories must not require a self alias.
ambilight.default(option)
// @ts-expect-error Legacy retains the root required-argument type.
legacy()
// @ts-expect-error Root Parameters retains the required options argument.
const missing: Parameters<typeof ambilight>[0] = undefined
// @ts-expect-error Blur is a CSS string, not a number.
ambilight({ blur: 50 })
@@ -32,9 +68,10 @@ ambilight({ zIndex: '9' })
// @ts-expect-error Null was never accepted by the public types.
ambilight(null)
// @ts-expect-error Unknown options do not silently enter the contract.
ambilight({ speed: 10 })
runtime({ speed: 10 })
// @ts-expect-error Methods are synchronous.
const promise: Promise<void> = result.start()
// @ts-expect-error No public plugin destroy method is introduced.
result.destroy()
void [blur, name, calls, registration, missing, promise]
void [assignToOld, assignFromOld, blur, name, calls, registration, missing, promise]
export type { RootArguments, RootFactory, RootResult, RuntimeArguments, RuntimeTypes }
+28 -7
View File
@@ -1,12 +1,26 @@
import type { Factory, MediaCanvas, Option, Result } from 'artplayer-proxy-canvas'
import type { Factory, Option, Result, RuntimeFactory } from 'artplayer-proxy-canvas'
import type { MediaCanvas, RuntimeFactory as RuntimeEntry, Factory as RuntimeEntryFactory, Option as RuntimeOption, Result as RuntimeResult } from 'artplayer-proxy-canvas/runtime'
import Artplayer from 'artplayer'
import canvas from 'artplayer-proxy-canvas'
import * as namespace from 'artplayer-proxy-canvas'
import legacy from 'artplayer-proxy-canvas/legacy'
import runtime from 'artplayer-proxy-canvas/runtime'
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
type Assert<T extends true> = T
type OptionalInput = Assert<Equal<Parameters<typeof canvas>, [option?: Option]>>
type ExactResult = Assert<Equal<ReturnType<ReturnType<typeof canvas>>, HTMLCanvasElement>>
type HistoricalFactory = (option?: (ctx: CanvasRenderingContext2D, video: HTMLVideoElement) => void) => (art: Artplayer) => HTMLCanvasElement
type ExactFactory = Assert<Equal<typeof canvas, HistoricalFactory>>
type RuntimeEntries = Assert<Equal<[RuntimeEntryFactory, RuntimeOption, RuntimeResult, RuntimeEntry], [Factory, Option, Result, RuntimeFactory]>>
type ExactRuntime = Assert<Equal<typeof runtime, RuntimeFactory>>
const replacement: HistoricalFactory = (_option?: Option) => (_art: Artplayer) => document.createElement('canvas')
const toOld: HistoricalFactory = canvas
const fromOld: typeof canvas = replacement
const namedFactory: Factory = replacement
const namespaceFactory: HistoricalFactory = namespace.default
const legacyFactory: typeof legacy = replacement
const option: Option = (context, video) => {
context.drawImage(video, 0, 0)
@@ -17,8 +31,9 @@ const option: Option = (context, video) => {
const art = new Artplayer({ container: '#player', url: 'video.mp4', proxy: canvas(option) })
canvas()
canvas(undefined)
canvas.default(option)
legacy.default()
runtime.default(option)
runtime.default.default()
legacy()
declare const optional: Option | undefined
canvas(optional)
const missing: Parameters<typeof canvas>[0] = undefined
@@ -51,9 +66,15 @@ const promise: Promise<HTMLCanvasElement> = canvas()(art)
media.getContext('2d')?.play()
// @ts-expect-error Native Canvas dimensions remain numeric.
media.width = '320'
// @ts-expect-error Self default identity is readonly in declarations.
canvas.default = legacy
// @ts-expect-error Historical root factory has no required or exposed self default member.
canvas.default()
// @ts-expect-error Legacy factory retains the same plain factory shape.
legacy.default()
// @ts-expect-error A precise runtime replacement must provide its self identity.
const missingAlias: RuntimeFactory = replacement
// @ts-expect-error Self default identity is readonly in precise declarations.
runtime.default = runtime
// @ts-expect-error Registration requires a host.
canvas()()
void [missing, originalResult, play, context, listen, width, video, promise]
export type { ExactResult, OptionalInput }
void [toOld, fromOld, namedFactory, namespaceFactory, legacyFactory, missing, originalResult, play, context, listen, width, video, promise, missingAlias]
export type { ExactFactory, ExactResult, ExactRuntime, OptionalInput, RuntimeEntries }