了解带有回调的 JavaScript 代码

发布于 2024-10-08 23:38:06 字数 2424 浏览 4 评论 0原文

下面您可以看到 twich.me node.js 聊天的服务器端代码的一部分:

exports.channel = function(MESSAGE_BACKLOG, MESSAGE_TRUNCATE) {
    return (function() {
        var messages  = [],
            callbacks = [];

        return {
            appendMessage : function (nick, room, type, text) {

  //truncate message if necessary
  if (type == 'msg' && text.length > MESSAGE_TRUNCATE) {
   text = text.substr(0, MESSAGE_TRUNCATE) + "... (trunc.)";
  }

  //message
  var m = {
   nick: nick,
   type: type, // "msg", "join", "part"
   text: text,
   room: room,
   timestamp: (new Date()).getTime()
  };

  //output to console
//  mlog(m);

  //push msg on message stack
  messages.push( m );

  //???
                while (callbacks.length > 0) {
                    callbacks.shift().callback([m]);
                }

  //old messages get pushed out of message stack
                while (messages.length > MESSAGE_BACKLOG) {
                    messages.shift();
                }
            },

            query : function (room, since, callback) {
                var matching = [];
                for (var i = 0; i < messages.length; i++) {
                    var message = messages[i];
                    if (message.timestamp > since && room == message.room) {
                        matching.push(message)
                    }
                }

    //???
                if (matching.length != 0) {
                    callback(matching);
                }
                else {
                    callbacks.push({ timestamp: new Date(), callback: callback });
                }
            },

     //run initially when script starts
            init : function() {
                // clear old callbacks older than 25 seconds (lowered from 30 seconds to get round rmit proxy server's 30sec timeout
                setInterval(function () {
                    var now = new Date();
                    while (callbacks.length > 0 && now - callbacks[0].timestamp > 25*1000) {
                        callbacks.shift().callback([]);
                    }
                }, 3000);
                return "hi";
            }
        }
    }());
}

代码负责存储和检索其中一个聊天室的聊天消息。 我不是 JavaScript 程序员。我的背景是 PHP,一切都是程序化的。我想用 memcached 来解决这个问题。但首先我需要了解到底发生了什么。我添加了额外的评论。我不明白的是回调的所有内容。你能帮我理解回调在做什么吗?

Below you see a part of the server-side code of the twich.me node.js chat:

exports.channel = function(MESSAGE_BACKLOG, MESSAGE_TRUNCATE) {
    return (function() {
        var messages  = [],
            callbacks = [];

        return {
            appendMessage : function (nick, room, type, text) {

  //truncate message if necessary
  if (type == 'msg' && text.length > MESSAGE_TRUNCATE) {
   text = text.substr(0, MESSAGE_TRUNCATE) + "... (trunc.)";
  }

  //message
  var m = {
   nick: nick,
   type: type, // "msg", "join", "part"
   text: text,
   room: room,
   timestamp: (new Date()).getTime()
  };

  //output to console
//  mlog(m);

  //push msg on message stack
  messages.push( m );

  //???
                while (callbacks.length > 0) {
                    callbacks.shift().callback([m]);
                }

  //old messages get pushed out of message stack
                while (messages.length > MESSAGE_BACKLOG) {
                    messages.shift();
                }
            },

            query : function (room, since, callback) {
                var matching = [];
                for (var i = 0; i < messages.length; i++) {
                    var message = messages[i];
                    if (message.timestamp > since && room == message.room) {
                        matching.push(message)
                    }
                }

    //???
                if (matching.length != 0) {
                    callback(matching);
                }
                else {
                    callbacks.push({ timestamp: new Date(), callback: callback });
                }
            },

     //run initially when script starts
            init : function() {
                // clear old callbacks older than 25 seconds (lowered from 30 seconds to get round rmit proxy server's 30sec timeout
                setInterval(function () {
                    var now = new Date();
                    while (callbacks.length > 0 && now - callbacks[0].timestamp > 25*1000) {
                        callbacks.shift().callback([]);
                    }
                }, 3000);
                return "hi";
            }
        }
    }());
}

The code is responsible for storing and retrieving chat messages from one of the chat rooms.
I am not a javascript programmer. My background is with PHP where everything is procedural. I want to solve this with memcached instead. But first I need to understand what exactly is going on. I have added extra comments. What I don't understand is all the stuff with the callbacks. Could you help me understand what the callbacks are doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-15 23:38:06

我不太明白你想要什么,但事情是这样的:

            while (callbacks.length > 0) {
                callbacks.shift().callback([m]);
            }

虽然数组回调中的对象数量大于 0,
callbacks.shift() 函数显然会返回一个具有名为回调的属性的对象,该属性是一个函数。它使用一个包含变量 m 的数组来调用该函数。

            if (matching.length != 0) {
                callback(matching);
            }
            else {
                callbacks.push({ timestamp: new Date(), callback: callback });
            }
        }

如果数组中匹配的对象数量不为 0,则调用回调函数;如果为 0,则使用对象调用回调函数callback.push。

I don't really understand what you want but here's what going on:

            while (callbacks.length > 0) {
                callbacks.shift().callback([m]);
            }

while the amount of objects in the array callbacks is greater than 0,
callbacks.shift() function will apparently return an object with a property called callback which is a function. and it's calling that function with an array that has the variable m in it.

            if (matching.length != 0) {
                callback(matching);
            }
            else {
                callbacks.push({ timestamp: new Date(), callback: callback });
            }
        }

if the amount of objects in the array matching is not 0, call the function callback or if it does, cal the function callback.push with an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文