Skip to content

Instantly share code, notes, and snippets.

@cristianoc
Created January 4, 2019 09:57
Show Gist options
  • Save cristianoc/8f69c833a94e13a8ed659c76c97b8b20 to your computer and use it in GitHub Desktop.
Save cristianoc/8f69c833a94e13a8ed659c76c97b8b20 to your computer and use it in GitHub Desktop.
module type SlotsType = {
type t('slot, 'nextSlots);
let create: unit => t('slot, 'nextSlots);
let use:
(
~default: 'slot,
('slot, t('slot2, 'nextSlots)) => 'c,
t('slot, t('slot2, 'nextSlots))
) =>
'c;
};
module Slots: SlotsType = {
type t('slot, 'nextSlots) = ref(option(('slot, 'nextSlots)));
let create = () => ref(None);
let use = (~default, continuation, slots: t(_)) => {
switch (slots^) {
| None =>
let slot = default;
let nextSlots = create();
slots := Some((slot, nextSlots));
continuation(slot, nextSlots);
| Some((slot, nextSlots)) => continuation(slot, nextSlots)
};
};
};
let useInt = slots =>
slots |> Slots.use(~default=0, (x, _nextSlots) => x + 1);
let useString = slots =>
slots |> Slots.use(~default="", (x, _nextSlots) => x ++ "*");
let useIntString = slots =>
slots
|> Slots.use(~default=0, (x, nextSlots) => (x + 1, nextSlots |> useString));
let runUseInt = Slots.create() |> useInt;
let runUseString = Slots.create() |> useString;
let runUseIntString = Slots.create() |> useIntString;
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