@omnigrid/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/columns.d.ts +33 -0
- package/dist/columns.js +77 -0
- package/dist/dom-pool.d.ts +70 -0
- package/dist/dom-pool.js +138 -0
- package/dist/events.d.ts +6 -0
- package/dist/events.js +22 -0
- package/dist/grid.d.ts +53 -0
- package/dist/grid.js +290 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/slots.d.ts +34 -0
- package/dist/slots.js +76 -0
- package/dist/store.d.ts +10 -0
- package/dist/store.js +21 -0
- package/dist/types.d.ts +358 -0
- package/dist/types.js +1 -0
- package/dist/virtualizer.d.ts +41 -0
- package/dist/virtualizer.js +188 -0
- package/package.json +27 -0
package/dist/grid.js
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { flattenColumns, getColumnGroups } from "./columns";
|
|
2
|
+
import { EventBus } from "./events";
|
|
3
|
+
import { SlotManager } from "./slots";
|
|
4
|
+
import { Store } from "./store";
|
|
5
|
+
import { Virtualizer } from "./virtualizer";
|
|
6
|
+
export class Grid {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.events = new EventBus();
|
|
9
|
+
this.cleanupPlugins = new Set();
|
|
10
|
+
this.dataProcessors = new Set();
|
|
11
|
+
this.processedDataCache = null;
|
|
12
|
+
this.revision = 0;
|
|
13
|
+
this.destroyed = false;
|
|
14
|
+
/**
|
|
15
|
+
* Separate ephemeral scroll position that is NOT stored in the Store.
|
|
16
|
+
*
|
|
17
|
+
* Scroll-only updates (scrollTop/scrollLeft) flow through here and never
|
|
18
|
+
* trigger Store notifications, so React (or any adapter subscribed to the
|
|
19
|
+
* Store) does NOT re-render on every scroll frame. Dimension changes
|
|
20
|
+
* (width / height) still go through the Store and DO trigger re-renders.
|
|
21
|
+
*/
|
|
22
|
+
this.scrollPosition = { scrollTop: 0, scrollLeft: 0 };
|
|
23
|
+
const state = {
|
|
24
|
+
data: options.data ?? [],
|
|
25
|
+
columns: options.columns,
|
|
26
|
+
viewport: { width: 0, height: 0, scrollTop: 0, scrollLeft: 0 },
|
|
27
|
+
rowHeight: options.rowHeight ?? 32,
|
|
28
|
+
rowOverscan: options.rowOverscan ?? 5,
|
|
29
|
+
columnOverscan: options.columnOverscan ?? 2,
|
|
30
|
+
};
|
|
31
|
+
this.store = new Store(state);
|
|
32
|
+
this.resolveRowId = options.getRowId ?? ((_row, index) => index);
|
|
33
|
+
this.virtualizer = new Virtualizer({
|
|
34
|
+
rowHeight: state.rowHeight,
|
|
35
|
+
rowOverscan: state.rowOverscan,
|
|
36
|
+
columnOverscan: state.columnOverscan,
|
|
37
|
+
});
|
|
38
|
+
this.slots = new SlotManager({
|
|
39
|
+
onChange: (slot, mounts) => {
|
|
40
|
+
this.store.setState({});
|
|
41
|
+
this.events.emit("slotsChange", { slot, mounts });
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
this.store.subscribe(() => this.events.emit("stateChange", this.store.getState()));
|
|
45
|
+
options.plugins?.forEach((plugin) => this.registerPlugin(plugin));
|
|
46
|
+
}
|
|
47
|
+
getState() {
|
|
48
|
+
return this.store.getState();
|
|
49
|
+
}
|
|
50
|
+
/** Returns the current ephemeral scroll position (not part of Store state). */
|
|
51
|
+
getScrollPosition() {
|
|
52
|
+
return this.scrollPosition;
|
|
53
|
+
}
|
|
54
|
+
getViewportData() {
|
|
55
|
+
const state = this.getState();
|
|
56
|
+
const processedData = this.getProcessedData();
|
|
57
|
+
const width = state.viewport.width;
|
|
58
|
+
const scrollLeft = this.scrollPosition.scrollLeft;
|
|
59
|
+
// Column grouping: the working set for virtualization is the FLAT
|
|
60
|
+
// array of leaf columns. The hierarchy lives in `state.columns`;
|
|
61
|
+
// groups are projected onto the visible leaf order in `columnGroups`
|
|
62
|
+
// so adapters can render stacked group header rows.
|
|
63
|
+
const visibleColumns = flattenColumns(state.columns).filter((column) => !column.hidden);
|
|
64
|
+
const columnGroups = getColumnGroups(state.columns, (leaf) => !leaf.hidden);
|
|
65
|
+
// Pre-compute group layout metadata for adapters:
|
|
66
|
+
// - `headerRowCount`: number of group header rows above the leaf row
|
|
67
|
+
// - `columnGroupDepth`: maps each leaf column's flat visible index to
|
|
68
|
+
// the deepest group depth that covers it (used to detect gap rows)
|
|
69
|
+
const headerRowCount = columnGroups.reduce((depth, group) => Math.max(depth, group.depth), -1) + 1;
|
|
70
|
+
const columnGroupDepth = new Map();
|
|
71
|
+
for (const groupView of columnGroups) {
|
|
72
|
+
for (let i = groupView.startIndex; i < groupView.endIndex; i++) {
|
|
73
|
+
const prev = columnGroupDepth.get(i) ?? -1;
|
|
74
|
+
if (groupView.depth > prev)
|
|
75
|
+
columnGroupDepth.set(i, groupView.depth);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Partition into three groups, preserving definition order within each.
|
|
79
|
+
const pinnedLeft = visibleColumns.filter((c) => c.pinned === "left");
|
|
80
|
+
const scrollable = visibleColumns.filter((c) => !c.pinned);
|
|
81
|
+
const pinnedRight = visibleColumns.filter((c) => c.pinned === "right");
|
|
82
|
+
// Pinned widths are computed first. Pinned columns are typically
|
|
83
|
+
// fixed-width (no flex), so getColumnOffsets returns correct sizes
|
|
84
|
+
// regardless of the viewportWidth argument — letting us derive the
|
|
85
|
+
// scrollable viewport width before sizing any flex columns.
|
|
86
|
+
const pinnedLeftOffsets = this.virtualizer.getColumnOffsets(pinnedLeft, width);
|
|
87
|
+
const pinnedLeftWidth = pinnedLeftOffsets.reduce((sum, item) => sum + item.size, 0);
|
|
88
|
+
const pinnedRightOffsets = this.virtualizer.getColumnOffsets(pinnedRight, width);
|
|
89
|
+
const pinnedRightWidth = pinnedRightOffsets.reduce((sum, item) => sum + item.size, 0);
|
|
90
|
+
// Effective viewport for the scrollable section: pinned columns take space.
|
|
91
|
+
const scrollableViewWidth = Math.max(0, width - pinnedLeftWidth - pinnedRightWidth);
|
|
92
|
+
const scrollableOffsets = this.virtualizer.getColumnOffsets(scrollable, scrollableViewWidth);
|
|
93
|
+
const scrollableWidth = scrollableOffsets.reduce((sum, item) => sum + item.size, 0);
|
|
94
|
+
const totalWidth = pinnedLeftWidth + scrollableWidth + pinnedRightWidth;
|
|
95
|
+
// Virtual range for the scrollable section only.
|
|
96
|
+
const columnRange = scrollable.length ? this.virtualizer.getColumnRange(scrollable, scrollLeft, scrollableViewWidth) : { start: 0, end: 0 };
|
|
97
|
+
const rowRange = this.virtualizer.getRowRange(processedData.length, this.scrollPosition.scrollTop, state.viewport.height);
|
|
98
|
+
// Offset composition:
|
|
99
|
+
// pinned-left: absolute from 0
|
|
100
|
+
// scrollable: shifted by pinnedLeftWidth
|
|
101
|
+
// pinned-right: shifted by pinnedLeftWidth + scrollableWidth
|
|
102
|
+
const pinnedLeftColumns = pinnedLeft.map((column, i) => {
|
|
103
|
+
const item = pinnedLeftOffsets[i];
|
|
104
|
+
return { column, index: visibleColumns.indexOf(column), offset: item.offset, width: item.size };
|
|
105
|
+
});
|
|
106
|
+
const columns = scrollable.slice(columnRange.start, columnRange.end).map((column, i) => {
|
|
107
|
+
const columnIndex = columnRange.start + i;
|
|
108
|
+
const item = scrollableOffsets[columnIndex];
|
|
109
|
+
return {
|
|
110
|
+
column,
|
|
111
|
+
index: visibleColumns.indexOf(column),
|
|
112
|
+
offset: pinnedLeftWidth + item.offset,
|
|
113
|
+
width: item.size,
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
const pinnedRightColumns = pinnedRight.map((column, i) => {
|
|
117
|
+
const item = pinnedRightOffsets[i];
|
|
118
|
+
return {
|
|
119
|
+
column,
|
|
120
|
+
index: visibleColumns.indexOf(column),
|
|
121
|
+
offset: pinnedLeftWidth + scrollableWidth + item.offset,
|
|
122
|
+
width: item.size,
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
rows: processedData.slice(rowRange.start, rowRange.end).map((data, index) => {
|
|
127
|
+
const rowIndex = rowRange.start + index;
|
|
128
|
+
return { id: this.getRowId(data, rowIndex), data, index: rowIndex, offset: rowIndex * state.rowHeight };
|
|
129
|
+
}),
|
|
130
|
+
columns,
|
|
131
|
+
pinnedLeftColumns,
|
|
132
|
+
pinnedRightColumns,
|
|
133
|
+
columnGroups,
|
|
134
|
+
headerRowCount,
|
|
135
|
+
columnGroupDepth,
|
|
136
|
+
rowRange,
|
|
137
|
+
columnRange,
|
|
138
|
+
totalWidth,
|
|
139
|
+
totalHeight: this.virtualizer.getTotalHeight(processedData.length),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
getProcessedData() {
|
|
143
|
+
// Cache is invalidated by setData / setColumns / processor registration.
|
|
144
|
+
// During scroll the pipeline is NOT re-run — critical optimization for
|
|
145
|
+
// DOM Pool operation with large row counts.
|
|
146
|
+
if (this.processedDataCache !== null)
|
|
147
|
+
return this.processedDataCache;
|
|
148
|
+
const result = [...this.dataProcessors].reduce((data, processor) => processor(data), this.getState().data);
|
|
149
|
+
this.processedDataCache = result;
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
getRowId(row, index) {
|
|
153
|
+
return this.resolveRowId(row, index);
|
|
154
|
+
}
|
|
155
|
+
setData(data) {
|
|
156
|
+
this.assertActive();
|
|
157
|
+
this.processedDataCache = null;
|
|
158
|
+
this.revision += 1;
|
|
159
|
+
this.store.setState({ data });
|
|
160
|
+
this.events.emit("dataChange", data);
|
|
161
|
+
}
|
|
162
|
+
setViewport(viewport) {
|
|
163
|
+
this.assertActive();
|
|
164
|
+
const state = this.store.getState();
|
|
165
|
+
const current = state.viewport;
|
|
166
|
+
const nextViewport = { ...current, ...viewport };
|
|
167
|
+
// isUnchanged compares against the ephemeral scrollPosition, NOT
|
|
168
|
+
// the Store's stale scrollTop (which scroll-only updates never
|
|
169
|
+
// write to). Comparing against Store.scrollTop would make every
|
|
170
|
+
// return-to-zero a no-op: 0 === 0 → early return → scrollPosition
|
|
171
|
+
// never updated → DomPool renders rows at the old offset → empty grid.
|
|
172
|
+
const isUnchanged = nextViewport.width === current.width &&
|
|
173
|
+
nextViewport.height === current.height &&
|
|
174
|
+
nextViewport.scrollTop === this.scrollPosition.scrollTop &&
|
|
175
|
+
nextViewport.scrollLeft === this.scrollPosition.scrollLeft;
|
|
176
|
+
if (isUnchanged)
|
|
177
|
+
return;
|
|
178
|
+
// Update the ephemeral scroll position ONLY for properties explicitly
|
|
179
|
+
// passed. Dimension-only updates (e.g. scrollbar-detection calling
|
|
180
|
+
// `setViewport({ width })`) must NOT clobber scrollPosition with a
|
|
181
|
+
// stale Store scrollTop — otherwise the DomPool syncs to the wrong
|
|
182
|
+
// position until the next rAF tick, causing blank/half-empty grids
|
|
183
|
+
// on rapid scroll-to-top.
|
|
184
|
+
if (viewport.scrollTop !== undefined) {
|
|
185
|
+
this.scrollPosition.scrollTop = viewport.scrollTop;
|
|
186
|
+
}
|
|
187
|
+
if (viewport.scrollLeft !== undefined) {
|
|
188
|
+
this.scrollPosition.scrollLeft = viewport.scrollLeft;
|
|
189
|
+
}
|
|
190
|
+
// Only notify Store subscribers (e.g. React via useSyncExternalStore)
|
|
191
|
+
// when dimensions change. Scroll-only updates must NOT trigger a
|
|
192
|
+
// Store notification — otherwise every scroll frame re-renders React.
|
|
193
|
+
const hasStructuralChange = nextViewport.width !== current.width || nextViewport.height !== current.height;
|
|
194
|
+
if (hasStructuralChange) {
|
|
195
|
+
// Dimension changes are structural for the DOM Pool: adapters use
|
|
196
|
+
// the revision to force-rebind row geometry (pixel widths) that
|
|
197
|
+
// was computed for the previous viewport size. Without this bump a
|
|
198
|
+
// shrunken grid could keep rows at their old WIDE width, stretching
|
|
199
|
+
// scrollWidth beyond the last column and leaving phantom empty
|
|
200
|
+
// space to the right of the table.
|
|
201
|
+
this.revision += 1;
|
|
202
|
+
this.store.setState({ viewport: nextViewport });
|
|
203
|
+
}
|
|
204
|
+
this.events.emit("viewportChange", nextViewport);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Repaints the current viewport: notifies Store subscribers that
|
|
208
|
+
* visible rows and row classes (rowClassRules) may need rebinding
|
|
209
|
+
* in a single batch. Increments revision so adapters can detect
|
|
210
|
+
* structural changes.
|
|
211
|
+
*/
|
|
212
|
+
refresh() {
|
|
213
|
+
this.assertActive();
|
|
214
|
+
this.revision += 1;
|
|
215
|
+
this.store.setState({});
|
|
216
|
+
}
|
|
217
|
+
setColumns(columns) {
|
|
218
|
+
this.assertActive();
|
|
219
|
+
this.processedDataCache = null;
|
|
220
|
+
this.revision += 1;
|
|
221
|
+
this.store.setState({ columns });
|
|
222
|
+
}
|
|
223
|
+
registerDataProcessor(processor) {
|
|
224
|
+
this.assertActive();
|
|
225
|
+
this.dataProcessors.add(processor);
|
|
226
|
+
this.processedDataCache = null;
|
|
227
|
+
const unregister = () => {
|
|
228
|
+
this.dataProcessors.delete(processor);
|
|
229
|
+
this.processedDataCache = null;
|
|
230
|
+
};
|
|
231
|
+
return unregister;
|
|
232
|
+
}
|
|
233
|
+
headerClick(columnId, multiSort = false) {
|
|
234
|
+
this.assertActive();
|
|
235
|
+
this.events.emit("headerClick", { columnId, multiSort });
|
|
236
|
+
}
|
|
237
|
+
rowClick(row) {
|
|
238
|
+
this.assertActive();
|
|
239
|
+
this.events.emit("rowClick", row);
|
|
240
|
+
}
|
|
241
|
+
rowHover(row) {
|
|
242
|
+
this.assertActive();
|
|
243
|
+
this.events.emit("rowHover", row);
|
|
244
|
+
}
|
|
245
|
+
subscribe(listener) {
|
|
246
|
+
if (this.destroyed)
|
|
247
|
+
return () => undefined;
|
|
248
|
+
return this.store.subscribe(listener);
|
|
249
|
+
}
|
|
250
|
+
on(event, listener) {
|
|
251
|
+
this.assertActive();
|
|
252
|
+
return this.events.on(event, listener);
|
|
253
|
+
}
|
|
254
|
+
registerPlugin(plugin) {
|
|
255
|
+
this.assertActive();
|
|
256
|
+
const cleanup = plugin.register(this);
|
|
257
|
+
const unregister = () => {
|
|
258
|
+
cleanup?.();
|
|
259
|
+
this.cleanupPlugins.delete(unregister);
|
|
260
|
+
};
|
|
261
|
+
this.cleanupPlugins.add(unregister);
|
|
262
|
+
return unregister;
|
|
263
|
+
}
|
|
264
|
+
getSlotMounts(slot) {
|
|
265
|
+
return this.slots.getMounts(slot);
|
|
266
|
+
}
|
|
267
|
+
/** Monotonic counter for structural changes (refresh / data / columns). */
|
|
268
|
+
getRevision() {
|
|
269
|
+
return this.revision;
|
|
270
|
+
}
|
|
271
|
+
destroy() {
|
|
272
|
+
if (this.destroyed)
|
|
273
|
+
return;
|
|
274
|
+
this.cleanupPlugins.forEach((cleanup) => cleanup());
|
|
275
|
+
this.cleanupPlugins.clear();
|
|
276
|
+
this.dataProcessors.clear();
|
|
277
|
+
this.processedDataCache = null;
|
|
278
|
+
this.slots.clear();
|
|
279
|
+
this.store.clear();
|
|
280
|
+
this.events.clear();
|
|
281
|
+
this.destroyed = true;
|
|
282
|
+
}
|
|
283
|
+
isDestroyed() {
|
|
284
|
+
return this.destroyed;
|
|
285
|
+
}
|
|
286
|
+
assertActive() {
|
|
287
|
+
if (this.destroyed)
|
|
288
|
+
throw new Error("Grid has been destroyed");
|
|
289
|
+
}
|
|
290
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { DomPool } from "./dom-pool";
|
|
2
|
+
export { EventBus } from "./events";
|
|
3
|
+
export { Grid } from "./grid";
|
|
4
|
+
export { Grid as GridCore } from "./grid";
|
|
5
|
+
export { SlotManager } from "./slots";
|
|
6
|
+
export { Store } from "./store";
|
|
7
|
+
export type { CellAlign, CellRenderParams, CellClassRules, CheckboxControl, CheckboxRenderParams, ColumnDef, ColumnGroupDef, ColumnGroupViewport, ColumnLeafDef, DataProcessor, DomPoolBindings, DomPoolUpdate, GridApi, GridEvents, GridOptions, GridPlugin, GridState, PooledRow, Range, RowClassRules, RowClickEvent, RowHost, RowHoverEvent, RowId, RowRenderParams, RowStyle, ScrollPosition, SlotComponentContent, SlotContent, SlotHtmlContent, SlotMount, SlotMountOptions, SlotName, SlotNodeContent, SlotNodeEventName, SlotPosition, SlotProvider, SlotRenderContext, SortDirection, ViewportData, ViewportState, VirtualItem, } from "./types";
|
|
8
|
+
export { getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf } from "./columns";
|
|
9
|
+
export { Virtualizer } from "./virtualizer";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { DomPool } from "./dom-pool";
|
|
2
|
+
export { EventBus } from "./events";
|
|
3
|
+
export { Grid } from "./grid";
|
|
4
|
+
export { Grid as GridCore } from "./grid";
|
|
5
|
+
export { SlotManager } from "./slots";
|
|
6
|
+
export { Store } from "./store";
|
|
7
|
+
export { getColumnGroups, flattenColumns, isColumnGroup, isColumnLeaf } from "./columns";
|
|
8
|
+
export { Virtualizer } from "./virtualizer";
|
package/dist/slots.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { SlotMount, SlotMountOptions, SlotName, SlotProvider } from "./types";
|
|
2
|
+
export interface SlotManagerOptions<T> {
|
|
3
|
+
/**
|
|
4
|
+
* Called after each slot mount change. The grid uses this hook to
|
|
5
|
+
* emit a `slotsChange` event and wake up the adapter.
|
|
6
|
+
*/
|
|
7
|
+
onChange?: (slot: SlotName, mounts: SlotMount<T>[]) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Slot manager (`top`, `bottom`, `left`, `right`).
|
|
11
|
+
*
|
|
12
|
+
* Plugins mount their widgets via `api.slots.mount(...)` — the core stores
|
|
13
|
+
* only a headless description (`SlotContent`) and materializes it through the
|
|
14
|
+
* adapter. Order within a slot is controlled by `priority`, alignment by
|
|
15
|
+
* `position`.
|
|
16
|
+
*/
|
|
17
|
+
export declare class SlotManager<T> {
|
|
18
|
+
private readonly mounts;
|
|
19
|
+
private readonly onChange;
|
|
20
|
+
private sequence;
|
|
21
|
+
constructor(options?: SlotManagerOptions<T>);
|
|
22
|
+
/**
|
|
23
|
+
* Mounts content in a slot. If an `id` matches an existing mount, it is
|
|
24
|
+
* replaced. Returns a handle with `unmount()`.
|
|
25
|
+
*/
|
|
26
|
+
mount(slot: SlotName, content: SlotProvider<T>, options?: SlotMountOptions): SlotMount<T>;
|
|
27
|
+
/** Removes a mount by id within a slot. Returns true if found. */
|
|
28
|
+
unmount(slot: SlotName, id: string): boolean;
|
|
29
|
+
/** Sorted copy of mounts for the slot. */
|
|
30
|
+
getMounts(slot: SlotName): SlotMount<T>[];
|
|
31
|
+
hasMounts(slot: SlotName): boolean;
|
|
32
|
+
clear(slot?: SlotName): void;
|
|
33
|
+
size(slot?: SlotName): number;
|
|
34
|
+
}
|
package/dist/slots.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slot manager (`top`, `bottom`, `left`, `right`).
|
|
3
|
+
*
|
|
4
|
+
* Plugins mount their widgets via `api.slots.mount(...)` — the core stores
|
|
5
|
+
* only a headless description (`SlotContent`) and materializes it through the
|
|
6
|
+
* adapter. Order within a slot is controlled by `priority`, alignment by
|
|
7
|
+
* `position`.
|
|
8
|
+
*/
|
|
9
|
+
export class SlotManager {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.mounts = new Map();
|
|
12
|
+
this.sequence = 0;
|
|
13
|
+
this.onChange = options.onChange;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Mounts content in a slot. If an `id` matches an existing mount, it is
|
|
17
|
+
* replaced. Returns a handle with `unmount()`.
|
|
18
|
+
*/
|
|
19
|
+
mount(slot, content, options = {}) {
|
|
20
|
+
const id = options.id ?? `${slot}-${++this.sequence}`;
|
|
21
|
+
const priority = options.priority ?? 0;
|
|
22
|
+
const position = options.position ?? "start";
|
|
23
|
+
const slotMounts = (this.mounts.get(slot) ?? []).filter((existing) => existing.id !== id);
|
|
24
|
+
const mount = {
|
|
25
|
+
id,
|
|
26
|
+
slot,
|
|
27
|
+
content,
|
|
28
|
+
priority,
|
|
29
|
+
position,
|
|
30
|
+
unmount: () => {
|
|
31
|
+
this.unmount(slot, id);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
slotMounts.push(mount);
|
|
35
|
+
slotMounts.sort((left, right) => (left.priority === right.priority ? 0 : left.priority < right.priority ? -1 : 1));
|
|
36
|
+
this.mounts.set(slot, slotMounts);
|
|
37
|
+
this.onChange?.(slot, this.getMounts(slot));
|
|
38
|
+
return mount;
|
|
39
|
+
}
|
|
40
|
+
/** Removes a mount by id within a slot. Returns true if found. */
|
|
41
|
+
unmount(slot, id) {
|
|
42
|
+
const slotMounts = this.mounts.get(slot);
|
|
43
|
+
if (!slotMounts)
|
|
44
|
+
return false;
|
|
45
|
+
const next = slotMounts.filter((existing) => existing.id !== id);
|
|
46
|
+
if (next.length === slotMounts.length)
|
|
47
|
+
return false;
|
|
48
|
+
this.mounts.set(slot, next);
|
|
49
|
+
if (next.length === 0)
|
|
50
|
+
this.mounts.delete(slot);
|
|
51
|
+
this.onChange?.(slot, this.getMounts(slot));
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
/** Sorted copy of mounts for the slot. */
|
|
55
|
+
getMounts(slot) {
|
|
56
|
+
return [...(this.mounts.get(slot) ?? [])];
|
|
57
|
+
}
|
|
58
|
+
hasMounts(slot) {
|
|
59
|
+
return (this.mounts.get(slot)?.length ?? 0) > 0;
|
|
60
|
+
}
|
|
61
|
+
clear(slot) {
|
|
62
|
+
if (slot) {
|
|
63
|
+
if (this.mounts.delete(slot))
|
|
64
|
+
this.onChange?.(slot, []);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const affected = [...this.mounts.keys()];
|
|
68
|
+
this.mounts.clear();
|
|
69
|
+
affected.forEach((affectedSlot) => this.onChange?.(affectedSlot, []));
|
|
70
|
+
}
|
|
71
|
+
size(slot) {
|
|
72
|
+
if (slot)
|
|
73
|
+
return this.mounts.get(slot)?.length ?? 0;
|
|
74
|
+
return [...this.mounts.values()].reduce((total, items) => total + items.length, 0);
|
|
75
|
+
}
|
|
76
|
+
}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type StateUpdater<State> = Partial<State> | ((state: State) => Partial<State>);
|
|
2
|
+
export declare class Store<State extends object> {
|
|
3
|
+
private state;
|
|
4
|
+
private readonly listeners;
|
|
5
|
+
constructor(initialState: State);
|
|
6
|
+
getState(): State;
|
|
7
|
+
setState(updater: StateUpdater<State>): void;
|
|
8
|
+
subscribe(listener: () => void): () => void;
|
|
9
|
+
clear(): void;
|
|
10
|
+
}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class Store {
|
|
2
|
+
constructor(initialState) {
|
|
3
|
+
this.listeners = new Set();
|
|
4
|
+
this.state = initialState;
|
|
5
|
+
}
|
|
6
|
+
getState() {
|
|
7
|
+
return this.state;
|
|
8
|
+
}
|
|
9
|
+
setState(updater) {
|
|
10
|
+
const update = typeof updater === 'function' ? updater(this.state) : updater;
|
|
11
|
+
this.state = { ...this.state, ...update };
|
|
12
|
+
this.listeners.forEach((listener) => listener());
|
|
13
|
+
}
|
|
14
|
+
subscribe(listener) {
|
|
15
|
+
this.listeners.add(listener);
|
|
16
|
+
return () => this.listeners.delete(listener);
|
|
17
|
+
}
|
|
18
|
+
clear() {
|
|
19
|
+
this.listeners.clear();
|
|
20
|
+
}
|
|
21
|
+
}
|