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

bindable-bloc

v1.2.4

Published

A simple BLoC pattern library

Downloads

3

Readme

bindable-bloc

A simple BLoC pattern library

Usage

How it's different from the BLoC pattern in Flutter

One of the biggest difference would be that there is no concept of sink and streams. We assume the following data flow to simulate a similar effect.

  1. A component calls a business logic function of a BLoC instance OR broadcasts an event to the BLoC container and have it caught by an event handler.
  2. The state is modified on the bindable object within a BLoC instance.
  3. For a component to listen to this state change, the component will call useBindable hook with the bindable state it wants to listen to.

But then again, it all depends on how you utilize the classes included in this library. I initially had no intention on establishing a solid pattern that must be followed to use it.

Example

Using Bindable objects

The basic usage of bindable object is as simple as follows:

// Defining a bindable state
username = new Bindable<string>("");

// Setting a value to the bindable state
username.setValue("John");

// Retrieving the value in the bindable state
const name = username.getValue();

You can subscribe and unsubscribe to changes of a bindable state. However, it is almost unlikely that you'll need these functionalities yourself.

// Subscribing to a bindable object.
// It returns a subscriber ID which can be used for unsubscribing at a later time.
const id = username.subscribe((v) => {
    /* Do something with the value... */
    console.log("New username: " + v);
});

// Unsubscribing from the bindable requires the id that was returned when you subscribed to the bindable object.
username.unsubscribe(id);

Bindables can be used within a component to receive new state values when they change, using a React hook "useBindable". They internally use the subscribe/unsubscribe functions mentioned above to achieve this.

// Let's assume this bindable object exists somewhere in your app.
const username = new Bindable<string>("John");

// Then in your component,
const MyComponent = () => {

    // Listen to state change in the "username" bindable.
    // If the internal value of the specified bindable changes, it will trigger a component refresh.
    const name = useBindable(username);

    // The resulting output will be "Username is: John"
    return (
        <div>
            <p>Username is: {name}</p>
        </div>
    );
};

You may wrap any type of object in a Bindable but remember that "useBindable" internally uses "useState" hook, which only triggers component refresh when the object reference changes.

// Say we have this
const myData = [1, 2, 3];
const bindableData = new Bindable<Array<number>>(myData);

// Doing this will update the bindable data, but not trigger component refresh because the value is still the same object reference!
myData.push(4);
bindableData.setValue(myData);

// This will successfully trigger component refresh.
bindableData.setValue([
    ...myData, 4
]);

TODOs

  • Add more examples in README

Versions

1.2.4

  • Implemented useBindableUnsafe to support null or undefined bindables as parameter.
  • Fixed useBindable's onChange callback not being invoked when the bindable changes.

1.2.1

  • Added bind/unbind functions to cover some limitations imposed by subscribe/unsubscribe.

1.2.0

  • Added a flag to trigger on setValue only when the new value is not equal to the existing value.
  • Implemented ability to continuously receive value from another Bindable using startProxy and stopProxy.

1.1.8

  • Fixed bug in useBindable where changing the Bindable instance wouldn't trigger state update.

1.1.7

  • A quick fix where bindable subscription would potentially occur every render.

1.1.6

  • Added value getter / setter properties to Bindable for convenience.
  • Added ability to set Bindable's value without triggering an event.

1.1.5

  • Added undefined value check against listener during Bindable.trigger().

1.1.4

  • Fixed bug where the Bindable.trigger() method will throw an error if the callback became undefined or null.

1.1.3

  • Added ability to subscribe to a bindable and immediately receive trigger callback.

1.1.2

  • Support for persistent state using localForage dependency.

1.1.1

  • Added more tests.
  • Changed BlocContextValue.getBloc's return type from T | null to T. If you provide a constructor which a BLoC instance hasn't been cached for, it will throw an error instead.

1.0.0

  • Initial publish to NPM.