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

data-synchronizer

v0.0.6

Published

A common library for transferring data to multi-page applications

Downloads

101

Readme

data-synchronizer

A versatile library for transferring data across multi-page applications.

Background

In multi-page applications, each page operates in a different JavaScript context. This means that typical methods such as using global variables, publish-subscribe patterns, or observers do not work for data transfer across pages. To address this challenge, I developed this library. If your application is a single-page app, consider using other libraries better suited for that environment.

Requirements

ES5 & Browser

Installation

Using npm:

npm install data-synchronizer --save

Using yarn:

yarn add data-synchronizer

Using pnpm:

pnpm install data-synchronizer --save

Functional Example Here's an example with two pages: list and details.

// list.vue
import { ref } from 'vue';
import { useDataSynchronizer } from 'data-synchronizer';

const channel = 'cancelLike';

type ListItem = {
  id: number;
  title: string;
  like: number;
}

// Example: the list contains 100 items
const list = ref<ListItem[]>([
  { id: 0, title: 'learning javascript', like: 2 },
  // ...
  { id: 99, title: 'learning javascript', like: 9 },
]);

const { onMessage, onSendMessageError, close } = useDataSynchronizer({
  // engine: 'LocalStorage' | 'BroadcastChannel' // optional
});

onSendMessageError(channel, (event: DOMException | MessageEvent) => {
  console.error(event);
});

onMessage(channel, (params: ListItem) => {
  const target = list.value.find(item => item.id === params.id);
  if (target) target.like = params.like;
  // The like count of the target will decrease by 1.
});

close(); // Calling close will stop receiving data.
// details.vue
import { useDataSynchronizer } from 'data-synchronizer';

const channel = 'cancelLike'; // The channel must match the previous one.

const { sendMessage } = useDataSynchronizer({
  // engine: 'LocalStorage' | 'BroadcastChannel' // optional
});

type ListItem = {
  id: number;
  title: string;
  like: number;
}

const cancelLike = (item: ListItem) => {
  item.like--;
  sendMessage(channel, item);
};

Class-based Example

import { ref } from 'vue';
import { DataSynchronizer } from 'data-synchronizer';

const channel = 'cancelLike';

type ListItem = {
  id: number;
  title: string;
  like: number;
}

// Example: the list contains 100 items
const list = ref<ListItem[]>([
  { id: 0, title: 'learning javascript', like: 2 },
  // ...
  { id: 99, title: 'learning javascript', like: 9 },
]);

const instance = new DataSynchronizer({
  // engine: 'LocalStorage' | 'BroadcastChannel' // optional
});

instance.onSendMessageError(channel, (event: DOMException | MessageEvent) => {
  console.error(event);
});

instance.onMessage(channel, (params: ListItem) => {
  const target = list.value.find(item => item.id === params.id);
  if (target) target.like = params.like;
  // The like count of the target will decrease by 1.
});

instance.close(); // Calling close will stop receiving data.
// details.vue
import { DataSynchronizer } from 'data-synchronizer';

const channel = 'cancelLike'; // The channel must match the previous one.

const instance = new DataSynchronizer({
  // engine: 'LocalStorage' | 'BroadcastChannel' // optional
});

type ListItem = {
  id: number;
  title: string;
  like: number;
}

const cancelLike = (item: ListItem) => {
  item.like--;
  instance.sendMessage(channel, item);
};

Types

DataSynchronizer

type Engine = 'LocalStorage' | 'BroadcastChannel';

export type ChannelKey = string | string[];

type Options = {
  engine?: Engine; // Default: 'BroadcastChannel'
};

export type OnCallback = (args: any) => void;

export type OnSendMessageErrorCallback = (error: MessageEvent | DOMException) => void;

export type OnMessageMethod = (channel: ChannelKey, callback: OnCallback) => void;

export type SendMessageMethod = <T>(channel: ChannelKey, data: T, target?: SendTarget) => void;

export type OnSendMessageErrorMethod = (channel: ChannelKey, callback: OnSendMessageErrorCallback) => void;

export type CloseMethod = (channel: ChannelKey) => void;

export type SendTarget = RegExp | string | undefined;

export type EngineOptions = {
  engine: Engine;
  support: boolean;
  onMessage: OnMessageMethod;
  sendMessage: SendMessageMethod;
  onSendMessageError: OnSendMessageErrorMethod;
  close: CloseMethod;
};

type DataSynchronizer = (options: Options) => EngineOptions;

Future Enhancements

[1] Targeted Receivers Currently, the library broadcasts data, meaning that all pages with the same origin receive the data. This may not always be ideal. [Completed]

[2] Server-side Data Transfer At present, the library only supports data transfer within the same browser. In the future, I plan to explore cross-browser data transfer!

[3] Plugins or Middleware [4] Custom Engines