Files
med-notes/.pnpm-store/v10/files/e7/a3f3dbb6b71ab15b062ef57a672621241dc9871793cc15272a9341ccd35dda66febdacb1a8758bc6e5f6d02e625ec6e0b34e9aa09bf6d33f0f11bfb21ddd41
2025-05-09 05:30:08 +02:00

43 lines
698 B
Plaintext

import { Derived } from './derived'
import type { DerivedOptions } from './derived'
interface EffectOptions
extends Omit<
DerivedOptions<unknown>,
'onUpdate' | 'onSubscribe' | 'lazy' | 'fn'
> {
/**
* Should the effect trigger immediately?
* @default false
*/
eager?: boolean
fn: () => void
}
export class Effect {
/**
* @private
*/
_derived: Derived<void>
constructor(opts: EffectOptions) {
const { eager, fn, ...derivedProps } = opts
this._derived = new Derived({
...derivedProps,
fn: () => {},
onUpdate() {
fn()
},
})
if (eager) {
fn()
}
}
mount() {
return this._derived.mount()
}
}