Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 zero cost #13

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .bsb.lock

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lib
/node_modules/
.merlin
*.js
.bsb.lock
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
This package is not yet released on npm; feel free to install it directly from github! Thanks.

# 2.0.0

- Upgraded to BS v7
- Upgraded to socket.io-client 2.3
- Exposing a zero-cost layer w/o Functor interface

# 1.0.0

Breaking change!
Expand Down
2 changes: 2 additions & 0 deletions example/BsSocket.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
module Server = Server;
module Client = Client;
module Namespace = Namespace;

module V2 = V2;
46 changes: 46 additions & 0 deletions example/V2Example.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
let socket = V2.Client.createWithUrl("https://localhost:8080");

open V2.Socket;

let logConnectAttempt = attempt => {
Js.log("Attempt number: " ++ Belt.Int.toString(attempt));
};

// Connect Events
socket->onConnect(() => {Js.log("connected")});
socket->onConnectError(err => {Js.log("Error happened: " ++ err.message)});

// Reconnect Events
socket->onReconnect(logConnectAttempt);
socket->onReconnecting(logConnectAttempt);
socket->onReconnectFailed(() => {Js.log("reconnect failed")});

socket->onEvent("news", arg => {
// Different ways to handle `arg` here
// 1) Either use Js.Json.classify (low level JSON decoding layer)
// 2) Use libraries such as bs-json / bs-decode / ...
// 3) Use a unsafe raw / %identity function for unsafe coercion

/*switch(arg->Js.Json.classify) {*/
/*| Js.Json.String => */
/*};*/
let result =
Js.Json.stringifyAny(arg)
->Belt.Option.getWithDefault("Could not stringify arg");
Js.log("news event happened: " ++ result);
});

socket->onEvent2("news", (arg1, arg2) => {
Js.log3("News event happened", arg1, arg2)
});

socket->onEvent3("news", (arg1, arg2, arg3) => {
Js.log4("News event happened", arg1, arg2, arg3)
});

socket->onPing(() => Js.log("ping occurred"));
socket->onPong(latency => Js.log2("pong occurred. Latency: ", latency));

socket->onError(err => {Js.log("An error event occurred: " ++ err.message)});

socket->connect;
Loading