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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rx-commands

v1.3.0

Published

Reactive Command Pattern Implementation

Downloads

4

Readme

RxCommand is an Reactive Extensions (Rx) based abstraction for event handlers. It is based on ReactiveCommand for the ReactiveUI framework and RxCommand Flutter Package.

RxCommand capsules a given handler function that can then be executed by its execute method or directly call because it's a callable class. The result of this method is then published through its Observable interface. Additionally it offers Observables for it's current execution state, if the command can be executed and for all possibly thrown exceptions during command execution.

const command = RxCommand.create<int, string>((myInt) => "$myInt");

command.subscribe((s) => print(s)); // Setup the listener that now waits for events, not doing anything

// Somwhere else
command.execute(10); // the listener will print "10"

// or
command(10);

Usage

npm i rx-commands

An RxCommand is a generic class of type RxCommand<TParam = void, TResult = void> where TParam is the type of data that is passed when calling execute and TResult denotes the return type of the handler function. To signal that a handler doesn't take a parameter or returns a null value use void as type. Even if you create a RxCommand<void,void> you will receive a null value when the wrapped function finishes so you can listen for the successful completion.

An example of the declaration

RxCommand < string, List < WeatherEntry >> updateWeatherCommand;
RxCommand < bool, bool > switchChangedCommand;
  • updateWeatherCommand expects a handler that takes a string as parameter and returns a List<WeatherEntry>.
  • switchChangedCommand expects and returns a bool value.

Creating RxCommands

You can create commands that are sync, async (returns promises) or from observables.

const commandSync = RxCommand.create((myInt) => "$myInt");
const commandNoResult = RxCommand.create<void>(() => {
	// Do Something
});
const commandNoParam = RxCommand.createNoParam<number>(() => {
	return 1;
});
const commandNoParamNoReturn = RxCommand.create<void, void>(() => {
	// Do something
});
const commandAsync = RxCommand.create(async () => {
	// do async
});
const commandFromObservable = RxCommand.create(() => of(1, 2, 3));

Options

const command = RxCommand.create(() => {
    throw new Error("Something went wrong");
}, {
	restriction?: Observable<boolean>;
	emitInitialCommandResult?: boolean;
	emitLastResult?: boolean;
	emitsLastValueToNewSubscriptions?: boolean;
	initialLastResult?: TResult;
	debugName?: string;
});

| Option | Description | | -------------------------------- | ---------------------------------------------------------------------------------------------------------------- | | restriction | An observable that will determine if the command can be executed. | | emitInitialCommandResult | Emit the result of the command when the command is created. | | emitsLastValueToNewSubscriptions | Use a Behavior Subject to emit values to new subscriptions | | emitLastResult | Will include the value of the last successful execution in all [CommandResult] events unless there is no result. | | initialLastResult | sets the value of the [lastResult] property before the first item was received. |

Error handling with RxCommands

All exceptions thrown by the wrapped function will be caught and redirected to throwExceptions observable. If you want to react on the, you can listen on the thrownException property.

const command = RxCommand.create(() => {
	throw new Error("Something went wrong");
});

command.thrownExceptions.subscribe((e) => {
	console.error(e);
});

RxListener Sink

You can use RxListener class to handle observables and commands subscriptions.

import { RxListener } from 'rx-commands';

const listeners = new RxListener();
const myObservable = of(1);
const myCommnad = RxCommand.create(() => 1);


listeners.sink = myObservable.subscribe(...);
listeners.sink = myCommand;

// add more than one at time
listeners.add([subscription1, subscription2, command1, command2]);

// Will close all subscriptions and commands
listeners.dispose();

RxListener will subscribe to command thrownExceptions observable and log all errors to console.error. You can disable this behavior gloabaly by setting RxListener.canLogCommandExceptions = false;

RxListener.canLogCommandExceptions = false;

Development

See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md. Thanks! 💖

Contributors

💝 This package was templated with create-typescript-app using the Bingo engine.