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

shared.js

v0.0.3

Published

A factory for a passive mediator design pattern in <25 lines of Javascript

Downloads

15

Readme

shared.js Build Status

A factory for a passive mediator design pattern in <20 lines of Javascript

Use case:

The motivation of this project was to easily share state amongst React Native components allowing for lose coupling between them. This allows a component to update the state and the others to see the update, and is a light weight alternative to using an event emitter since it does not use callbacks. Rather it is up to the Observers to check the state changes.

Installation

Use npm install shared.js --save

Example

A use case could include a button that needs to check the state of multiple on/off switches before proceeding. Rather than coupling the switches to the button, a shared object can be used by the switches to change state,and the button to perform a check when it is pressed.

var Share = require('shared.js');
var id = 'buttonTest';
Share.getShare(id).canProceed = function() {

  for (var internal in this) {
    if (internal.indexOf('toggle') !== -1) {
      if (!this[internal]) {
        return false;
      }
    }
  }
  return true;
};

Share.getShare(id).switch = function() {
  Share.getShare(id)[this.name] ?
    Share.getShare(id)[this.name] = false : Share.getShare(id)[this.name] = true;
};
var toggle1 = {
  name: 'toggle1',
  share: Share.getShare(id, 'toggle1', false),
  toggle: Share.getShare(id).switch
};


var toggle2 = {
  name: 'toggle2',
  share: Share.getShare(id, 'toggle2', false),
  toggle: Share.getShare(id).switch
};

var button = {
  share: Share.getShare(id),
  press: function() {
    return this.share.canProceed();
  }
};

console.log(button.press());// false
toggle1.toggle();
console.log(button.press());// false
toggle2.toggle();
console.log(button.press());// true

// lets add a new toggle
var toggle3 = {
  name: 'toggle3',
  share: Share.getShare(id, 'toggle3', false),
  toggle: Share.getShare(id).switch
};
console.log(button.press()); // false
toggle3.toggle();
console.log(button.press());// true

Functions

  • getShare (id, property, initialValue) - retrieve a shared state object as identified by the id argument. All arguments are optional and if none are passed the function returns a default system-wide share object. The same system-wide object is returned when getShare is called without arguments.

MIT Licenced