@metamask/ocap-kernel
Version:
OCap kernel core components
1 lines • 3.92 kB
Source Map (JSON)
{"version":3,"file":"pinned.mjs","sourceRoot":"","sources":["../../../src/store/methods/pinned.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,uBAAsB;AAInD;;;;;GAKG;AACH,SAAS,UAAU,CAAC,MAAc,EAAE;IAClC,OAAO,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACnC,CAAC;AAED;;;;;GAKG;AACH,4EAA4E;AAC5E,MAAM,UAAU,aAAa,CAAC,GAAiB;IAC7C,MAAM,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAEzE;;;;;OAKG;IACH,SAAS,SAAS,CAAC,IAAU;QAC3B,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC;QACxD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC/B,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,eAAe,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IACxD,CAAC;IAED;;;;;;OAMG;IACH,SAAS,WAAW,CAAC,IAAU;QAC7B,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC;QACxD,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YACjC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;YACzC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,eAAe,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,SAAS,gBAAgB;QACvB,OAAO,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC;IACjD,CAAC;IAED;;;;;OAKG;IACH,SAAS,cAAc,CAAC,IAAU;QAChC,OAAO,gBAAgB,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO;QACL,SAAS;QACT,WAAW;QACX,gBAAgB;QAChB,cAAc;KACf,CAAC;AACJ,CAAC","sourcesContent":["import { getRefCountMethods } from './refcount.ts';\nimport type { KRef } from '../../types.ts';\nimport type { StoreContext } from '../types.ts';\n\n/**\n * Split a comma-separated string into an array.\n *\n * @param str - The string to split.\n * @returns An array of strings.\n */\nfunction commaSplit(str: string = ''): string[] {\n return str ? str.split(',') : [];\n}\n\n/**\n * Create a pinned store that provides high-level functionality for managing pinned objects.\n *\n * @param ctx - The store context.\n * @returns A pinned store with functions for pinning/unpinning objects and managing pinned objects.\n */\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nexport function getPinMethods(ctx: StoreContext) {\n const { incrementRefCount, decrementRefCount } = getRefCountMethods(ctx);\n\n /**\n * Pin a kernel object to prevent it from being garbage collected.\n * Multiple calls will increment the pin count for the object.\n *\n * @param kref - The KRef of the object to pin.\n */\n function pinObject(kref: KRef): void {\n const pinList = commaSplit(ctx.kv.get('pinnedObjects'));\n pinList.push(kref);\n incrementRefCount(kref, 'pin');\n ctx.kv.set('pinnedObjects', pinList.sort().join(','));\n }\n\n /**\n * Unpin a kernel object, allowing it to be garbage collected if no other references exist.\n * Each call decrements the pin count for the object. The object is only fully unpinned\n * when all pins are removed.\n *\n * @param kref - The KRef of the object to unpin.\n */\n function unpinObject(kref: KRef): void {\n const pinList = commaSplit(ctx.kv.get('pinnedObjects'));\n if (pinList.includes(kref)) {\n decrementRefCount(kref, 'unpin');\n pinList.splice(pinList.indexOf(kref), 1);\n ctx.kv.set('pinnedObjects', pinList.sort().join(','));\n }\n }\n\n /**\n * Get all pinned objects.\n *\n * @returns An array of KRefs for all pinned objects.\n */\n function getPinnedObjects(): KRef[] {\n return commaSplit(ctx.kv.get('pinnedObjects'));\n }\n\n /**\n * Check if an object is pinned.\n *\n * @param kref - The KRef of the object to check.\n * @returns True if the object is pinned, false otherwise.\n */\n function isObjectPinned(kref: KRef): boolean {\n return getPinnedObjects().includes(kref);\n }\n\n return {\n pinObject,\n unpinObject,\n getPinnedObjects,\n isObjectPinned,\n };\n}\n"]}