Last active
January 10, 2019 10:17
-
-
Save cristianoc/88d28074c86c276c5e501b1cb61b0ce2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module type SlotsType = { | |
type t('slot, 'nextSlots); | |
let create: unit => t('slot, 'nextSlots); | |
let use: | |
(~default: 'slot, t('slot, t('slot2, 'nextSlots))) => | |
('slot, t('slot2, 'nextSlots)); | |
}; | |
module Slots: SlotsType = { | |
type t('slot, 'nextSlots) = ref(option(('slot, 'nextSlots))); | |
let create = () => ref(None); | |
let use = (~default, slots: t(_)) => { | |
switch (slots^) { | |
| None => | |
let slot = default; | |
let nextSlots = create(); | |
slots := Some((slot, nextSlots)); | |
(slot, nextSlots); | |
| Some((slot, nextSlots)) => (slot, nextSlots) | |
}; | |
}; | |
}; | |
let useInt = slots => { | |
let (x, nextSlots) = slots |> Slots.use(~default=0); | |
(x + 1, nextSlots); | |
}; | |
let useString = slots => { | |
let (x, nextSlots) = slots |> Slots.use(~default=""); | |
(x ++ "*", nextSlots); | |
}; | |
let useIntString = slots => { | |
let (x, slots) = slots |> useInt; | |
let (s, slots) = slots |> useString; | |
((x + 1, s), slots); | |
}; | |
let runUseInt = Slots.create() |> useInt |> fst; | |
let runUseString = Slots.create() |> useString |> fst; | |
let runUseIntString = Slots.create() |> useIntString |> fst; | |
Js.log("-----Hooks-----"); | |
Js.log(runUseInt); | |
Js.log(runUseString); | |
Js.log(runUseIntString); | |
Js.log("-----Hooks-----"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment