Update misc documentation and spell indices correctly
This commit is contained in:
parent
f8cbadaba5
commit
aafbd7f208
4 changed files with 27 additions and 30 deletions
|
@ -121,7 +121,7 @@ class RoomListStore extends Store {
|
||||||
_checkDisabled() {
|
_checkDisabled() {
|
||||||
this.disabled = SettingsStore.isFeatureEnabled("feature_new_room_list");
|
this.disabled = SettingsStore.isFeatureEnabled("feature_new_room_list");
|
||||||
if (this.disabled) {
|
if (this.disabled) {
|
||||||
console.warn("DISABLING LEGACY ROOM LIST STORE");
|
console.warn("👋 legacy room list store has been disabled");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
|
||||||
private checkEnabled() {
|
private checkEnabled() {
|
||||||
this.enabled = SettingsStore.isFeatureEnabled("feature_new_room_list");
|
this.enabled = SettingsStore.isFeatureEnabled("feature_new_room_list");
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
console.log("ENABLING NEW ROOM LIST STORE");
|
console.log("⚡ new room list store engaged");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,7 +225,7 @@ class _RoomListStore extends AsyncStore<ActionPayload> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.state.tagsEnabled) {
|
if (this.state.tagsEnabled) {
|
||||||
// TODO: Find a more reliable way to get tags
|
// TODO: Find a more reliable way to get tags (this doesn't work)
|
||||||
const roomTags = TagOrderStore.getOrderedTags() || [];
|
const roomTags = TagOrderStore.getOrderedTags() || [];
|
||||||
console.log("rtags", roomTags);
|
console.log("rtags", roomTags);
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,6 @@ const CATEGORY_ORDER = [Category.Red, Category.Grey, Category.Bold, Category.Idl
|
||||||
*/
|
*/
|
||||||
export class ImportanceAlgorithm extends Algorithm {
|
export class ImportanceAlgorithm extends Algorithm {
|
||||||
|
|
||||||
// TODO: Update documentation
|
|
||||||
// HOW THIS WORKS
|
// HOW THIS WORKS
|
||||||
// --------------
|
// --------------
|
||||||
//
|
//
|
||||||
|
@ -93,19 +92,17 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
// can be found from `this.indices[tag][category]` and the sticky room information
|
// can be found from `this.indices[tag][category]` and the sticky room information
|
||||||
// from `this.stickyRoom`.
|
// from `this.stickyRoom`.
|
||||||
//
|
//
|
||||||
// Room categories are constantly re-evaluated and tracked in the `this.categorized`
|
|
||||||
// object. Note that this doesn't track rooms by category but instead by room ID.
|
|
||||||
// The theory is that by knowing the previous position, new desired position, and
|
|
||||||
// category indices we can avoid tracking multiple complicated maps in memory.
|
|
||||||
//
|
|
||||||
// The room list store is always provided with the `this.cached` results, which are
|
// The room list store is always provided with the `this.cached` results, which are
|
||||||
// updated as needed and not recalculated often. For example, when a room needs to
|
// updated as needed and not recalculated often. For example, when a room needs to
|
||||||
// move within a tag, the array in `this.cached` will be spliced instead of iterated.
|
// move within a tag, the array in `this.cached` will be spliced instead of iterated.
|
||||||
|
// The `indices` help track the positions of each category to make splicing easier.
|
||||||
|
|
||||||
private indices: {
|
private indices: {
|
||||||
// @ts-ignore - TS wants this to be a string but we know better than it
|
// @ts-ignore - TS wants this to be a string but we know better than it
|
||||||
[tag: TagID]: ICategoryIndex;
|
[tag: TagID]: ICategoryIndex;
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
|
// TODO: Use this (see docs above)
|
||||||
private stickyRoom: {
|
private stickyRoom: {
|
||||||
roomId: string;
|
roomId: string;
|
||||||
tag: TagID;
|
tag: TagID;
|
||||||
|
@ -178,14 +175,14 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
const newlyOrganized: Room[] = [];
|
const newlyOrganized: Room[] = [];
|
||||||
const newIndicies: ICategoryIndex = {};
|
const newIndices: ICategoryIndex = {};
|
||||||
|
|
||||||
for (const category of CATEGORY_ORDER) {
|
for (const category of CATEGORY_ORDER) {
|
||||||
newIndicies[category] = newlyOrganized.length;
|
newIndices[category] = newlyOrganized.length;
|
||||||
newlyOrganized.push(...categorized[category]);
|
newlyOrganized.push(...categorized[category]);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.indices[tagId] = newIndicies;
|
this.indices[tagId] = newIndices;
|
||||||
updatedTagMap[tagId] = newlyOrganized;
|
updatedTagMap[tagId] = newlyOrganized;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,7 +202,7 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
const taggedRooms = this.cached[tag];
|
const taggedRooms = this.cached[tag];
|
||||||
const indicies = this.indices[tag];
|
const indices = this.indices[tag];
|
||||||
let roomIdx = taggedRooms.indexOf(room);
|
let roomIdx = taggedRooms.indexOf(room);
|
||||||
if (roomIdx === -1) {
|
if (roomIdx === -1) {
|
||||||
console.warn(`Degrading performance to find missing room in "${tag}": ${room.roomId}`);
|
console.warn(`Degrading performance to find missing room in "${tag}": ${room.roomId}`);
|
||||||
|
@ -217,16 +214,16 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
|
|
||||||
// Try to avoid doing array operations if we don't have to: only move rooms within
|
// Try to avoid doing array operations if we don't have to: only move rooms within
|
||||||
// the categories if we're jumping categories
|
// the categories if we're jumping categories
|
||||||
const oldCategory = this.getCategoryFromIndicies(roomIdx, indicies);
|
const oldCategory = this.getCategoryFromIndices(roomIdx, indices);
|
||||||
if (oldCategory !== category) {
|
if (oldCategory !== category) {
|
||||||
// Move the room and update the indicies
|
// Move the room and update the indices
|
||||||
this.moveRoomIndexes(1, oldCategory, category, indicies);
|
this.moveRoomIndexes(1, oldCategory, category, indices);
|
||||||
taggedRooms.splice(roomIdx, 1); // splice out the old index (fixed position)
|
taggedRooms.splice(roomIdx, 1); // splice out the old index (fixed position)
|
||||||
taggedRooms.splice(indicies[category], 0, room); // splice in the new room (pre-adjusted)
|
taggedRooms.splice(indices[category], 0, room); // splice in the new room (pre-adjusted)
|
||||||
// Note: if moveRoomIndexes() is called after the splice then the insert operation
|
// Note: if moveRoomIndexes() is called after the splice then the insert operation
|
||||||
// will happen in the wrong place. Because we would have already adjusted the index
|
// will happen in the wrong place. Because we would have already adjusted the index
|
||||||
// for the category, we don't need to determine how the room is moving in the list.
|
// for the category, we don't need to determine how the room is moving in the list.
|
||||||
// If we instead tried to insert before updating the indicies, we'd have to determine
|
// If we instead tried to insert before updating the indices, we'd have to determine
|
||||||
// whether the room was moving later (towards IDLE) or earlier (towards RED) from its
|
// whether the room was moving later (towards IDLE) or earlier (towards RED) from its
|
||||||
// current position, as it'll affect the category's start index after we remove the
|
// current position, as it'll affect the category's start index after we remove the
|
||||||
// room from the array.
|
// room from the array.
|
||||||
|
@ -240,8 +237,8 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
// array and slot the changed room in quickly.
|
// array and slot the changed room in quickly.
|
||||||
const nextCategoryStartIdx = category === CATEGORY_ORDER[CATEGORY_ORDER.length - 1]
|
const nextCategoryStartIdx = category === CATEGORY_ORDER[CATEGORY_ORDER.length - 1]
|
||||||
? Number.MAX_SAFE_INTEGER
|
? Number.MAX_SAFE_INTEGER
|
||||||
: indicies[CATEGORY_ORDER[CATEGORY_ORDER.indexOf(category) + 1]];
|
: indices[CATEGORY_ORDER[CATEGORY_ORDER.indexOf(category) + 1]];
|
||||||
const startIdx = indicies[category];
|
const startIdx = indices[category];
|
||||||
const numSort = nextCategoryStartIdx - startIdx; // splice() returns up to the max, so MAX_SAFE_INT is fine
|
const numSort = nextCategoryStartIdx - startIdx; // splice() returns up to the max, so MAX_SAFE_INT is fine
|
||||||
const unsortedSlice = taggedRooms.splice(startIdx, numSort);
|
const unsortedSlice = taggedRooms.splice(startIdx, numSort);
|
||||||
const sorted = await sortRoomsWithAlgorithm(unsortedSlice, tag, this.sortAlgorithms[tag]);
|
const sorted = await sortRoomsWithAlgorithm(unsortedSlice, tag, this.sortAlgorithms[tag]);
|
||||||
|
@ -253,12 +250,12 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCategoryFromIndicies(index: number, indicies: ICategoryIndex): Category {
|
private getCategoryFromIndices(index: number, indices: ICategoryIndex): Category {
|
||||||
for (let i = 0; i < CATEGORY_ORDER.length; i++) {
|
for (let i = 0; i < CATEGORY_ORDER.length; i++) {
|
||||||
const category = CATEGORY_ORDER[i];
|
const category = CATEGORY_ORDER[i];
|
||||||
const isLast = i === (CATEGORY_ORDER.length - 1);
|
const isLast = i === (CATEGORY_ORDER.length - 1);
|
||||||
const startIdx = indicies[category];
|
const startIdx = indices[category];
|
||||||
const endIdx = isLast ? Number.MAX_SAFE_INTEGER : indicies[CATEGORY_ORDER[i + 1]];
|
const endIdx = isLast ? Number.MAX_SAFE_INTEGER : indices[CATEGORY_ORDER[i + 1]];
|
||||||
if (index >= startIdx && index < endIdx) {
|
if (index >= startIdx && index < endIdx) {
|
||||||
return category;
|
return category;
|
||||||
}
|
}
|
||||||
|
@ -268,21 +265,21 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
throw new Error("Programming error: somehow you've ended up with an index that isn't in a category");
|
throw new Error("Programming error: somehow you've ended up with an index that isn't in a category");
|
||||||
}
|
}
|
||||||
|
|
||||||
private moveRoomIndexes(nRooms: number, fromCategory: Category, toCategory: Category, indicies: ICategoryIndex) {
|
private moveRoomIndexes(nRooms: number, fromCategory: Category, toCategory: Category, indices: ICategoryIndex) {
|
||||||
// We have to update the index of the category *after* the from/toCategory variables
|
// We have to update the index of the category *after* the from/toCategory variables
|
||||||
// in order to update the indicies correctly. Because the room is moving from/to those
|
// in order to update the indices correctly. Because the room is moving from/to those
|
||||||
// categories, the next category's index will change - not the category we're modifying.
|
// categories, the next category's index will change - not the category we're modifying.
|
||||||
// We also need to update subsequent categories as they'll all shift by nRooms, so we
|
// We also need to update subsequent categories as they'll all shift by nRooms, so we
|
||||||
// loop over the order to achieve that.
|
// loop over the order to achieve that.
|
||||||
|
|
||||||
for (let i = CATEGORY_ORDER.indexOf(fromCategory) + 1; i < CATEGORY_ORDER.length; i++) {
|
for (let i = CATEGORY_ORDER.indexOf(fromCategory) + 1; i < CATEGORY_ORDER.length; i++) {
|
||||||
const nextCategory = CATEGORY_ORDER[i];
|
const nextCategory = CATEGORY_ORDER[i];
|
||||||
indicies[nextCategory] -= nRooms;
|
indices[nextCategory] -= nRooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = CATEGORY_ORDER.indexOf(toCategory) + 1; i < CATEGORY_ORDER.length; i++) {
|
for (let i = CATEGORY_ORDER.indexOf(toCategory) + 1; i < CATEGORY_ORDER.length; i++) {
|
||||||
const nextCategory = CATEGORY_ORDER[i];
|
const nextCategory = CATEGORY_ORDER[i];
|
||||||
indicies[nextCategory] += nRooms;
|
indices[nextCategory] += nRooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do a quick check to see if we've completely broken the index
|
// Do a quick check to see if we've completely broken the index
|
||||||
|
@ -290,9 +287,9 @@ export class ImportanceAlgorithm extends Algorithm {
|
||||||
const lastCat = CATEGORY_ORDER[i - 1];
|
const lastCat = CATEGORY_ORDER[i - 1];
|
||||||
const thisCat = CATEGORY_ORDER[i];
|
const thisCat = CATEGORY_ORDER[i];
|
||||||
|
|
||||||
if (indicies[lastCat] > indicies[thisCat]) {
|
if (indices[lastCat] > indices[thisCat]) {
|
||||||
// "should never happen" disclaimer goes here
|
// "should never happen" disclaimer goes here
|
||||||
console.warn(`!! Room list index corruption: ${lastCat} (i:${indicies[lastCat]}) is greater than ${thisCat} (i:${indicies[thisCat]}) - category indicies are likely desynced from reality`);
|
console.warn(`!! Room list index corruption: ${lastCat} (i:${indices[lastCat]}) is greater than ${thisCat} (i:${indices[thisCat]}) - category indices are likely desynced from reality`);
|
||||||
|
|
||||||
// TODO: Regenerate index when this happens
|
// TODO: Regenerate index when this happens
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,5 +38,5 @@ export type TagID = string | DefaultTagID;
|
||||||
|
|
||||||
export enum RoomUpdateCause {
|
export enum RoomUpdateCause {
|
||||||
Timeline = "TIMELINE",
|
Timeline = "TIMELINE",
|
||||||
RoomRead = "ROOM_READ",
|
RoomRead = "ROOM_READ", // TODO: Use this.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue