From 7a7e4e2e8ce81b66184e28416ad85a5c1fefa554 Mon Sep 17 00:00:00 2001 From: Woonki Moon Date: Fri, 28 Jan 2022 09:56:30 +0900 Subject: [PATCH 1/5] Add module functor to make CustomEvent with typed detail --- .../Dom/Webapi__Dom__CustomEvent__test.js | 33 +++++++++++++ .../Dom/Webapi__Dom__EventTarget__test.js | 17 +++++++ src/Webapi/Dom/Webapi__Dom__CustomEvent.res | 16 +++++++ .../Dom/Webapi__Dom__CustomEvent__test.res | 46 +++++++++++++++++++ .../Dom/Webapi__Dom__EventTarget__test.res | 13 ++++++ 5 files changed, 125 insertions(+) diff --git a/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js b/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js index f64a23b8..16adba12 100644 --- a/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js +++ b/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js @@ -13,5 +13,38 @@ $$event.stopImmediatePropagation(); $$event.stopPropagation(); +var Detail = {}; + +var t = { + component: "test-component" +}; + +var EventWithDetail = Webapi__Dom__CustomEvent.Make(Detail); + +var eventWithDetail = new CustomEvent("event-with-detail"); + +var eventWithOptions = new CustomEvent("event-with-detail", t); + +Curry._1(Webapi__Dom__CustomEvent.eventPhase, eventWithDetail); + +Curry._1(Webapi__Dom__CustomEvent.eventPhase, eventWithOptions); + +eventWithDetail.preventDefault(); + +eventWithDetail.stopImmediatePropagation(); + +eventWithDetail.stopPropagation(); + +eventWithOptions.preventDefault(); + +eventWithOptions.stopImmediatePropagation(); + +eventWithOptions.stopPropagation(); + exports.$$event = $$event; +exports.Detail = Detail; +exports.t = t; +exports.EventWithDetail = EventWithDetail; +exports.eventWithDetail = eventWithDetail; +exports.eventWithOptions = eventWithOptions; /* event Not a pure module */ diff --git a/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js b/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js index 6205115e..7159a3d5 100644 --- a/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js +++ b/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js @@ -1,5 +1,6 @@ 'use strict'; +var Webapi__Dom__CustomEvent = require("../../../src/Webapi/Dom/Webapi__Dom__CustomEvent.js"); var target = document.createElement("strong"); @@ -39,8 +40,24 @@ var customEvent = new CustomEvent("custom-event", { target.dispatchEvent(customEvent); +var Detail = {}; + +var t = { + test: "test" +}; + +var EventWithDetail = Webapi__Dom__CustomEvent.Make(Detail); + +var typedCustomEvent = new CustomEvent("event-with-detail", t); + +target.dispatchEvent(typedCustomEvent); + exports.target = target; exports.$$event = $$event; exports.handleClick = handleClick; exports.customEvent = customEvent; +exports.Detail = Detail; +exports.t = t; +exports.EventWithDetail = EventWithDetail; +exports.typedCustomEvent = typedCustomEvent; /* target Not a pure module */ diff --git a/src/Webapi/Dom/Webapi__Dom__CustomEvent.res b/src/Webapi/Dom/Webapi__Dom__CustomEvent.res index d411b360..ee7b89c8 100644 --- a/src/Webapi/Dom/Webapi__Dom__CustomEvent.res +++ b/src/Webapi/Dom/Webapi__Dom__CustomEvent.res @@ -6,3 +6,19 @@ include Webapi__Dom__Event.Impl({ @new external make: string => t = "CustomEvent" @new external makeWithOptions: (string, {..}) => t = "CustomEvent" + +module Make = ( + Detail: { + type t + }, +) => { + type t = Dom.customEvent + + include Webapi__Dom__Event.Impl({ + type t = t + }) + + @new external make: string => t = "CustomEvent" + @new external makeWithOptions: (string, Detail.t) => t = "CustomEvent" + @get external detail: t => Detail.t = "detail" +} diff --git a/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res b/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res index 385171eb..9afc85fe 100644 --- a/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res +++ b/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res @@ -18,3 +18,49 @@ let _ = isTrusted(event) preventDefault(event) stopImmediatePropagation(event) stopPropagation(event) + +/* Event with detail */ +module Detail = { + type t = {component: string} +} + +let t: Detail.t = { + component: "test-component", +} + +module EventWithDetail = Make(Detail) +let eventWithDetail = EventWithDetail.make("event-with-detail") +let eventWithOptions = EventWithDetail.makeWithOptions("event-with-detail", t) + +/* Event */ +let _ = bubbles(eventWithDetail) +let _ = cancelable(eventWithDetail) +let _ = composed(eventWithDetail) +let _ = currentTarget(eventWithDetail) +let _ = defaultPrevented(eventWithDetail) +let _ = eventPhase(eventWithDetail) +let _ = target(eventWithDetail) +let _ = timeStamp(eventWithDetail) +let _ = type_(eventWithDetail) +let _ = isTrusted(eventWithDetail) + +let _ = bubbles(eventWithOptions) +let _ = cancelable(eventWithOptions) +let _ = composed(eventWithOptions) +let _ = currentTarget(eventWithOptions) +let _ = defaultPrevented(eventWithOptions) +let _ = eventPhase(eventWithOptions) +let _ = target(eventWithOptions) +let _ = timeStamp(eventWithOptions) +let _ = type_(eventWithOptions) +let _ = isTrusted(eventWithOptions) + +preventDefault(eventWithDetail) +stopImmediatePropagation(eventWithDetail) +stopPropagation(eventWithDetail) + +preventDefault(eventWithOptions) +stopImmediatePropagation(eventWithOptions) +stopPropagation(eventWithOptions) + +let _ = (eventWithDetail->EventWithDetail.detail).component diff --git a/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res b/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res index c6013eaf..958662f2 100644 --- a/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res +++ b/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res @@ -27,3 +27,16 @@ let customEvent = CustomEvent.makeWithOptions( }, ) let _ = target->dispatchEvent(customEvent) + +/* another way */ +module Detail = { + type t = {test: string} +} + +let t: Detail.t = { + test: "test", +} + +module EventWithDetail = CustomEvent.Make(Detail) +let typedCustomEvent = EventWithDetail.makeWithOptions("event-with-detail", t) +let _ = target->dispatchEvent(typedCustomEvent) From ce18b9c5a61986864af3a5715f4f48f40bb4b3a0 Mon Sep 17 00:00:00 2001 From: woonki Date: Fri, 4 Feb 2022 16:14:32 +0900 Subject: [PATCH 2/5] change the comment Co-authored-by: spocke --- tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res b/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res index 958662f2..9455d534 100644 --- a/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res +++ b/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res @@ -28,7 +28,7 @@ let customEvent = CustomEvent.makeWithOptions( ) let _ = target->dispatchEvent(customEvent) -/* another way */ +/* dispatch custom event with typed detail */ module Detail = { type t = {test: string} } From 7cfccb86759e78db083707ac2fdbf6c504b7674f Mon Sep 17 00:00:00 2001 From: woonki Date: Fri, 4 Feb 2022 16:41:16 +0900 Subject: [PATCH 3/5] add wrapper type for type safety Co-authored-by: spocke --- src/Webapi/Dom/Webapi__Dom__CustomEvent.res | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Webapi/Dom/Webapi__Dom__CustomEvent.res b/src/Webapi/Dom/Webapi__Dom__CustomEvent.res index ee7b89c8..53f33d27 100644 --- a/src/Webapi/Dom/Webapi__Dom__CustomEvent.res +++ b/src/Webapi/Dom/Webapi__Dom__CustomEvent.res @@ -7,12 +7,14 @@ include Webapi__Dom__Event.Impl({ @new external make: string => t = "CustomEvent" @new external makeWithOptions: (string, {..}) => t = "CustomEvent" +type _typedCustomEvent<'a> + module Make = ( Detail: { type t }, ) => { - type t = Dom.customEvent + type t = Dom.event_like<_typedCustomEvent> include Webapi__Dom__Event.Impl({ type t = t From 72d237d697f213e95c91faa35b455316c4dca35d Mon Sep 17 00:00:00 2001 From: Woonki Moon Date: Fri, 4 Feb 2022 16:42:55 +0900 Subject: [PATCH 4/5] fix test for typed custom event module functor --- .../Dom/Webapi__Dom__CustomEvent__test.js | 4 +- .../Dom/Webapi__Dom__CustomEvent__test.res | 52 +++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js b/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js index 16adba12..fb3328a6 100644 --- a/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js +++ b/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js @@ -25,9 +25,9 @@ var eventWithDetail = new CustomEvent("event-with-detail"); var eventWithOptions = new CustomEvent("event-with-detail", t); -Curry._1(Webapi__Dom__CustomEvent.eventPhase, eventWithDetail); +Curry._1(EventWithDetail.eventPhase, eventWithDetail); -Curry._1(Webapi__Dom__CustomEvent.eventPhase, eventWithOptions); +Curry._1(EventWithDetail.eventPhase, eventWithOptions); eventWithDetail.preventDefault(); diff --git a/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res b/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res index 9afc85fe..73a007f0 100644 --- a/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res +++ b/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res @@ -33,34 +33,34 @@ let eventWithDetail = EventWithDetail.make("event-with-detail") let eventWithOptions = EventWithDetail.makeWithOptions("event-with-detail", t) /* Event */ -let _ = bubbles(eventWithDetail) -let _ = cancelable(eventWithDetail) -let _ = composed(eventWithDetail) -let _ = currentTarget(eventWithDetail) -let _ = defaultPrevented(eventWithDetail) -let _ = eventPhase(eventWithDetail) -let _ = target(eventWithDetail) -let _ = timeStamp(eventWithDetail) -let _ = type_(eventWithDetail) -let _ = isTrusted(eventWithDetail) +let _ = EventWithDetail.bubbles(eventWithDetail) +let _ = EventWithDetail.cancelable(eventWithDetail) +let _ = EventWithDetail.composed(eventWithDetail) +let _ = EventWithDetail.currentTarget(eventWithDetail) +let _ = EventWithDetail.defaultPrevented(eventWithDetail) +let _ = EventWithDetail.eventPhase(eventWithDetail) +let _ = EventWithDetail.target(eventWithDetail) +let _ = EventWithDetail.timeStamp(eventWithDetail) +let _ = EventWithDetail.type_(eventWithDetail) +let _ = EventWithDetail.isTrusted(eventWithDetail) -let _ = bubbles(eventWithOptions) -let _ = cancelable(eventWithOptions) -let _ = composed(eventWithOptions) -let _ = currentTarget(eventWithOptions) -let _ = defaultPrevented(eventWithOptions) -let _ = eventPhase(eventWithOptions) -let _ = target(eventWithOptions) -let _ = timeStamp(eventWithOptions) -let _ = type_(eventWithOptions) -let _ = isTrusted(eventWithOptions) +let _ = EventWithDetail.bubbles(eventWithOptions) +let _ = EventWithDetail.cancelable(eventWithOptions) +let _ = EventWithDetail.composed(eventWithOptions) +let _ = EventWithDetail.currentTarget(eventWithOptions) +let _ = EventWithDetail.defaultPrevented(eventWithOptions) +let _ = EventWithDetail.eventPhase(eventWithOptions) +let _ = EventWithDetail.target(eventWithOptions) +let _ = EventWithDetail.timeStamp(eventWithOptions) +let _ = EventWithDetail.type_(eventWithOptions) +let _ = EventWithDetail.isTrusted(eventWithOptions) -preventDefault(eventWithDetail) -stopImmediatePropagation(eventWithDetail) -stopPropagation(eventWithDetail) +EventWithDetail.preventDefault(eventWithDetail) +EventWithDetail.stopImmediatePropagation(eventWithDetail) +EventWithDetail.stopPropagation(eventWithDetail) -preventDefault(eventWithOptions) -stopImmediatePropagation(eventWithOptions) -stopPropagation(eventWithOptions) +EventWithDetail.preventDefault(eventWithOptions) +EventWithDetail.stopImmediatePropagation(eventWithOptions) +EventWithDetail.stopPropagation(eventWithOptions) let _ = (eventWithDetail->EventWithDetail.detail).component From f620b557f40dd133692557807b0a401894487fcb Mon Sep 17 00:00:00 2001 From: Woonki Moon Date: Mon, 7 Feb 2022 13:33:23 +0900 Subject: [PATCH 5/5] fix test to remove let t declaration --- .../tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js | 9 +++------ .../tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js | 9 +++------ tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res | 9 ++++----- tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res | 6 +----- 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js b/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js index fb3328a6..c38ad05d 100644 --- a/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js +++ b/lib/js/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.js @@ -15,15 +15,13 @@ $$event.stopPropagation(); var Detail = {}; -var t = { - component: "test-component" -}; - var EventWithDetail = Webapi__Dom__CustomEvent.Make(Detail); var eventWithDetail = new CustomEvent("event-with-detail"); -var eventWithOptions = new CustomEvent("event-with-detail", t); +var eventWithOptions = new CustomEvent("event-with-detail", { + component: "test-component" + }); Curry._1(EventWithDetail.eventPhase, eventWithDetail); @@ -43,7 +41,6 @@ eventWithOptions.stopPropagation(); exports.$$event = $$event; exports.Detail = Detail; -exports.t = t; exports.EventWithDetail = EventWithDetail; exports.eventWithDetail = eventWithDetail; exports.eventWithOptions = eventWithOptions; diff --git a/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js b/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js index 7159a3d5..e9dec838 100644 --- a/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js +++ b/lib/js/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.js @@ -42,13 +42,11 @@ target.dispatchEvent(customEvent); var Detail = {}; -var t = { - test: "test" -}; - var EventWithDetail = Webapi__Dom__CustomEvent.Make(Detail); -var typedCustomEvent = new CustomEvent("event-with-detail", t); +var typedCustomEvent = new CustomEvent("event-with-detail", { + test: "test" + }); target.dispatchEvent(typedCustomEvent); @@ -57,7 +55,6 @@ exports.$$event = $$event; exports.handleClick = handleClick; exports.customEvent = customEvent; exports.Detail = Detail; -exports.t = t; exports.EventWithDetail = EventWithDetail; exports.typedCustomEvent = typedCustomEvent; /* target Not a pure module */ diff --git a/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res b/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res index 73a007f0..e1d3af39 100644 --- a/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res +++ b/tests/Webapi/Dom/Webapi__Dom__CustomEvent__test.res @@ -24,13 +24,12 @@ module Detail = { type t = {component: string} } -let t: Detail.t = { - component: "test-component", -} - module EventWithDetail = Make(Detail) let eventWithDetail = EventWithDetail.make("event-with-detail") -let eventWithOptions = EventWithDetail.makeWithOptions("event-with-detail", t) +let eventWithOptions = EventWithDetail.makeWithOptions( + "event-with-detail", + {component: "test-component"}, +) /* Event */ let _ = EventWithDetail.bubbles(eventWithDetail) diff --git a/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res b/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res index 9455d534..e6319ae5 100644 --- a/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res +++ b/tests/Webapi/Dom/Webapi__Dom__EventTarget__test.res @@ -33,10 +33,6 @@ module Detail = { type t = {test: string} } -let t: Detail.t = { - test: "test", -} - module EventWithDetail = CustomEvent.Make(Detail) -let typedCustomEvent = EventWithDetail.makeWithOptions("event-with-detail", t) +let typedCustomEvent = EventWithDetail.makeWithOptions("event-with-detail", {test: "test"}) let _ = target->dispatchEvent(typedCustomEvent)