Skip to content

Commit 27bf62b

Browse files
authored
Beta 5 (#6)
1 parent 4bae544 commit 27bf62b

File tree

9 files changed

+104
-104
lines changed

9 files changed

+104
-104
lines changed

README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Here's our `User` module.
7272
const users = [
7373
{ id: 0, name: "Dan" },
7474
{ id: 1, name: "Jessica" },
75-
{ id: 2, name: "Mike" }
75+
{ id: 2, name: "Mike" },
7676
];
7777

7878
export async function list() {
@@ -94,22 +94,22 @@ import * as Subprotocol from "@daniel-nagy/transporter/Subprotocol";
9494
import * as User from "./User";
9595

9696
const Api = {
97-
User
97+
User,
9898
};
9999

100100
export type Api = typeof Api;
101101

102102
const protocol = Subprotocol.init({
103103
connectionMode: Subprotocol.ConnectionMode.Connectionless,
104+
dataType: Subprotocol.Datatype<Json.t>(),
104105
operationMode: Subprotocol.OperationMode.Unicast,
105-
protocol: Subprotocol.Protocol<Json.t>(),
106-
transmissionMode: Subprotocol.TransmissionMode.HalfDuplex
106+
transmissionMode: Subprotocol.TransmissionMode.HalfDuplex,
107107
});
108108

109109
const session = Session.server({ protocol, provide: Api });
110110
```
111111

112-
For now don't worry about the different modes and just focus on the data format. In this case we are telling Transporter that our API only uses JSON data types. With strict type checking enabled we get a type error.
112+
For now don't worry about the different modes and just focus on the data type. In this case we are telling Transporter that our API only uses JSON data types. With strict type checking enabled we get a type error.
113113

114114
```
115115
Type 'undefined' is not assignable to type 'Json'.
@@ -121,8 +121,8 @@ Can you spot the problem? If you can't then don't worry because the compiler spo
121121
- import * as Json from "@daniel-nagy/transporter/Json";
122122
+ import * as SuperJson from "@daniel-nagy/transporter/SuperJson";
123123

124-
- protocol: Subprotocol.Protocol<Json.t>(),
125-
+ protocol: Subprotocol.Protocol<SuperJson.t>(),
124+
- dataType: Subprotocol.DataType<Json.t>(),
125+
+ dataType: Subprotocol.DataType<SuperJson.t>(),
126126
```
127127

128128
With that change the error will go away.
@@ -140,14 +140,14 @@ import type { Api } from "./Server";
140140

141141
const protocol = Subprotocol.init({
142142
connectionMode: Subprotocol.ConnectionMode.Connectionless,
143+
dataType: Subprotocol.DataType<SuperJson.t>(),
143144
operationMode: Subprotocol.OperationMode.Unicast,
144-
protocol: Subprotocol.Protocol<SuperJson.t>(),
145-
transmissionMode: Subprotocol.TransmissionMode.HalfDuplex
145+
transmissionMode: Subprotocol.TransmissionMode.HalfDuplex,
146146
});
147147

148148
const session = Session.client({
149149
protocol,
150-
resource: Session.Resource<Api>()
150+
resource: Session.Resource<Api>(),
151151
});
152152

153153
const client = session.createProxy();
@@ -174,11 +174,11 @@ Bun.serve({
174174
async fetch(req) {
175175
using session = Session.server({ protocol, provide: Api });
176176
const reply = Observable.firstValueFrom(session.output);
177-
const message = SuperJson.fromJson(await req.json())
177+
const message = SuperJson.fromJson(await req.json());
178178
session.input.next(message as Message.t<SuperJson.t>);
179179
return Response.json(SuperJson.toJson(await reply));
180180
},
181-
port: 3000
181+
port: 3000,
182182
});
183183
```
184184

@@ -195,27 +195,27 @@ const session = Session.client({
195195
protocol,
196196
resource: Session.Resource<Api>(),
197197
});
198-
198+
199199
const toRequest = (message: string) =>
200200
new Request("http://localhost:3000", {
201201
body: message,
202202
headers: {
203-
"Content-Type": "application/json"
203+
"Content-Type": "application/json",
204204
},
205205
method: "POST",
206206
});
207-
207+
208208
session.output
209209
.pipe(
210210
Observable.map(SuperJson.toJson),
211211
Observable.map(JSON.stringify),
212212
Observable.map(toRequest),
213213
Observable.flatMap(fetch),
214-
Observable.flatMap(response => response.json()),
214+
Observable.flatMap((response) => response.json()),
215215
Observable.map(SuperJson.fromJson)
216216
)
217217
.subscribe(session.input);
218-
218+
219219
const client = session.createProxy();
220220
```
221221

@@ -232,9 +232,9 @@ It is important to make sure your subprotocol and your transport layer are compa
232232
```ts
233233
const protocol = Subprotocol.init({
234234
connectionMode: Subprotocol.ConnectionMode.ConnectionOriented,
235+
dataType: Subprotocol.DataType<SuperJson.t>(),
235236
operationMode: Subprotocol.OperationMode.Unicast,
236-
protocol: Subprotocol.Protocol<SuperJson.t>(),
237-
transmissionMode: Subprotocol.TransmissionMode.Duplex
237+
transmissionMode: Subprotocol.TransmissionMode.Duplex,
238238
});
239239
```
240240

@@ -271,4 +271,3 @@ This example uses the `BrowserServer` API to communicate with a service worker.
271271
This example renders a webview with a button to scan a barcode. When the button is tapped it will use the `BarCodeScanner` component from Expo to access the camera to scan a barcode. Because this example uses the camera you will need to run it on a real device. I just use the Expo Go app on my phone.
272272

273273
Transporter does not currently offer any React Native specific APIs. However, I may add React Native specific APIs similar to the browser APIs. It's just that React Native can be..._time consuming_.
274-

packages/browser/src/BroadcastSubject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type Observer<T> = Required<Observable.Observer<T>>;
1111

1212
const protocol = Subprotocol.init({
1313
connectionMode: Subprotocol.ConnectionMode.Connectionless,
14+
dataType: Subprotocol.DataType<StructuredCloneable.t>(),
1415
operationMode: Subprotocol.OperationMode.Broadcast,
15-
protocol: Subprotocol.Protocol<StructuredCloneable.t>(),
1616
transmissionMode: Subprotocol.TransmissionMode.Simplex
1717
});
1818

packages/browser/src/StructuredCloneable.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ test("memoizing a proxied function", async () => {
377377

378378
const protocol = Subprotocol.init({
379379
connectionMode: Subprotocol.ConnectionMode.ConnectionOriented,
380+
dataType: Subprotocol.DataType<StructuredCloneable.t>(),
380381
operationMode: Subprotocol.OperationMode.Unicast,
381-
protocol: Subprotocol.Protocol<StructuredCloneable.t>(),
382382
transmissionMode: Subprotocol.TransmissionMode.Duplex
383383
});
384384

@@ -396,13 +396,13 @@ function expose<const T>(
396396
} = {}
397397
) {
398398
const client = Session.client({
399-
protocol: protocol,
399+
protocol,
400400
resource: Session.Resource<T>()
401401
});
402402

403403
const server = Session.server({
404404
...serverConfig,
405-
protocol: protocol,
405+
protocol,
406406
provide: value
407407
});
408408

packages/browser/src/StructuredCloneable.typetest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import * as Subprotocol from "../../../node_modules/@daniel-nagy/transporter/src
66

77
const protocol = Subprotocol.init({
88
connectionMode: Subprotocol.ConnectionMode.ConnectionOriented,
9+
dataType: Subprotocol.DataType<StructuredCloneable.t>(),
910
operationMode: Subprotocol.OperationMode.Unicast,
10-
protocol: Subprotocol.Protocol<StructuredCloneable.t>(),
1111
transmissionMode: Subprotocol.TransmissionMode.Duplex
1212
});
1313

0 commit comments

Comments
 (0)