@casual-simulation/aux-common
Version:
Common library for AUX projects
141 lines • 4.75 kB
JavaScript
import { connectionInfoSchema } from './ConnectionInfo';
import { z } from 'zod';
export const deviceActionSchema = z.object({
type: z.literal('device'),
connection: connectionInfoSchema,
event: z.any(),
taskId: z.union([z.string(), z.number()]).optional(),
});
export const deviceSelectorSchema = z.object({
connectionId: z.string().optional(),
sessionId: z.string().optional(),
userId: z.string().optional(),
broadcast: z.boolean().optional(),
});
export const remoteActionSchema = deviceSelectorSchema.extend({
type: z.literal('remote'),
event: z.any(),
allowBatching: z.boolean().optional(),
taskId: z.union([z.string(), z.number()]).optional(),
});
export const remoteActionResultSchema = deviceSelectorSchema.extend({
type: z.literal('remote_result'),
result: z.any().optional(),
taskId: z.union([z.string(), z.number()]).optional(),
});
export const remoteActionErrorSchema = deviceSelectorSchema.extend({
type: z.literal('remote_error'),
error: z.any(),
taskId: z.union([z.string(), z.number()]).optional(),
});
export const remoteActionsSchema = z.discriminatedUnion('type', [
remoteActionSchema,
remoteActionResultSchema,
remoteActionErrorSchema,
]);
export const deviceActionResultSchema = z.object({
type: z.literal('device_result'),
result: z.any(),
taskId: z.union([z.string(), z.number()]).optional(),
connection: connectionInfoSchema,
});
export const deviceActionErrorSchema = z.object({
type: z.literal('device_error'),
error: z.any(),
taskId: z.union([z.string(), z.number()]).optional(),
connection: connectionInfoSchema,
});
export const deviceActionsSchema = z.discriminatedUnion('type', [
deviceActionSchema,
deviceActionResultSchema,
deviceActionErrorSchema,
]);
/**
* Creates a new remote event.
* @param event The event.
* @param selector The selector that should be used to determine which devices this event should be sent to.
* @param allowBatching Whether this action is allowed to be batched with other remote actions.
* Batching will preserve ordering between remote actions but may
* break ordering with respect to bot actions. Defaults to true.
* @param taskId The ID of the task that this remote action represents.
*/
export function remote(event, selector, allowBatching, taskId) {
return {
type: 'remote',
event: event,
allowBatching,
taskId,
...selector,
};
}
/**
* Creates a new remote event that represents a result to an async task.
* @param result The result data.
* @param selector The selector that should be used to determine which devices this event should be sent to.
* @param taskId The ID of the task that this remote action represents.
*/
export function remoteResult(result, selector, taskId) {
return {
type: 'remote_result',
result,
taskId,
...selector,
};
}
/**
* Creates a new remote event that represents an error result to an async task.
* @param result The result data.
* @param selector The selector that should be used to determine which devices this event should be sent to.
* @param taskId The ID of the task that this remote action represents.
*/
export function remoteError(error, selector, taskId) {
return {
type: 'remote_error',
error,
taskId,
...selector,
};
}
/**
* Creates a new device event.
* @param info The info about the device that is sending the event.
* @param data The data included in the result.
* @param taskId The ID of the task that this device action represents.
*/
export function device(info, event, taskId) {
return {
type: 'device',
connection: info,
event: event,
taskId,
};
}
/**
* Creates a new device event that represents the result of an async task.
* @param info The info about the device that is sending the event.
* @param result The result data included in the result.
* @param taskId The ID of the task that this device action represents.
*/
export function deviceResult(info, result, taskId) {
return {
type: 'device_result',
connection: info,
result,
taskId,
};
}
/**
* Creates a new device event that represents an error of an async task.
* @param info The info about the device that is sending the event.
* @param error The error data included in the result.
* @param taskId The ID of the task that this device action represents.
*/
export function deviceError(info, error, taskId) {
return {
type: 'device_error',
connection: info,
error,
taskId,
};
}
//# sourceMappingURL=RemoteActions.js.map