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-service/react

v0.1.3

Published

[![Build Status](https://github.com/shared-service/shared-service/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/shared-service/shared-service/actions) [![NPM Version](https://img.shields.io/npm/v/@shared-service/core.svg?style=flat-s

Downloads

5

Readme

SharedService

Build Status NPM Version NPM Version

SharedService is a Javascript library for building multiple tabs app.

  • Use SharedWorker to share UI state between tabs.
  • Make all data and services in SharedWorker.

Demo project

A TODO demo project here:

Online demo open in multiple tabs

Installation

$ npm install @shared-service/core @shared-service/react

Get Started

  1. In React app root endpoint:
import React from 'react';
import ReactDOM from 'react-dom';

import { initSharedService } from '@shared-service/react';

import App from './App';

const worker = new SharedWorker('./worker.js', { type: 'module' });
initSharedService({ port: worker.port });

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
  1. In SharedWorker file worker.js:
import { SharedServiceServer } from '@shared-service/core';

const sharedServiceServer = new SharedServiceServer({
  count: 0,
});

/*global onconnect*/
onconnect = function(e) {
  sharedServiceServer.onNewPort(e.ports[0]);
};
  1. In React component:
import React from 'react';
import { useSharedState } from '@shared-service/react';

export default function App() {
  const [count, setCount] = useSharedState('count', 0);
  return (
    <div className="App">
      <div className="Counter">
        <p>
          Counter:
          {count}
        </p>
        <button type="button" onClick={() => setCount(count + 1)}>
          +1 to global
        </button>
      </div>
    </div>
  );
}

Advanced

Run actions in SharedService

In SharedWorker file worker.js:

sharedServiceServer.registerExecutor('increaseCount', () => {
  const count = sharedServiceServer.getState('count');
  sharedServiceServer.setState('count', count + 1);
});
sharedServiceServer.registerExecutor('markAsCompleted', (id) => {
  const tasks = sharedServiceServer.getState('tasks');
  const updatedTasks = tasks.map(task => {
    if (id === task.id) {
      return {...task, completed: true }
    }
    return task;
  });
  sharedServiceServer.setState('tasks', updatedTasks);
});

In React component:

import React from 'react';
import { useSharedState } from '@shared-service/react';

export default function App() {
  const [count] = useSharedState('count', 0);
  const increaseCount = () => {
    return $sharedService.execute('increaseCount');
  };
  const markAsCompleted = () => {
    return $sharedService.execute('markAsCompleted', ['todo-id']);
  };
  return (
    <div className="App">
      <div className="Counter">
        <p>
          Counter:
          {count}
        </p>
        <button type="button" onClick={increaseCount}>
          +1 to global
        </button>
        <button type="button" onClick={markAsCompleted}>
          Click to mark as completed
        </button>
      </div>
    </div>
  );
}

Data persistence

In SharedWorker file worker.js:

import localforage from 'localforage';

async function initStorage() {
  const storage = localforage.createInstance({
    name: 'myApp',
  });
  await storage.ready();
  const keys = await storage.keys();
  const promises = keys.map((key) =>
    storage.getItem(key).then((data) => {
      sharedServiceServer.setState(key, data);
    }),
  );
  await Promise.all(promises);
  sharedServiceServer.on('stateChange', ({ key, state }) => {
    storage.setItem(key, state);
  });
}

initStorage();

TODO

  • [ ] Support Vue
  • [ ] Run ShareService at browser extension background and normal page
  • [ ] Run ShareService at Electron main and render process