Debian Codegraph @

js/streamingfeathergrid.ts

raw
1 import { DataGrid } from '@lumino/datagrid';
2 import { Debouncer } from '@lumino/polling';
3 import { IMessageHandler, Message } from '@lumino/messaging';
4 import { FeatherGrid } from './feathergrid';
5
6 export class StreamingFeatherGrid extends FeatherGrid {
7 constructor(options: StreamingFeatherGrid.IOptions) {
8 super(options);
9
10 this._requestData = options.requestData;
11
12 this._pullData = new Debouncer(
13 this.pullDataImpl.bind(this),
14 options.debounceDelay,
15 );
16 }
17
18 messageHook(handler: IMessageHandler, msg: Message): boolean {
19 if (handler === this.grid.viewport) {
20 if (
21 msg.type === 'scroll-request' ||
22 msg.type === 'resize' ||
23 msg.type === 'row-resize-request' ||
24 msg.type === 'column-resize-request'
25 ) {
26 this.tick();
27 }
28 }
29
30 return true;
31 }
32
33 tick() {
34 this._pullData.invoke();
35 }
36
37 private pullDataImpl() {
38 let width = this.grid.viewport.node.offsetWidth;
39 let height = this.grid.viewport.node.offsetHeight;
40
41 width = Math.round(width);
42 height = Math.round(height);
43
44 if (width <= 0 || height <= 0) {
45 return;
46 }
47
48 // TODO Remove all this low-level code once https://github.com/jupyterlab/lumino/pull/695 is merged and available
49 // CAUTION WHEN REMOVING, we need an escape path for older Lumino as we don't control its version. Full removal
50 // can only happen when a backward incompatible release is made.
51 // @ts-ignore
52 const contentW = this.grid._columnSections.length - this.grid.scrollX;
53 // @ts-ignore
54 const contentH = this.grid._rowSections.length - this.grid.scrollY;
55
56 const contentX = this.grid.headerWidth;
57 const contentY = this.grid.headerHeight;
58
59 const x1 = contentX;
60 const y1 = contentY;
61 const x2 = Math.min(width - 1, contentX + contentW - 1);
62 const y2 = Math.min(height - 1, contentY + contentH - 1);
63
64 // @ts-ignore
65 const r1 = this.grid._rowSections.indexOf(
66 y1 - contentY + this.grid.scrollY,
67 );
68 // @ts-ignore
69 const c1 = this.grid._columnSections.indexOf(
70 x1 - contentX + this.grid.scrollX,
71 );
72 // @ts-ignore
73 const r2 = this.grid._rowSections.indexOf(
74 y2 - contentY + this.grid.scrollY,
75 );
76 // @ts-ignore
77 const c2 = this.grid._columnSections.indexOf(
78 x2 - contentX + this.grid.scrollX,
79 );
80
81 this._requestData(r1, r2, c1, c2);
82 }
83
84 private _requestData: (
85 r1: number,
86 r2: number,
87 c1: number,
88 c2: number,
89 ) => void;
90 private _pullData: Debouncer;
91 }
92
93 export namespace StreamingFeatherGrid {
94 export interface IOptions extends DataGrid.IOptions {
95 /**
96 * The function for requesting data to the back-end.
97 */
98 requestData: (r1: number, r2: number, c1: number, c2: number) => void;
99
100 /**
101 * Delay for debouncing data requests.
102 */
103 debounceDelay: number;
104 }
105 }