Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: setup node
uses: actions/setup-node@v2
with:
node-version: 18.x
node-version: 20.x
registry-url: https://registry.npmjs.com
- name: install
run: npm install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: setup node
uses: actions/setup-node@v2
with:
node-version: 18.x
node-version: 20.x
registry-url: https://registry.npmjs.com
- name: install
run: npm install
Expand Down
23 changes: 23 additions & 0 deletions examples/yjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint";
import { defineConfig, globalIgnores } from "eslint/config";

export default defineConfig([
globalIgnores(["dist"]),
{
files: ["**/*.{ts,tsx}"],
extends: [
js.configs.recommended,
tseslint.configs.recommended,
reactHooks.configs["recommended-latest"],
reactRefresh.configs.vite,
],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
},
]);
25 changes: 25 additions & 0 deletions examples/yjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.yarn/**
12 changes: 12 additions & 0 deletions examples/yjs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>vite + react + starfx</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
30 changes: 30 additions & 0 deletions examples/yjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "yjs-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"start": "vite --host 0.0.0.0",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.1.0",
"react-dom": "^19.1.0",
"starfx": "file:../..",
"yjs": "^13.6.27"
},
"devDependencies": {
"@types/react": "^19.1.8",
"@types/react-dom": "^19.1.6",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"@vitejs/plugin-react": "^4.6.0",
"eslint": "^8.38.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"typescript": "^5.3.2",
"vite": "^4.3.2"
}
}
42 changes: 42 additions & 0 deletions examples/yjs/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
32 changes: 32 additions & 0 deletions examples/yjs/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
TypedUseSelectorHook,
useDispatch,
useSelector as useSel,
} from "starfx/react";
import "./App.css";
import { AppState, createFolder, schema } from "./thunks.js";

const useSelector: TypedUseSelectorHook<AppState> = useSel;

function App({ id }: { id: string }) {
const dispatch = useDispatch();
const state = useSelector((s) => s);
console.log("state", state);
// const user = useSelector((s) => schema.users.selectById(s, { id }));
// const userList = useSelector(schema.users.selectTableAsList);
return (
<div>
<div>hi there, user.name</div>
<button onClick={() => dispatch(createFolder())}>Fetch users</button>
{/* {userList.map((u) => {
return (
<div key={u.id}>
({u.id}) {u.name}; age {u.age}
</div>
);
})} */}
</div>
);
}

export default App;
69 changes: 69 additions & 0 deletions examples/yjs/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
37 changes: 37 additions & 0 deletions examples/yjs/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { Provider } from "starfx/react";
import { createStore, take } from "starfx";
import { defaultStoreUpdater } from "./store/updater.js";
import { thunks, initialState, schema } from "./thunks.js";
import App from "./App.js";
import "./index.css";

init();

function init() {
const store = createStore({
initialState,
setStoreUpdater: defaultStoreUpdater,
});
// makes `fx` available in devtools
(window as any).fx = store;

store.initialize([
function* logger() {
while (true) {
const action = yield* take("*");
console.log("action", action);
}
},
thunks.register,
]);

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<Provider schema={schema} store={store}>
<App id="1" />
</Provider>
</React.StrictMode>
);
}
21 changes: 21 additions & 0 deletions examples/yjs/src/store/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
type FxMap,
type FxSchema,
type StoreUpdater,
updateStore,
} from "starfx";

export function createSchema<
O extends FxMap,
S extends { [key in keyof O]: ReturnType<O[key]>["initialState"] }
>(): [FxSchema<S, O>, S] {
const initialState = {} as S;
function* update(ups: StoreUpdater<S> | StoreUpdater<S>[]) {
return yield* updateStore(ups);
}

const db = {} as FxSchema<S, O>;
db.update = update;

return [db, initialState];
}
32 changes: 32 additions & 0 deletions examples/yjs/src/store/updater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as Y from "yjs";
import type { AnyState, UpdaterCtx, Next, Operation } from "starfx";

export const defaultStoreUpdater = <S extends AnyState>(
setState: (state: S) => void,
getState: () => S
) => {
console.log("Creating Y.Doc");
const ydoc = new Y.Doc({ autoLoad: true });
const root = ydoc.getMap();

const data = new Y.Map();
root.set("data", data);
data.set("items", new Y.Array());

root.observeDeep((events, transaction) => {
console.log("Y.Doc changed", { events, transaction });
setState(root.toJSON() as S);
});

function* updateMdw(ctx: UpdaterCtx<S>, next: Next) {
ydoc.transact(() => ctx.updater(root));
console.log({ updater: ctx.updater });
setState(root.toJSON() as S);
yield* next();
}

const initializeStore: () => Operation<void> = function* () {
setState(root.toJSON() as S);
};
return { updateMdw, initializeStore };
};
31 changes: 31 additions & 0 deletions examples/yjs/src/thunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createThunks, mdw, StoreContext } from "starfx";
import { createSchema } from "./store/schema.js";

export const [schema, initialState] = createSchema();
export type AppState = typeof initialState;

export const thunks = createThunks();
// catch errors from task and logs them with extra info
thunks.use(mdw.err);
// where all the thunks get called in the middleware stack
thunks.use(thunks.routes());
thunks.use(function* (ctx, next) {
console.log("last mdw in the stack");
yield* next();
});

export const createFolder = thunks.create<any>("/users", function* (ctx, next) {
console.log("Creating folder", ctx);
yield* schema.update((root) => {
const yarray = root.get("data").get("items");
yarray.push([
{
id: Date.now().toString(),
name: "New folder",
children: [],
},
]);
});

yield* next();
});
1 change: 1 addition & 0 deletions examples/yjs/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
Loading
Loading