From 22da36158c8a1a0dcccf9638acbeba9584e00c7e Mon Sep 17 00:00:00 2001 From: sveisvei Date: Mon, 7 Nov 2016 08:44:46 +0100 Subject: [PATCH] fix list logic typ0 --- packages/unleash-api/lib/client-metrics/list.js | 6 +++--- packages/unleash-api/lib/client-metrics/list.test.js | 6 +++--- packages/unleash-api/lib/client-metrics/ttl-list.test.js | 9 +++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/unleash-api/lib/client-metrics/list.js b/packages/unleash-api/lib/client-metrics/list.js index 676b70b054..1b53a121a0 100644 --- a/packages/unleash-api/lib/client-metrics/list.js +++ b/packages/unleash-api/lib/client-metrics/list.js @@ -11,6 +11,7 @@ class Node { link (next) { this.next = next; next.prev = this; + return this; } } @@ -30,9 +31,8 @@ module.exports = class List extends EventEmitter { add (obj) { const node = new Node(obj); - if (this.tail) { - this.tail.link(node); - this.tail = node; + if (this.start) { + this.start = node.link(this.start); } else { this.start = node; this.tail = node; diff --git a/packages/unleash-api/lib/client-metrics/list.test.js b/packages/unleash-api/lib/client-metrics/list.test.js index 4de293914f..f60e342f82 100644 --- a/packages/unleash-api/lib/client-metrics/list.test.js +++ b/packages/unleash-api/lib/client-metrics/list.test.js @@ -51,10 +51,10 @@ test('list should be able remove until given value', (t) => { list.reverseRemoveUntilTrue(({ value }) => value === 4); t.true(list.toArray().length === 4); - list.reverseRemoveUntilTrue(({ value }) => value === 3); + list.reverseRemoveUntilTrue(({ value }) => value === 5); t.true(list.toArray().length === 3); - list.reverseRemoveUntilTrue(({ value }) => value === 3); + list.reverseRemoveUntilTrue(({ value }) => value === 5); t.true(list.toArray().length === 3); }); @@ -98,7 +98,7 @@ test('should reverse iterate', (t) => { let iterateCount = 0; list.iterateReverse(({ value }) => { iterateCount++; - if (value === 3) { + if (value === 5) { return false; } return true; diff --git a/packages/unleash-api/lib/client-metrics/ttl-list.test.js b/packages/unleash-api/lib/client-metrics/ttl-list.test.js index 0a7b7dd3ab..41a41729f0 100644 --- a/packages/unleash-api/lib/client-metrics/ttl-list.test.js +++ b/packages/unleash-api/lib/client-metrics/ttl-list.test.js @@ -28,16 +28,13 @@ test.cb('should slice off list', (t) => { }); // console.time('4'); - list.add({ n: '4' }, moment().add(300, 'milliseconds')); - // console.time('3'); - list.add({ n: '3' }, moment().add(200, 'milliseconds')); - // console.time('2'); - list.add({ n: '2' }, moment().add(50, 'milliseconds')); - // console.time('1'); list.add({ n: '1' }, moment().add(1, 'milliseconds')); + list.add({ n: '2' }, moment().add(50, 'milliseconds')); + list.add({ n: '3' }, moment().add(200, 'milliseconds')); + list.add({ n: '4' }, moment().add(300, 'milliseconds')); const expired = [];