@difizen/libro-widget 0.1.33 → 0.1.34

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.
Files changed (119) hide show
  1. package/es/index.d.ts +3 -4
  2. package/es/index.d.ts.map +1 -1
  3. package/es/index.js +3 -4
  4. package/es/module.d.ts +3 -0
  5. package/es/module.d.ts.map +1 -0
  6. package/es/module.js +5 -0
  7. package/es/slider/contribution.d.ts +10 -0
  8. package/es/slider/contribution.d.ts.map +1 -0
  9. package/es/{widgets/instances-progress-widget-view-contribution.js → slider/contribution.js} +15 -9
  10. package/es/slider/index.d.ts +3 -0
  11. package/es/slider/index.d.ts.map +1 -0
  12. package/es/slider/index.js +2 -0
  13. package/es/slider/index.less +3 -0
  14. package/es/slider/view.d.ts +23 -0
  15. package/es/slider/view.d.ts.map +1 -0
  16. package/es/{widgets/progress-widget-view.js → slider/view.js} +62 -68
  17. package/es/text/contribution.d.ts +10 -0
  18. package/es/text/contribution.d.ts.map +1 -0
  19. package/es/{widgets/text-widget-view-contribution.js → text/contribution.js} +3 -3
  20. package/es/text/index.d.ts +3 -0
  21. package/es/text/index.d.ts.map +1 -0
  22. package/es/text/index.js +2 -0
  23. package/es/{widgets/text-widget-view.d.ts → text/view.d.ts} +5 -6
  24. package/es/text/view.d.ts.map +1 -0
  25. package/es/{widgets/text-widget-view.js → text/view.js} +11 -12
  26. package/package.json +6 -5
  27. package/src/index.spec.ts +2 -3
  28. package/src/index.ts +3 -4
  29. package/src/module.ts +15 -0
  30. package/src/slider/contribution.ts +25 -0
  31. package/src/slider/index.less +3 -0
  32. package/src/slider/index.ts +2 -0
  33. package/src/slider/view.tsx +92 -0
  34. package/src/{widgets/text-widget-view-contribution.ts → text/contribution.ts} +4 -5
  35. package/src/text/index.ts +2 -0
  36. package/src/{widgets/text-widget-view.tsx → text/view.tsx} +7 -10
  37. package/es/base/comm.d.ts +0 -65
  38. package/es/base/comm.d.ts.map +0 -1
  39. package/es/base/comm.js +0 -151
  40. package/es/base/index.d.ts +0 -7
  41. package/es/base/index.d.ts.map +0 -1
  42. package/es/base/index.js +0 -6
  43. package/es/base/libro-widgets.d.ts +0 -79
  44. package/es/base/libro-widgets.d.ts.map +0 -1
  45. package/es/base/libro-widgets.js +0 -286
  46. package/es/base/protocal.d.ts +0 -171
  47. package/es/base/protocal.d.ts.map +0 -1
  48. package/es/base/protocal.js +0 -21
  49. package/es/base/utils.d.ts +0 -27
  50. package/es/base/utils.d.ts.map +0 -1
  51. package/es/base/utils.js +0 -59
  52. package/es/base/version.d.ts +0 -3
  53. package/es/base/version.d.ts.map +0 -1
  54. package/es/base/version.js +0 -2
  55. package/es/base/widget-manager.d.ts +0 -18
  56. package/es/base/widget-manager.d.ts.map +0 -1
  57. package/es/base/widget-manager.js +0 -68
  58. package/es/base/widget-view-contribution.d.ts +0 -10
  59. package/es/base/widget-view-contribution.d.ts.map +0 -1
  60. package/es/base/widget-view-contribution.js +0 -36
  61. package/es/base/widget-view.d.ts +0 -52
  62. package/es/base/widget-view.d.ts.map +0 -1
  63. package/es/base/widget-view.js +0 -137
  64. package/es/components/index.d.ts +0 -3
  65. package/es/components/index.d.ts.map +0 -1
  66. package/es/components/index.js +0 -2
  67. package/es/components/progressBar.d.ts +0 -15
  68. package/es/components/progressBar.d.ts.map +0 -1
  69. package/es/components/progressBar.js +0 -20
  70. package/es/components/progressCircle.d.ts +0 -11
  71. package/es/components/progressCircle.d.ts.map +0 -1
  72. package/es/components/progressCircle.js +0 -16
  73. package/es/widget-module.d.ts +0 -3
  74. package/es/widget-module.d.ts.map +0 -1
  75. package/es/widget-module.js +0 -39
  76. package/es/widgets/hbox-widget-view-contribution.d.ts +0 -10
  77. package/es/widgets/hbox-widget-view-contribution.d.ts.map +0 -1
  78. package/es/widgets/hbox-widget-view-contribution.js +0 -39
  79. package/es/widgets/hbox-widget-view.d.ts +0 -17
  80. package/es/widgets/hbox-widget-view.d.ts.map +0 -1
  81. package/es/widgets/hbox-widget-view.js +0 -69
  82. package/es/widgets/index.d.ts +0 -9
  83. package/es/widgets/index.d.ts.map +0 -1
  84. package/es/widgets/index.js +0 -8
  85. package/es/widgets/index.less +0 -3
  86. package/es/widgets/instances-progress-widget-view-contribution.d.ts +0 -10
  87. package/es/widgets/instances-progress-widget-view-contribution.d.ts.map +0 -1
  88. package/es/widgets/instances-progress-widget-view.d.ts +0 -31
  89. package/es/widgets/instances-progress-widget-view.d.ts.map +0 -1
  90. package/es/widgets/instances-progress-widget-view.js +0 -181
  91. package/es/widgets/progress-widget-view-contribution.d.ts +0 -10
  92. package/es/widgets/progress-widget-view-contribution.d.ts.map +0 -1
  93. package/es/widgets/progress-widget-view-contribution.js +0 -39
  94. package/es/widgets/progress-widget-view.d.ts +0 -25
  95. package/es/widgets/progress-widget-view.d.ts.map +0 -1
  96. package/es/widgets/text-widget-view-contribution.d.ts +0 -10
  97. package/es/widgets/text-widget-view-contribution.d.ts.map +0 -1
  98. package/es/widgets/text-widget-view.d.ts.map +0 -1
  99. package/src/base/comm.ts +0 -152
  100. package/src/base/index.ts +0 -6
  101. package/src/base/libro-widgets.ts +0 -207
  102. package/src/base/protocal.ts +0 -218
  103. package/src/base/utils.ts +0 -67
  104. package/src/base/version.ts +0 -2
  105. package/src/base/widget-manager.ts +0 -41
  106. package/src/base/widget-view-contribution.ts +0 -14
  107. package/src/base/widget-view.tsx +0 -146
  108. package/src/components/index.ts +0 -3
  109. package/src/components/progressBar.tsx +0 -29
  110. package/src/components/progressCircle.tsx +0 -18
  111. package/src/widget-module.ts +0 -68
  112. package/src/widgets/hbox-widget-view-contribution.ts +0 -20
  113. package/src/widgets/hbox-widget-view.tsx +0 -64
  114. package/src/widgets/index.less +0 -3
  115. package/src/widgets/index.ts +0 -8
  116. package/src/widgets/instances-progress-widget-view-contribution.ts +0 -20
  117. package/src/widgets/instances-progress-widget-view.tsx +0 -160
  118. package/src/widgets/progress-widget-view-contribution.ts +0 -24
  119. package/src/widgets/progress-widget-view.tsx +0 -85
