@gamestdio/signals
Version:
Minimal Signal implementation for JavaScript.
146 lines (145 loc) • 4.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var SlotList = (function () {
function SlotList(head, tail) {
if (tail === void 0) { tail = null; }
this.nonEmpty = false;
if (!head && !tail) {
if (SlotList.NIL) {
throw new Error("Parameters head and tail are null. Use the NIL element instead.");
}
this.nonEmpty = false;
}
else if (!head) {
throw new Error("Parameter head cannot be null.");
}
else {
this.head = head;
this.tail = tail || SlotList.NIL;
this.nonEmpty = true;
}
}
Object.defineProperty(SlotList.prototype, "length", {
get: function () {
if (!this.nonEmpty) {
return 0;
}
if (this.tail === SlotList.NIL) {
return 1;
}
var result = 0;
var p = this;
while (p.nonEmpty) {
++result;
p = p.tail;
}
return result;
},
enumerable: true,
configurable: true
});
SlotList.prototype.prepend = function (slot) {
return new SlotList(slot, this);
};
SlotList.prototype.append = function (slot) {
if (!slot) {
return this;
}
if (!this.nonEmpty) {
return new SlotList(slot);
}
if (this.tail === SlotList.NIL) {
return new SlotList(slot).prepend(this.head);
}
var wholeClone = new SlotList(this.head);
var subClone = wholeClone;
var current = this.tail;
while (current.nonEmpty) {
subClone = subClone.tail = new SlotList(current.head);
current = current.tail;
}
subClone.tail = new SlotList(slot);
return wholeClone;
};
SlotList.prototype.insertWithPriority = function (slot) {
if (!this.nonEmpty) {
return new SlotList(slot);
}
var priority = slot.priority;
if (priority > this.head.priority) {
return this.prepend(slot);
}
var wholeClone = new SlotList(this.head);
var subClone = wholeClone;
var current = this.tail;
while (current.nonEmpty) {
if (priority > current.head.priority) {
subClone.tail = current.prepend(slot);
return wholeClone;
}
subClone = subClone.tail = new SlotList(current.head);
current = current.tail;
}
subClone.tail = new SlotList(slot);
return wholeClone;
};
SlotList.prototype.filterNot = function (listener) {
if (!this.nonEmpty || listener == null) {
return this;
}
if (listener === this.head.listener) {
return this.tail;
}
var wholeClone = new SlotList(this.head);
var subClone = wholeClone;
var current = this.tail;
while (current.nonEmpty) {
if (current.head.listener === listener) {
subClone.tail = current.tail;
return wholeClone;
}
subClone = subClone.tail = new SlotList(current.head);
current = current.tail;
}
return this;
};
SlotList.prototype.contains = function (listener) {
if (!this.nonEmpty) {
return false;
}
var p = this;
while (p.nonEmpty) {
if (p.head.listener === listener) {
return true;
}
p = p.tail;
}
return false;
};
SlotList.prototype.find = function (listener) {
if (!this.nonEmpty) {
return null;
}
var p = this;
while (p.nonEmpty) {
if (p.head.listener === listener) {
return p.head;
}
p = p.tail;
}
return null;
};
SlotList.prototype.toString = function () {
var buffer = "";
var p = this;
while (p.nonEmpty) {
buffer += p.head + " -> ";
p = p.tail;
}
buffer += "NIL";
return "[List " + buffer + "]";
};
SlotList.NIL = new SlotList(null, null);
return SlotList;
}());
exports.SlotList = SlotList;