Skip to content

Commit

Permalink
Add validation and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
peternandersson committed Sep 20, 2024
1 parent 17f331b commit 27648a1
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 118 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,36 +434,36 @@ interface PluginInstance<T> {
/**
* Gets a static image of a workbook variable
*/
getVariable(id: string): WorkbookVariable;
getVariable(configId: string): WorkbookVariable;
/**
* Setter for workbook variable passed in
*/
setVariable(id: string, ...values: unknown[]): void;
setVariable(configId: string, ...values: unknown[]): void;
/**
* Getter for interaction selection state
*/
getInteraction(id: string): WorkbookSelection[];
getInteraction(configId: string): WorkbookSelection[];
/**
* Setter for interaction selection state
*/
setInteraction(
id: string,
configId: string,
elementId: string,
selection: WorkbookSelection[],
): void;
/**
* Triggers an action based on the provided action trigger ID
*/
triggerAction(id: string): void;
triggerAction(configId: string): void;
/**
* Registers an effect with the provided action effect ID
*/
registerEffect(id: string, effect: Function): void;
registerEffect(configId: string, effect: Function): void;
/**
* Overrider function for Config Ready state
Expand All @@ -474,7 +474,7 @@ interface PluginInstance<T> {
* Allows users to subscribe to changes in the passed in variable
*/
subscribeToWorkbookVariable(
id: string,
configId: string,
callback: (input: WorkbookVariable) => void,
): Unsubscriber;
Expand All @@ -483,7 +483,7 @@ interface PluginInstance<T> {
* Allows users to subscribe to changes in the passed in interaction ID
*/
subscribeToWorkbookInteraction(
id: string,
configId: string,
callback: (input: WorkbookSelection[]) => void,
): Unsubscriber;
};
Expand All @@ -492,21 +492,21 @@ interface PluginInstance<T> {
/**
* Getter for Column Data by parent sheet ID
*/
getElementColumns(id: string): Promise<WbElementColumns>;
getElementColumns(configId: string): Promise<WbElementColumns>;
/**
* Subscriber to changes in column data by ID
*/
subscribeToElementColumns(
id: string,
configId: string,
callback: (cols: WbElementColumns) => void,
): Unsubscriber;
/**
* Subscriber for the data within a given sheet
*/
subscribeToElementData(
id: string,
configId: string,
callback: (data: WbElementData) => void,
): Unsubscriber;
};
Expand Down Expand Up @@ -647,12 +647,12 @@ interface WorkbookElementColumns {
Provides the latest data values from corresponding sheet, up to 25000 values.
```ts
function useElementData(elementId: string): WorkbookElementData;
function useElementData(configId: string): WorkbookElementData;
```
Arguments
- `elementId : string` - A workbook element’s unique identifier.
- `configId : string` - A workbook element’s unique identifier from the plugin config.
Returns the row data from the specified element.
Expand All @@ -668,12 +668,12 @@ Provides the latest data values from the corresponding sheet (initially 25000),
callback for fetching more data in chunks of 25000 values.
```ts
function useElementData(elementId: string): [WorkbookElementData, () => void];
function useElementData(configId: string): [WorkbookElementData, () => void];
```
Arguments
- `elementId : string` - A workbook element’s unique identifier.
- `configId : string` - A workbook element’s unique identifier from the plugin config.
Returns the row data from the specified element, and a callback for fetching
more data.
Expand All @@ -690,13 +690,13 @@ Returns a given variable's value and a setter to update that variable
```ts
function useVariable(
variableId: string,
configId: string,
): [WorkbookVariable | undefined, (...values: unknown[]) => void];
```
Arguments
- `variableId : string` - The ID of the variable
- `configId : string` - The config ID corresponding to the workbook control variable
The returned setter function accepts 1 or more variable values expressed as an
array or multiple parameters
Expand All @@ -711,14 +711,14 @@ Returns a given interaction's selection state and a setter to update that intera
```ts
function useInteraction(
interactionId: string,
configId: string,
elementId: string,
): [WorkbookSelection | undefined, (value: WorkbookSelection[]) => void];
```
Arguments
- `interactionId : string` - The ID of the interaction
- `configId : string` - The config ID corresponding to the workbook interaction
- `elementId : string` - The ID of the element that this interaction is
associated with
Expand All @@ -730,7 +730,7 @@ function setVariableCallback(value: WorkbookSelection[]): void;
#### useActionTrigger()
- `configId : string` - The ID of the action trigger from the Plugin Config
- `configId : string` - The config ID corresponding to the action trigger
Returns a callback function to trigger one or more action effects for a given action trigger
Expand All @@ -742,7 +742,7 @@ function useActionTrigger(configId: string): () => void;
Arguments
- `configId : string` - The ID of the action trigger from the Plugin Config
- `configId : string` - The config ID corresponding to the action trigger
The function that can be called to asynchronously trigger the action
Expand All @@ -755,12 +755,12 @@ function triggerActionCallback(configId: string): void;
Registers and unregisters an action effect within the plugin
```ts
function useActionEffect(effectId: string, effect: () => void);
function useActionEffect(configId: string, effect: () => void);
```
Arguments
- `effectId : string` - The ID of the action effect
- `configId : string` - The config ID corresponding to the action effect
- `effect : Function` - The function to be called when the effect is triggered
#### useConfig()
Expand Down
95 changes: 63 additions & 32 deletions src/client/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
PluginMessageResponse,
WorkbookSelection,
WorkbookVariable,
Unsubscriber,
} from '../types';

export function initialize<T = {}>(): PluginInstance<T> {
Expand Down Expand Up @@ -126,28 +125,48 @@ export function initialize<T = {}>(): PluginInstance<T> {
on('config', listener);
return () => off('config', listener);
},
getVariable(id: string): WorkbookVariable {
return subscribedWorkbookVars[id];
getVariable(configId: string) {
if (configId === undefined) {
console.warn(`Invalid config variable: ${configId}`);
}
return subscribedWorkbookVars[configId];
},
setVariable(id: string, ...values: unknown[]) {
void execPromise('wb:plugin:variable:set', id, ...values);
setVariable(configId: string, ...values: unknown[]) {
if (configId === undefined) {
console.warn(`Invalid config variable: ${configId}`);
}
void execPromise('wb:plugin:variable:set', configId, ...values);
},
getInteraction(id: string) {
return subscribedInteractions[id];
getInteraction(configId: string) {
if (configId === undefined) {
console.warn(`Invalid config interaction: ${configId}`);
}
return subscribedInteractions[configId];
},
setInteraction(
id: string,
configId: string,
elementId: string,
selection:
| string[]
| Array<Record<string, { type: string; val?: unknown }>>,
) {
void execPromise('wb:plugin:selection:set', id, elementId, selection);
void execPromise(
'wb:plugin:selection:set',
configId,
elementId,
selection,
);
},
triggerAction(configId: string) {
if (configId === undefined) {
console.warn(`Invalid config action trigger: ${configId}`);
}
void execPromise('wb:plugin:action-trigger:invoke', configId);
},
registerEffect(configId: string, effect: () => void) {
if (configId === undefined) {
console.warn(`Invalid config action effect: ${configId}`);
}
registeredEffects[configId] = effect;
return () => {
delete registeredEffects[configId];
Expand All @@ -159,24 +178,24 @@ export function initialize<T = {}>(): PluginInstance<T> {
setLoadingState(loadingState) {
void execPromise('wb:plugin:config:loading-state', loadingState);
},
subscribeToWorkbookVariable(
id: string,
callback: (input: WorkbookVariable) => void,
): Unsubscriber {
subscribeToWorkbookVariable(configId, callback) {
if (configId === undefined) {
console.warn(`Invalid config variable: ${configId}`);
}
const setValues = (values: Record<string, WorkbookVariable>) => {
callback(values[id]);
callback(values[configId]);
};
on('wb:plugin:variable:update', setValues);
return () => {
off('wb:plugin:variable:update', setValues);
};
},
subscribeToWorkbookInteraction(
id: string,
callback: (input: WorkbookSelection[]) => void,
): Unsubscriber {
subscribeToWorkbookInteraction(configId, callback) {
if (configId === undefined) {
console.warn(`Invalid config interaction: ${configId}`);
}
const setValues = (values: Record<string, WorkbookSelection[]>) => {
callback(values[id]);
callback(values[configId]);
};
on('wb:plugin:selection:update', setValues);
return () => {
Expand All @@ -185,31 +204,43 @@ export function initialize<T = {}>(): PluginInstance<T> {
},
},
elements: {
getElementColumns(id) {
return execPromise('wb:plugin:element:columns:get', id);
},
subscribeToElementColumns(id, callback) {
const eventName = `wb:plugin:element:${id}:columns`;
getElementColumns(configId) {
if (configId === undefined) {
console.warn(`Invalid config element: ${configId}`);
}
return execPromise('wb:plugin:element:columns:get', configId);
},
subscribeToElementColumns(configId, callback) {
if (configId === undefined) {
console.warn(`Invalid config element: ${configId}`);
}
const eventName = `wb:plugin:element:${configId}:columns`;
on(eventName, callback);
void execPromise('wb:plugin:element:subscribe:columns', id);
void execPromise('wb:plugin:element:subscribe:columns', configId);

return () => {
off(eventName, callback);
void execPromise('wb:plugin:element:unsubscribe:columns', id);
void execPromise('wb:plugin:element:unsubscribe:columns', configId);
};
},
subscribeToElementData(id, callback) {
const eventName = `wb:plugin:element:${id}:data`;
subscribeToElementData(configId, callback) {
if (configId === undefined) {
console.warn(`Invalid config element: ${configId}`);
}
const eventName = `wb:plugin:element:${configId}:data`;
on(eventName, callback);
void execPromise('wb:plugin:element:subscribe:data', id);
void execPromise('wb:plugin:element:subscribe:data', configId);

return () => {
off(eventName, callback);
void execPromise('wb:plugin:element:unsubscribe:data', id);
void execPromise('wb:plugin:element:unsubscribe:data', configId);
};
},
fetchMoreElementData(id) {
void execPromise('wb:plugin:element:fetch-more', id);
fetchMoreElementData(configId) {
if (configId === undefined) {
console.warn(`Invalid config element: ${configId}`);
}
void execPromise('wb:plugin:element:fetch-more', configId);
},
},
destroy() {
Expand Down
Loading

0 comments on commit 27648a1

Please sign in to comment.