menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

TypeScript

A place to talk about TypeScript.

Channels
Team

plz provide me working solution for this:

May 17, 2021 at 12:17pm

plz provide me working solution for this:

May 17, 2021 at 12:17pm
I'm working on a practice problem using setTimeOut and callbacks for the first time. This is the situation:
You receive a JavaScript object 'recordMap' of the following
type: { [recordName: string]: Client }
Client is an interface allowing to a) send a text message, b) receive a text message by registering a callback function.
interface Client {
sendMessage: (message: string) => void;
//to send messages to the Client
onMessage: (callback: (message: string) => void) =>
void; // a callback for receiving messages from the
Client
}
Public method 'initialize' will initialize the tracking of active Clients.
The goal is to make sure that no records in 'recordMap' are kept for inactive Clients.
//to register Send a 'ping' message to all Clients every 10 seconds. Register a callback function to receive messages from Clients.
Sending and receiving messages is an asynchronous form of communication - give Clients 3 seconds to reply to your message.
For every Client who hasn't replied with a 'pong' message, remove his record from 'recordMap'.
type RecordMap = { [recordName: string]: Client };
class RecordTracker {
constructor (private recordMap: RecordMap) {}
public initialize (): void {
...?
}
public getRecordMap (): RecordMap {
return { ...this.recordMap };
}
}
I feel like I need to use setInterval for a message every 10 seconds and setTimeOut to send and receive the message, but not sure where to go from there.

May 18, 2021 at 7:31am
interface Client {
}
type RecordMap = { [recordName: string]: Client };
class RecordTracker {
constructor (private recordMap: RecordMap) {}
public initialize (): void {
const pingWait = 10000;
Object.entries(this.recordMap).forEach(([recordName, client]) => {
let checking = false;
const checkClient = async () => {
checking = true;
setTimeout(() => {
if(!checking) return;
clearInterval(ping);
delete this.recordMap[recordName];
}, 3000)
await fetch("/url-to-check-client");
checking = false;
};
const ping = setInterval(checkClient, pingWait);
})
}
public getRecordMap (): RecordMap {
return { ...this.recordMap };
}
}

May 24, 2021 at 6:14am
how to create dummy records in RecordMap