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

smallorange-reactive-store

v1.1.13

Published

A lighweight set of reactive Models and Collections built on top of RxJS and Lodash.

Downloads

15

Readme

CircleCI

About Small Orange Reactive Store

A lighweight set of reactive Models and Collections built on top of RxJS and Lodash. It gives structure to web applications by providing models and collections with a rich API.

Data is represented by models, which can optionally belongs to a collection, that can be created, destroyed. Models and collections are built on top of RxJS, and they can be subscribed as them. So, any changes is going to be reflected on view state, keeping data in sync.

Model

A model manages entity properties, and takes care to broadcast changes along other application entities.

Collections

Collections helps you to group models, and encapsulate actions for them, like create, destroy. Further, it can sort models.

Model API

Properties

	cuid: string = Model's unique identifier;
	id: string | number = Attribute used as id defined by idAttribute;
	idAttribute: string = Attribute to be used as id;
	destroyed: boolean = true after destroy;
	lastSet: number = Last set's timestamp;
	stream: Observable = Events' stream;
	replayStream: Observable = Last events' stream;
	isNew: boolean = When there is no id yet;
	resource: string = Resource identifier, prefixes model.id at url;
	url: string = model.resource/model.id or collection.resource/model.resource/model.id or collection.resource/model.id;
	defaults: {[attributeName: string]: any} = Define default attributes;
	validate: (attributtes: object) => object = Define validation rules;
	parse: (attributtes: object) => object = Define parse rules;
	collection: Collection = The collection the models belongs to, usually is settled automatically by collection;
	typedAttributes: {[attributeName: string]: typeof Model | typeof Collection} = Allows define attributes which will be converted into Models or Collections, but behaves like any other attribute, and also report changes to parent Model

Methods

	constructor(attributes: object, options: object = null, extend: object = null);
	on(...filter: string, [replay: boolean = false | debounce: number]): Observable<any>;
	once(...filter: string, [replay: boolean = false | debounce: number]): Observable<any>;
	trigger(key: string, data: any = null): void;
	set(attributes: object, options: object = {replace: false, merge: true, silent: false, validate: true, typedAttributes: object = {[attribute: string]: [Model.set options] | [Collection.set options]}}): Model;
	unset(attribute: string, options: object = {silent: false}): Model;
	reset(options: object = {silent: false}): Model;
	destroy(options: object = {silent: false}): Model;
	toJS(): object;
	get(attribute: string, defaultValue: any = null): Model;
	has(attribute: string): boolean;
	keys(): Array<string>;
	values(): Array<string>;
	pick(...values): any;
	omit(...values): any;
	equals(key: string, value: any): boolean;

Usage Sample

	import {
		Model,
		rxjs
	} from 'smallorange-reactive-store';

	export class Message extends Model {
		idAttribute = '_id';
	}

Collection API

Properties

	cuid: string = Collection's unique identifier;
	id: string | number = Attribute used as id defined by idAttribute;
	destroyed: boolean = true after destroy;
	lastSet: number = Last set's timestamp;
	stream: Observable = Events' stream;
	replayStream: Observable = Last events' stream;
	resource: string = Resource identifier, prefixes model.id at url;
	url: string = collection.resource/collection.id or collection.id;
	defaultOrder: {[attributeName: string]: 'asc' | 'desc'} = Default collection order;
	parse: (args: Array<Model | object> | Model | object) => Array<Model | object> = Define parse rules;

Methods

	constructor(model: Model, models: Array<Model> | Array<object> | Model | object, options: object = null, extend: object = null);
	on(...filter: string, [replay: boolean = false | debounce: number]): Observable<any>;
	once(...filter: string, [replay: boolean = false | debounce: number]): Observable<any>;
	trigger(key: string, data: any = null): void;
	toJS(): Array<object>;
	get(id?: string | number): Model | Array<Model>;
	getOne(id: string | number): Model;
	has(id: string | number): boolean;
	set(models: object | Array<object> | Model | Array<Model>, options: object = {replace: false, merge: true, reset: false, silent: false, validate: true}): Array<Model>;
	reset(options: object = {silent: false}): Collection;
	destroy(options: object = {silent: false}): Collection;
	orderBy(iteratees: Array<string>, orders: Array<string>): Array<Model>;
	map(iteratee: Function | string): Array<Model>;
	reduce(iteratee: Function): Array<Model>;
	each(iteratee: Function): void;
	invokeMap(path: string, ...args): Array<Model>;
	every(predicate: Function | string): boolean;
	some(predicate: Function | string): boolean;
	max(predicate: Function | string): number;
	min(predicate: Function | string): number;
	keyBy(predicate: Function | string): object;
	groupBy(predicate: Function | string): object;
	filter(predicate: Function | string): Array<Model>;
	reject(predicate: Function | string): Array<Model>;
	find(predicate: Function | string, last: boolean = false): Model;
	findInde	x(predicate: Function | string, last: boolean = false): number;
	first(): Model;
	last(): Model;
	initial(amount?: number): Array<Model>;
	tail(amount?: number): Array<Model>;
	size(): number;
	keep(length: number): void;
	keepLast(length: number): void;

Usage Sample with React

	// collection
	import _ from 'libs/lodash';
	import {
		Collection
	} from 'smallorange-reactive-store';
	import {
		Message
	} from 'models';
	import {
		transport
	} from 'libs';

	export class Messages extends Collection {
		static defaultNick = `sir-${Date.now()}`;

		constructor(thread) {
			super(Message);

			this.thread = thread;

			transport
				.fetch()
				.subscribe(::this.set);
		}

		set nick(name) {
			this._nick = name;
		}

		get nick() {
			return this._nick || Messages.defaultNick;
		}

		send(message) {
			const _message = {
				_thread: this.thread,
				_id: Date.now(),
				nick: this.nick,
				message
			};

			this.set(_message);

			return transport.post(_message);
		}
	}

	// component
	import _ from 'libs/lodash';
	import React from 'react';
	import {
		Component
	} from 'smallorange-reactive-store-react';
	import {
		Messages as MessagesCollection
	} from 'collections';

	export class Messages extends Component {
		constructor(props) {
			super(props);
			
			this.messages = new MessagesCollection();
			this.state = {
				lastSet: null
			};
		}

		componentDidMount() {
			this.messages.on('set')
				.takeUntil(this.once('destroy'))
				.map(() => ({
					lastSet: this.messages.lastSet
				}))
				.subscribe(::this.setState);
		}

		shouldComponentUpdate(nextProps, nextState){
			return nextState.lastSet !== this.state.lastSet;
		}

		render() {
			return (
				<div>
					<label>Messages</label>
					<ul>
						{this.messages
							.map(model => (
								<li key={model.cid}>
									{model.get('nick')} said: {model.get('message')}
								</li>
							)
						)}
					</ul>
				</div>
			);
		}
	}