Skip to content

Commit 0f2a56f

Browse files
Add FFI for CustomEvent constructor (#26)
* Add FFI for CustomEvent constructor * Add FFI for detail * Add foreign as bower dep * Add changelog Co-authored-by: Thomas Honeyman <[email protected]>
1 parent 5470e50 commit 0f2a56f

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Breaking changes:
88
- Migrate FFI to ES modules (#24 by @JordanMartinez)
99

1010
New features:
11+
- Add FFI for `CustomEvent` constructor (#25 by @JordanMartinez)
1112
- Add `addEventListenerWithOptions` to expose more options (#25 by @JordanMartinez)
1213

1314
Bugfixes:

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"purescript-datetime": "master",
1919
"purescript-enums": "master",
20-
"purescript-nullable": "main"
20+
"purescript-nullable": "main",
21+
"purescript-foreign": "master"
2122
}
2223
}

src/Web/Event/CustomEvent.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const newSimple = function(ty) {
2+
return function () {
3+
return new CustomEvent(ty);
4+
};
5+
};
6+
export { newSimple as new };
7+
8+
export function newOptionsImpl (ty) {
9+
return function (opt) {
10+
return function () {
11+
return new CustomEvent(ty, opt);
12+
};
13+
};
14+
}
15+
16+
export function detail(e) {
17+
return e.detail;
18+
}

src/Web/Event/CustomEvent.purs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
module Web.Event.CustomEvent where
22

33
import Data.Maybe (Maybe)
4+
import Data.Nullable (Nullable, toNullable)
5+
import Effect (Effect)
6+
import Foreign (Foreign)
47
import Unsafe.Coerce as U
5-
import Web.Event.Event (Event)
8+
import Web.Event.Event (Event, EventType)
69
import Web.Internal.FFI (unsafeReadProtoTagged)
710

811
foreign import data CustomEvent :: Type
@@ -12,3 +15,32 @@ fromEvent = unsafeReadProtoTagged "CustomEvent"
1215

1316
toEvent :: CustomEvent -> Event
1417
toEvent = U.unsafeCoerce
18+
19+
foreign import new :: EventType -> Effect CustomEvent
20+
21+
-- | Create a new `CustomEvent`, storing some data in its `detail` field,
22+
-- | and using defaults for everything else.
23+
new'
24+
:: forall a
25+
. EventType
26+
-> Maybe a
27+
-> Effect CustomEvent
28+
new' ty det =
29+
newWithOptions ty { detail: det, bubbles: false, cancelable: false, composed: false }
30+
31+
-- | Create a new `CustomEvent` with all of its constructor's options exposed.
32+
foreign import newOptionsImpl
33+
:: forall a
34+
. EventType
35+
-> { detail :: Nullable a , bubbles :: Boolean, cancelable :: Boolean, composed :: Boolean }
36+
-> Effect CustomEvent
37+
38+
newWithOptions
39+
:: forall a
40+
. EventType
41+
-> { detail :: Maybe a , bubbles :: Boolean, cancelable :: Boolean, composed :: Boolean }
42+
-> Effect CustomEvent
43+
newWithOptions ty rec@{ bubbles, cancelable, composed } =
44+
newOptionsImpl ty { detail: toNullable rec.detail, bubbles, cancelable, composed }
45+
46+
foreign import detail :: CustomEvent -> Foreign

0 commit comments

Comments
 (0)