Untitled

 avatar
unknown
plain_text
a month ago
2.9 kB
6
Indexable

//make/ initiate a variable called startTime that is assigned to static method Date.now()
//Date.Now returns number of milliseconds since the epoch
const startTime = Date.now();

//make/initiative a variable that is of type null. In JS, null is an object
var conversation = null;

//making a class called Conversation- classes define the structure for custom objects that do a thing
class Conversation {
    /*a constructor that creates new conversation objects- the parameters are the properties of the class
    The constructor method is basically declaring all of the variables the class will rely on- as MDN 
    states, it lets you do custom initialisation that is necessary for other methods to be called on these 
    instantiated objects)
     */
    constructor(messageData, contactName, onMessageReceived, onMessageSent) {
        //instantiates these objects
        this.messageData = messageData;
        this.contactName = contactName;

        //messageIdx is declared as int type of -1
        this.messageIdx = -1;
        
        // contains both sent and received messages- messages is an empty array that stores sent and received messages
        this.messages = [];

        // callbacks- these are set up to be passed as arguments later on to do something (read code further)
        this.onMessageReceived = onMessageReceived;
        this.onMessageSent = onMessageSent;
    }
    

    //new method- called start
    start() {
        //we have a method contained within it called set timeout- here the messageIndx is set to 0 and pings are set to 1
        setTimeout(() => {
            //array index is set to 0
            this.messageIdx = 0;
            this.pings = 1;
            
            //data is a variable that accesses messageData at the specified index of 0. Assumotion that messages are array
            //this is due to messages object defined in our constructor
            const data = this.messageData[this.messageIdx];
            
            //message is a variable that accesses an instance of ReceivedMessage- this holds the message at position 0
            const message = new ReceivedMessage(data);
            //we add that received message into our empty array of messages
            this.messages.push(message);
            
            //callback happens! It receives the messages and executes here
            this.onMessageReceived(message);
        }, 3623); //this message is executed after specified timeout of 3623 ms
    }
    
    /*Summary: the start method takes in message data at the index of 0, adds it to the message store and receives it after 3262 ms */

    //new method- called peekNextMessage - it returns an instance of messageData at the position of the message index (currently 0) + 1
    peekNextMessageData() {
        return this.messageData[this.messageIdx + 1];
    }

   
Leave a Comment