Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit fed5db9

Browse files
author
Iwan
committed
✈️
0 parents  commit fed5db9

8 files changed

+210
-0
lines changed

.#README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
node_modules/
3+
lib
4+
npm-debug.log
5+
.merlin

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Transit-js Bucklescript bindings #
2+
3+
Make client-server communication great again.
4+
5+
This repo provides rudimentary bindings to transit-js with Bucklescript,
6+
prototyped in under 15min. thanks to Bucklescript compiling at the speed of light.
7+
8+
Json is nice, but I want to send records and variants among other things straight over
9+
the wire. Feels like a shame to lose all this information & type everything as js objects.
10+
Since transit provides a way to encode/decode custom data, it seemed like
11+
a good fit for encoding ml types. If this thing works out, remind me to automate the
12+
encoding/decoding with ppx.
13+
14+
Head over to `src/example.re` to get up & running ASAP.

bsconfig.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name" : "transit-bs",
3+
"sources" : { "dir" : "src"},
4+
"generate-merlin": true,
5+
}

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"devDependencies": {
3+
"bs-platform": "^1.4.1",
4+
"transit-js": "^0.8.846"
5+
},
6+
"scripts": {
7+
"build": "bsb",
8+
"watch": "bsb -w"
9+
}
10+
}

src/example.re

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
module Coordinate = {
2+
type t = {x: int, y: int};
3+
let make x y => {x, y};
4+
5+
/**
6+
Transit read & write helpers
7+
*/
8+
let writerSpec =
9+
Transit.makeWriteHandler ({pub tag () => "coord"; pub rep {x, y} () => (x, y)} [@bs]);
10+
let readerSpec (x, y) => {x, y};
11+
};
12+
13+
module Size = {
14+
type t = {width: int, height: int};
15+
let make width height => {width, height};
16+
17+
/**
18+
Transit read & write helpers
19+
*/
20+
let writerSpec =
21+
Transit.makeWriteHandler ({pub tag () => "size"; pub rep v () => (v.width, v.height)} [@bs]);
22+
let readerSpec (width, height) => {width, height};
23+
};
24+
25+
module Language = {
26+
type t =
27+
| Ocaml
28+
| Lisp
29+
| Other;
30+
31+
/**
32+
Transit read & write helpers
33+
*/
34+
let writerSpec = {
35+
pub tag () => "lang";
36+
pub rep v () =>
37+
switch v {
38+
| Ocaml => 0
39+
| Lisp => 1
40+
| Other => 3
41+
}
42+
};
43+
let readerSpec rep =>
44+
switch rep {
45+
| 0 => Ocaml
46+
| 1 => Lisp
47+
| _ => Other
48+
};
49+
};
50+
51+
module Rectangle = {
52+
type t = {origin: Coordinate.t, size: Size.t};
53+
let make origin size => {origin, size};
54+
55+
/**
56+
Transit read & write helpers
57+
*/
58+
let writerSpec =
59+
Transit.makeWriteHandler (
60+
{pub tag () => "coord"; pub rep {origin, size} () => (origin, size)} [@bs]
61+
);
62+
let readerSpec (origin, size) => {origin, size};
63+
};
64+
65+
let reader = Transit.reader {"handlers": {point: Coordinate.readerSpec, size: Size.writerSpec}};
66+
67+
let writer =
68+
Transit.writer @@
69+
/*
70+
In javascript this is a transit map from function constructors as keys to
71+
writehandlers as values. Function constructors as keys only
72+
matter if you have something like:
73+
var Point = function(x, y) { this.x = x; this.y = y; };
74+
(I think, need to investigate more)
75+
*/
76+
Transit.map (
77+
"Coordinate",
78+
Coordinate.writerSpec,
79+
"Size",
80+
Size.writerSpec,
81+
"Language",
82+
Language.writerSpec,
83+
"Rectangle",
84+
Rectangle.writerSpec
85+
);
86+
87+
let encodedCoordinate = Transit.write writer (Coordinate.make 8 8);
88+
89+
let encodedSize = Transit.write writer (Size.make 5 5);
90+
91+
let encodedLanguage = Transit.write writer Language.Ocaml;
92+
93+
let encodedRectangle =
94+
Transit.write writer (Rectangle.make (Coordinate.make 0 0) (Size.make 10 10));
95+
96+
let coordinate: Coordinate.t = Transit.read reader encodedCoordinate;
97+
98+
let size: Size.t = Transit.read reader encodedSize;
99+
100+
let lang: Language.t = Transit.read reader encodedLanguage;
101+
102+
let rect: Rectangle.t = Transit.read reader encodedRectangle;
103+
104+
let () =
105+
Language.(
106+
switch lang {
107+
| Ocaml => print_endline "OCaml!!!"
108+
| Lisp => print_endline "The most intelligent way to misuse a computer"
109+
| _ => print_endline "other stuff"
110+
}
111+
);

src/transit.re

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
type reader;
2+
3+
type format =
4+
| Json
5+
| JsonVerbose;
6+
7+
let string_of_format t =>
8+
switch t {
9+
| Json => "json"
10+
| JsonVerbose => "json-verbose"
11+
};
12+
13+
external raw_reader : string => 'options => reader =
14+
"reader" [@@bs.val] [@@bs.module "transit-js"];
15+
16+
let reader ::format=Json options => {
17+
let readerType = string_of_format format;
18+
raw_reader readerType options
19+
};
20+
21+
external read : reader => 'a => 'b = "read" [@@bs.send];
22+
23+
type writehandler;
24+
25+
external makeWriteHandler : 'a => writehandler =
26+
"makeWriteHandler" [@@bs.val] [@@bs.module "transit-js"];
27+
28+
type writer;
29+
30+
type transitMap;
31+
32+
external map : 'handlers => transitMap = "map" [@@bs.val] [@@bs.module "transit-js"];
33+
34+
external raw_writer : string => 'handler => writer =
35+
"writer" [@@bs.val] [@@bs.module "transit-js"];
36+
37+
let writer ::format=Json options => {
38+
let writerType = string_of_format format;
39+
raw_writer writerType {"handlers": options}
40+
};
41+
42+
external write : writer => 'a => 'b = "write" [@@bs.send];

src/transit.rei

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type format;
2+
3+
type reader;
4+
5+
let reader: format::format? => 'options => reader;
6+
7+
type writer;
8+
9+
external read : reader => 'a => 'b = "read" [@@bs.send];
10+
11+
type writehandler;
12+
13+
external makeWriteHandler : 'a => writehandler =
14+
"makeWriteHandler" [@@bs.val] [@@bs.module "transit-js"];
15+
16+
type transitMap;
17+
18+
external map : 'handlers => transitMap = "map" [@@bs.val] [@@bs.module "transit-js"];
19+
20+
let writer: format::format? => transitMap => writer;
21+
22+
external write : writer => 'a => 'b = "write" [@@bs.send];

0 commit comments

Comments
 (0)