primal extensions: auto-loading, additional hooks, props fix

This commit is contained in:
pritk 2023-09-07 09:27:42 +00:00 committed by Bojan Mojsilovic
parent 66ad7a7909
commit 4c06fd1534
4 changed files with 29 additions and 3 deletions

View File

@ -74,6 +74,20 @@ const Router: Component = () => {
primalWindow.onPrimalComponentCleanup = () => {}; primalWindow.onPrimalComponentCleanup = () => {};
primalWindow.onPrimalCacheServerConnected = () => {}; primalWindow.onPrimalCacheServerConnected = () => {};
primalWindow.onPrimalUploadServerConnected = () => {}; primalWindow.onPrimalUploadServerConnected = () => {};
primalWindow.onPrimalCacheServerMessageReceived = () => {};
primalWindow.onPrimalCacheServerMessageSent = () => {};
try {
const exts = localStorage.getItem('primalExtensions');
if (exts) {
for (const url of JSON.parse(exts)) {
console.log("loading primal extension:", url);
const el = window.document.createElement("script");
el.setAttribute("src", url);
window.document.body.appendChild(el);
}
}
} catch (ex) { console.log(ex); }
} }
const getKnownProfiles = ({ params }: RouteDataFuncArgs) => { const getKnownProfiles = ({ params }: RouteDataFuncArgs) => {

View File

@ -23,7 +23,11 @@ export const hookForDev = (fn: Function) => {
hook('onPrimalComponentCleanup', scope); hook('onPrimalComponentCleanup', scope);
}) })
props.id = domId; try {
props.id = domId;
} catch (ex) {
console.log(ex);
}
return fn(props); return fn(props);
}; };

View File

@ -11,8 +11,12 @@ const onOpen = () => {
setConnected(true); setConnected(true);
if (localStorage.getItem('devMode') === 'true') { if (localStorage.getItem('devMode') === 'true') {
const hook = (window as PrimalWindow).onPrimalCacheServerConnected; const hook = (window as PrimalWindow).onPrimalCacheServerConnected;
hook && hook(cacheServer, socket()); hook && hook(cacheServer, socket());
socket().addEventListener('message', function(event) {
const hook = (window as PrimalWindow).onPrimalCacheServerMessageReceived;
hook && hook(cacheServer, event.data);
});
} }
} }
@ -63,6 +67,9 @@ export const sendMessage = (message: string) => {
const e = new CustomEvent('send', { detail: { message, ws: socket() }}); const e = new CustomEvent('send', { detail: { message, ws: socket() }});
socket()?.send(message); socket()?.send(message);
socket()?.dispatchEvent(e); socket()?.dispatchEvent(e);
const hook = (window as PrimalWindow).onPrimalCacheServerMessageSent;
hook && hook(cacheServer, message);
} }
} }
@ -103,7 +110,6 @@ export const subscribeTo = (subId: string, cb: (type: NostrEventType, subId: str
if (subId === subscriptionId) { if (subId === subscriptionId) {
cb(type, subscriptionId, content); cb(type, subscriptionId, content);
} }
}; };
socket()?.addEventListener('message', listener); socket()?.addEventListener('message', listener);

View File

@ -344,6 +344,8 @@ export type PrimalWindow = Window & typeof globalThis & {
onPrimalComponentCleanup?: (data: any) => void, onPrimalComponentCleanup?: (data: any) => void,
onPrimalCacheServerConnected?: (url: string, ws: WebSocket | undefined) => void, onPrimalCacheServerConnected?: (url: string, ws: WebSocket | undefined) => void,
onPrimalUploadServerConnected?: (url: string, ws: WebSocket | undefined) => void, onPrimalUploadServerConnected?: (url: string, ws: WebSocket | undefined) => void,
onPrimalCacheServerMessageReceived?: (url: string, data: any) => void,
onPrimalCacheServerMessageSent?: (url: string, data: any) => void,
}; };
export type NostrEventType = "EVENT" | "EOSE" | "NOTICE"; export type NostrEventType = "EVENT" | "EOSE" | "NOTICE";