This epic refactors the Store sorting mechanism to be more memory-efficient and robust by eliminating the duplication of data into initialData and instead using a lightweight, symbol-based index on records.
Problem
- Crash on Reset:
Store.sort({direction: null}) caused a crash (TypeError: me.initialData is not iterable) when initialData was null or not synced.
- Memory Overhead: The previous approach duplicated the entire dataset into
initialData solely for restoring sort order, doubling memory usage for large stores.
- Synchronization Issues: Keeping
initialData in sync with dynamic add/remove/filter operations was complex and error-prone.
- UI Updates: The Grid failed to refresh the view when sorting was reset to the initial state.
Solution Architecture
1. Symbol-Based Indexing (initialIndexSymbol)
Instead of storing a separate array of data, we now tag each record with its insertion index using a non-enumerable symbol property.
src/collection/Base.mjs: Introduced initialIndexCounter. The splice method now automatically assigns a unique, incrementing index to every new item using Symbol.for('initialIndex').
src/data/RecordFactory.mjs: Updated the generated Record class to include a field for [initialIndexSymbol]. The constructor ensures this index is preserved when converting raw data objects into Record instances.
2. Streamlined Store Sorting
src/data/Store.mjs:
- Removed
initialData config and all related synchronization logic (beforeSetInitialData, afterSetData).
- Updated
sort() to restore the natural order by sorting on initialIndexSymbol instead of clearing and reloading data.
- Enabled
me.fire('load') in onCollectionSort to ensure explicit sort operations trigger UI updates.
3. Optimized Event Flow
src/collection/Base.mjs: Modified splice to perform sorting silently (silent=true). This prevents redundant sort events (and subsequent double-reloads) during data mutations, allowing Store to listen to sort events strictly for user-initiated sorting.
4. Grid Container Updates
src/grid/Container.mjs: Updated onSortColumn to ensure the UI refreshes correctly even when the sort direction is null (reset), fixing the specific UI regression.
Benefits
- Performance: Reduced memory footprint by removing data duplication.
- Stability: Eliminated the crash vector associated with
initialData syncing.
- Maintainability: Centralized index logic in
Collection.Base, making it available to all collections, not just Stores.
- Correctness: Sorting reset now reliably updates the UI.
This epic refactors the
Storesorting mechanism to be more memory-efficient and robust by eliminating the duplication of data intoinitialDataand instead using a lightweight, symbol-based index on records.Problem
Store.sort({direction: null})caused a crash (TypeError: me.initialData is not iterable) wheninitialDatawas null or not synced.initialDatasolely for restoring sort order, doubling memory usage for large stores.initialDatain sync with dynamicadd/remove/filteroperations was complex and error-prone.Solution Architecture
1. Symbol-Based Indexing (
initialIndexSymbol)Instead of storing a separate array of data, we now tag each record with its insertion index using a non-enumerable symbol property.
src/collection/Base.mjs: IntroducedinitialIndexCounter. Thesplicemethod now automatically assigns a unique, incrementing index to every new item usingSymbol.for('initialIndex').src/data/RecordFactory.mjs: Updated the generatedRecordclass to include a field for[initialIndexSymbol]. The constructor ensures this index is preserved when converting raw data objects into Record instances.2. Streamlined Store Sorting
src/data/Store.mjs:initialDataconfig and all related synchronization logic (beforeSetInitialData,afterSetData).sort()to restore the natural order by sorting oninitialIndexSymbolinstead of clearing and reloading data.me.fire('load')inonCollectionSortto ensure explicit sort operations trigger UI updates.3. Optimized Event Flow
src/collection/Base.mjs: Modifiedspliceto perform sorting silently (silent=true). This prevents redundantsortevents (and subsequent double-reloads) during data mutations, allowingStoreto listen tosortevents strictly for user-initiated sorting.4. Grid Container Updates
src/grid/Container.mjs: UpdatedonSortColumnto ensure the UI refreshes correctly even when the sort direction isnull(reset), fixing the specific UI regression.Benefits
initialDatasyncing.Collection.Base, making it available to all collections, not just Stores.