Skip to content

Commit

Permalink
demo 01
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Qun committed Aug 23, 2024
1 parent f2aadee commit 4e7c523
Show file tree
Hide file tree
Showing 10 changed files with 12,111 additions and 28,785 deletions.
28,780 changes: 0 additions & 28,780 deletions examples/01/package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion examples/01/package.json → examples/demo-01/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"jotai-scheduler": "^0.0.1",
"jotai-scheduler": "^0.0.4",
"jotai": "latest",
"react": "latest",
"react-dom": "latest",
Expand Down
11,981 changes: 11,981 additions & 0 deletions examples/demo-01/pnpm-lock.yaml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions examples/demo-01/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>

<head>
<title>jotai-scheduler example</title>
</head>

<body>
<div id="app"></div>
</body>

</html>
31 changes: 31 additions & 0 deletions examples/demo-01/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.app {
display: flex;
flex-direction: column;
height: 100vh;
}

.header, .footer, .sidebar, .content {
border: 2px solid black;
padding: 20px;
font-size: 24px;
text-align: center;
}


.footer {
margin-top: auto;
}

.body {
display: flex;
flex: 1;
}

.sidebar {
width: 200px;
}

.content {
flex: 1;
}

75 changes: 75 additions & 0 deletions examples/demo-01/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { atom } from 'jotai';
import { useAtomValueWithSchedule, useAtomWithSchedule, ImmediatePriority, LowPriority } from 'jotai-scheduler';
import { useAtom, useAtomValue } from 'jotai';

import './App.css';

const anAtom = atom(0);

const simulateHeavyRender = () => {
const start = performance.now();
while (performance.now() - start < 500) {}
};

const Header = () => {
simulateHeavyRender();
// const num = useAtomValue(anAtom);
console.log('Header Component Render');
const num = useAtomValueWithSchedule(anAtom, {
priority: LowPriority,
});
return <div className="header">Header-{num}</div>;
};

const Footer = () => {
simulateHeavyRender();
// const num = useAtomValue(anAtom);
console.log('Footer Component Render');
const num = useAtomValueWithSchedule(anAtom, {
priority: LowPriority,
});
return <div className="footer">Footer-{num}</div>;
};

const Sidebar = () => {
simulateHeavyRender();
// const num = useAtomValue(anAtom);
console.log('Sidebar Component Render');
const num = useAtomValueWithSchedule(anAtom);
return <div className="sidebar">Sidebar-{num}</div>;
};

const Content = () => {
simulateHeavyRender();
// const [num, setNum] = useAtom(anAtom);
console.log('Content Component Render');
const [num, setNum] = useAtomWithSchedule(anAtom, {
priority: ImmediatePriority,
});
return (
<div className="content">
<div>Content-{num}</div>
<button
onClick={() => {
setNum((num) => ++num);
console.log('trigger');
}}
>
+1
</button>
</div>
);
};

export function App() {
return (
<div className="app">
<Header />
<div className="body">
<Sidebar />
<Content />
</div>
<Footer />
</div>
);
}
8 changes: 8 additions & 0 deletions examples/demo-01/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './App';

const ele = document.getElementById('app');
if (ele) {
createRoot(ele).render(React.createElement(App));
}
File renamed without changes.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "jotai-scheduler",
"version": "0.0.2",
"version": "0.0.4",
"author": "Lincoln",
"repository": {
"type": "git",
"url": "https://github.com/L-Qun/jotai-scheduler"
"url": "https://github.com/jotaijs/jotai-scheduler"
},
"source": "./src/index.ts",
"main": "./dist/index.umd.js",
Expand All @@ -21,12 +21,12 @@
},
"sideEffects": false,
"files": [
"src",
"dist"
],
"scripts": {
"compile": "microbundle build -f modern,umd --globals react=React --no-compress",
"postcompile": "cp dist/index.modern.mjs dist/index.modern.js && cp dist/index.modern.mjs.map dist/index.modern.js.map",
"prepublishOnly": "pnpm run compile && pnpm run postcompile",
"test": "run-s eslint tsc-test jest",
"eslint": "eslint --ext .js,.ts,.tsx .",
"jest": "jest",
Expand Down
2 changes: 1 addition & 1 deletion src/useAtomValueWithSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function useAtomValueWithSchedule<Value>(
for (const listener of listeners) {
addTask({
subscribe: listener,
priority: prioritySubscriptionsMap.get(subscribe)!,
priority: prioritySubscriptionsMap.get(listener)!,
});
}
initiateWorkLoop();
Expand Down

0 comments on commit 4e7c523

Please sign in to comment.