Skip to content

Commit

Permalink
Update fetch API
Browse files Browse the repository at this point in the history
  • Loading branch information
nodegin committed Feb 11, 2018
1 parent c365aab commit d3a5138
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 8 deletions.
29 changes: 27 additions & 2 deletions Client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ref = require('ref')
const uuidv4 = require('uuid/v4')
const ffi = require('ffi-napi')
const ref = require('ref')
const path = require('path')

const { buildQuery, getInput, emptyFunction } = require('./utils')
Expand Down Expand Up @@ -35,6 +36,7 @@ class Client {
'_update': emptyFunction,
'_error': emptyFunction,
}
this.fetching = {}
this.init()
}

Expand Down Expand Up @@ -167,7 +169,30 @@ class Client {
}

async handleUpdate(update) {
this.listeners['_update'].call(null, update)
const id = update['@extra']
if (this.fetching[id]) {
delete update['@extra']
this.fetching[id](update)
delete this.fetching[id]
} else {
this.listeners['_update'].call(null, update)
}
}

async fetch(query) {
const id = uuidv4()
query['@extra'] = id
const receiveUpdate = new Promise((resolve, reject) => {
this.fetching[id] = resolve
// timeout after 10 seconds
setTimeout(() => {
delete this.fetching[id]
reject('Query timed out after 10 seconds.')
}, 1000 * 10)
})
await this._send(query)
const result = await receiveUpdate
return result
}

_create() {
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ TDLib (Telegram Database library) bindings for Node.js

-----

### Handling updates and errors

By default, tglib will handle the incoming updates internally. You can attach your own event listener for receiving updates and errors.

-----

### Fetch

Since TDLib is asynchronous, if you need to use some API like `getChats`, you need to add the `@extra` property to your query in `client._send` and filter the corresponding result in `client._receive` by matching the same `@extra` property that defined previously.

tglib provides an easy and convenient way to deal with these API called `client.fetch`, all you need to do is call the function and awaits for the result. tglib will take care of `client._send` and `client._receive`.

-----

### License

MIT
23 changes: 23 additions & 0 deletions examples/getChats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { Client } = require('tglib')

async function main() {
const client = new Client({
apiId: 'YOUR_API_ID',
apiHash: 'YOUR_API_HASH',
phoneNumber: 'YOUR_PHONE_NUMBER',
})

await client.connect()

const result = await client.fetch({
'@type': 'getChats',
'offset_order': '9223372036854775807',
'offset_chat_id': 0,
'limit': 100,
})

// latest 100 chats will be returned
console.log(result)
}

main()
21 changes: 21 additions & 0 deletions examples/handleUpdates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { Client } = require('tglib')

async function main() {
const client = new Client({
apiId: 'YOUR_API_ID',
apiHash: 'YOUR_API_HASH',
phoneNumber: 'YOUR_PHONE_NUMBER',
})

client.on('_update', (update) => {
console.log('Got update:', JSON.stringify(update, null, 2))
})

client.on('_error', (update) => {
console.error('Got error:', JSON.stringify(update, null, 2))
})

await client.connect()
}

main()
4 changes: 0 additions & 4 deletions examples/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ async function main() {
phoneNumber: 'YOUR_PHONE_NUMBER',
})

client.on('_update', (update) => {
console.log('Got update:', JSON.stringify(update, null, 2))
})

await client.connect()

await client._send({
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tglib",
"version": "0.0.2",
"version": "0.0.3",
"description": "TDLib (Telegram Database library) bindings for Node.js",
"main": "index.js",
"scripts": {
Expand All @@ -23,6 +23,7 @@
"dependencies": {
"ffi-napi": "^2.4.2",
"inquirer": "^5.1.0",
"ref": "^1.3.5"
"ref": "^1.3.5",
"uuid": "^3.2.1"
}
}

0 comments on commit d3a5138

Please sign in to comment.