@@ -1,207 +0,0 @@
1
- import type { JSONObject } from '@difizen/libro-common';
2
- import type { IKernelConnection, KernelMessage } from '@difizen/libro-kernel';
3
- import type { Contribution } from '@difizen/mana-app';
4
- import { contrib, inject, Priority, prop, transient } from '@difizen/mana-app';
5
- import { Emitter } from '@difizen/mana-app';
6
-
7
- import type { Comm } from './comm.js';
8
- import type {
9
- ISerializedState,
10
- IWidgets,
11
- IWidgetViewOptions,
12
- IClassicComm,
13
- WidgetCommOption,
14
- } from './protocal.js';
15
- import {
16
- LibroWidgetCommFactory,
17
- WidgetsOption,
18
- WidgetViewContribution,
19
- } from './protocal.js';
20
- import { put_buffers, reject } from './utils.js';
21
- import { PROTOCOL_VERSION } from './version.js';
22
- import type { WidgetView } from './widget-view.js';
23
-
24
- const PROTOCOL_MAJOR_VERSION = PROTOCOL_VERSION.split('.', 1)[0];
25
-
26
- @transient()
27
- export class LibroWidgets implements IWidgets {
28
- @contrib(WidgetViewContribution)
29
- WidgetViewProvider: Contribution.Provider<WidgetViewContribution>;
30
- widgetEmitter: Emitter<{ WidgetViewName: string }> = new Emitter();
31
- widgetCommFactory: (options: WidgetCommOption) => Comm;
32
- kernelConnection: IKernelConnection;
33
-
34
- constructor(
35
- @inject(WidgetsOption) options: WidgetsOption,
36
- @inject(LibroWidgetCommFactory)
37
- widgetCommFactory: (options: WidgetCommOption) => Comm,
38
- ) {
39
- this.kernelConnection = options.kc;
40
- this.id = options.id;
41
- this.widgetCommFactory = widgetCommFactory;
42
- this.kernelConnection.registerCommTarget(this.commTargetName, async (comm, msg) => {
43
- const widgetComm = this.widgetCommFactory({ comm });
44
- await this.handleCommOpen(widgetComm, msg);
45
- });
46
- }
47
- get onWidgetRender() {
48
- return this.widgetEmitter.event;
49
- }
50
-
51
- protected findProvider(attributes: any): WidgetViewContribution {
52
- const prioritized = Priority.sortSync(
53
- this.WidgetViewProvider.getContributions(),
54
- (contribution) => contribution.canHandle(attributes),
55
- );
56
- const sorted = prioritized.map((c) => c.value);
57
- return sorted[0]!;
58
- }
59
-
60
- /**
61
- * Create a comm which can be used for communication for a widget.
62
- *
63
- * If the data/metadata is passed in, open the comm before returning (i.e.,
64
- * send the comm_open message). If the data and metadata is undefined, we
65
- * want to reconstruct a comm that already exists in the kernel, so do not
66
- * open the comm by sending the comm_open message.
67
- *
68
- * @param comm_target_name Comm target name
69
- * @param model_id The comm id
70
- * @param data The initial data for the comm
71
- * @param metadata The metadata in the open message
72
- */
73
- async createComm(
74
- comm_target_name: string,
75
- model_id?: string,
76
- data?: JSONObject,
77
- metadata?: JSONObject,
78
- buffers?: ArrayBuffer[] | ArrayBufferView[],
79
- ): Promise<IClassicComm> {
80
- const kernel = this.kernelConnection;
81
- if (!kernel) {
82
- throw new Error('No current kernel');
83
- }
84
- const comm = kernel.createComm(comm_target_name, model_id);
85
- if (data || metadata) {
86
- comm.open(data, metadata, buffers);
87
- }
88
- return this.widgetCommFactory({ comm });
89
- }
90
-
91
- /**
92
- * Get a model by model id.
93
- *
94
- * #### Notes
95
- * If the model is not found, throw error.
96
- *
97
- * If you would like to synchronously test if a model exists, use .hasModel().
98
- */
99
- getModel(model_id: string): WidgetView {
100
- const model = this.models.get(model_id);
101
- if (model === undefined) {
102
- throw new Error('widget model not found');
103
- }
104
- return model;
105
- }
106
-
107
- /**
108
- * Returns true if the given model is registered, otherwise false.
109
- *
110
- * #### Notes
111
- * This is a synchronous way to check if a model is registered.
112
- */
113
- hasModel(model_id: string): boolean {
114
- return this.models.get(model_id) !== undefined;
115
- }
116
-
117
- /**
118
- * Handle when a comm is opened.
119
- */
120
- async handleCommOpen(
121
- comm: IClassicComm,
122
- msg: KernelMessage.ICommOpenMsg,
123
- ): Promise<WidgetView> {
124
- const protocolVersion = ((msg.metadata || {})['version'] as string) || '';
125
- if (protocolVersion.split('.', 1)[0] !== PROTOCOL_MAJOR_VERSION) {
126
- const error = `Wrong widget protocol version: received protocol version '${protocolVersion}', but was expecting major version '${PROTOCOL_MAJOR_VERSION}'`;
127
- console.error(error);
128
- return Promise.reject(error);
129
- }
130
- const data = msg.content.data as unknown as ISerializedState;
131
- const buffer_paths = data.buffer_paths || [];
132
- const buffers = msg.buffers || [];
133
- put_buffers(data.state, buffer_paths, buffers);
134
- // this.createComm(msg.content.target_name, msg.content.comm_id, msg.content.data, msg.metadata);
135
- return this.newWidgetView(data.state, {
136
- model_id: msg.content.comm_id,
137
- comm,
138
- }).catch(reject('Could not create a model.', true));
139
- }
140
-
141
- registerWidgetView(model_id: string, model: Promise<WidgetView>): void {
142
- model
143
- .then((WidgetView) => {
144
- this.models.set(model_id, WidgetView);
145
- this.widgetEmitter.fire({ WidgetViewName: WidgetView.model_name });
146
- return;
147
- })
148
- .catch(() => {
149
- //
150
- });
151
- }
152
-
153
- handleCommClose(msg: KernelMessage.ICommCloseMsg) {
154
- const comm_id = msg.content.comm_id;
155
- const model = this.getModel(comm_id);
156
- model.isCommClosed = true;
157
- }
158
-
159
- unregisterWidgetView(model_id: string): void {
160
- const model = this.models.get(model_id);
161
- model?.dispose();
162
- this.models.delete(model_id);
163
- }
164
-
165
- async newWidgetView(
166
- attributes: any,
167
- options: IWidgetViewOptions,
168
- ): Promise<WidgetView> {
169
- const model_id = options.model_id;
170
- if (!model_id) {
171
- throw new Error(
172
- 'Neither comm nor model_id provided in options object. At least one must exist.',
173
- );
174
- }
175
- options.model_id = model_id;
176
- const provider = this.findProvider(attributes);
177
- const WidgetView = provider.factory({ attributes: attributes, options: options });
178
- this.registerWidgetView(model_id, WidgetView);
179
- return WidgetView;
180
- }
181
-
182
- /**
183
- * Close all widgets and empty the widget state.
184
- * @return Promise that resolves when the widget state is cleared.
185
- */
186
- clearState() {
187
- this.models.clear();
188
- }
189
- /**
190
- * Disconnect the widget manager from the kernel, setting each model's comm
191
- * as dead.
192
- */
193
- disconnect(): void {
194
- // this.models.forEach(model => model.clear());
195
- }
196
- /**
197
- * Dictionary of model ids and model instance promises
198
- */
199
- @prop()
200
- protected models: Map<string, WidgetView> = new Map();
201
-
202
- /**
203
- * The comm target name to register
204
- */
205
- id: string;
206
- readonly commTargetName = 'jupyter.widget';
207
- }
@@ -1,218 +0,0 @@
1
- import type { JSONObject, JSONValue } from '@difizen/libro-common';
2
- import type { IComm, IKernelConnection, KernelMessage } from '@difizen/libro-kernel';
3
- import { Syringe } from '@difizen/mana-app';
4
-
5
- import type { WidgetView } from './widget-view.js';
6
-
7
- export interface IWidgetViewOptions {
8
- model_id: string;
9
- comm: IClassicComm;
10
- }
11
-
12
- export interface IWidgetViewProps {
13
- attributes: any;
14
- options: IWidgetViewOptions;
15
- }
16
-
17
- /**
18
- * A simple dictionary type.
19
- */
20
- export type Dict<T> = Record<string, T>;
21
-
22
- export type BufferJSON =
23
- | { [property: string]: BufferJSON }
24
- | BufferJSON[]
25
- | string
26
- | number
27
- | boolean
28
- | null
29
- | ArrayBuffer
30
- | DataView;
31
-
32
- export interface ISerializedState {
33
- state: JSONObject;
34
- buffers: ArrayBuffer[];
35
- buffer_paths: (string | number)[][];
36
- }
37
-
38
- /**
39
- * The widget manager interface exposed on the Widget instances
40
- */
41
- export interface IWidgets {
42
- getModel: (model_id: string) => WidgetView;
43
-
44
- /**
45
- * Returns true if the given model is registered, otherwise false.
46
- *
47
- * #### Notes
48
- * This is a synchronous way to check if a model is registered.
49
- */
50
- hasModel: (model_id: string) => boolean;
51
-
52
- /**
53
- * Register a model instance promise with the manager.
54
- *
55
- * By registering the model, it can later be retrieved with `getModel`.
56
- */
57
- registerWidgetView: (model_id: string, model: Promise<WidgetView>) => void;
58
-
59
- newWidgetView: (attributes: any, options: IWidgetViewOptions) => Promise<WidgetView>;
60
- }
61
-
62
- export const LibroWidgetsFactory = Symbol('LibroWidgetsFactory');
63
- export type LibroWidgetsFactory = (options: WidgetsOption) => IWidgets;
64
- export const WidgetsOption = Symbol('WidgetsOption');
65
- export interface WidgetsOption {
66
- kc: IKernelConnection;
67
- id: string;
68
- }
69
-
70
- export interface IWidgetView {
71
- initialize: (props: IWidgetViewProps) => void;
72
- toJSON: () => string;
73
- set_state: (state: Dict<unknown>) => void;
74
- handleCommMsg: (msg: KernelMessage.ICommMsgMsg) => Promise<void>;
75
- model_id: string;
76
- name: string;
77
- module: string;
78
-
79
- model_module: string;
80
- model_name: string;
81
- model_module_version: string;
82
- view_module: string;
83
- view_name: string | null;
84
- view_module_version: string;
85
- view_count: number | null;
86
- }
87
-
88
- export const IWidgetView = Symbol('IWidgetView');
89
- export const WidgetViewContribution = Syringe.defineToken('WidgetViewContribution');
90
- export interface WidgetViewContribution {
91
- canHandle: (attributes: any) => number;
92
- factory: (props: IWidgetViewProps) => Promise<WidgetView>;
93
- }
94
-
95
- export interface InstanceRecord {
96
- startDate: number;
97
- endDate: number;
98
- }
99
-
100
- interface Stage {
101
- name: string;
102
- backup_workers: number;
103
- terminated_workers: number;
104
- running_workers: number;
105
- total_workers: number;
106
- input_records: number;
107
- output_records: number;
108
- finished_percentage: number;
109
- }
110
-
111
- export interface Task {
112
- name: string;
113
- status: 'WAITING' | 'RUNNING' | 'SUCCESS' | 'FAILED' | 'SUSPENDED' | 'CANCELLED';
114
- stages: Stage[];
115
- }
116
-
117
- export interface ProgressInstance {
118
- id: string;
119
- status: 'Running' | 'Suspended' | 'Terminated';
120
- logview: string;
121
- tasks: Task[];
122
- }
123
-
124
- export interface ProgressItem {
125
- name: string;
126
- key: string;
127
- gen_time: string;
128
- logView: string;
129
- instances?: ProgressInstance[];
130
- }
131
-
132
- export type InstancesRecords = Record<string, InstanceRecord>;
133
-
134
- /**
135
- * Callbacks for services shim comms.
136
- */
137
- export interface ICallbacks {
138
- shell?: Record<string, (msg: KernelMessage.IShellMessage) => void>;
139
- iopub?: Record<string, (msg: KernelMessage.IIOPubMessage) => void>;
140
- input?: (msg: KernelMessage.IStdinMessage) => void;
141
- }
142
-
143
- export const LibroWidgetCommFactory = Symbol('LibroWidgetCommFactory');
144
- export type LibroWidgetCommFactory = (options: WidgetCommOption) => IClassicComm;
145
- export const WidgetCommOption = Symbol('WidgetCommOption');
146
- export interface WidgetCommOption {
147
- comm: IComm;
148
- }
149
- export interface IClassicComm {
150
- /**
151
- * Comm id
152
- * @return {string}
153
- */
154
- comm_id: string;
155
-
156
- /**
157
- * Target name
158
- * @return {string}
159
- */
160
- target_name: string;
161
-
162
- /**
163
- * Opens a sibling comm in the backend
164
- * @param data
165
- * @param callbacks
166
- * @param metadata
167
- * @param buffers
168
- * @return msg id
169
- */
170
- open(
171
- data: JSONValue,
172
- callbacks?: ICallbacks,
173
- metadata?: JSONObject,
174
- buffers?: ArrayBuffer[] | ArrayBufferView[],
175
- ): string;
176
-
177
- /**
178
- * Sends a message to the sibling comm in the backend
179
- * @param data
180
- * @param callbacks
181
- * @param metadata
182
- * @param buffers
183
- * @return message id
184
- */
185
- send(
186
- data: JSONValue,
187
- callbacks?: ICallbacks,
188
- metadata?: JSONObject,
189
- buffers?: ArrayBuffer[] | ArrayBufferView[],
190
- ): string;
191
-
192
- /**
193
- * Closes the sibling comm in the backend
194
- * @param data
195
- * @param callbacks
196
- * @param metadata
197
- * @param buffers
198
- * @return msg id
199
- */
200
- close(
201
- data?: JSONValue,
202
- callbacks?: ICallbacks,
203
- metadata?: JSONObject,
204
- buffers?: ArrayBuffer[] | ArrayBufferView[],
205
- ): string;
206
-
207
- /**
208
- * Register a message handler
209
- * @param callback, which is given a message
210
- */
211
- on_msg(callback: (x: any) => void): void;
212
-
213
- /**
214
- * Register a handler for when the comm is closed by the backend
215
- * @param callback, which is given a message
216
- */
217
- on_close(callback: (x: any) => void): void;
218
- }
package/src/base/utils.ts DELETED
@@ -1,67 +0,0 @@
1
- import type { BufferJSON, Dict } from './protocal.js';
2
-
3
- /**
4
- * Takes an object 'state' and fills in buffer[i] at 'path' buffer_paths[i]
5
- * where buffer_paths[i] is a list indicating where in the object buffer[i] should
6
- * be placed
7
- * Example: state = {a: 1, b: {}, c: [0, null]}
8
- * buffers = [array1, array2]
9
- * buffer_paths = [['b', 'data'], ['c', 1]]
10
- * Will lead to {a: 1, b: {data: array1}, c: [0, array2]}
11
- */
12
- export function put_buffers(
13
- state: Dict<BufferJSON>,
14
- buffer_paths: (string | number)[][],
15
- buffers: (DataView | ArrayBuffer | ArrayBufferView | { buffer: ArrayBuffer })[],
16
- ): void {
17
- for (let i = 0; i < buffer_paths.length; i++) {
18
- const buffer_path = buffer_paths[i];
19
- // make sure the buffers are DataViews
20
- let buffer = buffers[i];
21
- if (!(buffer instanceof DataView)) {
22
- buffer = new DataView(buffer instanceof ArrayBuffer ? buffer : buffer.buffer);
23
- }
24
- // say we want to set state[x][y][z] = buffer
25
- let obj = state as any;
26
- // we first get obj = state[x][y]
27
- for (let j = 0; j < buffer_path.length - 1; j++) {
28
- obj = obj[buffer_path[j]];
29
- }
30
- // and then set: obj[z] = buffer
31
- obj[buffer_path[buffer_path.length - 1]] = buffer;
32
- }
33
- }
34
-
35
- /**
36
- * Creates a wrappable Promise rejection function.
37
- *
38
- * Creates a function that logs an error message before rethrowing
39
- * the original error that caused the promise to reject.
40
- */
41
- export function reject(message: string, log: boolean) {
42
- return function promiseRejection(error: Error): never {
43
- if (log) {
44
- console.error(new Error(message));
45
- }
46
- throw error;
47
- };
48
- }
49
-
50
- /**
51
- * A polyfill for Object.assign
52
- *
53
- * This is from code that Typescript 2.4 generates for a polyfill.
54
- */
55
- export const assign =
56
- (Object as any).assign ||
57
- function (t: any, ...args: any[]): any {
58
- for (let i = 1; i < args.length; i++) {
59
- const s = args[i];
60
- for (const p in s) {
61
- if (Object.prototype.hasOwnProperty.call(s, p)) {
62
- t[p] = s[p];
63
- }
64
- }
65
- }
66
- return t;
67
- };
@@ -1,2 +0,0 @@
1
- export const LIBRO_WIDGETS_VERSION = '2.0.0';
2
- export const PROTOCOL_VERSION = '2.1.0';
@@ -1,41 +0,0 @@
1
- import { LibroService } from '@difizen/libro-core';
2
- import type { IKernelConnection } from '@difizen/libro-kernel';
3
- import { KernelConnection, LibroKernelManager } from '@difizen/libro-kernel';
4
- import { inject, prop, singleton, ApplicationContribution } from '@difizen/mana-app';
5
-
6
- import type { LibroWidgets } from './libro-widgets.js';
7
- import { LibroWidgetsFactory } from './protocal.js';
8
- import type { WidgetsOption } from './protocal.js';
9
-
10
- @singleton({ contrib: ApplicationContribution })
11
- export class LibroWidgetManager implements ApplicationContribution {
12
- @inject(LibroWidgetsFactory) widgetsFactory!: (
13
- options: WidgetsOption,
14
- ) => LibroWidgets;
15
- @inject(LibroKernelManager) kernelManager: LibroKernelManager;
16
- @inject(LibroService) libroService: LibroService;
17
-
18
- initialize = () => {
19
- this.kernelManager.onConnectToKernel((kc) => {
20
- if (kc instanceof KernelConnection) {
21
- this.getOrCreateWidgets(kc);
22
- }
23
- });
24
- };
25
-
26
- getOrCreateWidgets = (kc: IKernelConnection) => {
27
- const widgets = this.widgets.get(kc.id);
28
- if (widgets) {
29
- return widgets;
30
- }
31
- const newWidgets = this.widgetsFactory({ kc, id: kc.id });
32
- this.widgets.set(kc.id, newWidgets);
33
- return newWidgets;
34
- };
35
-
36
- /**
37
- * Dictionary of model ids and model instance promises
38
- */
39
- @prop()
40
- protected widgets: Map<string, LibroWidgets> = new Map();
41
- }
@@ -1,14 +0,0 @@
1
- import { ViewManager, inject, singleton } from '@difizen/mana-app';
2
-
3
- import type { IWidgetViewProps } from './protocal.js';
4
- import { WidgetViewContribution } from './protocal.js';
5
- import { WidgetView } from './widget-view.js';
6
-
7
- @singleton({ contrib: WidgetViewContribution })
8
- export class DefaultWidgetViewContribution implements WidgetViewContribution {
9
- @inject(ViewManager) viewManager: ViewManager;
10
- canHandle = () => 1;
11
- factory(props: IWidgetViewProps) {
12
- return this.viewManager.getOrCreateView(WidgetView, props);
13
- }
14
- }
@@ -1,146 +0,0 @@
1
- import type { JSONValue } from '@difizen/libro-common';
2
- import { LibroContextKey } from '@difizen/libro-core';
3
- import type { KernelMessage } from '@difizen/libro-kernel';
4
- import { inject, transient, ViewOption, view, BaseView } from '@difizen/mana-app';
5
- import type { ViewComponent } from '@difizen/mana-app';
6
- import { forwardRef } from 'react';
7
-
8
- import type { IWidgetViewProps } from './protocal.js';
9
- import type {
10
- BufferJSON,
11
- Dict,
12
- IWidgets,
13
- IWidgetView,
14
- IClassicComm,
15
- ICallbacks,
16
- } from './protocal.js';
17
- import { assign } from './utils.js';
18
-
19
- export const LibroWidgetComponent = forwardRef<HTMLDivElement>(
20
- function LibroWidgetComponent() {
21
- return <></>;
22
- },
23
- );
24
-
25
- @transient()
26
- @view('libro-widget-view')
27
- export class WidgetView extends BaseView implements IWidgetView {
28
- override view: ViewComponent = LibroWidgetComponent;
29
- libroContextKey: LibroContextKey;
30
- disableCommandMode = true;
31
- constructor(
32
- @inject(ViewOption) props: IWidgetViewProps,
33
- @inject(LibroContextKey) libroContextKey: LibroContextKey,
34
- ) {
35
- super();
36
- this.initialize(props);
37
- this.libroContextKey = libroContextKey;
38
- }
39
-
40
- override onViewMount() {
41
- if (this.container && this.container.current && this.disableCommandMode) {
42
- this.container.current.addEventListener('focusin', () => {
43
- this.libroContextKey.disableCommandMode();
44
- });
45
- this.container.current.addEventListener('blur', (e) => {
46
- if (this.container?.current?.contains(e.relatedTarget as Node)) {
47
- this.libroContextKey.disableCommandMode();
48
- } else {
49
- this.libroContextKey.enableCommandMode();
50
- }
51
- });
52
- }
53
- }
54
-
55
- initialize(props: IWidgetViewProps): void {
56
- this.model_module = props.attributes._model_module;
57
- this.model_name = props.attributes._model_name;
58
- this.model_module_version = props.attributes._model_module_version;
59
- this.view_module = props.attributes._view_module;
60
- this.view_name = props.attributes._view_name;
61
- this.view_module_version = props.attributes._view_module_version;
62
- this.view_count = props.attributes._view_count;
63
-
64
- // Attributes should be initialized here, since user initialization may depend on it
65
- // this.libroWidgets = props.options.libroWidgets;
66
- const comm = props.options.comm;
67
- if (comm) {
68
- // Remember comm associated with the model.
69
- this.comm = comm;
70
-
71
- // Hook comm messages up to model.
72
- comm.on_close(this.handleCommClosed.bind(this));
73
- comm.on_msg(this.handleCommMsg.bind(this));
74
- } else {
75
- this.isCommClosed = false;
76
- }
77
- this.model_id = props.options.model_id;
78
-
79
- this.state_change = Promise.resolve();
80
- }
81
-
82
- /**
83
- * Handle incoming comm msg.
84
- */
85
- handleCommMsg(msg: KernelMessage.ICommMsgMsg): Promise<void> {
86
- const data = msg.content.data as any;
87
- const method = data.method;
88
- switch (method) {
89
- case 'update':
90
- case 'echo_update':
91
- // eslint-disable-next-line no-case-declarations
92
- const state: Dict<BufferJSON> = data.state;
93
- this.set_state(state);
94
- }
95
- return Promise.resolve();
96
- }
97
-
98
- handleCommClosed = () => {
99
- this.isCommClosed = true;
100
- };
101
- /**
102
- * Handle when a widget is updated from the backend.
103
- *
104
- * This function is meant for internal use only. Values set here will not be propagated on a sync.
105
- */
106
- set_state(state: Dict<unknown>): void {
107
- assign(this, state);
108
- }
109
-
110
- /**
111
- * Serialize the model. See the deserialization function at the top of this file
112
- * and the kernel-side serializer/deserializer.
113
- */
114
- toJSON(): string {
115
- return `IPY_MODEL_${this.model_id}`;
116
- }
117
-
118
- /**
119
- * Send a custom msg over the comm.
120
- */
121
- send = (
122
- data: JSONValue,
123
- callbacks?: ICallbacks,
124
- buffers?: ArrayBuffer[] | ArrayBufferView[],
125
- ) => {
126
- if (this.comm !== undefined) {
127
- this.comm.send(data, callbacks, {}, buffers);
128
- }
129
- };
130
-
131
- comm: IClassicComm;
132
- libroWidgets: IWidgets;
133
- model_id: string;
134
- state_change: Promise<any>;
135
- name: string;
136
- module: string;
137
- isCommClosed = false;
138
-
139
- model_module: string;
140
- model_name: string;
141
- model_module_version: string;
142
- view_module: string;
143
- view_name: string | null;
144
- view_module_version: string;
145
- view_count: number | null;
146
- }
@@ -1,3 +0,0 @@
1
- export * from './progressBar.js';
2
-
3
- export * from './progressCircle.js';