npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

echo-prop

v1.0.1

Published

A lightweight library for creating reactive properties with RxJS

Downloads

6

Readme

EchoProp

EchoProp is a lightweight library that enables reactive properties on JavaScript objects using RxJS ReplaySubject. It allows you to track and observe property changes with options for validation and configurable replay counts, making state management in reactive programming more straightforward.

Features

  • Adds reactive properties to any object.
  • Automatically creates RxJS observables for property changes.
  • Supports configurable replay counts for observable history.
  • Optional validation for property updates.
  • Option to use existing property values as the initial value.
  • Configurable logging for validation failures.

Installation

Install EchoProp using npm:

npm install echo-prop

Usage

Single Reactive Property

To create a single reactive property, use the createEchoProp function:

import { createEchoProp } from 'echo-prop';

// Example target object
const target = {};

// Create a reactive property
const prop = createEchoProp(target, 'value', 10, {
    replayCount: 1, // Keep 1 previous value in history
    validate: (newValue, oldValue) => newValue >= 0, // Validation function
    log: true, // Enable logging for validation failures
    useExistingValueAsInitial: true // Use existing value if initialValue is null (default: true)
});

// Subscribe to changes
prop.subscribe((newValue) => console.log('New value:', newValue));

// Update the property value
target.value = 20; // Outputs: New value: 20

// Access the observable
const valueObservable = target.value$;

Multiple Reactive Properties

To add multiple reactive properties at once, use createEchoProps:

import { createEchoProps } from 'echo-prop';

const target = {};

// Define initial values for properties
const propertyBook = {
    score: 0,
    health: 100
};

// Create reactive properties
const props = createEchoProps(target, propertyBook, {
    replayCount: 1,
    validate: (newValue, oldValue) => newValue >= 0,
    useExistingValueAsInitial: true // Use existing value if initialValue is null (default: true)
});

// Subscribe to a specific property
target.score$.subscribe((newScore) => console.log('New score:', newScore));

// Update the property
target.score = 5; // Outputs: New score: 5

API

createEchoProp(target, propertyName, initialValue, config)

Creates a reactive property on the target object.

  • target (Object): The object on which to add the property.
  • propertyName (String): The name of the property.
  • initialValue (Any): Initial value of the property.
  • config (Object):
    • addAsObservableToTarget (Boolean, default: true): Adds the observable to target with the name <propertyName>$.
    • replayCount (Number, default: 1): Number of previous values the observable will remember.
    • validate (Function, default: null): Optional validation function with the signature (newValue, oldValue) => Boolean.
    • log (Boolean, default: false): If true, logs a warning when validation fails.
    • useExistingValueAsInitial (Boolean, default: true): Uses the existing property value as the initial value if initialValue is null or undefined.

Returns an object:

  • value (Any): The current value of the property.
  • name (String): The name of the property.
  • subscribe(callback): Subscribe to property changes.
  • asObservable(): Access the observable for custom handling.
  • complete(): Complete the observable.

createEchoProps(target, propertyBook, config)

Creates multiple reactive properties on the target object.

  • target (Object): The object to enhance with reactive properties.
  • propertyBook (Object): Key-value pairs where each key is a property name and each value is its initial value.
  • config (Object): Same as in createEchoProp.

Returns an array of reactive property objects for custom management.

License

EchoProp is licensed under the MIT License.