CorePlugin
The CorePlugin provides foundational data and state management at phase 0.
The CorePlugin is the foundation of ZenGrid’s plugin system, running at phase 0. It establishes the core reactive state that all other plugins build upon.
Overview
CorePlugin creates the fundamental store keys for managing grid data, rows, and columns:
rows.raw // Raw data arrayrows.indices // Row index mappingrows.viewIndices // Filtered/sorted view indicesrows.viewCount // Number of visible rowsCorePlugin has no dependencies and must be loaded before any other plugin.
Creation
Create the CorePlugin using the factory function:
import { createCorePlugin } from '@zengrid/core';
const corePlugin = createCorePlugin();grid.usePlugin(corePlugin);Store Structure
CorePlugin initializes these reactive signals in the store:
Row State
// Raw datastore.get('rows.raw') // Original data arraystore.set('rows.raw', newData) // Update data
// Index mappingstore.get('rows.indices') // Array of row indicesstore.get('rows.viewIndices') // Filtered/sorted indicesstore.get('rows.viewCount') // Number of visible rowsColumn State
store.get('columns.definitions') // Column configurationsstore.get('columns.visible') // Visible column liststore.get('columns.order') // Column display orderData Management
store.get('data.source') // Data source referencestore.get('data.loading') // Loading statestore.get('data.error') // Error statePhase 0 Initialization
CorePlugin runs at phase 0, making it the first plugin to initialize:
const CorePlugin: GridPlugin = { name: 'CorePlugin', phase: 0, dependencies: undefined,
setup(store, api) { // Initialize core state store.extend('rows.raw', [], 'CorePlugin', 0); store.extend('rows.indices', [], 'CorePlugin', 0); store.extend('rows.viewIndices', [], 'CorePlugin', 0); store.extend('rows.viewCount', 0, 'CorePlugin', 0);
return { teardown: [] }; }};Never modify CorePlugin’s phase or remove it. All other plugins depend on its initialization.
State Foundation
CorePlugin establishes the reactive foundation:
// Raw data changesstore.set('rows.raw', newData);
// Automatically updates dependent signals// → rows.indices recalculated// → rows.viewIndices updated (if filters/sort active)// → rows.viewCount recalculatedIntegration with Other Plugins
All plugins depend on CorePlugin’s state:
// SortPlugin reads rows.rawconst sortPlugin = { name: 'SortPlugin', phase: 10, dependencies: ['CorePlugin'], setup(store, api) { const rawData = store.get('rows.raw'); // Sort operations... }};
// FilterPlugin reads rows.indicesconst filterPlugin = { name: 'FilterPlugin', phase: 20, dependencies: ['CorePlugin'], setup(store, api) { const indices = store.get('rows.indices'); // Filter operations... }};API Methods
CorePlugin doesn’t register API methods directly, but provides the state other plugins use to register their methods:
// Other plugins use core stateapi.register('data', { getData: () => store.get('rows.raw'), getRowCount: () => store.get('rows.viewCount'), getRow: (index: number) => { const indices = store.get('rows.viewIndices'); const data = store.get('rows.raw'); return data[indices[index]]; }});Performance Considerations
CorePlugin uses efficient reactive patterns:
- Lazy evaluation: Computed signals only recalculate when dependencies change
- Batch updates: Multiple state changes batch into single update cycle
- Memory efficiency: Uses typed arrays for index mappings when possible
CorePlugin’s reactive state automatically propagates changes to dependent plugins without manual coordination.
Cleanup
CorePlugin cleanup happens automatically during grid destruction:
dispose() { // Store automatically cleans up signals owned by 'CorePlugin' // No manual cleanup required}