@metamask/ocap-kernel
Version:
OCap kernel core components
1 lines • 3.12 kB
Source Map (JSON)
{"version":3,"file":"queue.cjs","sourceRoot":"","sources":["../../../src/store/methods/queue.ts"],"names":[],"mappings":";;AAWA,0CAyDC;AAjED;;;;;;GAMG;AACH,4EAA4E;AAC5E,SAAgB,eAAe,CAAC,GAAiB;IAC/C;;;;;;OAMG;IACH,SAAS,cAAc,CAAC,SAAiB;QACvC,MAAM,EAAE,GAAG,SAAS,SAAS,EAAE,CAAC;QAChC,MAAM,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACtC,MAAM,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACtC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YAC7C,MAAM,KAAK,CAAC,iBAAiB,SAAS,EAAE,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,SAAS,UAAU,CAAC,OAAqB;QACvC,GAAG,CAAC,mBAAmB,IAAI,CAAC,CAAC;QAC7B,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAED;;;;;OAKG;IACH,SAAS,UAAU;QACjB,GAAG,CAAC,mBAAmB,IAAI,CAAC,CAAC;QAC7B,OAAO,GAAG,CAAC,QAAQ,CAAC,OAAO,EAA8B,CAAC;IAC5D,CAAC;IAED;;;;OAIG;IACH,SAAS,cAAc;QACrB,IAAI,GAAG,CAAC,mBAAmB,GAAG,CAAC,EAAE,CAAC;YAChC,GAAG,CAAC,mBAAmB,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;QAClD,CAAC;QACD,OAAO,GAAG,CAAC,mBAAmB,CAAC;IACjC,CAAC;IAED,OAAO;QACL,cAAc;QACd,UAAU;QACV,UAAU;QACV,cAAc;KACf,CAAC;AACJ,CAAC","sourcesContent":["import type { RunQueueItem } from '../../types.ts';\nimport type { StoreContext } from '../types.ts';\n\n/**\n * Get a queue store object that provides functionality for managing queues.\n *\n * @param ctx - The store context.\n * @returns A queue store object that maps various persistent kernel data\n * structures onto `kv`.\n */\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport function getQueueMethods(ctx: StoreContext) {\n /**\n * Find out how long some queue is.\n *\n * @param queueName - The name of the queue of interest.\n *\n * @returns the number of items in the given queue.\n */\n function getQueueLength(queueName: string): number {\n const qk = `queue.${queueName}`;\n const head = ctx.kv.get(`${qk}.head`);\n const tail = ctx.kv.get(`${qk}.tail`);\n if (head === undefined || tail === undefined) {\n throw Error(`unknown queue ${queueName}`);\n }\n return Number(head) - Number(tail);\n }\n\n /**\n * Append a message to the kernel's run queue.\n *\n * @param message - The message to enqueue.\n */\n function enqueueRun(message: RunQueueItem): void {\n ctx.runQueueLengthCache += 1;\n ctx.runQueue.enqueue(message);\n }\n\n /**\n * Fetch the next message on the kernel's run queue.\n *\n * @returns The next message on the run queue, or undefined if the queue is\n * empty.\n */\n function dequeueRun(): RunQueueItem | undefined {\n ctx.runQueueLengthCache -= 1;\n return ctx.runQueue.dequeue() as RunQueueItem | undefined;\n }\n\n /**\n * Obtain the number of entries in the run queue.\n *\n * @returns the number of items in the run queue.\n */\n function runQueueLength(): number {\n if (ctx.runQueueLengthCache < 0) {\n ctx.runQueueLengthCache = getQueueLength('run');\n }\n return ctx.runQueueLengthCache;\n }\n\n return {\n getQueueLength,\n enqueueRun,\n dequeueRun,\n runQueueLength,\n };\n}\n"]}