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

m-model-common

v0.0.11

Published

m-model-common is designed to be used with [m-model-core](https://www.npmjs.com/package/m-model-core) in browsers. It supports synchronous storages, like localStorage and sessionStorage. You can easily make your storage in sync with our model and even s

Downloads

16

Readme

m-model-common

m-model-common is designed to be used with m-model-core in browsers. It supports synchronous storages, like localStorage and sessionStorage. You can easily make your storage in sync with our model and even spread data into other tabs too.

Please, check m-model-core first

Creating model

// models/book.ts
import { createModel, createCRUDActionTypes } from "m-model-core";
import {
	getDefaultStorageSettings,
	getDefaultReducer,
	loadFromStorage,
	filterByLoadTime,
} from "m-model-common";
import { store } from "../store"; // you will see this file later

interface IBook {
	uuid: string;
	title: string;
	description: string;
}

const storageSettings = getDefaultStorageSettings("books", localStorage); // we will save documents in localStorage with item key `books`
const isLoadedRecentlyEnough = filterByLoadTime(2 * 60 * 1000); // only load documents that have been loaded 2 hours ago

const BookBaseModel = createModel<"uuid", IBook>({
	keyOfId: "uuid",
	dockeys: ["uuid", "title", "description"],
	actionTypes: createCRUDActionTypes("BOOK"),
	getInstances: (() => store.getState().books) as any,
	dispatch: (action => store.dispatch(action)) as any,
	subscribe: (listener => store.subscribe(listener)) as any,
	loadInstancesFromStorage: () =>
		loadFromStorage({
			storage: storageSettings.storage,
			key: storageSettings.itemName,
			filter: isLoadedRecentlyEnough,
		}),
});

export class Book extends BookBaseModel {
	static getRanking(book: Book) {
		const docs = this.getAllSync();
		let rating = 1;
		for (const doc of docs) {
			if (doc.rating > book.rating) {
				rating++;
			}
		}
		return rating;
	}
}

export const booksReducer = getDefaultReducer(storageSettings, () => Book); // use booksReducer instead of Book.reducer if you want to save data in (local)storage and support data synch accross tabs

In this example, we will use redux as our state container. To create store, we can cimply write

// store.ts
import { combineReducers } from "redux";
import { booksReducer } from "./models/book";

const appRootReducer = combineReducers({
	//... other reducers
	books: booksReducer,
	//... other reducers
});
export const store = createStore(appRootReducer);

And after creation of store, you can simply call initialize method to load documents from storage

Book.initialize();

Refreshing the browser will load the previously loaded documents in model after initialize method is invoked.

Now, to listen changes made in other tabs, simply add those lines once for every store (not for every model)

import { handleActionOfOtherTab } from "m-model-common";

window.addEventListener("storage", e => { // listen to localStorage change
	handleActionOfOtherTab(e, store.dispatch);
});

Voilà! Updating/saving any data instance in one tab will be perceived by other models in other tabs.

Extra

Documentation for MetaInformation class and usage of it will be added soon!