openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
66 lines (65 loc) • 1.86 kB
JavaScript
//#region src/plugins/plugin-instance-owned-values.ts
var PluginHostObject = class {
constructor(value) {
return value;
}
};
var PluginCallToken = class extends PluginHostObject {
#hostCleanup;
constructor(value, hostCleanup) {
super(value);
this.#hostCleanup = hostCleanup;
}
static isHostCleanup(token) {
return #hostCleanup in token && token.#hostCleanup;
}
};
/** Installed once in the shared instance singleton, including its private-field brand. */
function createPluginValueInstances() {
class OwnedValue extends PluginHostObject {
#state;
constructor(value, state) {
super(value);
this.#state = state;
}
static get(value) {
if (value === null || typeof value !== "object" && typeof value !== "function") return;
return #state in value ? value.#state : void 0;
}
}
const foreign = /* @__PURE__ */ new WeakMap();
const install = (value) => {
const current = OwnedValue.get(value);
if (current) return current;
const state = { instance: foreign.get(value) };
if (state.instance) foreign.delete(value);
new OwnedValue(value, state);
return state;
};
return {
get(value) {
return OwnedValue.get(value)?.instance ?? foreign.get(value);
},
set(value, instance) {
const owned = OwnedValue.get(value);
if (owned) owned.instance = instance;
else foreign.set(value, instance);
return this;
},
setHost(value, instance) {
install(value).instance = instance;
return this;
},
getOriginal(value, instance) {
const owned = OwnedValue.get(value);
return owned?.originalInstance === instance ? owned.original : void 0;
},
setOriginal(value, original, instance) {
const owned = install(value);
owned.originalInstance = instance;
owned.original = original;
}
};
}
//#endregion
export { PluginHostObject as n, createPluginValueInstances as r, PluginCallToken as t };