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

@swear-js/core

v3.1.0

Published

Simple observer state manager

Downloads

13

Readme

🍭 Swear JS

@swear-js/core


npm npm type definitions npm bundle size GitHub

Core package of SwearJS state manager

Frameworks:

Demo

Demo application is runnable via npx:

  • [Framework agnostic] $ npx swear-demo-agnostic

Don't forget to remove created project directory after

Features


  • 🍥 Simple abstraction. No need to get into FLUX, life cycle, the flow. You just have a state, and have functions(actions) that somehow mutates that state. Just like React's useState. No way simpler.
  • 🍩 Deep type inferences. No need to guess what type you're working with(hey, Redux)!
  • 🍡 Code splitting. Your swears can lay wherever you want. Even in the scope of another microapplication.
  • 🧊 Framework-agnostic. If your framework has no support, or you just simply don't use one, core package is for you.
  • 🪡 Small size. 584B
  • 🐞 Logging. Store has default support for logging. And SwearJS has it's our own beautiful logger

Installation

$ npm install @swear-js/core

or in case you are using Yarn:

$ yarn add @swear-js/core

Usage


First you have to initialize your store.

import { createStore, createSwear } from "@swear-js/core";

const store = createStore();

// Create swear
const countSwear = createSwear('count', 0, (mutate) => ({
   increase: () => mutate(prev => prev + 1),
   decrease: () => mutate(prev => prev - 1),
}));

// Callback is triggered on every swear update
store.subscribe(countSwear, (newValue) => {
    // Handle updates of countSwear
    console.log(newValue);
});

// This will return object of actions with default actions: set() and reset(), and your own defined actions.
const actions = store.getSwearActions(countSwear);
const { set, reset, increase, decrease } = actions; // set() and reset() are default actions

increase(); // When you dispatch an action, swear value is mutated, and subscribtion callback is triggered.

Default actions

store.getSwearActions() returns an object of combined default actions and your actions.

set() action implements the same usage interface as React's useState setter. It means you can pass both a payload, and callback with previous value. Example: set((prev) => prev + 10);

Logging


You can pass your custom logger to the store, or use @swear-js/logger. Swear-js logger usage:

import { createStore } from "@swear-js/core";
import { swearLogger } from "@swear-js/logger";

const store = createStore({ onPatch: swearLogger });

In order to implement your own logger solution, you just have to keep in mind an API of onPatch argument of the store. onPatch is any callback that get SwearPatch(you can find it in package) type as an argument. Simply SwearPatch is:

{
  swearId, // String value which gets a name of patched swear state
  tag, // String value which gets an optional tag, you want to set
  prev, // Previous store state (object)
  payload, // Payload passed
  next // Current store state
}

Example very-simple implementation of logger:

import { SwearPatch, createStore } from "@swear-js/core";

const logger = ({ swearId, tag, prev, payload, next }: SwearPatch) => {
    console.log(`Swear: ${swearId}, Tag: ${tag}, Payload: ${payload}`);
    console.log(`Previous state: ${prev}`, `Current state: ${next}`);
};

const store = createStore({ onPatch: logger });

Inspired by @artalar's Reatom.