From 2df0d0b0dc71efeb32ad79806f445cfd2758877d Mon Sep 17 00:00:00 2001 From: Daniel Yoder Date: Sun, 27 Jun 2021 23:30:01 -0700 Subject: [PATCH] fine tuning the event model --- src/actions/dispatch.coffee | 2 +- src/actions/index.coffee | 1 + src/event/matches.coffee | 23 +++++++++++++++++------ src/event/within.coffee | 11 +++++++---- src/handle.coffee | 2 +- src/initialize/capture.coffee | 15 +++++++++++++++ src/initialize/index.coffee | 1 + src/initialize/observe.coffee | 9 +++++++-- test/index.coffee | 2 ++ 9 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 src/initialize/capture.coffee diff --git a/src/actions/dispatch.coffee b/src/actions/dispatch.coffee index 411e200..ed818d7 100644 --- a/src/actions/dispatch.coffee +++ b/src/actions/dispatch.coffee @@ -1,7 +1,7 @@ import {curry, pipe} from "@dashkite/joy/function" import {pop, read} from "@dashkite/katana/sync" -_dispatch = curry (name, handle) -> handle.dispatch name +_dispatch = curry (name, handle, detail) -> handle.dispatch name, detail dispatch = (name) -> pipe [ diff --git a/src/actions/index.coffee b/src/actions/index.coffee index 01f0284..dcb87ae 100644 --- a/src/actions/index.coffee +++ b/src/actions/index.coffee @@ -1,6 +1,7 @@ export * from "./call" export * from "./get" export * from "./set" +export * from "./dispatch" export * from "./render" export * from "./form" export * from "./disable" diff --git a/src/event/matches.coffee b/src/event/matches.coffee index 24ad0a9..8d54e99 100644 --- a/src/event/matches.coffee +++ b/src/event/matches.coffee @@ -1,11 +1,22 @@ -import {curry, pipe} from "@dashkite/joy/function" +import {curry, binary, pipe} from "@dashkite/joy/function" +import {generic} from "@dashkite/joy/generic" +import {isString} from "@dashkite/joy/type" import {test} from "@dashkite/katana/sync" -_matches = curry (selector, event, {handle}) -> - if (event?.target?.matches selector)? - true - else - false +isEvent = (x) -> x instanceof Event +isNode = (x) -> x instanceof Node + +_matches = generic + name: "_matches" + default: -> false + +generic _matches, isString, isEvent, (selector, event) -> + _matches selector, event.target + +generic _matches, isString, isNode, (selector, el) -> + el.matches selector + +_matches = curry binary _matches matches = (selector, fx) -> test (_matches selector), pipe fx diff --git a/src/event/within.coffee b/src/event/within.coffee index dddf1d3..ea5fabc 100644 --- a/src/event/within.coffee +++ b/src/event/within.coffee @@ -2,10 +2,13 @@ import * as F from "@dashkite/joy/function" import * as T from "@dashkite/joy/type" import {push, test} from "@dashkite/katana/sync" -_within = (selector) -> (event, handle) -> - if (target = (event?.target?.closest? selector))? - if handle.root.contains target - target +_within = (selector) -> + (event, handle) -> + event.composedPath() + .find (el) -> + (el instanceof Node) && + (handle.root.contains el) && + (el.matches? selector) within = (selector, fx) -> F.pipe [ diff --git a/src/handle.coffee b/src/handle.coffee index b1fff18..489bb70 100644 --- a/src/handle.coffee +++ b/src/handle.coffee @@ -14,7 +14,7 @@ class Handle on: (name, handler) -> @root.addEventListener name, handler.bind @ dispatch: (name, detail) -> - @root.dispatchEvent new CustomEvent name, + @dom.dispatchEvent new CustomEvent name, detail: detail ? @ bubbles: true cancelable: false diff --git a/src/initialize/capture.coffee b/src/initialize/capture.coffee new file mode 100644 index 0000000..cb3bff4 --- /dev/null +++ b/src/initialize/capture.coffee @@ -0,0 +1,15 @@ +import * as F from "@dashkite/joy/function" +import * as Ks from "@dashkite/katana/sync" + +capture = (name, fx) -> + Ks.peek (handle) -> + handler = F.pipe [ + Ks.read "handle" + Ks.read "event" + F.pipe fx + ] + handle.root.addEventListener name, + ((event) -> handler { handle, event }), + capture: true + +export { capture } diff --git a/src/initialize/index.coffee b/src/initialize/index.coffee index 8e7f3d9..d50a4b6 100644 --- a/src/initialize/index.coffee +++ b/src/initialize/index.coffee @@ -3,6 +3,7 @@ export * from "./describe" export * from "./observe" export * from "./navigate" export * from "./event" +export * from "./capture" export * from "./click" export * from "./submit" export * from "./ready" diff --git a/src/initialize/observe.coffee b/src/initialize/observe.coffee index fc056be..d85025d 100644 --- a/src/initialize/observe.coffee +++ b/src/initialize/observe.coffee @@ -1,5 +1,6 @@ import {Observable} from "object-observer" import {curry, flow} from "@dashkite/joy/function" +import {read} from "@dashkite/katana/async" import {peek} from "@dashkite/katana/sync" _observe = curry (name, handler, handle) -> @@ -7,9 +8,13 @@ _observe = curry (name, handler, handle) -> Object.defineProperty handle, name, value: wrapper writeable: false - wrapper.observe -> handler [ wrapper ], { handle } + wrapper.observe -> handler { data: wrapper, handle } -observe = (name, fx) -> peek _observe name, flow fx +observe = (name, fx) -> + peek _observe name, flow [ + read "data" + flow fx + ] observe._ = _observe diff --git a/test/index.coffee b/test/index.coffee index 9ee1e61..ce2a83e 100644 --- a/test/index.coffee +++ b/test/index.coffee @@ -9,6 +9,8 @@ do -> await do browser ({browser, port}) -> + await _.sleep 1000 + results = await test "Carbon", [ test