Skip to content

Commit

Permalink
Outter从IORef到Lens。
Browse files Browse the repository at this point in the history
  • Loading branch information
kalxd committed Nov 16, 2023
1 parent 35d4c15 commit ac4cca5
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/abstract/outter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as m from "mithril";
import { Just, Maybe, Nothing } from "purify-ts";
import { IORef } from "../data/ref";
import { mutable } from "../data/lens";

type MouseEventCallback = (e: MouseEvent) => void;

export interface OutterAttr {
connectOutterClick: (e: MouseEvent) => void;
connectOutterClick: MouseEventCallback;
}

export const Outter: m.FactoryComponent<OutterAttr> = _ => {
const bodyClickIO: IORef<Maybe<(ev: MouseEvent) => void>> = new IORef(Nothing);
const state = mutable<Maybe<MouseEventCallback>>(Nothing);

return {
oncreate: (vnode) => {
Expand All @@ -20,13 +22,12 @@ export const Outter: m.FactoryComponent<OutterAttr> = _ => {
}
};

bodyClickIO.put(Just(f));

state.set(Just(f));
document.body.addEventListener("click", f);
},

onremove: (_) => {
bodyClickIO.ask().ifJust(f => {
state.get().ifJust(f => {
document.body.removeEventListener("click", f);
});
},
Expand Down

0 comments on commit ac4cca5

Please sign in to comment.