Skip to content

Commit

Permalink
refactor: move internal packages to pkg directory
Browse files Browse the repository at this point in the history
  • Loading branch information
prdnk committed Jan 6, 2025
1 parent c368f2b commit afd6b86
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 106 deletions.
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package vault
import (
"github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware"
"github.com/onsonr/motr/app/context"
motr "github.com/onsonr/motr/config"
motrorm "github.com/onsonr/motr/internal/models"
motr "github.com/onsonr/motr/pkg/config"
"github.com/onsonr/motr/pkg/context"
motrorm "github.com/onsonr/motr/pkg/models"
)

type Vault = *echo.Echo
Expand Down
4 changes: 2 additions & 2 deletions cmd/vault/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
vault "github.com/onsonr/motr/app"
motr "github.com/onsonr/motr/config"
motrorm "github.com/onsonr/motr/internal/models"
sink "github.com/onsonr/motr/internal/sink"
motr "github.com/onsonr/motr/pkg/config"
motrorm "github.com/onsonr/motr/pkg/models"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/pkl/App.pkl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@go.Package { name = "github.com/onsonr/motr/config" }
@go.Package { name = "github.com/onsonr/motr/pkg/config" }

module sonr.net.Motr

Expand Down
2 changes: 1 addition & 1 deletion internal/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ sql:
emit_interface: true
emit_json_tags: true
package: "models"
out: "../models"
out: "../pkg/models"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion embed/codec.go → pkg/embed/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"

"github.com/ipfs/boxo/files"
motr "github.com/onsonr/motr/config"
motr "github.com/onsonr/motr/pkg/config"
)

const SchemaVersion = 1
Expand Down
File renamed without changes.
82 changes: 44 additions & 38 deletions embed/main.js → pkg/embed/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ async function initWasmChannel() {
wasmPort.onmessage = (event) => {
const { type, data } = event.data;
switch (type) {
case 'WASM_READY':
console.log('WASM is ready');
document.dispatchEvent(new CustomEvent('wasm-ready'));
case "WASM_READY":
console.log("WASM is ready");
document.dispatchEvent(new CustomEvent("wasm-ready"));
break;
case 'RESPONSE':
case "RESPONSE":
handleWasmResponse(data);
break;
case 'SYNC_COMPLETE':
case "SYNC_COMPLETE":
handleSyncComplete(data);
break;
}
Expand All @@ -28,9 +28,9 @@ async function initWasmChannel() {
async function init() {
try {
// Register service worker
if ('serviceWorker' in navigator) {
const registration = await navigator.serviceWorker.register('./sw.js');
console.log('ServiceWorker registered');
if ("serviceWorker" in navigator) {
const registration = await navigator.serviceWorker.register("./sw.js");
console.log("ServiceWorker registered");

// Wait for the service worker to be ready
await navigator.serviceWorker.ready;
Expand All @@ -39,32 +39,35 @@ async function init() {
await initWasmChannel();

// Send the MessageChannel port to the service worker
navigator.serviceWorker.controller.postMessage({
type: 'PORT_INITIALIZATION',
port: wasmChannel.port2
}, [wasmChannel.port2]);
navigator.serviceWorker.controller.postMessage(
{
type: "PORT_INITIALIZATION",
port: wasmChannel.port2,
},
[wasmChannel.port2],
);

// Register for periodic sync if available
if ('periodicSync' in registration) {
if ("periodicSync" in registration) {
try {
await registration.periodicSync.register('wasm-sync', {
minInterval: 24 * 60 * 60 * 1000 // 24 hours
await registration.periodicSync.register("wasm-sync", {
minInterval: 24 * 60 * 60 * 1000, // 24 hours
});
} catch (error) {
console.log('Periodic sync could not be registered:', error);
console.log("Periodic sync could not be registered:", error);
}
}
}

// Initialize HTMX with custom config
htmx.config.withCredentials = true;
htmx.config.wsReconnectDelay = 'full-jitter';
htmx.config.wsReconnectDelay = "full-jitter";

// Override HTMX's internal request handling
htmx.config.beforeRequest = function (config) {
// Add request ID for tracking
const requestId = 'req_' + Date.now();
config.headers['X-Wasm-Request-ID'] = requestId;
const requestId = "req_" + Date.now();
config.headers["X-Wasm-Request-ID"] = requestId;

// If offline, handle through service worker
if (!navigator.onLine) {
Expand All @@ -80,19 +83,20 @@ async function init() {

// Handle HTMX errors
htmx.config.errorHandler = function (error) {
console.error('HTMX Error:', error);
console.error("HTMX Error:", error);
};

} catch (error) {
console.error('Initialization failed:', error);
console.error("Initialization failed:", error);
}
}

function handleWasmResponse(data) {
const { requestId, response } = data;
// Process the WASM response
// This might update the UI or trigger HTMX swaps
const targetElement = document.querySelector(`[data-request-id="${requestId}"]`);
const targetElement = document.querySelector(
`[data-request-id="${requestId}"]`,
);
if (targetElement) {
htmx.process(targetElement);
}
Expand All @@ -102,51 +106,53 @@ function handleSyncComplete(data) {
const { url } = data;
// Handle successful sync
// Maybe refresh the relevant part of the UI
htmx.trigger('body', 'sync:complete', { url });
htmx.trigger("body", "sync:complete", { url });
}

// Handle offline status changes
window.addEventListener('online', () => {
document.body.classList.remove('offline');
window.addEventListener("online", () => {
document.body.classList.remove("offline");
// Trigger sync when back online
if (wasmPort) {
wasmPort.postMessage({ type: 'SYNC_REQUEST' });
wasmPort.postMessage({ type: "SYNC_REQUEST" });
}
});

window.addEventListener('offline', () => {
document.body.classList.add('offline');
window.addEventListener("offline", () => {
document.body.classList.add("offline");
});

// Custom event handlers for HTMX
document.addEventListener('htmx:beforeRequest', (event) => {
document.addEventListener("htmx:beforeRequest", (event) => {
const { elt, xhr } = event.detail;
// Add request tracking
const requestId = xhr.headers['X-Wasm-Request-ID'];
elt.setAttribute('data-request-id', requestId);
const requestId = xhr.headers["X-Wasm-Request-ID"];
elt.setAttribute("data-request-id", requestId);
});

document.addEventListener('htmx:afterRequest', (event) => {
document.addEventListener("htmx:afterRequest", (event) => {
const { elt, successful } = event.detail;
if (successful) {
elt.removeAttribute('data-request-id');
elt.removeAttribute("data-request-id");
}
});

// Initialize everything when the page loads
document.addEventListener('DOMContentLoaded', init);
document.addEventListener("DOMContentLoaded", init);

// Export functions that might be needed by WASM
window.wasmBridge = {
triggerUIUpdate: function (selector, content) {
const target = document.querySelector(selector);
if (target) {
htmx.process(htmx.parse(content).forEach(node => target.appendChild(node)));
htmx.process(
htmx.parse(content).forEach((node) => target.appendChild(node)),
);
}
},

showNotification: function (message, type = 'info') {
showNotification: function (message, type = "info") {
// Implement notification system
console.log(`${type}: ${message}`);
}
},
};
Loading

0 comments on commit afd6b86

Please sign in to comment.