Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { equalByValue } from '@js/core/utils/common';
import { compileGetter } from '@js/core/utils/data';
import { Deferred } from '@js/core/utils/deferred';
import type { DataController } from '@ts/grids/grid_core/data_controller/data_controller';
import { focusModule } from '@ts/grids/grid_core/focus/m_focus';
import { focusModule } from '@ts/grids/grid_core/focus/focus_module';
import type { ModuleType } from '@ts/grids/grid_core/m_types';

import type { GroupingDataControllerExtension, GroupingDataSourceAdapter } from '../grouping/m_grouping';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { logger } from '@js/core/utils/console';
import type { DeferredObj } from '@js/core/utils/deferred';
import { Deferred, when } from '@js/core/utils/deferred';
import { isDefined } from '@js/core/utils/type';
import type { StoreChange } from '@js/data/store';
import errors from '@js/ui/widget/ui.errors';
import { findChanges } from '@ts/core/utils/m_array_compare';
import { fromPromise } from '@ts/core/utils/m_deferred';
Expand Down Expand Up @@ -110,8 +109,6 @@ export class DataController extends modules.Controller {

public pageChanged!: Callback<[number?]>;

public pushed!: Callback<[StoreChange[]]>;

public changed!: Callback<[DataChange]>;

public loadingChanged!: Callback<[boolean, string?]>;
Expand All @@ -129,8 +126,6 @@ export class DataController extends modules.Controller {

private loadErrorHandlerProxy!: (e: Error | string) => void;

private dataPushedHandlerProxy!: (changes: StoreChange[]) => void;

private dataChangedHandlerProxy!: (e?: ChangedEvent) => void;

public init(): void {
Expand All @@ -144,7 +139,6 @@ export class DataController extends modules.Controller {
this._currentOperationTypes = null;
this.dataChangedHandlerProxy = this.dataChangedHandler.bind(this);
this.loadErrorHandlerProxy = this.loadErrorHandler.bind(this);
this.dataPushedHandlerProxy = this.dataPushedHandler.bind(this);

this._columnsController.columnsChanged.add(this.columnsChangedHandler.bind(this));

Expand Down Expand Up @@ -181,7 +175,7 @@ export class DataController extends modules.Controller {
}

protected callbackNames(): string[] {
return ['changed', 'loadingChanged', 'dataErrorOccurred', 'pageChanged', 'dataSourceChanged', 'pushed', 'rowIndicesChanged'];
return ['changed', 'loadingChanged', 'dataErrorOccurred', 'pageChanged', 'dataSourceChanged', 'rowIndicesChanged'];
}

protected callbackFlags(name?: string): CallbackFlags | undefined {
Expand Down Expand Up @@ -608,10 +602,6 @@ export class DataController extends modules.Controller {
this.dataErrorOccurred.fire(e);
}

protected dataPushedHandler(changes: StoreChange[]): void {
this.pushed.fire(changes);
}

public fireError(...args: unknown[]): void {
this.dataErrorOccurred.fire(errors.Error(...args));
}
Expand Down Expand Up @@ -1302,7 +1292,6 @@ export class DataController extends modules.Controller {
dataSourceAdapter.loadError.add(this.loadErrorHandlerProxy);
dataSourceAdapter.customizeStoreLoadOptions.add(this.customizeStoreLoadOptionsHandler);
dataSourceAdapter.changing.add(this.changingHandler);
dataSourceAdapter.pushed.add(this.dataPushedHandlerProxy);
}

private unsubscribeFromDataSource(dataSourceAdapter: DataSourceAdapter): void {
Expand All @@ -1311,7 +1300,6 @@ export class DataController extends modules.Controller {
dataSourceAdapter.loadError.remove(this.loadErrorHandlerProxy);
dataSourceAdapter.customizeStoreLoadOptions.remove(this.customizeStoreLoadOptionsHandler);
dataSourceAdapter.changing.remove(this.changingHandler);
dataSourceAdapter.pushed.remove(this.dataPushedHandlerProxy);
}

private setDataSource(dataSource: DataSource): void {
Expand Down Expand Up @@ -1628,10 +1616,6 @@ export class DataController extends modules.Controller {
return this._dataSource?.reload(reload, changesOnly) as DeferredObj<unknown>;
}

public push(changes: StoreChange[], fromStore = false): void {
this._dataSource?.push(changes, fromStore);
}

private itemsCount(): number {
return (this._dataSource ? this._dataSource.itemsCount() : 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
jest,
} from '@jest/globals';
import { DataSource as DataSourceClass } from '@js/common/data/data_source/data_source';
import type { Callback } from '@js/core/utils/callbacks';
import Callbacks from '@js/core/utils/callbacks';
import type { StoreChange } from '@js/data/store';
import type Store from '@ts/data/abstract_store';
import type { StoreKey } from '@ts/data/abstract_store';
import type { DataSource } from '@ts/data/data_source/data_source';
Expand All @@ -24,6 +27,8 @@ interface AdapterStub {
getDataIndexGetter: jest.Mock<() => (data: RawItemData) => number>;
dispose: jest.Mock<(isShared?: boolean) => void>;
init: jest.Mock<(dataSource: DataSource) => void>;
push: jest.Mock<(changes: StoreChange[], fromStore: boolean) => void>;
pushed: Callback<[StoreChange[]]>;
}

interface ProviderStub {
Expand All @@ -40,6 +45,8 @@ const createAdapterStub = (marker: string): AdapterStub => ({
getDataIndexGetter: jest.fn(() => (): number => 0),
dispose: jest.fn(),
init: jest.fn(),
push: jest.fn(),
pushed: Callbacks(),
});

const asAdapter = (stub: AdapterStub): DataSourceAdapter => stub as unknown as DataSourceAdapter;
Expand Down Expand Up @@ -87,6 +94,8 @@ const asProvider = (

const SOURCE = { marker: 'source' } as unknown as DataSource;

const CHANGES: StoreChange[] = [{ type: 'remove', key: 1 }];

const withProvider = (adapter: AdapterStub): {
controller: TestDataSourceController;
component: InternalGrid;
Expand Down Expand Up @@ -258,6 +267,7 @@ describe('DataSourceController', () => {
controller.key();
controller.remoteOperations();
controller.getDataIndexGetter();
controller.push(CHANGES);
controller.disposeAdapter();

expect(getController).not.toHaveBeenCalled();
Expand Down Expand Up @@ -490,4 +500,63 @@ describe('DataSourceController', () => {
expect(getController).not.toHaveBeenCalled();
});
});

describe('the pushed callback', () => {
it('re-fires what the adapter pushed', () => {
const { controller, adapter } = withAdapter();
const handler = jest.fn<(changes: StoreChange[]) => void>();

controller.pushed.add(handler);
adapter.pushed.fire(CHANGES);

expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(CHANGES);
});

it('goes quiet once the adapter is disposed', () => {
const { controller, adapter } = withAdapter();
const handler = jest.fn<(changes: StoreChange[]) => void>();

controller.pushed.add(handler);
controller.disposeAdapter();
adapter.pushed.fire(CHANGES);

expect(handler).not.toHaveBeenCalled();
});

it('follows the adapter that replaced the previous one', () => {
const { controller, provider } = withAdapter();
const second = createAdapterStub('second');
const handler = jest.fn<(changes: StoreChange[]) => void>();

provider.nextAdapter = second;
controller.createAdapter(SOURCE);
controller.pushed.add(handler);
second.pushed.fire(CHANGES);

expect(handler).toHaveBeenCalledTimes(1);
});
});

describe('push', () => {
it('delegates to the adapter, defaulting fromStore to false', () => {
const { controller, adapter } = withAdapter();

controller.push(CHANGES);

expect(adapter.push).toHaveBeenCalledWith(CHANGES, false);
});

it('passes fromStore through', () => {
const { controller, adapter } = withAdapter();

controller.push(CHANGES, true);

expect(adapter.push).toHaveBeenCalledWith(CHANGES, true);
});

it('does nothing when there is no adapter', () => {
expect(() => createController().push(CHANGES)).not.toThrow();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DataSource as DataSourceClass } from '@js/common/data/data_source/data_source';
import { normalizeDataSourceOptions } from '@js/common/data/data_source/utils';
import type { Callback } from '@js/core/utils/callbacks';
import { extend } from '@js/core/utils/extend';
import type { StoreChange } from '@js/data/store';
import type Store from '@ts/data/abstract_store';
import type { StoreKey } from '@ts/data/abstract_store';
import type { DataSource } from '@ts/data/data_source/data_source';
Expand All @@ -20,13 +22,21 @@ export class DataSourceController<

private isShared = false;

public pushed!: Callback<[StoreChange[]]>;

private readonly dataPushedHandlerProxy = this.dataPushedHandler.bind(this);

/**
* @extended: DataGrid's and TreeList's data_source_controller
*/
protected getAdapterProvider(): DataSourceAdapterProvider<TAdapter> {
throw new Error('Method not implemented.');
}

protected callbackNames(): string[] {
return ['pushed'];
}

public publicMethods(): string[] {
return ['getDataSource', 'keyOf'];
}
Expand Down Expand Up @@ -77,8 +87,11 @@ export class DataSourceController<
const adapter = this.getAdapterProvider().create(this.component);

adapter.init(dataSource);

this.adapter = adapter;

adapter.pushed.add(this.dataPushedHandlerProxy);

return adapter;
}

Expand All @@ -91,14 +104,26 @@ export class DataSourceController<
}

public disposeAdapter(): void {
this.adapter?.pushed.remove(this.dataPushedHandlerProxy);
this.adapter?.dispose(this.isShared);
this.adapter = null;
}

/**
* @extended: focus
*/
protected dataPushedHandler(changes: StoreChange[]): void {
this.pushed.fire(changes);
}

public store(): Store | undefined {
return this.adapter?.store();
}

public push(changes: StoreChange[], fromStore = false): void {
this.adapter?.push(changes, fromStore);
}

/**
* The key the component identifies rows by. Not interchangeable with `store()?.key()` (TreeList)
* Callers that need what the store itself can identify a row by have to ask the store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ class EditingControllerImpl extends modules.ViewController {
const isFullRefresh = refreshMode !== 'reshape' && refreshMode !== 'repaint';

if (!isFullRefresh) {
dataController.push(dataChanges);
this.dataSourceController.push(dataChanges);
}

when(dataController.refresh({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
afterEach, beforeEach, describe, expect, it,
} from '@jest/globals';
import {
afterTest, beforeTest, createDataGrid, flushAsync,
} from '@ts/grids/grid_core/__tests__/__mock__/helpers/utils';

describe('Focus data source controller', () => {
beforeEach(beforeTest);
afterEach(afterTest);

describe("when a push empties the grid and a pushed subscriber's own change cycle runs first", () => {
it('still resets the focused row', async () => {
const { instance } = await createDataGrid({
dataSource: {
store: {
type: 'array',
key: 'id',
data: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
},
reshapeOnPush: true,
pushAggregationTimeout: 0,
},
focusedRowEnabled: true,
focusedRowKey: 1,
selection: { mode: 'multiple' },
selectedRowKeys: [1],
columns: ['id', 'name'],
});

instance.getDataSource().store().push([
{ type: 'remove', key: 1 },
{ type: 'remove', key: 2 },
]);
await flushAsync();

expect(instance.getVisibleRows()).toHaveLength(0);
expect(instance.getSelectedRowKeys()).toEqual([]);
expect(instance.option('focusedRowKey')).toBeNull();
expect(instance.option('focusedRowIndex')).toBe(-1);
});
});

describe('when an unrelated change empties the grid after an earlier push was handled', () => {
it('keeps the focused row, because one push forces exactly one update', async () => {
const { instance } = await createDataGrid({
dataSource: {
store: {
type: 'array',
key: 'id',
data: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
],
},
reshapeOnPush: true,
pushAggregationTimeout: 0,
},
focusedRowEnabled: true,
focusedRowKey: 2,
columns: ['id', 'name'],
});

instance.getDataSource().store().push([
{ type: 'update', key: 3, data: { id: 3, name: 'Item 3 renamed' } },
]);
await flushAsync();

instance.filter(['name', '=', 'nothing matches']);
await flushAsync();

expect(instance.getVisibleRows()).toHaveLength(0);
expect(instance.option('focusedRowKey')).toBe(2);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isDefined } from '@js/core/utils/type';
import type { StoreChange } from '@js/data/store';
import type { DataSourceController } from '@ts/grids/grid_core/data_source/data_source_controller';
import type { ModuleType } from '@ts/grids/grid_core/m_types';

export interface FocusDataSourceControllerExtension {
consumeDataPushed: () => boolean;
}

export const focusDataSourceControllerExtender = (
Base: ModuleType<DataSourceController>,
): ModuleType<
DataSourceController & FocusDataSourceControllerExtension
> => class FocusDataSourceControllerExtender extends Base {
private isDataPushed = false;

public consumeDataPushed(): boolean {
const wasDataPushed = this.isDataPushed;

this.isDataPushed = false;

return wasDataPushed;
}

protected dataPushedHandler(changes: StoreChange[]): void {
super.dataPushedHandler(changes);

const focusedRowKey = this.option('focusedRowKey');

this.isDataPushed = isDefined(focusedRowKey) && !!changes.length;
}
};
Loading
Loading