import { Event, isValue, Observer, Scope } from "./abstractions"
import { applyScopeMaybe } from "./applyscope"
import {
transform,
Transformer,
UnaryTransformOp,
UnaryTransformOpScoped,
} from "./transform"
export type Predicate2 = (prev: A, next: A) => boolean
export function skipDuplicates(fn: Predicate2): UnaryTransformOp
export function skipDuplicates(
fn: Predicate2,
scope: Scope
): UnaryTransformOpScoped
export function skipDuplicates(fn: Predicate2, scope?: Scope): any {
return transform(
["skipDuplicates", [fn]],
skipDuplicatesT(fn),
scope as Scope
)
}
function skipDuplicatesT(fn: Predicate2): Transformer {
let current: A | typeof uninitialized = uninitialized
return {
changes: (subscribe) => (onValue, onEnd) =>
subscribe((value) => {
if (current === uninitialized || !fn(current, value)) {
current = value
onValue(value)
}
}, onEnd),
init: (value: A) => {
current = value
return value
},
}
}
const uninitialized: unique symbol = Symbol()