@atproto/sync
Version:
atproto sync library
43 lines • 1.14 kB
JavaScript
/**
* Add items to a list, and mark those items as
* completed. Upon item completion, get list of consecutive
* items completed at the head of the list. Example:
*
* const consecutive = new ConsecutiveList<number>()
* const item1 = consecutive.push(1)
* const item2 = consecutive.push(2)
* const item3 = consecutive.push(3)
* item2.complete() // []
* item1.complete() // [1, 2]
* item3.complete() // [3]
*
*/
export class ConsecutiveList {
constructor() {
this.list = [];
}
push(value) {
const item = new ConsecutiveItem(this, value);
this.list.push(item);
return item;
}
complete() {
let i = 0;
while (this.list[i]?.isComplete) {
i += 1;
}
return this.list.splice(0, i).map((item) => item.value);
}
}
export class ConsecutiveItem {
constructor(consecutive, value) {
this.consecutive = consecutive;
this.value = value;
this.isComplete = false;
}
complete() {
this.isComplete = true;
return this.consecutive.complete();
}
}
//# sourceMappingURL=consecutive-list.js.map