Skip to content

Instantly share code, notes, and snippets.

@gaku-sei
Last active July 7, 2020 08:49
Show Gist options
  • Save gaku-sei/e3b4bb2a415e880ccfe29d368c3ede56 to your computer and use it in GitHub Desktop.
Save gaku-sei/e3b4bb2a415e880ccfe29d368c3ede56 to your computer and use it in GitHub Desktop.
Attempt to map Recoiljs (https://recoiljs.org/) to ReasonML
type r = [ | `read];
type w = [ | `write];
type rw = [ r | w];
type t('a, 'rights) constraint 'rights = [< rw];
type getArg = {get: 'a 'rights. t('a, [> r] as 'rights) => 'a};
type setArg = {set: 'a 'rights. (t('a, [> w] as 'rights), 'a) => unit};
module Atom = {
type option('a) = {
key: string,
default: 'a,
};
[@bs.module "recoil"] external atom: option('a) => t('a, rw) = "atom";
let make = (~key, ~default) => atom({key, default});
};
module Selector = {
type option('a, 'b, 'rights) = {
key: string,
get: getArg => 'b,
};
[@bs.module "recoil"]
external selector: option('a, 'b, 'rights) => t('b, r) = "selector";
let make = (~key, ~get) => selector({key, get});
};
module WritableSelector = {
type option('a, 'b, 'c) = {
key: string,
get: getArg => 'b,
set: (setArg, 'b) => unit,
};
[@bs.module "recoil"]
external selector: option('a, 'b, 'c) => t('b, rw) = "selector";
let make = (~key, ~get, ~set) => selector({key, get, set});
};
[@bs.module "recoil"]
external useState: t('a, [> w] as 'rights) => ('a, 'a => unit) =
"useRecoilState";
[@bs.module "recoil"]
external useValue: t('a, [> r] as 'rights) => 'a = "useRecoilValue";
type r = [ | `read];
type w = [ | `write];
type rw = [ r | w];
type t('a, 'rights) constraint 'rights = [< rw];
type getArg = {get: 'a 'rights. t('a, [> r] as 'rights) => 'a};
type setArg = {set: 'a 'rights. (t('a, [> w] as 'rights), 'a) => unit};
module Atom: {let make: (~key: string, ~default: 'a) => t('a, rw);};
module Selector: {let make: (~key: string, ~get: getArg => 'b) => t('b, r);};
module WritableSelector: {
let make:
(~key: string, ~get: getArg => 'b, ~set: (setArg, 'b) => unit) =>
t('b, rw);
};
let useState: t('a, [> w] as 'rights) => ('a, 'a => unit);
let useValue: t('a, [> r] as 'rights) => 'a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment