Skip to content

Commit

Permalink
fine tuning the event model
Browse files Browse the repository at this point in the history
  • Loading branch information
dyoder committed Jun 28, 2021
1 parent 032e08b commit 2df0d0b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/actions/dispatch.coffee
Original file line number Diff line number Diff line change
@@ -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 [
Expand Down
1 change: 1 addition & 0 deletions src/actions/index.coffee
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
23 changes: 17 additions & 6 deletions src/event/matches.coffee
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 7 additions & 4 deletions src/event/within.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
2 changes: 1 addition & 1 deletion src/handle.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/initialize/capture.coffee
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions src/initialize/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions src/initialize/observe.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
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) ->
wrapper = Observable.from (handle[name] ? {}), async: true
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

Expand Down
2 changes: 2 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ do ->

await do browser ({browser, port}) ->

await _.sleep 1000

results = await test "Carbon", [

test
Expand Down

0 comments on commit 2df0d0b

Please sign in to comment.