-
Notifications
You must be signed in to change notification settings - Fork 1
/
shared.mjs
85 lines (79 loc) · 2.39 KB
/
shared.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// <reference types="./shared.d.mts" />
import * as $json from "../gleam_json/gleam/json.mjs";
import * as $dynamic from "../gleam_stdlib/gleam/dynamic.mjs";
import { DecodeError } from "../gleam_stdlib/gleam/dynamic.mjs";
import * as $result from "../gleam_stdlib/gleam/result.mjs";
import { Error, toList, CustomType as $CustomType } from "./gleam.mjs";
export class ChatMessage extends $CustomType {
constructor(text, author, created_at) {
super();
this.text = text;
this.author = author;
this.created_at = created_at;
}
}
export class RoomUpdate extends $CustomType {
constructor(num_participants) {
super();
this.num_participants = num_participants;
}
}
export function message_to_json(msg) {
if (msg instanceof ChatMessage) {
let text = msg.text;
let author = msg.author;
let created_at = msg.created_at;
return $json.object(
toList([
["$", $json.string("ChatMessage")],
["text", $json.string(text)],
["author", $json.string(author)],
["created_at", $json.string(created_at)],
]),
);
} else {
let num_participants = msg.num_participants;
return $json.object(
toList([
["$", $json.string("RoomUpdate")],
["num_participants", $json.int(num_participants)],
]),
);
}
}
function decoder(dynamic) {
return $result.then$(
$dynamic.field("$", $dynamic.string)(dynamic),
(tag) => {
let decoder$1 = (() => {
if (tag === "ChatMessage") {
return $dynamic.decode3(
(var0, var1, var2) => { return new ChatMessage(var0, var1, var2); },
$dynamic.field("text", $dynamic.string),
$dynamic.field("author", $dynamic.string),
$dynamic.field("created_at", $dynamic.string),
);
} else if (tag === "RoomUpdate") {
return $dynamic.decode1(
(var0) => { return new RoomUpdate(var0); },
$dynamic.field("num_participants", $dynamic.int),
);
} else {
return (_) => {
return new Error(
toList([new DecodeError("Message", tag, toList(["$"]))]),
);
};
}
})();
return decoder$1(dynamic);
},
);
}
export function message_to_string(msg) {
let _pipe = message_to_json(msg);
return $json.to_string(_pipe);
}
export function message_from_string(json) {
return $json.decode(json, decoder);
}