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

observers

v0.5.2

Published

A simple implentation of JavaScript observables

Downloads

9

Readme

Observers

A light implementation of JavaScript observables

NPM version Travis build status

Installation

npm install observers --save

Sample

import obs = require('observers');

var obj = obs.observe({ key: 'value' });
assert(obj().key === 'value');

obj.subscribe(val => assert(val.key === 'newvalue'));
obj({ key: 'newvalue' }); 


var array = obs.observeArray(['a', 'b', 'c']);

assert(array.join('') === 'abc');

array.push('d');

array.find(v => v === 'a'); // 'a'

array.findIndex(v => v === 'd'); // 3

array.filter(v => v < 'c'); // '['a', 'b']

array.map(v => v === 'd' ? 'e' : v); // ['a', 'b', 'c', 'e']

array.remove(v => v > 'c'); // ['a', 'b', 'c']

// ...

API

observe

function <T>observe(value?: T): Observable<T>;

// Example
var anObservable = obs.observe(5);
var another = obs.observe(5);
another(7);

observeArray

function <T>observeArray(values?: Array<T>): ObservableArray<T>;

// Example
var obsArray = obs.observeArray([1,2,3,4,5,6]);

computed

Computed observables that depend on other observables (object, array or computed) will re-evaluate when their dependent observables are modifed.
See the below example:

function <T>computed(evaluator: () => T): Computed<T>;

// Example (using above example values)
var comp = obs.computed(() => anObservable() + another()); // 12
comp.subscribe(value => console.log(`My computed changed to ${value}`);
another(9); // Will cause the computed to re-evaluate and notify subscribers 

Shared functions

All observables (Obervable, ObservableArray, Computed) have the following functions:

getter

Returns the current value of an observable with no side effects

function (): any;

// Example
var someNumber = obs.observe(12);
someNumber(); // Returns 12 with no side effects

subscribe

Takes a function that will be called with the new value of an observable

function subscribe(func: (newValue: T) => void): void;

// Example
someComputed.subscribe(newValue => $.post('/api/something', { data: newValue });

removeSubscribers

Remove all listener functions on a specific observable.
Warning: May have undesirable side effects.

function removeSubscribers();

// Example
someObservable.removeSubscribers();

Supported array functions

every

function every(predicate: (value: T) => boolean): boolean;

find

function find(predicate: (value: T) => boolean): T;

findIndex

function findIndex(predicate: (value: T) => boolean): number;

filter

function filter(predicate: (value: T) => boolean): Array<T>;

join

function join(seperator?: string): string;

map

function map(predicate: (value: T) => any): Array<any>;

pop

function pop(): T;

push

function push(value: T): number;

reverse

function reverse(): Array<T>;

reduce

function reduce(predicate: (previous: U, current: T, index: number, array: Array<T>) => U): U;

remove

Returns an array of the removed items

function remove(predicate: (value: T) => boolean): Array<T>;

removeAll

Returns an array of the removed items

function removeAll(): Array<T>;

removeSubscribers

Clears the list of subscribers

function removeSubscribers(): void;

shift

function shift(): number;

slice

function slice(start?: number, end?: number): Array<T>;

sort

function sort(comparer: (left: T, right: T) => number|boolean): Array<T>;

splice

function splice(start?: number, end?: number): Array<T>;

subscribe

Subscribes to changes to the observable

function subscribe(callback: (newValue: Array<T>) => void): void;

some

function some(predicate: (value: T) => boolean): boolean;

unshift

function unshift(...values: T[]): number;

update

Returns the newValue if successful.
Returns undefined if unsuccessful.

function update(predicate: (value: T) => boolean, newValue: T): T;