Skip to content

Commit b91533e

Browse files
committed
methods to get Buffer's length, first and last
1 parent 0b62060 commit b91533e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/modules/adapter.js

+20
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class Adapter {
6464
});
6565
}
6666

67+
// read-only immediately calculated public properties
68+
const publicPropsImmediate = ['bufferFirst', 'bufferLast', 'bufferLength'];
69+
for (let i = publicPropsImmediate.length - 1; i >= 0; i--) {
70+
Object.defineProperty(this.publicContext, publicPropsImmediate[i], {
71+
get: () => this[publicPropsImmediate[i]]
72+
});
73+
}
74+
6775
// non-read-only public property
6876
Object.defineProperty(this.publicContext, 'disabled', {
6977
get: () => this.disabled,
@@ -87,6 +95,18 @@ class Adapter {
8795
return !this.buffer.length;
8896
}
8997

98+
get bufferLength() {
99+
return this.buffer.getItems().length;
100+
}
101+
102+
get bufferFirst() {
103+
return this.buffer.getFirstItem();
104+
}
105+
106+
get bufferLast() {
107+
return this.buffer.getLastItem();
108+
}
109+
90110
append(newItems) {
91111
this.buffer.append(newItems);
92112
this.doAdjust();

src/modules/buffer.js

+20
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ export default function ScrollBuffer(elementRoutines, bufferSize, startIndex) {
144144
}
145145
});
146146
return Math.max(0, bottom - top);
147+
},
148+
149+
getItems() {
150+
return buffer.filter(item => item.op === 'none');
151+
},
152+
153+
getFirstItem() {
154+
const list = buffer.getItems();
155+
if (!list.length) {
156+
return null;
157+
}
158+
return list[0].item;
159+
},
160+
161+
getLastItem() {
162+
const list = buffer.getItems();
163+
if (!list.length) {
164+
return null;
165+
}
166+
return list[list.length - 1].item;
147167
}
148168

149169
});

0 commit comments

Comments
 (0)