|
3 | 3 | </div> |
4 | 4 |
|
5 | 5 | ```bash |
6 | | -npm i @hazae41/binary |
| 6 | +npm i @hazae41/future |
7 | 7 | ``` |
8 | 8 |
|
9 | | -[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/binary) |
| 9 | +[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/future) |
10 | 10 |
|
11 | 11 | ## Features |
12 | 12 |
|
13 | 13 | ### Current features |
14 | 14 | - 100% TypeScript and ESM |
15 | 15 | - No external dependencies |
16 | 16 | - Unit-tested |
17 | | -- Zero-copy reading and writing |
18 | 17 |
|
19 | 18 | ## Usage |
20 | 19 |
|
21 | | -### Cursor |
22 | | - |
23 | | -#### Writing |
24 | | - |
25 | 20 | ```typescript |
26 | | -const cursor = Cursor.allocUnsafe(1024) |
27 | | - |
28 | | -cursor.writeUint8(123) |
29 | | -cursor.writeUint16(1234) |
30 | | - |
31 | | -console.log(cursor.offset) // 3 |
32 | | -``` |
33 | | - |
34 | | -#### Reading |
35 | | - |
36 | | -```typescript |
37 | | -const bytes = new Uint8Array(/*...*/) |
38 | | -const cursor = new Cursor(bytes) |
39 | | - |
40 | | -const uint8 = cursor.readUint8() |
41 | | -const uint16 = cursor.readUint16() |
42 | | - |
43 | | -console.log(cursor.offset) // 3 |
44 | | -``` |
| 21 | +const future = new Future<void, unknown>() |
45 | 22 |
|
46 | | -### Binary data types |
47 | | - |
48 | | -#### Writable |
49 | | - |
50 | | -```typescript |
51 | | -class MyObject implements Writable { |
52 | | - |
53 | | - constructor( |
54 | | - readonly x: number, |
55 | | - readonly y: number |
56 | | - ) {} |
57 | | - |
58 | | - size() { |
59 | | - return 1 + 2 |
60 | | - } |
61 | | - |
62 | | - write(cursor: Cursor) { |
63 | | - cursor.writeUint8(this.x) |
64 | | - cursor.writeUint16(this.y) |
65 | | - } |
| 23 | +const okTimeout = setTimeout(() => future.ok(), 1000) |
| 24 | +const errTimeout = setTimeout(() => future.err(), 2000) |
66 | 25 |
|
| 26 | +try { |
| 27 | + await future.promise |
| 28 | +} finally { |
| 29 | + clearTimeout(okTimeout) |
| 30 | + clearTimeout(errTimeout) |
67 | 31 | } |
68 | | -``` |
69 | | - |
70 | | -```typescript |
71 | | -const myobject = new MyObject(1, 515) |
72 | | -const bytes = Writable.toBytes(myobject) // Uint8Array([1, 2, 3]) |
73 | | -``` |
74 | | - |
75 | | -#### Readable |
76 | | - |
77 | | -```typescript |
78 | | -class MyObject { |
79 | | - |
80 | | - constructor( |
81 | | - readonly x: number, |
82 | | - readonly y: number |
83 | | - ) {} |
84 | | - |
85 | | - static read(cursor: Cursor) { |
86 | | - const x = cursor.readUint8() |
87 | | - const y = cursor.readUint16() |
88 | | - |
89 | | - return new this(x, y) |
90 | | - } |
91 | | - |
92 | | -} |
93 | | -``` |
94 | | - |
95 | | -```typescript |
96 | | -const bytes = new Uint8Array([1, 2, 3]) |
97 | | -const myobject = Readable.fromBytes(MyObject, bytes) // MyObject(1, 515) |
98 | | -``` |
99 | | - |
100 | | -#### Opaque |
101 | | - |
102 | | -This is a binary data type that just holds bytes, it can be used when a binary data type is required |
103 | | - |
104 | | -```typescript |
105 | | -const bytes = new Uint8Array([1, 2, 3]) |
106 | | -const opaque = Readable.fromBytes(SafeOpaque, bytes) // Opaque(Uint8Array([1, 2, 3])) |
107 | | -const myobject = opaque.into(MyObject) // MyObject(1, 515) |
108 | | -``` |
109 | | - |
110 | | -```typescript |
111 | | -const myobject = new MyObject(1, 515) |
112 | | -const opaque = Opaque.from(myobject) // Opaque(Uint8Array([1, 2, 3])) |
113 | | -const bytes = Writable.toBytes(opaque) // Uint8Array([1, 2, 3]) |
114 | | -``` |
| 32 | +``` |
0 commit comments