@jsm-mit/rabbit-motoko-package
v0.6.4
Published
Wrapper TypeScript package for Rabbit Motoko Canister.
Readme
Rabbit Motoko Package
A TypeScript library for interacting with the Rabbit Motoko actor on the Internet Computer (IC) platform. Simplifies interaction with the Rabbit task queuing system through a convenient wrapper around @dfinity/agent.
Installation
npm install @jsm-mit/rabbit-motoko-packageUsage
Initialization
import { RabbitMotokoActor } from '@jsm-mit/rabbit-motoko-package';
const canisterId = 'your-canister-id';
const rabbitActor = new RabbitMotokoActor(canisterId);Adding a task
const taskId = await rabbitActor.addTask({
channel: 'my-channel',
taskType: 'process-data',
payload: 'task payload data'
});
console.log('Task added with ID:', taskId);Getting available tasks
const availableTaskIds = await rabbitActor.getAvailableTaskIds('my-channel');
console.log('Available tasks:', availableTaskIds);Claiming a task
const task = await rabbitActor.claimTask({
taskId: 123n,
workerId: 'worker-1'
});
if (task.length > 0) {
console.log('Task claimed:', task[0]);
} else {
console.log('Task not available');
}Completing a task
const success = await rabbitActor.completeTask({
taskId: 123n,
result: 'task completed successfully'
});
if (success) {
console.log('Task completed successfully');
}Getting all tasks
const allTasks = await rabbitActor.getTasks();
console.log('All tasks:', allTasks);Agent synchronization
In case of time/certificate issues:
await rabbitActor.sync();API
RabbitMotokoActor Class
Constructor
new RabbitMotokoActor(canisterId: string)- Creates a new instance with the provided canister ID
Public Methods
sync(): Promise<void>- Synchronizes the agent time with the IC networkaddTask(args: AddTaskArgs): Promise<bigint>- Adds a new task to the queueclaimTask(args: ClaimTaskArgs): Promise<[] | [Task]>- Claims a task for a workercompleteTask(args: CompleteTaskArgs): Promise<boolean>- Completes a claimed taskgetAvailableTaskIds(channel: string): Promise<bigint[]>- Gets IDs of available tasks in a channelgetTasks(): Promise<Task[]>- Gets a list of all tasks
Types
The library uses types generated from the Rabbit Motoko canister:
AddTaskArgs- Arguments for adding a taskClaimTaskArgs- Arguments for claiming a taskCompleteTaskArgs- Arguments for completing a taskTask- Task structure
Requirements
- Node.js 16+
- TypeScript 4.5+
License
ISC
Author
@jsm-mit
