Skip to content

Commit 97c27fd

Browse files
authored
Merge pull request #2 from mikamisonodev/main
chore: fix type when client is fully ready
2 parents 4bd634b + 5275a46 commit 97c27fd

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

DOCS.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [`new Client(cookies, options)`](#constructor)
99
* [`client.connect()`](#connect)
1010
* [`client.disconnect()`](#disconnect)
11+
* [`client.isFullyReady()`](#isfullyready)
1112
* [Properties](#properties)
1213
* [Regular Messages](#regular-messages)
1314
* [`client.sendMessage()`](#sendMessage)
@@ -188,6 +189,19 @@ console.log('Disconnected')
188189

189190
---
190191

192+
<a name="isFullyReady"></a>
193+
## client.isFullyReady()
194+
195+
Check if client is fully ready (socket + E2EE if enabled).
196+
197+
__Example__
198+
199+
```typescript
200+
console.log(client.isFullyReady())
201+
```
202+
203+
---
204+
191205
<a name="properties"></a>
192206
## Properties
193207

@@ -236,15 +250,6 @@ __Type:__ `boolean`
236250

237251
---
238252

239-
<a name="isFullyReady"></a>
240-
### client.isFullyReady
241-
242-
Check if client is fully ready (socket + E2EE if enabled).
243-
244-
__Type:__ `boolean`
245-
246-
---
247-
248253
# Regular Messages
249254

250255
<a name="sendMessage"></a>

DOCS_VI.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [`new Client(cookies, options)`](#constructor)
99
* [`client.connect()`](#connect)
1010
* [`client.disconnect()`](#disconnect)
11+
* [`client.isFullyReady()`](#isfullyready)
1112
* [Thuộc tính](#thuộc-tính)
1213
* [Tin nhắn thường](#tin-nhắn-thường)
1314
* [`client.sendMessage()`](#sendMessage)
@@ -188,6 +189,19 @@ console.log('Đã ngắt kết nối')
188189

189190
---
190191

192+
<a name="isFullyReady"></a>
193+
## client.isFullyReady()
194+
195+
Kiểm tra client đã hoàn toàn sẵn sàng (socket + E2EE nếu enabled).
196+
197+
__Example__
198+
199+
```typescript
200+
console.log(client.isFullyReady())
201+
```
202+
203+
---
204+
191205
<a name="thuộc-tính"></a>
192206
## Thuộc tính
193207

@@ -236,15 +250,6 @@ __Type:__ `boolean`
236250

237251
---
238252

239-
<a name="isFullyReady"></a>
240-
### client.isFullyReady
241-
242-
Kiểm tra client đã hoàn toàn sẵn sàng (socket + E2EE nếu enabled).
243-
244-
__Type:__ `boolean`
245-
246-
---
247-
248253
# Tin nhắn thường
249254

250255
<a name="sendMessage"></a>

src/client.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ export interface ClientEventMap {
8888
* await client.connect()
8989
* ```
9090
*/
91-
export class Client extends (EventEmitter as new () => TypedEventEmitter<ClientEventMap>) {
91+
92+
export type If<T extends boolean, A, B = null> = T extends true ? A : T extends false ? B : A | B;
93+
94+
export class Client<
95+
Ready extends boolean = boolean,
96+
> extends (EventEmitter as new () => TypedEventEmitter<ClientEventMap>) {
9297
private handle: number | null = null;
9398
private options: ClientOptions;
9499
private cookies: Cookies;
@@ -124,28 +129,28 @@ export class Client extends (EventEmitter as new () => TypedEventEmitter<ClientE
124129
/**
125130
* Get the current user info
126131
*/
127-
get user(): User | null {
128-
return this._user;
132+
get user(): If<Ready, User> {
133+
return this._user as If<Ready, User>;
129134
}
130135

131136
/**
132137
* Get the current user's Facebook ID
133138
*/
134-
get currentUserId(): number | null {
135-
return this._user?.id ?? null;
139+
get currentUserId(): If<Ready, number> {
140+
return (this._user?.id ?? null) as If<Ready, number>;
136141
}
137142

138143
/**
139144
* Get initial sync data (threads and messages)
140145
*/
141-
get initialData(): InitialData | null {
142-
return this._initialData;
146+
get initialData(): If<Ready, InitialData> {
147+
return this._initialData as If<Ready, InitialData>;
143148
}
144149

145150
/**
146151
* Check if client is fully ready (socket ready + E2EE connected if enabled)
147152
*/
148-
get isFullyReady(): boolean {
153+
public isFullyReady(): this is Client<true> {
149154
if (!this._socketReady) return false;
150155
if (this.options.enableE2EE && !this._e2eeConnected) return false;
151156
return true;
@@ -848,7 +853,7 @@ export class Client extends (EventEmitter as new () => TypedEventEmitter<ClientE
848853
}
849854

850855
private checkFullyReady(): void {
851-
if (this.isFullyReady && !this._fullyReadyEmitted) {
856+
if (this.isFullyReady() && !this._fullyReadyEmitted) {
852857
this._fullyReadyEmitted = true;
853858
this.emit("fullyReady");
854859
// flush pending events

0 commit comments

Comments
 (0)