Skip to content

Commit 41cdf89

Browse files
Add validation and some small cleanup (#19)
1 parent 17f331b commit 41cdf89

File tree

5 files changed

+134
-117
lines changed

5 files changed

+134
-117
lines changed

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -434,36 +434,36 @@ interface PluginInstance<T> {
434434
/**
435435
* Gets a static image of a workbook variable
436436
*/
437-
getVariable(id: string): WorkbookVariable;
437+
getVariable(configId: string): WorkbookVariable;
438438
439439
/**
440440
* Setter for workbook variable passed in
441441
*/
442-
setVariable(id: string, ...values: unknown[]): void;
442+
setVariable(configId: string, ...values: unknown[]): void;
443443
444444
/**
445445
* Getter for interaction selection state
446446
*/
447-
getInteraction(id: string): WorkbookSelection[];
447+
getInteraction(configId: string): WorkbookSelection[];
448448
449449
/**
450450
* Setter for interaction selection state
451451
*/
452452
setInteraction(
453-
id: string,
453+
configId: string,
454454
elementId: string,
455455
selection: WorkbookSelection[],
456456
): void;
457457
458458
/**
459459
* Triggers an action based on the provided action trigger ID
460460
*/
461-
triggerAction(id: string): void;
461+
triggerAction(configId: string): void;
462462
463463
/**
464464
* Registers an effect with the provided action effect ID
465465
*/
466-
registerEffect(id: string, effect: Function): void;
466+
registerEffect(configId: string, effect: Function): void;
467467
468468
/**
469469
* Overrider function for Config Ready state
@@ -474,7 +474,7 @@ interface PluginInstance<T> {
474474
* Allows users to subscribe to changes in the passed in variable
475475
*/
476476
subscribeToWorkbookVariable(
477-
id: string,
477+
configId: string,
478478
callback: (input: WorkbookVariable) => void,
479479
): Unsubscriber;
480480
@@ -483,7 +483,7 @@ interface PluginInstance<T> {
483483
* Allows users to subscribe to changes in the passed in interaction ID
484484
*/
485485
subscribeToWorkbookInteraction(
486-
id: string,
486+
configId: string,
487487
callback: (input: WorkbookSelection[]) => void,
488488
): Unsubscriber;
489489
};
@@ -492,21 +492,21 @@ interface PluginInstance<T> {
492492
/**
493493
* Getter for Column Data by parent sheet ID
494494
*/
495-
getElementColumns(id: string): Promise<WbElementColumns>;
495+
getElementColumns(configId: string): Promise<WbElementColumns>;
496496
497497
/**
498498
* Subscriber to changes in column data by ID
499499
*/
500500
subscribeToElementColumns(
501-
id: string,
501+
configId: string,
502502
callback: (cols: WbElementColumns) => void,
503503
): Unsubscriber;
504504
505505
/**
506506
* Subscriber for the data within a given sheet
507507
*/
508508
subscribeToElementData(
509-
id: string,
509+
configId: string,
510510
callback: (data: WbElementData) => void,
511511
): Unsubscriber;
512512
};
@@ -647,12 +647,12 @@ interface WorkbookElementColumns {
647647
Provides the latest data values from corresponding sheet, up to 25000 values.
648648
649649
```ts
650-
function useElementData(elementId: string): WorkbookElementData;
650+
function useElementData(configId: string): WorkbookElementData;
651651
```
652652
653653
Arguments
654654
655-
- `elementId : string` - A workbook element’s unique identifier.
655+
- `configId : string` - A workbook element’s unique identifier from the plugin config.
656656
657657
Returns the row data from the specified element.
658658
@@ -668,12 +668,12 @@ Provides the latest data values from the corresponding sheet (initially 25000),
668668
callback for fetching more data in chunks of 25000 values.
669669
670670
```ts
671-
function useElementData(elementId: string): [WorkbookElementData, () => void];
671+
function useElementData(configId: string): [WorkbookElementData, () => void];
672672
```
673673
674674
Arguments
675675
676-
- `elementId : string` - A workbook element’s unique identifier.
676+
- `configId : string` - A workbook element’s unique identifier from the plugin config.
677677
678678
Returns the row data from the specified element, and a callback for fetching
679679
more data.
@@ -690,13 +690,13 @@ Returns a given variable's value and a setter to update that variable
690690
691691
```ts
692692
function useVariable(
693-
variableId: string,
693+
configId: string,
694694
): [WorkbookVariable | undefined, (...values: unknown[]) => void];
695695
```
696696
697697
Arguments
698698
699-
- `variableId : string` - The ID of the variable
699+
- `configId : string` - The config ID corresponding to the workbook control variable
700700
701701
The returned setter function accepts 1 or more variable values expressed as an
702702
array or multiple parameters
@@ -711,14 +711,14 @@ Returns a given interaction's selection state and a setter to update that intera
711711
712712
```ts
713713
function useInteraction(
714-
interactionId: string,
714+
configId: string,
715715
elementId: string,
716716
): [WorkbookSelection | undefined, (value: WorkbookSelection[]) => void];
717717
```
718718
719719
Arguments
720720
721-
- `interactionId : string` - The ID of the interaction
721+
- `configId : string` - The config ID corresponding to the workbook interaction
722722
- `elementId : string` - The ID of the element that this interaction is
723723
associated with
724724
@@ -730,7 +730,7 @@ function setVariableCallback(value: WorkbookSelection[]): void;
730730
731731
#### useActionTrigger()
732732
733-
- `configId : string` - The ID of the action trigger from the Plugin Config
733+
- `configId : string` - The config ID corresponding to the action trigger
734734
735735
Returns a callback function to trigger one or more action effects for a given action trigger
736736
@@ -742,7 +742,7 @@ function useActionTrigger(configId: string): () => void;
742742
743743
Arguments
744744
745-
- `configId : string` - The ID of the action trigger from the Plugin Config
745+
- `configId : string` - The config ID corresponding to the action trigger
746746
747747
The function that can be called to asynchronously trigger the action
748748
@@ -755,12 +755,12 @@ function triggerActionCallback(configId: string): void;
755755
Registers and unregisters an action effect within the plugin
756756
757757
```ts
758-
function useActionEffect(effectId: string, effect: () => void);
758+
function useActionEffect(configId: string, effect: () => void);
759759
```
760760
761761
Arguments
762762
763-
- `effectId : string` - The ID of the action effect
763+
- `configId : string` - The config ID corresponding to the action effect
764764
- `effect : Function` - The function to be called when the effect is triggered
765765
766766
#### useConfig()

src/client/initialize.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { validateConfigId } from '../error';
12
import {
23
PluginConfig,
34
PluginInstance,
45
PluginMessageResponse,
56
WorkbookSelection,
67
WorkbookVariable,
7-
Unsubscriber,
88
} from '../types';
99

1010
export function initialize<T = {}>(): PluginInstance<T> {
@@ -126,28 +126,39 @@ export function initialize<T = {}>(): PluginInstance<T> {
126126
on('config', listener);
127127
return () => off('config', listener);
128128
},
129-
getVariable(id: string): WorkbookVariable {
130-
return subscribedWorkbookVars[id];
129+
getVariable(configId: string) {
130+
validateConfigId(configId, 'variable');
131+
return subscribedWorkbookVars[configId];
131132
},
132-
setVariable(id: string, ...values: unknown[]) {
133-
void execPromise('wb:plugin:variable:set', id, ...values);
133+
setVariable(configId: string, ...values: unknown[]) {
134+
validateConfigId(configId, 'variable');
135+
void execPromise('wb:plugin:variable:set', configId, ...values);
134136
},
135-
getInteraction(id: string) {
136-
return subscribedInteractions[id];
137+
getInteraction(configId: string) {
138+
validateConfigId(configId, 'interaction');
139+
return subscribedInteractions[configId];
137140
},
138141
setInteraction(
139-
id: string,
142+
configId: string,
140143
elementId: string,
141144
selection:
142145
| string[]
143146
| Array<Record<string, { type: string; val?: unknown }>>,
144147
) {
145-
void execPromise('wb:plugin:selection:set', id, elementId, selection);
148+
validateConfigId(configId, 'interaction');
149+
void execPromise(
150+
'wb:plugin:selection:set',
151+
configId,
152+
elementId,
153+
selection,
154+
);
146155
},
147156
triggerAction(configId: string) {
157+
validateConfigId(configId, 'action-trigger');
148158
void execPromise('wb:plugin:action-trigger:invoke', configId);
149159
},
150160
registerEffect(configId: string, effect: () => void) {
161+
validateConfigId(configId, 'action-effect');
151162
registeredEffects[configId] = effect;
152163
return () => {
153164
delete registeredEffects[configId];
@@ -159,24 +170,20 @@ export function initialize<T = {}>(): PluginInstance<T> {
159170
setLoadingState(loadingState) {
160171
void execPromise('wb:plugin:config:loading-state', loadingState);
161172
},
162-
subscribeToWorkbookVariable(
163-
id: string,
164-
callback: (input: WorkbookVariable) => void,
165-
): Unsubscriber {
173+
subscribeToWorkbookVariable(configId, callback) {
174+
validateConfigId(configId, 'variable');
166175
const setValues = (values: Record<string, WorkbookVariable>) => {
167-
callback(values[id]);
176+
callback(values[configId]);
168177
};
169178
on('wb:plugin:variable:update', setValues);
170179
return () => {
171180
off('wb:plugin:variable:update', setValues);
172181
};
173182
},
174-
subscribeToWorkbookInteraction(
175-
id: string,
176-
callback: (input: WorkbookSelection[]) => void,
177-
): Unsubscriber {
183+
subscribeToWorkbookInteraction(configId, callback) {
184+
validateConfigId(configId, 'interaction');
178185
const setValues = (values: Record<string, WorkbookSelection[]>) => {
179-
callback(values[id]);
186+
callback(values[configId]);
180187
};
181188
on('wb:plugin:selection:update', setValues);
182189
return () => {
@@ -185,31 +192,35 @@ export function initialize<T = {}>(): PluginInstance<T> {
185192
},
186193
},
187194
elements: {
188-
getElementColumns(id) {
189-
return execPromise('wb:plugin:element:columns:get', id);
195+
getElementColumns(configId) {
196+
validateConfigId(configId, 'element');
197+
return execPromise('wb:plugin:element:columns:get', configId);
190198
},
191-
subscribeToElementColumns(id, callback) {
192-
const eventName = `wb:plugin:element:${id}:columns`;
199+
subscribeToElementColumns(configId, callback) {
200+
validateConfigId(configId, 'element');
201+
const eventName = `wb:plugin:element:${configId}:columns`;
193202
on(eventName, callback);
194-
void execPromise('wb:plugin:element:subscribe:columns', id);
203+
void execPromise('wb:plugin:element:subscribe:columns', configId);
195204

196205
return () => {
197206
off(eventName, callback);
198-
void execPromise('wb:plugin:element:unsubscribe:columns', id);
207+
void execPromise('wb:plugin:element:unsubscribe:columns', configId);
199208
};
200209
},
201-
subscribeToElementData(id, callback) {
202-
const eventName = `wb:plugin:element:${id}:data`;
210+
subscribeToElementData(configId, callback) {
211+
validateConfigId(configId, 'element');
212+
const eventName = `wb:plugin:element:${configId}:data`;
203213
on(eventName, callback);
204-
void execPromise('wb:plugin:element:subscribe:data', id);
214+
void execPromise('wb:plugin:element:subscribe:data', configId);
205215

206216
return () => {
207217
off(eventName, callback);
208-
void execPromise('wb:plugin:element:unsubscribe:data', id);
218+
void execPromise('wb:plugin:element:unsubscribe:data', configId);
209219
};
210220
},
211-
fetchMoreElementData(id) {
212-
void execPromise('wb:plugin:element:fetch-more', id);
221+
fetchMoreElementData(configId) {
222+
validateConfigId(configId, 'element');
223+
void execPromise('wb:plugin:element:fetch-more', configId);
213224
},
214225
},
215226
destroy() {

src/error.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { CustomPluginConfigOptions } from './types';
2+
3+
export function validateConfigId(
4+
configId: string,
5+
expectedConfigType: CustomPluginConfigOptions['type'],
6+
) {
7+
if (configId === undefined) {
8+
console.warn(`Invalid config ${expectedConfigType}: ${configId}`);
9+
}
10+
}

0 commit comments

Comments
 (0)