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

yjs-normalized

v0.0.7

Published

Yjs extension that offers generic maintainer and observer classes for normalized semi-structured data

Downloads

25

Readme

yjs-normalized

This yjs extension offers generic maintainer and observer classes for normalized semi-structured data.

When dealing with nested or relational data in yjs applications, using a normalized state shape can be beneficial. This approach involves storing data in a flat structure similar to a database, which offers several benefits:

  • Consistency: No duplicated data
  • Simplicity: Nested data increases the complexity when observing for certain changes

The concept is based on the section: normalizing state shape in the redux documentation.

Example structure:

{
    "cells":{
        "byId":{
            "abc":{
                "id":"abc"
            },
        },
        "allIds":["abc"]
    }
}

This library comes with 2 features: Maintainer and Observer

Maintainer

This class provides methods to add, delete, update, and manipulate objects within a Yjs Y.Map, ensuring that all changes are made within a transaction to maintain consistency.

Features

  • Add Objects: Add new objects to the collection.
  • Delete Objects: Remove objects from the collection, with optional cascade functionality.
  • Update Objects: Modify existing objects.
  • Reorder Objects: Change the position of objects in the collection.
  • Manage Property Arrays: Add, remove, and reorder elements in array properties of objects.

Example Implementation

Define a maintainer for a certain normalized table:

export default class CellsMaintainer extends Maintainer<ICell> {
}

Initialize the maintainer:

    const cellsMaintainer = new CellsMaintainer(
      ydoc.getMap('cells'),
      ydoc.transact  
    );

Usage:

    const cell: ICell = {...};
    cellsMaintainer.changeObject(cell);

Observer

In general, observers facilitate the observation of semi-structured, normalized data within a Yjs Y.Map, track changes to the data and dispatch events accordingly.

RootObserver

The RootObserver class is an abstract class that primarily functions as a root data observer and secondarily acts as a registry for sub-observers that track more granular changes within the data.

The class defines several abstract dispatcher methods (rootDispatcher, addDispatcher, deleteDispatcher, updatePropertyDispatcher, and allIdsDispatcher) that subclasses must implement to handle various types of data updates. These methods enable dispatching events accordingly.

  • Root Dispatcher: Dispatches the entire normalized state.
  • Add Dispatcher: Dispatches an event to add an object.
  • Delete Dispatcher: Dispatches an event to delete an object by its id.
  • Update Property Dispatcher: Dispatches an event to update a specific property of an object.
  • All Ids Dispatcher: Dispatches an event to update the list of all object ids.

Sub-observers

Sub-Observers are managed by the library itself. Following sub-observers are created / deleted and listened to during runtime:

  • AllIdsObserver: Observes Changes in the order of the allIds Y.Array
  • ByIdObserver: Observes Additions and Deletions of Objects with in the byId Y.Map
  • ObjectObserver: Observes changes in each Y.Map within the byId Y.Map

Example Implementation

Users can integrate this class with their preferred state management library (e.g., Redux, MobX).

export class CellsObserver extends RootObserver<ICell> {
    protected rootDispatcher: (payload: RootDispatch<ICell>) => void = (payload) => {
        //store.dispatch(setCells(payload))
    }
    protected addDispatcher: (payload: AddDispatch<ICell>) => void = (payload) => {
        //store.dispatch(addCell(payload))
    };
    protected deleteDispatcher: (payload: DeleteDispatch) => void = (payload) => {
        //store.dispatch(deleteCell(payload))
    };
    protected updatePropertyDispatcher: (payload: UpdatePropertyDispatch) => void = (payload) =>{
        //store.dispatch(updateCellProperty(payload))
    };
    protected allIdsDispatcher: (payload: AllIdsDispatch) => void = (payload) => {
        //store.dispatch(updateCellsAllIds(payload))
    };
}