Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/solid bindings #88

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ module.exports = {
moduleNameMapper: {
"@syncedstore/core": "<rootDir>/packages/core/src",
"@syncedstore/yjs-reactive-bindings": "<rootDir>/packages/yjs-reactive-bindings/src",
"solid-js/web": "<rootDir>/node_modules/solid-js/web/dist/web.cjs",
"solid-js/store": "<rootDir>/node_modules/solid-js/store/dist/store.cjs",
"solid-js": "<rootDir>/node_modules/solid-js/dist/solid.cjs",
},
};
24 changes: 22 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"devDependencies": {
"@vue/reactivity": "^3.2.21",
"solid-js": "^1.6.10",
"microbundle": "^0.13.0",
"rimraf": "^3.0.2"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ export function crdtArray<T>(initializer: T[], arr = new Y.Array<T>()) {
if (p === "length") {
return arr.length;
}

// proxy-trap to enable an idiomatic use of arrays and objects in Solid's Flow-component (without spreading)
// if (typeof p === "symbol" && p !== $reactiveproxy && p !== $reactive && p !== $skipreactive) {
// let ic = receiver[$reactiveproxy]?.implicitObserver;
// // (arr as any)._implicitObserver = ic;
// return arr.map((item) => {
// const ret = parseYjsReturnValue(item, ic);
// return ret;
// });
// }

// forward to arrayimplementation
const ret = Reflect.get(target, p, receiver);
return ret;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "yjs";
import { crdtDoc, DocTypeDescription } from "./doc";

export { enableMobxBindings, enableVueBindings } from "@syncedstore/yjs-reactive-bindings";
export { enableMobxBindings, enableVueBindings, enableSolidBindings } from "@syncedstore/yjs-reactive-bindings";
export { Box, boxed } from "./boxed";
export * from "./util";
/**
Expand Down
62 changes: 62 additions & 0 deletions packages/core/test/solid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createEffect, createRoot } from "solid-js";
import * as solid from "solid-js/store";
import { createMutable } from "solid-js/store";

import { enableSolidBindings, syncedStore, Y } from "../src";

describe("solid", () => {
type StoreType = {
arr: number[];
object: {
nested?: number;
};
todosNotBoxed: { text: string; completed: boolean }[];
};

let fnSpy1: jest.Mock<void, []>;
let fnSpy2: jest.Mock<void, []>;
let implicitStore1: StoreType;
let doc1: Y.Doc;
let doc2: Y.Doc;
let store: StoreType;

beforeEach(() => {
enableSolidBindings(solid);
fnSpy1 = jest.fn(() => {});
fnSpy2 = jest.fn(() => {});

doc1 = new Y.Doc();

store = syncedStore(
{
arr: [],
object: {} as { nested?: number },
todos: [],
todosNotBoxed: [],
xml: "xml" as "xml",
},
doc1
);
});

it("indexOf works on store", async () => {
let renderCount = 0;

createRoot(() => {
createEffect(() => {
renderCount++;
// should first log undefined then 'text'
console.log(store.todosNotBoxed[0]);
});
queueMicrotask(() => {
store.todosNotBoxed.push({
text: "text",
completed: false,
});
});
});

await new Promise<void>((resolve) => setTimeout(() => resolve(), 500));
expect(renderCount).toBe(2);
});
});
7 changes: 6 additions & 1 deletion packages/yjs-reactive-bindings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ export function makeYDocObservable(doc: Y.Doc) {
});
}

export { enableMobxBindings, enableReactiveBindings, enableVueBindings } from "./observableProvider";
export {
enableMobxBindings,
enableReactiveBindings,
enableVueBindings,
enableSolidBindings,
} from "./observableProvider";
25 changes: 25 additions & 0 deletions packages/yjs-reactive-bindings/src/observableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ export function enableVueBindings(vue: any) {
customReaction = undefined;
}

/**
* Enable Solid integration
*
* @param solid An instance of Solid, e.g. import * as solid from "solid/store";
*/
export function enableSolidBindings(solid: any) {
customCreateAtom = function (name: any, onBecomeObserved: any) {
let id = 0;
const data = solid.createMutable({ data: id });
const atom = {
reportObserved() {
return data.data as any as boolean;
},
reportChanged() {
data.data = ++id;
},
};
if (onBecomeObserved) {
onBecomeObserved();
}
return atom;
};
customReaction = undefined;
}

export function enableReactiveBindings(reactive: any) {
customCreateAtom = function (name, onBecomeObserved, onBecomeUnobserved) {
// TMP
Expand Down