don't put database in vue reactive as it causes DataCloneError: Failed to execute 'postMessage' on 'BroadcastChannel'

This commit is contained in:
liamcottle
2025-02-14 03:52:11 +13:00
parent 9245382060
commit 54a5587462
3 changed files with 21 additions and 17 deletions

View File

@@ -4,22 +4,23 @@ import {getRxStorageDexie} from 'rxdb/plugins/storage-dexie';
import GlobalState from "./GlobalState.js"; import GlobalState from "./GlobalState.js";
import Utils from "./Utils.js"; import Utils from "./Utils.js";
var database = null;
async function initDatabase(publicKeyHex) { async function initDatabase(publicKeyHex) {
// close any exsiting database connection // close any exsiting database connection
if(GlobalState.database){ if(database){
await GlobalState.database.close(); await database.close();
} }
// create a database with a unique name per identity // create a database with a unique name per identity
GlobalState.database = await createRxDatabase({ database = await createRxDatabase({
name: `meshcore_companion_db_${publicKeyHex}`, name: `meshcore_companion_db_${publicKeyHex}`,
storage: getRxStorageDexie(), storage: getRxStorageDexie(),
allowSlowCount: true, allowSlowCount: true,
}); });
// add database schemas // add database schemas
await GlobalState.database.addCollections({ await database.addCollections({
messages: { messages: {
schema: { schema: {
version: 0, version: 0,
@@ -118,13 +119,16 @@ async function initDatabase(publicKeyHex) {
}, },
}); });
// database is now ready
GlobalState.isDatabaseReady = true;
} }
class Message { class Message {
// insert a message into the database // insert a message into the database
static async insert(data) { static async insert(data) {
return await GlobalState.database.messages.insert({ return await database.messages.insert({
id: v4(), id: v4(),
status: data.status, status: data.status,
to: Utils.bytesToHex(data.to), to: Utils.bytesToHex(data.to),
@@ -141,7 +145,7 @@ class Message {
} }
static async getMessageById(id) { static async getMessageById(id) {
return await GlobalState.database.messages.findOne({ return await database.messages.findOne({
selector: { selector: {
id: { id: {
$eq: id, $eq: id,
@@ -151,7 +155,7 @@ class Message {
} }
static async deleteMessageById(id) { static async deleteMessageById(id) {
return await GlobalState.database.messages.findOne({ return await database.messages.findOne({
selector: { selector: {
id: { id: {
$eq: id, $eq: id,
@@ -165,7 +169,7 @@ class Message {
// find one latest message by ack code // find one latest message by ack code
// this will prevent updating older messages that might have the same ack code // this will prevent updating older messages that might have the same ack code
const message = GlobalState.database.messages.findOne({ const message = database.messages.findOne({
selector: { selector: {
expected_ack_crc: { expected_ack_crc: {
$eq: ackCode, $eq: ackCode,
@@ -212,12 +216,12 @@ class Message {
// get all messages // get all messages
static getAllMessages() { static getAllMessages() {
return GlobalState.database.messages.find(); return database.messages.find();
} }
// get direct messages for the provided public key // get direct messages for the provided public key
static getContactMessages(publicKey) { static getContactMessages(publicKey) {
return GlobalState.database.messages.find({ return database.messages.find({
selector: { selector: {
$or: [ $or: [
// messages from us to other contact // messages from us to other contact
@@ -250,7 +254,7 @@ class Message {
// get unread direct messages count for the provided public key // get unread direct messages count for the provided public key
static getContactMessagesUnreadCount(publicKey, messagesLastReadTimestamp) { static getContactMessagesUnreadCount(publicKey, messagesLastReadTimestamp) {
return GlobalState.database.messages.count({ return database.messages.count({
selector: { selector: {
timestamp: { timestamp: {
$gt: messagesLastReadTimestamp, $gt: messagesLastReadTimestamp,
@@ -276,7 +280,7 @@ class ContactMessagesReadState {
// update the read state of messages for the provided public key // update the read state of messages for the provided public key
static async touch(publicKey) { static async touch(publicKey) {
return await GlobalState.database.contact_messages_read_state.upsert({ return await database.contact_messages_read_state.upsert({
id: Utils.bytesToHex(publicKey), id: Utils.bytesToHex(publicKey),
timestamp: Date.now(), timestamp: Date.now(),
}); });
@@ -284,7 +288,7 @@ class ContactMessagesReadState {
// get the read state of messages for the provided public key // get the read state of messages for the provided public key
static get(publicKey) { static get(publicKey) {
return GlobalState.database.contact_messages_read_state.findOne({ return database.contact_messages_read_state.findOne({
selector: { selector: {
id: { id: {
$eq: Utils.bytesToHex(publicKey), $eq: Utils.bytesToHex(publicKey),
@@ -299,7 +303,7 @@ class ChannelMessage {
// insert a channel message into the database // insert a channel message into the database
static async insert(data) { static async insert(data) {
return await GlobalState.database.channel_messages.insert({ return await database.channel_messages.insert({
id: v4(), id: v4(),
channel_idx: data.channel_idx, channel_idx: data.channel_idx,
from: data.from != null ? Utils.bytesToHex(data.from) : null, from: data.from != null ? Utils.bytesToHex(data.from) : null,
@@ -313,7 +317,7 @@ class ChannelMessage {
// get channel messages for the provided channel idx // get channel messages for the provided channel idx
static getChannelMessages(channelIdx) { static getChannelMessages(channelIdx) {
return GlobalState.database.channel_messages.find({ return database.channel_messages.find({
selector: { selector: {
channel_idx: { channel_idx: {
$eq: channelIdx, $eq: channelIdx,

View File

@@ -3,7 +3,7 @@ import {reactive} from "vue";
// global state // global state
const globalState = reactive({ const globalState = reactive({
connection: null, connection: null,
database: null, isDatabaseReady: false,
selfInfo: null, selfInfo: null,
contacts: [], contacts: [],
channels: [ channels: [

View File

@@ -8,7 +8,7 @@ import GlobalState from "./js/GlobalState.js";
// helper function that force redirects to the main page if there is no database connection // helper function that force redirects to the main page if there is no database connection
function handleRouteThatRequiresDatabase() { function handleRouteThatRequiresDatabase() {
if(!GlobalState.database){ if(!GlobalState.isDatabaseReady){
return { return {
name: 'main', name: 'main',
}; };