UNPKG

@resin-js/core

Version:

A lightweight reactive library for enhancing native web development. Resin provides reactivity, and aims to remove common pain points found in vanilla JavaScript web development.

90 lines 3.31 kB
import { DeriveOptions, EffectFunction, NestedKeyOf, NestedValue, Resin, ResinArray, ResinError, ResinMap, ResinOptions } from "./types"; /** * Creates a reactive `Resin` instance for tracking and managing state. * Provides properties for value access, error tracking, and loading state. * * @example * const counter = resin(0); * counter.value = 5; // Reactive update for subscribers */ export declare function resin<T extends any[]>(array: T): Resin<T> & ResinArray<T[number]>; export declare function resin<K, V>(map: Map<K, V>): Resin<Map<K, V>> & ResinMap<K, V>; export declare function resin<T>(value: T, options?: ResinOptions<T>): Resin<T>; /** * Groups multiple updates together. * Executes updates within the batch in a single cycle. * * @example * batch(() => { * resinInstance1.value = 1; * resinInstance2.value = 2; * }); */ export declare function batch(fn: () => void): void; /** * Creates a computed `Resin` instance that derives its value from a function. * Automatically updates when dependent `Resin` instances change. * * @example * const count = resin(2); * const doubleCount = computed(() => count.value * 2); */ export declare function computed<T>(fn: () => T): Resin<T>; /** * Creates a derived `Resin` instance based on multiple source `Resin` values. * Automatically recomputes when any source value changes. * * @example * const firstName = resin('John'); * const lastName = resin('Doe'); * const fullName = derive({ * from: [firstName, lastName], * compute: (first, last) => `${first} ${last}` * }); */ export declare function derive<Sources extends Resin<any>[], Result>(options: DeriveOptions<Sources, Result>): Resin<Result>; /** * Creates a reactive view of a nested property from a Resin state object. * Returns a derived Resin that updates when the source property changes. * * @param {Resin<T>} state The source Resin object * @param {P} path Dot-notation path to the nested property (e.g., 'user.profile.name') * @returns {Resin<NestedValue<T, P>>} A reactive Resin containing the value at the specified path * * @example * const user = resin({ profile: { name: 'John' } }); * const name = select(user, 'profile.name'); * console.log(name.value); // 'John' * * // When the original object changes, the selected property updates * user.value.profile.name = 'Jane'; * console.log(name.value); // 'Jane' */ export declare function select<T extends object, P extends NestedKeyOf<T>>(state: Resin<T>, path: P): Resin<NestedValue<T, P>>; /** * Watches a function for changes in dependent `Resin` instances, rerunning it reactively. * * @example * watchEffect(() => { * console.log(resinInstance.value); * }); */ export declare function watchEffect(effect: EffectFunction): void; /** * Registers an effect to run independent of binding. * * @example * // * const count = resin<number>(0); * subscribe(count, (value: number) => { * if (number % 2 === 0) { * console.log('even'); * } else { * console.log('odd'); * } * }); */ export declare function subscribe(effect: () => void): void; export declare function createResinError(message: string, resinName?: string, value?: any): ResinError; export declare function enableDebug(): void; //# sourceMappingURL=resin.d.ts.map