@nullplatform/ui-plugin-sdk 0.6.0 → 0.7.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/dist/bridge.d.ts +3 -0
- package/dist/bridge.js +6 -0
- package/dist/catalog.d.ts +9 -1
- package/dist/catalog.js +1 -1
- package/dist/components.d.ts +1 -1
- package/dist/components.generated.d.ts +269 -0
- package/dist/components.generated.js +31 -1
- package/dist/components.js +1 -1
- package/dist/context.d.ts +1 -0
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +1 -0
- package/dist/contract/manifest.d.ts +2 -0
- package/dist/contract/manifest.js +43 -0
- package/dist/contract/topics.d.ts +40 -0
- package/dist/contract/topics.js +128 -0
- package/dist/contract-cjs/index.js +12 -1
- package/dist/contract-cjs/manifest.js +43 -0
- package/dist/contract-cjs/topics.js +140 -0
- package/dist/contractProps.d.ts +1 -1
- package/dist/contracts.generated.d.ts +370 -0
- package/dist/contracts.generated.js +211 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/protocol.d.ts +21 -0
- package/dist/topics.d.ts +75 -0
- package/dist/topics.js +146 -0
- package/dist/worker.js +4 -0
- package/package.json +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated by scripts/sync-contracts.mjs from @nullplatform/react-ui-components@0.15.
|
|
1
|
+
// Generated by scripts/sync-contracts.mjs from @nullplatform/react-ui-components@0.15.9. Don't edit: change the contract in
|
|
2
2
|
// react-ui-components, bump `uiComponentsContracts` in package.json and run `pnpm sync:contracts`.
|
|
3
3
|
export const UI_COMPONENTS_CONTRACTS = [
|
|
4
4
|
{
|
|
@@ -55,4 +55,214 @@ export const UI_COMPONENTS_CONTRACTS = [
|
|
|
55
55
|
values: { "variant": ["determinate", "indeterminate"], "color": ["inherit", "primary", "secondary", "success", "error", "info", "warning"] },
|
|
56
56
|
defaults: { "thickness": 3.6 },
|
|
57
57
|
},
|
|
58
|
+
{
|
|
59
|
+
tag: "np-detail-view",
|
|
60
|
+
component: "DetailView",
|
|
61
|
+
properties: { "spacing": "number", "padding": "number", "disableDividers": "boolean", "loading": "boolean", "loadingSections": "number" },
|
|
62
|
+
events: {},
|
|
63
|
+
children: true,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
tag: "np-detail-header",
|
|
67
|
+
component: "DetailHeader",
|
|
68
|
+
properties: { "title": "string" },
|
|
69
|
+
events: {},
|
|
70
|
+
slots: ["actions"],
|
|
71
|
+
children: true,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
tag: "np-detail-section",
|
|
75
|
+
component: "DetailSection",
|
|
76
|
+
properties: { "label": "string", "spacing": "number" },
|
|
77
|
+
events: {},
|
|
78
|
+
children: true,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
tag: "np-detail-columns",
|
|
82
|
+
component: "DetailColumns",
|
|
83
|
+
properties: { "spacing": "number" },
|
|
84
|
+
events: {},
|
|
85
|
+
children: true,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
tag: "np-detail-column",
|
|
89
|
+
component: "DetailColumn",
|
|
90
|
+
properties: { "minWidth": "number", "spacing": "number" },
|
|
91
|
+
events: {},
|
|
92
|
+
children: true,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
tag: "np-detail-row",
|
|
96
|
+
component: "DetailRow",
|
|
97
|
+
properties: { "label": "string", "labelMinWidth": "number", "valueColor": "string", "labelColor": "string", "hideColon": "boolean" },
|
|
98
|
+
events: {},
|
|
99
|
+
slots: ["icon"],
|
|
100
|
+
cssValues: ["valueColor", "labelColor"],
|
|
101
|
+
children: true,
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
tag: "np-filter-dropdown",
|
|
105
|
+
component: "FilterDropdown",
|
|
106
|
+
properties: { "label": "string", "placeholder": "string", "elements": "array", "selected": "string", "loading": "boolean", "disabled": "boolean", "disableSort": "boolean", "disableClear": "boolean" },
|
|
107
|
+
events: { "change": "onChange" },
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
tag: "np-filter-dropdown-multiple",
|
|
111
|
+
component: "FilterDropdownMultiple",
|
|
112
|
+
properties: { "label": "string", "elements": "array", "selected": "array", "loading": "boolean", "disabled": "boolean", "showActions": "boolean" },
|
|
113
|
+
events: { "change": "onChange" },
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
tag: "np-filter-name",
|
|
117
|
+
component: "FilterName",
|
|
118
|
+
properties: { "filterName": "string", "placeholder": "string", "loading": "boolean" },
|
|
119
|
+
events: { "change": "onFilterName" },
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
tag: "np-list-item-icon",
|
|
123
|
+
component: "ListItemIcon",
|
|
124
|
+
properties: {},
|
|
125
|
+
events: {},
|
|
126
|
+
children: true,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
tag: "np-list-item-avatar",
|
|
130
|
+
component: "ListItemAvatar",
|
|
131
|
+
properties: {},
|
|
132
|
+
events: {},
|
|
133
|
+
children: true,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
tag: "np-list-subheader",
|
|
137
|
+
component: "ListSubheader",
|
|
138
|
+
properties: { "color": "string", "disableGutters": "boolean", "disableSticky": "boolean", "inset": "boolean" },
|
|
139
|
+
events: {},
|
|
140
|
+
values: { "color": ["default", "primary", "inherit"] },
|
|
141
|
+
children: true,
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
tag: "np-relative-date",
|
|
145
|
+
component: "RelativeDate",
|
|
146
|
+
properties: { "date": "string", "icon": "string", "label": "string", "hideLabel": "boolean", "options": "object", "showTooltip": "boolean", "emptyText": "string", "live": "boolean" },
|
|
147
|
+
events: {},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
tag: "np-duration",
|
|
151
|
+
component: "Duration",
|
|
152
|
+
properties: { "ms": "number", "from": "string", "to": "string", "running": "boolean", "icon": "string", "label": "string", "hideLabel": "boolean", "emptyText": "string" },
|
|
153
|
+
events: {},
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
tag: "np-timeline-cell",
|
|
157
|
+
component: "TimelineCell",
|
|
158
|
+
properties: { "createdAt": "string", "updatedAt": "string", "duration": "number", "createdLabel": "string", "updatedLabel": "string", "durationLabel": "string", "options": "object", "hideLabels": "boolean", "running": "boolean", "spacing": "number" },
|
|
159
|
+
events: {},
|
|
160
|
+
children: true,
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
tag: "np-text-max-line",
|
|
164
|
+
component: "TextMaxLine",
|
|
165
|
+
properties: { "variant": "string", "line": "number", "persistent": "boolean" },
|
|
166
|
+
events: {},
|
|
167
|
+
values: { "variant": ["h1", "h2", "h3", "h4", "h5", "h6", "subtitle1", "subtitle2", "body1", "body2", "caption", "overline", "button"] },
|
|
168
|
+
children: true,
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
tag: "np-group-heading",
|
|
172
|
+
component: "GroupHeading",
|
|
173
|
+
properties: { "label": "string", "description": "string", "required": "boolean" },
|
|
174
|
+
events: {},
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
tag: "np-dimensions-byline",
|
|
178
|
+
component: "DimensionsByline",
|
|
179
|
+
properties: { "dimensions": "object", "catalog": "array", "disabled": "boolean", "showUnsetAxes": "boolean", "unsetValueLabel": "string" },
|
|
180
|
+
events: {},
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
tag: "np-container",
|
|
184
|
+
component: "Container",
|
|
185
|
+
properties: { "maxWidth": "string", "fixed": "boolean", "disableGutters": "boolean" },
|
|
186
|
+
events: {},
|
|
187
|
+
values: { "maxWidth": ["xs", "sm", "md", "lg", "xl"] },
|
|
188
|
+
children: true,
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
tag: "np-scrollbar",
|
|
192
|
+
component: "Scrollbar",
|
|
193
|
+
properties: { "fade": "boolean" },
|
|
194
|
+
events: {},
|
|
195
|
+
children: true,
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
tag: "np-grouped-filter-dropdown",
|
|
199
|
+
component: "GroupedFilterDropdown",
|
|
200
|
+
properties: { "label": "string", "elements": "array", "selected": "object", "loading": "boolean", "disabled": "boolean", "shouldFitFont": "boolean" },
|
|
201
|
+
events: { "change": "onChange", "open": "onOpen" },
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
tag: "np-copy-to-clipboard-button",
|
|
205
|
+
component: "CopyToClipboardButton",
|
|
206
|
+
properties: { "text": "string", "tooltipCopy": "string", "tooltipCopied": "string", "icon": "string", "iconSize": "number" },
|
|
207
|
+
events: { "copy": "onCopy" },
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
tag: "np-countdown-button",
|
|
211
|
+
component: "CountdownButton",
|
|
212
|
+
properties: { "label": "string", "cooldownTime": "number", "fullWidth": "boolean" },
|
|
213
|
+
events: { "press": "onClick" },
|
|
214
|
+
slots: ["startIcon"],
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
tag: "np-label",
|
|
218
|
+
component: "Label",
|
|
219
|
+
properties: { "color": "string", "variant": "string" },
|
|
220
|
+
events: {},
|
|
221
|
+
slots: ["startIcon", "endIcon"],
|
|
222
|
+
values: { "color": ["default", "primary", "secondary", "info", "success", "warning", "error"], "variant": ["filled", "outlined", "ghost", "soft"] },
|
|
223
|
+
children: true,
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
tag: "np-avatar",
|
|
227
|
+
component: "Avatar",
|
|
228
|
+
properties: { "name": "string", "color": "string", "size": "string", "variant": "string" },
|
|
229
|
+
events: {},
|
|
230
|
+
values: { "color": ["default", "primary", "secondary", "info", "success", "warning", "error"], "size": ["small", "medium", "large"], "variant": ["circular", "rounded", "square"] },
|
|
231
|
+
children: true,
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
tag: "np-icon-picker",
|
|
235
|
+
component: "IconPicker",
|
|
236
|
+
properties: { "value": "string", "prefix": "string", "minChars": "number", "debounceMs": "number", "placeholder": "string", "loadingText": "string", "noResultsText": "string", "errorText": "string", "helperText": "string", "error": "boolean", "label": "string", "size": "string", "disabled": "boolean", "iconWidth": "number" },
|
|
237
|
+
events: { "change": "onChange" },
|
|
238
|
+
values: { "size": ["small", "medium"] },
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
tag: "np-tab-bar",
|
|
242
|
+
component: "TabBar",
|
|
243
|
+
properties: { "items": "array", "value": "string", "variant": "string" },
|
|
244
|
+
events: { "change": "onChange" },
|
|
245
|
+
values: { "variant": ["standard", "scrollable", "fullWidth"] },
|
|
246
|
+
children: true,
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
tag: "np-breadcrumb-trail",
|
|
250
|
+
component: "BreadcrumbTrail",
|
|
251
|
+
properties: { "items": "array", "maxItems": "number" },
|
|
252
|
+
events: { "select": "onSelect" },
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
tag: "np-chip-list",
|
|
256
|
+
component: "ChipList",
|
|
257
|
+
properties: { "items": "array", "variant": "string", "loading": "boolean", "maxItemsToShow": "number", "disableListItem": "boolean" },
|
|
258
|
+
events: {},
|
|
259
|
+
values: { "variant": ["filled", "outlined", "soft"], "items[].color": ["default", "primary", "secondary", "error", "info", "success", "warning"] },
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
tag: "np-activity-timeline",
|
|
263
|
+
component: "ActivityTimeline",
|
|
264
|
+
properties: { "items": "array" },
|
|
265
|
+
events: {},
|
|
266
|
+
imageValues: ["items[].avatar"],
|
|
267
|
+
},
|
|
58
268
|
];
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import "./polyfill.js";
|
|
|
2
2
|
export { defineUI, type DefineUIOptions } from './defineUI.js';
|
|
3
3
|
export { usePluginContext, useNullplatform, useQuery, type WidgetProps, type QueryState } from './context.js';
|
|
4
4
|
export { nullplatform, type NullplatformBridge } from './bridge.js';
|
|
5
|
+
export { useTopic, useSetTopic, type UseTopicOptions, type UseTopicResult, type UseSetTopicOptions, type SetTopicState, type TopicMap, type ServiceDraftV1, } from './topics.js';
|
|
5
6
|
export * from './components.js';
|
|
6
|
-
export type { PluginContext, ApiRequest, HttpRequest, HttpResponse, NavigationTarget, SnackbarOptions } from './protocol.js';
|
|
7
|
+
export type { PluginContext, ApiRequest, HttpRequest, HttpResponse, NavigationTarget, SnackbarOptions, TopicSnapshot, TopicStatus } from './protocol.js';
|
|
7
8
|
export { BridgeError } from './protocol.js';
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,6 @@ import "./polyfill.js";
|
|
|
2
2
|
export { defineUI } from './defineUI.js';
|
|
3
3
|
export { usePluginContext, useNullplatform, useQuery } from './context.js';
|
|
4
4
|
export { nullplatform } from './bridge.js';
|
|
5
|
+
export { useTopic, useSetTopic, } from './topics.js';
|
|
5
6
|
export * from './components.js';
|
|
6
7
|
export { BridgeError } from './protocol.js';
|
package/dist/protocol.d.ts
CHANGED
|
@@ -97,8 +97,26 @@ export interface BridgeMethods {
|
|
|
97
97
|
'navigation.push': (target: NavigationTarget) => Promise<void>;
|
|
98
98
|
'ui.snackbar': (options: SnackbarOptions) => Promise<void>;
|
|
99
99
|
'context.get': () => Promise<PluginContext>;
|
|
100
|
+
'topics.get': (topic: string) => Promise<TopicSnapshot>;
|
|
101
|
+
'topics.subscribe': (topic: string) => Promise<TopicSnapshot>;
|
|
102
|
+
'topics.unsubscribe': (topic: string) => Promise<void>;
|
|
103
|
+
'topics.set': (topic: string, value: unknown) => Promise<void>;
|
|
104
|
+
'topics.clear': (topic: string) => Promise<void>;
|
|
100
105
|
}
|
|
101
106
|
export type BridgeMethodName = keyof BridgeMethods;
|
|
107
|
+
/**
|
|
108
|
+
* Whether a topic has a value on this page: `waiting` when something on the page provides it but has not
|
|
109
|
+
* set a value yet, `no-provider` when nothing on the page provides it.
|
|
110
|
+
*/
|
|
111
|
+
export type TopicStatus = 'waiting' | 'ready' | 'no-provider';
|
|
112
|
+
/** The current value of a topic as one plugin sees it: secrets removed, projected to the fields it declared. */
|
|
113
|
+
export interface TopicSnapshot<T = unknown> {
|
|
114
|
+
topic: string;
|
|
115
|
+
status: TopicStatus;
|
|
116
|
+
value?: T;
|
|
117
|
+
/** Increases on every change, so a late reply never overwrites a newer value. */
|
|
118
|
+
rev: number;
|
|
119
|
+
}
|
|
102
120
|
export type WorkerMessage = {
|
|
103
121
|
type: 'ready';
|
|
104
122
|
protocol: number;
|
|
@@ -159,6 +177,9 @@ export type HostMessage = {
|
|
|
159
177
|
ok: boolean;
|
|
160
178
|
value?: unknown;
|
|
161
179
|
error?: SerializedError;
|
|
180
|
+
} | {
|
|
181
|
+
type: 'topic';
|
|
182
|
+
snapshot: TopicSnapshot;
|
|
162
183
|
};
|
|
163
184
|
export interface SerializedError {
|
|
164
185
|
message: string;
|
package/dist/topics.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { TopicSnapshot, TopicStatus } from './protocol.js';
|
|
2
|
+
/** What the service create and edit forms publish, after secrets are removed. */
|
|
3
|
+
export interface ServiceDraftV1 {
|
|
4
|
+
mode: 'create' | 'edit';
|
|
5
|
+
specification: {
|
|
6
|
+
id: string;
|
|
7
|
+
slug?: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
};
|
|
10
|
+
name?: string;
|
|
11
|
+
dimensions?: Record<string, string>;
|
|
12
|
+
attributes?: Record<string, unknown>;
|
|
13
|
+
/** False while the form has validation errors. */
|
|
14
|
+
valid: boolean;
|
|
15
|
+
errors?: {
|
|
16
|
+
path: string;
|
|
17
|
+
message: string;
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
/** Host topics and the shape of their values. Plugin topics are typed by the caller instead. */
|
|
21
|
+
export interface TopicMap {
|
|
22
|
+
'np.service.draft@1': ServiceDraftV1;
|
|
23
|
+
}
|
|
24
|
+
/** Called by the worker runtime for every `topic` message from the host. */
|
|
25
|
+
export declare function receiveTopic(snapshot: TopicSnapshot): void;
|
|
26
|
+
/** Imperative access, for code outside React. Hooks are built on this. */
|
|
27
|
+
export declare const topics: {
|
|
28
|
+
/** The current snapshot, without subscribing. */
|
|
29
|
+
get<T = unknown>(topic: string): Promise<TopicSnapshot<T>>;
|
|
30
|
+
/** Calls `listener` with the current snapshot and on every change. Returns the unsubscribe function. */
|
|
31
|
+
subscribe<T = unknown>(topic: string, listener: (snapshot: TopicSnapshot<T>) => void): () => void;
|
|
32
|
+
/** Replaces the value of one of this plugin's topics. */
|
|
33
|
+
set(topic: string, value: unknown): Promise<void>;
|
|
34
|
+
/** Withdraws the value; readers go back to `waiting`. */
|
|
35
|
+
clear(topic: string): Promise<void>;
|
|
36
|
+
};
|
|
37
|
+
export interface UseTopicOptions<T, S> {
|
|
38
|
+
/** Picks the part the component uses; it only re-renders when that part changes. */
|
|
39
|
+
select?: (value: T) => S;
|
|
40
|
+
/** Do not subscribe; `status` stays `uninitialized`. */
|
|
41
|
+
skip?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface UseTopicResult<S> {
|
|
44
|
+
/** The value, or what `select` returned from it. Undefined until the topic is `ready`. */
|
|
45
|
+
data: S | undefined;
|
|
46
|
+
status: TopicStatus | 'uninitialized';
|
|
47
|
+
rev: number;
|
|
48
|
+
isReady: boolean;
|
|
49
|
+
/** False when nothing on this page provides the topic, so the widget can show its own default. */
|
|
50
|
+
hasProvider: boolean;
|
|
51
|
+
}
|
|
52
|
+
type TopicValue<K extends string, T> = K extends keyof TopicMap ? TopicMap[K] : T;
|
|
53
|
+
/**
|
|
54
|
+
* Reads a topic and re-renders when it changes.
|
|
55
|
+
*
|
|
56
|
+
* const { data, isReady } = useTopic('np.service.draft@1', { select: (d) => d.attributes });
|
|
57
|
+
*/
|
|
58
|
+
export declare function useTopic<K extends string, T = unknown, S = TopicValue<K, T>>(topic: K, options?: UseTopicOptions<TopicValue<K, T>, S>): UseTopicResult<S>;
|
|
59
|
+
export interface UseSetTopicOptions {
|
|
60
|
+
/** Withdraw the value when the component unmounts, so nobody keeps reading a stale one. Default true. */
|
|
61
|
+
clearOnUnmount?: boolean;
|
|
62
|
+
}
|
|
63
|
+
export interface SetTopicState {
|
|
64
|
+
clear: () => Promise<void>;
|
|
65
|
+
isPending: boolean;
|
|
66
|
+
error: Error | undefined;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Writes one of this plugin's topics. Does not subscribe, so it never re-renders because of the topic.
|
|
70
|
+
*
|
|
71
|
+
* const [setEstimate, { clear, error }] = useSetTopic('my-plugin/estimate@1');
|
|
72
|
+
* setEstimate({ amount: 184.2, currency: 'USD' });
|
|
73
|
+
*/
|
|
74
|
+
export declare function useSetTopic<T = unknown>(topic: string, options?: UseSetTopicOptions): [(value: T) => Promise<void>, SetTopicState];
|
|
75
|
+
export {};
|
package/dist/topics.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin side of topics: one local copy of each subscribed topic, shared by every widget of the plugin.
|
|
3
|
+
* The host owns the values; this keeps the latest snapshot it pushed and tells the widgets.
|
|
4
|
+
*/
|
|
5
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
6
|
+
import { bridge } from './bridge.js';
|
|
7
|
+
const entries = new Map();
|
|
8
|
+
/** Called by the worker runtime for every `topic` message from the host. */
|
|
9
|
+
export function receiveTopic(snapshot) {
|
|
10
|
+
const entry = entries.get(snapshot.topic);
|
|
11
|
+
if (!entry)
|
|
12
|
+
return;
|
|
13
|
+
// Replies and pushes can cross on the wire; the newest revision wins.
|
|
14
|
+
if (entry.snapshot && entry.snapshot.rev > snapshot.rev)
|
|
15
|
+
return;
|
|
16
|
+
entry.snapshot = snapshot;
|
|
17
|
+
for (const listener of entry.listeners)
|
|
18
|
+
listener(snapshot);
|
|
19
|
+
}
|
|
20
|
+
function subscribe(topic, listener) {
|
|
21
|
+
let entry = entries.get(topic);
|
|
22
|
+
if (!entry) {
|
|
23
|
+
entry = { listeners: new Set() };
|
|
24
|
+
entries.set(topic, entry);
|
|
25
|
+
}
|
|
26
|
+
entry.listeners.add(listener);
|
|
27
|
+
if (entry.snapshot)
|
|
28
|
+
listener(entry.snapshot);
|
|
29
|
+
if (!entry.subscribed) {
|
|
30
|
+
const current = entry;
|
|
31
|
+
current.subscribed = bridge.call('topics.subscribe', topic).then((snapshot) => receiveTopic(snapshot), (error) => {
|
|
32
|
+
// A topic the plugin cannot read reports as unavailable, and the error reaches the console.
|
|
33
|
+
console.error(`[plugin] cannot subscribe to ${topic}:`, error.message);
|
|
34
|
+
receiveTopic({ topic, status: 'no-provider', rev: -1 });
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return () => {
|
|
38
|
+
const current = entries.get(topic);
|
|
39
|
+
if (!current)
|
|
40
|
+
return;
|
|
41
|
+
current.listeners.delete(listener);
|
|
42
|
+
if (current.listeners.size > 0)
|
|
43
|
+
return;
|
|
44
|
+
entries.delete(topic);
|
|
45
|
+
void current.subscribed?.then(() => bridge.call('topics.unsubscribe', topic)).catch(() => undefined);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Imperative access, for code outside React. Hooks are built on this. */
|
|
49
|
+
export const topics = {
|
|
50
|
+
/** The current snapshot, without subscribing. */
|
|
51
|
+
get(topic) {
|
|
52
|
+
return bridge.call('topics.get', topic);
|
|
53
|
+
},
|
|
54
|
+
/** Calls `listener` with the current snapshot and on every change. Returns the unsubscribe function. */
|
|
55
|
+
subscribe(topic, listener) {
|
|
56
|
+
return subscribe(topic, listener);
|
|
57
|
+
},
|
|
58
|
+
/** Replaces the value of one of this plugin's topics. */
|
|
59
|
+
set(topic, value) {
|
|
60
|
+
return bridge.call('topics.set', topic, value);
|
|
61
|
+
},
|
|
62
|
+
/** Withdraws the value; readers go back to `waiting`. */
|
|
63
|
+
clear(topic) {
|
|
64
|
+
return bridge.call('topics.clear', topic);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
function shallowEqual(a, b) {
|
|
68
|
+
if (Object.is(a, b))
|
|
69
|
+
return true;
|
|
70
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object')
|
|
71
|
+
return false;
|
|
72
|
+
const keysA = Object.keys(a);
|
|
73
|
+
if (keysA.length !== Object.keys(b).length)
|
|
74
|
+
return false;
|
|
75
|
+
return keysA.every((key) => Object.is(a[key], b[key]));
|
|
76
|
+
}
|
|
77
|
+
const UNINITIALIZED = { data: undefined, status: 'uninitialized', rev: -1, isReady: false, hasProvider: false };
|
|
78
|
+
/**
|
|
79
|
+
* Reads a topic and re-renders when it changes.
|
|
80
|
+
*
|
|
81
|
+
* const { data, isReady } = useTopic('np.service.draft@1', { select: (d) => d.attributes });
|
|
82
|
+
*/
|
|
83
|
+
export function useTopic(topic, options = {}) {
|
|
84
|
+
const { skip = false } = options;
|
|
85
|
+
const selectRef = useRef(options.select);
|
|
86
|
+
selectRef.current = options.select;
|
|
87
|
+
const [result, setResult] = useState(UNINITIALIZED);
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (skip) {
|
|
90
|
+
setResult(UNINITIALIZED);
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
return subscribe(topic, (snapshot) => {
|
|
94
|
+
const select = selectRef.current;
|
|
95
|
+
const ready = snapshot.status === 'ready';
|
|
96
|
+
const data = ready ? (select ? select(snapshot.value) : snapshot.value) : undefined;
|
|
97
|
+
setResult((previous) => previous.status === snapshot.status && shallowEqual(previous.data, data)
|
|
98
|
+
? previous
|
|
99
|
+
: { data, status: snapshot.status, rev: snapshot.rev, isReady: ready, hasProvider: snapshot.status !== 'no-provider' });
|
|
100
|
+
});
|
|
101
|
+
}, [topic, skip]);
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Writes one of this plugin's topics. Does not subscribe, so it never re-renders because of the topic.
|
|
106
|
+
*
|
|
107
|
+
* const [setEstimate, { clear, error }] = useSetTopic('my-plugin/estimate@1');
|
|
108
|
+
* setEstimate({ amount: 184.2, currency: 'USD' });
|
|
109
|
+
*/
|
|
110
|
+
export function useSetTopic(topic, options = {}) {
|
|
111
|
+
const { clearOnUnmount = true } = options;
|
|
112
|
+
const [pending, setPending] = useState(0);
|
|
113
|
+
const [error, setError] = useState(undefined);
|
|
114
|
+
const wroteRef = useRef(false);
|
|
115
|
+
const mountedRef = useRef(true);
|
|
116
|
+
const track = useCallback((operation) => {
|
|
117
|
+
setPending((count) => count + 1);
|
|
118
|
+
return operation.then(() => {
|
|
119
|
+
if (mountedRef.current) {
|
|
120
|
+
setPending((count) => count - 1);
|
|
121
|
+
setError(undefined);
|
|
122
|
+
}
|
|
123
|
+
}, (failure) => {
|
|
124
|
+
if (mountedRef.current) {
|
|
125
|
+
setPending((count) => count - 1);
|
|
126
|
+
setError(failure);
|
|
127
|
+
}
|
|
128
|
+
throw failure;
|
|
129
|
+
});
|
|
130
|
+
}, []);
|
|
131
|
+
const set = useCallback((value) => {
|
|
132
|
+
wroteRef.current = true;
|
|
133
|
+
return track(topics.set(topic, value));
|
|
134
|
+
}, [topic, track]);
|
|
135
|
+
const clear = useCallback(() => track(topics.clear(topic)), [topic, track]);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
mountedRef.current = true;
|
|
138
|
+
return () => {
|
|
139
|
+
mountedRef.current = false;
|
|
140
|
+
if (clearOnUnmount && wroteRef.current)
|
|
141
|
+
void topics.clear(topic).catch(() => undefined);
|
|
142
|
+
wroteRef.current = false;
|
|
143
|
+
};
|
|
144
|
+
}, [topic, clearOnUnmount]);
|
|
145
|
+
return [set, { clear, isPending: pending > 0, error }];
|
|
146
|
+
}
|
package/dist/worker.js
CHANGED
|
@@ -9,6 +9,7 @@ import { BatchingRemoteConnection } from '@remote-dom/core/elements';
|
|
|
9
9
|
import { defineCatalogElements, ROOT_TAG } from './elements.js';
|
|
10
10
|
import { bridge } from './bridge.js';
|
|
11
11
|
import { PluginContextProvider, broadcastContext } from './context.js';
|
|
12
|
+
import { receiveTopic } from './topics.js';
|
|
12
13
|
import { PROTOCOL_VERSION, createIdGenerator, serializeError, serializeFunctions, } from './protocol.js';
|
|
13
14
|
const nextFunctionId = createIdGenerator('fn');
|
|
14
15
|
const mounts = new Map();
|
|
@@ -107,6 +108,9 @@ function handle(port, message) {
|
|
|
107
108
|
case 'result':
|
|
108
109
|
bridge.handleResult(message);
|
|
109
110
|
break;
|
|
111
|
+
case 'topic':
|
|
112
|
+
receiveTopic(message.snapshot);
|
|
113
|
+
break;
|
|
110
114
|
default:
|
|
111
115
|
break;
|
|
112
116
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nullplatform/ui-plugin-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "SDK for nullplatform UI plugins: catalog components, bridge client and defineUI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
33
|
"scripts": {
|
|
34
|
-
"build": "tsc -p tsconfig.json && tsc -p tsconfig.contract.cjs.json && node scripts/finish-build.mjs",
|
|
34
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.contract.cjs.json && node scripts/finish-build.mjs && node scripts/check-node-imports.mjs",
|
|
35
35
|
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test-types.json",
|
|
36
36
|
"test": "vitest run --passWithNoTests",
|
|
37
37
|
"sync:contracts": "node scripts/sync-contracts.mjs"
|
|
38
38
|
},
|
|
39
|
-
"uiComponentsContracts": "0.15.
|
|
39
|
+
"uiComponentsContracts": "0.15.9",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@remote-dom/core": "^1.11.1",
|
|
42
42
|
"@remote-dom/react": "^1.2.2",
|