Merge pull request #664 from thelounge/xpaw/fix-defaults

Fix memory and reference shuffling when creating models
This commit is contained in:
Pavel Djundik 2016-10-15 14:14:53 +03:00 committed by GitHub
commit 3711aefc2d
5 changed files with 67 additions and 14 deletions

View File

@ -15,7 +15,7 @@ Chan.Type = {
var id = 0; var id = 0;
function Chan(attr) { function Chan(attr) {
_.merge(this, _.extend({ _.defaults(this, attr, {
id: id++, id: id++,
messages: [], messages: [],
name: "", name: "",
@ -25,7 +25,7 @@ function Chan(attr) {
unread: 0, unread: 0,
highlight: false, highlight: false,
users: [] users: []
}, attr)); });
} }
Chan.prototype.pushMessage = function(client, msg) { Chan.prototype.pushMessage = function(client, msg) {

View File

@ -28,13 +28,13 @@ module.exports = Msg;
var id = 0; var id = 0;
function Msg(attr) { function Msg(attr) {
_.merge(this, _.extend({ _.defaults(this, attr, {
from: "", from: "",
id: id++, id: id++,
text: "", text: "",
type: Msg.Type.MESSAGE, type: Msg.Type.MESSAGE,
self: false self: false
}, attr)); });
if (this.time > 0) { if (this.time > 0) {
this.time = new Date(this.time); this.time = new Date(this.time);

View File

@ -8,7 +8,7 @@ module.exports = Network;
var id = 0; var id = 0;
function Network(attr) { function Network(attr) {
_.merge(this, _.extend({ _.defaults(this, attr, {
name: "", name: "",
host: "", host: "",
port: 6667, port: 6667,
@ -26,7 +26,8 @@ function Network(attr) {
PREFIX: [], PREFIX: [],
}, },
chanCache: [], chanCache: [],
}, attr)); });
this.name = attr.name || prettify(attr.host); this.name = attr.name || prettify(attr.host);
this.channels.unshift( this.channels.unshift(
new Chan({ new Chan({
@ -55,9 +56,10 @@ Network.prototype.setNick = function(nick) {
Network.prototype.toJSON = function() { Network.prototype.toJSON = function() {
return _.omit(this, [ return _.omit(this, [
"chanCache",
"highlightRegex",
"irc", "irc",
"password", "password",
"highlightRegex"
]); ]);
}; };

View File

@ -5,10 +5,10 @@ var _ = require("lodash");
module.exports = User; module.exports = User;
function User(attr, prefixLookup) { function User(attr, prefixLookup) {
_.merge(this, _.extend({ _.defaults(this, attr, {
modes: [], modes: [],
nick: "" nick: ""
}, attr)); });
// irc-framework sets character mode, but lounge works with symbols // irc-framework sets character mode, but lounge works with symbols
this.modes = this.modes.map(mode => prefixLookup[mode]); this.modes = this.modes.map(mode => prefixLookup[mode]);

View File

@ -3,18 +3,23 @@
var expect = require("chai").expect; var expect = require("chai").expect;
var Chan = require("../../src/models/chan"); var Chan = require("../../src/models/chan");
var Msg = require("../../src/models/msg");
var Network = require("../../src/models/network"); var Network = require("../../src/models/network");
describe("Network", function() { describe("Network", function() {
describe("#export()", function() { describe("#export()", function() {
it("should produce an valid object", function() { it("should produce an valid object", function() {
var network = new Network({name: "networkName"}); var network = new Network({
name: "networkName",
channels: [
new Chan({name: "#thelounge"}),
new Chan({name: "&foobar"}),
new Chan({name: "Channel List", type: Chan.Type.SPECIAL}),
new Chan({name: "PrivateChat", type: Chan.Type.QUERY}),
]
});
network.setNick("chillin`"); network.setNick("chillin`");
network.channels.push(new Chan({name: "#thelounge"}));
network.channels.push(new Chan({name: "&foobar"}));
network.channels.push(new Chan({name: "Lobby", type: Chan.Type.LOBBY}));
network.channels.push(new Chan({name: "PrivateChat", type: Chan.Type.QUERY}));
expect(network.export()).to.deep.equal({ expect(network.export()).to.deep.equal({
name: "networkName", name: "networkName",
@ -34,5 +39,51 @@ describe("Network", function() {
] ]
}); });
}); });
it("lobby should be at the top", function() {
var network = new Network({
name: "Super Nice Network",
channels: [
new Chan({name: "AAAA!", type: Chan.Type.QUERY}),
new Chan({name: "#thelounge"}),
new Chan({name: "&foobar"}),
]
});
network.channels.push(new Chan({name: "#swag"}));
expect(network.channels[0].name).to.equal("Super Nice Network");
expect(network.channels[0].type).to.equal(Chan.Type.LOBBY);
});
it("should maintain channel reference", function() {
var chan = new Chan({
name: "#506-bug-fix",
messages: [
new Msg({
text: "message in constructor"
})
]
});
var network = new Network({
name: "networkName",
channels: [
chan
]
});
chan.messages.push(new Msg({
text: "message in original instance"
}));
network.channels[1].messages.push(new Msg({
text: "message after network creation"
}));
expect(network.channels[1].messages).to.have.lengthOf(3);
expect(network.channels[1].messages[0].text).to.equal("message in constructor");
expect(network.channels[1].messages[1].text).to.equal("message in original instance");
expect(network.channels[1].messages[2].text).to.equal("message after network creation");
});
}); });
}); });