初始提交
This commit is contained in:
2
ai/static/src/utils/ai_channel_bus.js
Normal file
2
ai/static/src/utils/ai_channel_bus.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import { EventBus } from "@odoo/owl";
|
||||
export const aiChannelBus = new EventBus();
|
||||
18
ai/static/src/utils/ai_channel_data_registry.js
Normal file
18
ai/static/src/utils/ai_channel_data_registry.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export class AIChannelDataRegistry {
|
||||
constructor() {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
setData(channelId, key, value) {
|
||||
this.data[channelId] = this.data[channelId] || {};
|
||||
this.data[channelId][key] = value;
|
||||
}
|
||||
getData(channelId, key) {
|
||||
return this.data[channelId][key];
|
||||
}
|
||||
removeData(channelId) {
|
||||
delete this.data[channelId];
|
||||
}
|
||||
}
|
||||
|
||||
export const aiChannelDataRegistry = new AIChannelDataRegistry();
|
||||
4
ai/static/src/utils/ai_session_identifier.js
Normal file
4
ai/static/src/utils/ai_session_identifier.js
Normal file
@@ -0,0 +1,4 @@
|
||||
import { uuid } from "@web/core/utils/strings";
|
||||
|
||||
// Identify this page instance independently of the shared login session.
|
||||
export const aiSessionIdentifier = uuid();
|
||||
44
ai/static/src/utils/bus_data_getter.js
Normal file
44
ai/static/src/utils/bus_data_getter.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { useBus } from "@web/core/utils/hooks";
|
||||
import { aiChannelBus } from "./ai_channel_bus";
|
||||
|
||||
export const triggers = new Set(["view", "mediaDialog"]);
|
||||
|
||||
export async function getData(data_source) {
|
||||
if (!triggers.has(data_source)) {
|
||||
throw new Error(`Invalid data source ${data_source}`);
|
||||
}
|
||||
const requestTrigger = getRequestTrigger(data_source);
|
||||
const sendTrigger = getSendTrigger(data_source);
|
||||
return new Promise((resolve) => {
|
||||
const listener = ({ detail }) => {
|
||||
aiChannelBus.removeEventListener(sendTrigger, listener);
|
||||
clearTimeout(timeout);
|
||||
resolve(detail);
|
||||
};
|
||||
const timeout = setTimeout(() => {
|
||||
aiChannelBus.removeEventListener(sendTrigger, listener);
|
||||
resolve(null);
|
||||
}, 100);
|
||||
aiChannelBus.addEventListener(sendTrigger, listener);
|
||||
aiChannelBus.trigger(requestTrigger);
|
||||
});
|
||||
}
|
||||
|
||||
export function useDataGetter(data_source, getter) {
|
||||
if (!triggers.has(data_source)) {
|
||||
throw new Error(`Invalid data source ${data_source}`);
|
||||
}
|
||||
const requestTrigger = getRequestTrigger(data_source);
|
||||
const sendTrigger = getSendTrigger(data_source);
|
||||
useBus(aiChannelBus, requestTrigger, async () => {
|
||||
aiChannelBus.trigger(sendTrigger, await getter());
|
||||
});
|
||||
}
|
||||
|
||||
function getRequestTrigger(data_source) {
|
||||
return `${data_source}?`;
|
||||
}
|
||||
|
||||
function getSendTrigger(data_source) {
|
||||
return `${data_source}!`;
|
||||
}
|
||||
Reference in New Issue
Block a user