Skip to content

Commit

Permalink
🧱 atomWithBroadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
neila-a committed Jun 14, 2024
1 parent 2ffe49a commit dd94e13
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 54 deletions.
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@verkfi/core",
"version": "1.7.2",
"devVersion": "964",
"dev": false,
"version": "1.7.3",
"devVersion": "972",
"dev": true,
"description": "Platform for Neila's something useless tools.",
"private": true,
"repository": "github:neila-a/verkfi",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/app/declare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export interface BeforeInstallPromptEvent extends Event {
* Returns a Promise that resolves to a DOMString containing either "accepted" or "dismissed".
*/
readonly userChoice: Promise<{
outcome: "accepted" | "dismissed",
platform: string
outcome: "accepted" | "dismissed";
platform: string;
}>;

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export default function MouseOverPopover(props: {
{props.children}
</Box>
<Popover classes={{
root: "mouse-over-popover" /* 暂时没用,仅作为标记 */
/**
* 暂时没用,仅作为标记
*/
root: "mouse-over-popover"
}} sx={{
pointerEvents: "none",
zIndex: "38602"
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/atoms/recommend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import getTries from "./getTries";
import atomWithInitialValue, {
valueAtomReturn
} from "../../reader/atomWithInitialValue";
import {
emptySymbol
} from "../../reader/atomWithStorage";
const [recommendAtom] = atomWithInitialValue((valueAtom: valueAtomReturn<tool[]>) => atom(get => {
const got = get(valueAtom);
if (typeof got === "symbol") {
if (got === emptySymbol) {
return getTries(get);
}
return got;
Expand Down
43 changes: 43 additions & 0 deletions packages/shared/reader/atomWithBroadcast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export default function atomWithBroadcast<Value, Arg, Result>(read: (get: Getter) => Value, write: Write<Arg, Result>, broadcastChannelId: string) {
const created = atom(read, (get, set, update: Arg | {
__atom_update_type__: "message",
update: Arg;
}) => {
let message = false;
if (typeof update === "object") {
if ("__atom_update_type__" in update) {
if (update.__atom_update_type__ === "message") {
write(get, set, update.update);
message = true;
}
}
}
if (!message) {
write(get, set, update as Arg);
const channel = new BroadcastChannel(broadcastChannelId);
channel.postMessage(update);
}
});
created.debugLabel = broadcastChannelId;
created.onMount = setAtom => {
const channel = new BroadcastChannel(broadcastChannelId);
channel.addEventListener("message", event => {
setAtom({
__atom_update_type__: "message",
update: event.data
});
});
};
return created;
}
import {
Atom,
WritableAtom,
atom
} from "jotai";
/**
* @source jotai/atom.d.ts
*/
type Getter = <Value>(atom: Atom<Value>) => Value;
type Setter = <Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, ...args: Args) => Result;
type Write<Arg, Result> = (get: Getter, set: Setter, arg: Arg) => Result;
59 changes: 14 additions & 45 deletions packages/shared/reader/atomWithStorage.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,19 @@
import {
atom
} from "jotai";
import setSetting from "./setSetting";
import settingReader from "./settingReader";
import atomWithBroadcast from "./atomWithBroadcast";
import atomWithInitialValue, {
valueAtomReturn
} from "./atomWithInitialValue";
import setSetting from "./setSetting";
import settingReader from "./settingReader";
export const emptySymbol: unique symbol = Symbol("This atom is empty, it's waiting a value.");
const atomWithStorage = <setting = any>(id: string, name: string, empty: setting) => atomWithInitialValue((valueAtom: valueAtomReturn<setting>) => {
const packagedAtom = atom(get => {
const got = get(valueAtom);
if (typeof got === "symbol") {
const value = settingReader(id, empty);
return value;
}
return got;
}, (get, set, update: setting | {
__atom_update_type__: "message",
update: setting;
}) => {
let message = false;
if (typeof update === "object") {
if ("__atom_update_type__" in update) {
if (update.__atom_update_type__ === "message") {
set(valueAtom, update.update);
message = true;
}
}
}
if (!message) {
set(valueAtom, update as setting);
setSetting(id, name, update);
const channel = new BroadcastChannel(`atomWithStorage-${id}`);
channel.postMessage(update);
}
});
packagedAtom.debugLabel = id;
packagedAtom.onMount = setAtom => {
const channel = new BroadcastChannel(`atomWithStorage-${id}`);
channel.addEventListener("message", event => {
setAtom({
__atom_update_type__: "message",
update: event.data
});
});
};
return packagedAtom;
})[0];
const atomWithStorage = <setting = any>(id: string, name: string, empty: setting) => atomWithInitialValue((valueAtom: valueAtomReturn<setting>) => atomWithBroadcast(get => {
const got = get(valueAtom);
if (got === emptySymbol) {
const value = settingReader(id, empty);
return value;
}
return got;
}, (get, set, update: setting) => {
set(valueAtom, update);
setSetting(id, name, update);
}, `storagedAtom-${id}`))[0];
export default atomWithStorage;
2 changes: 1 addition & 1 deletion packages/tools/tool-countletter/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function CountLetter(): JSX.Element {
}} title={get("输入字符")} label={get("在这里输入")} />
<AlertDialog open={alertDialogOpen} title={get("输出")} description={out} onDone={() => {
setAlertDialogOpen(false);
}} /> {/* 输出对话框容器 */}
}} />
</Box>
<FormControl component="section">
<FormLabel id={systemId}>
Expand Down
6 changes: 5 additions & 1 deletion packages/tools/tool-cylinder/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {
throttle
} from "throttle-debounce";
import Cylinder from "./makeCylinder";
/**
* 1000(ms, = 1s) / 60(60fps) = 17(ms)
*/
const throttleTime = 17;
function CylinderPage(): JSX.Element {
const [radiusX, setRadiusX] = useState<number>(50),
[radiusZ, setRadiusZ] = useState<number>(50),
Expand All @@ -41,7 +45,7 @@ function CylinderPage(): JSX.Element {
posCache = useRef<block>([1, 1]),
cache = useRef<block[]>([]),
blocks = useMemo(() => new Cylinder(radiusX, radiusZ, thickness, filled), [radiusX, radiusZ, thickness, filled]),
updatePos = throttle(17 /* 1000(ms, = 1s) / 60(60fps) = 17(ms) */, (X: number, Z: number) => {
updatePos = throttle(throttleTime, (X: number, Z: number) => {
drawMatrix(blocks.slice(0), Math.max(radiusX, radiusZ), X, Z, posCache.current, cache.current, theme.palette, true);
posCache.current = [X, Z];
cache.current = blocks.slice(0);
Expand Down

0 comments on commit dd94e13

Please sign in to comment.