mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
41 lines
1009 B
TypeScript
41 lines
1009 B
TypeScript
import { Subject, subject } from './subject'
|
|
|
|
export const of = (...items: any[]) => subject(({ next, complete }) => {
|
|
items.forEach(item => { next(item) })
|
|
complete()
|
|
})
|
|
|
|
export const fromEvent = (element: EventTarget, eventName: string) => subject<Event>(({ next }) => {
|
|
element.addEventListener(eventName, next)
|
|
return () => element.removeEventListener(eventName, next)
|
|
})
|
|
|
|
export const fromPromise = <T>(promise: Promise<T>) => subject<T>(({ next, complete, error }) => {
|
|
promise
|
|
.then(next)
|
|
.catch(error)
|
|
.finally(complete)
|
|
})
|
|
|
|
export const bindCallback = <T>(cb: (...args: any[]) => any, ...args_: any[]) => subject<T>(
|
|
({ next }) => {
|
|
cb(...args_, next)
|
|
},
|
|
)
|
|
|
|
export const concat = (...subjects: Subject<unknown>[]) => subject<unknown>(({ next }) => {
|
|
const copiedSubjects = [...subjects]
|
|
|
|
const handleNext = () => {
|
|
const s = copiedSubjects.shift()
|
|
s.subscribe({
|
|
next,
|
|
complete: () => {
|
|
handleNext()
|
|
},
|
|
})
|
|
}
|
|
|
|
handleNext()
|
|
})
|