joebobmiles / zustand-middleware-yjs

Zustand middleware that enables sharing of state between clients via Yjs.
MIT License
103 stars 10 forks source link

A simple example? #55

Closed KenorFR closed 11 months ago

KenorFR commented 11 months ago

Hello, I'm sorry to bother you for nothing. In principle, your middleware seems perfect, but after several attempts, it's impossible to get it to work with WebRTC (piesocket in my example).

I can't see what's wrong, even if I turn the code upside down, so I made a basic code to create my "store", do you see anything shocking?

Namely that the setter works correctly without the middleware, I have other middleware that also works perfectly. I achieve a functional (basic) result by playing with the "storage" of "persist" to send messages directly, and intercept messages, so PieSocket is not the problem either (and the connection is indicated as being functional).

In short, I don't see :

import { create } from "zustand";
import * as Y from "yjs";
import { WebrtcProvider } from "y-webrtc";
import yjs from "zustand-middleware-yjs";

const ydoc = new Y.Doc();
const api_key = 'XXXX';
const docName = "doc_name";
const roomId = 1;
const wsUrl = `ws://free.blr2.piesocket.com/v3/${roomId}?api_key=${api_key}`;

new WebrtcProvider(docName, ydoc, {
    signaling: [ wsUrl ],
});

const createWithMiddleware = (storeConfig) => {
    return create(yjs(ydoc, "shared", storeConfig));
}

export const useAppConfigStore = createWithMiddleware(
    (set) => ({
        appConfig: {
            test: 'is a test',
        },
        setAppConfig: (newConfig) => {
            set(state => ({
                appConfig: {
                    ...state.appConfig,
                    ...newConfig
                }
            }))
        },
    })
);

Thank you very much in advance

joebobmiles commented 11 months ago

I can't help you if I don't know what behavior you are expecting. The code as you have written it should work. Since I don't have examples of the behavior you are expecting and the behavior you are receiving from the middleware, I can't help you.

I'm going to close this issue with "invalid" and encourage you to resubmit using the Bug Report template. When you do, please describe in detail what behavior you were expecting, what behavior you are seeing, and an explanation of how to reproduce the behavior you are seeing.