capacitor-community / sqlite

⚡Capacitor plugin for native & electron SQLite databases.
MIT License
492 stars 117 forks source link

WebWorkers #360

Closed bokolob closed 1 year ago

bokolob commented 1 year ago

Hello everyone. Is there a solution to run web version inside worker?

jepiqueau commented 1 year ago

@bokolob it should be but i did'nt look at it. May be you should be looking at sql.js and see how you can Apple this to the Web plugin

fegauthier commented 1 year ago

Hi!

I'm using sqlite plugin with Ionic Framework for Android. It's working well, but when I want to use the plugin inside a Web Worker. Nothing is working. Is it because it's not possible to use Capacitor plugin inside a Web Worker?

This is my Web Worker code.

import SQLite from '@/databases';
import CategoriesTable from "@/databases/categories.table";
import ConfigTable from "@/databases/config.table";
import { StalkerPortal } from "stalker-portal-capacitor";
import moment from 'moment';
import Promise from 'bluebird';

onmessage = (event) => {
  if(event.data.message === 'start') {
    getCategories(event.data.data);
  }
}

const getCategories = async (data: any) => {
  await SQLite.getInstance().init();
  let theCategories = [];
  //await ConfigTable.deleteByName('LAST_SYNC_CATEGORIES')
  const lastSync = await ConfigTable.findByName('LAST_SYNC_CATEGORIES');

    if(!lastSync) {
      await CategoriesTable.truncate();
      const categoriesString: string = (await StalkerPortal.getCategories({ 
        headers: {},
        cookies: { 
          mac: data.mac, 
          timezone: data.timezone
        },
        data: {
          baseURL: data.portal
        }
      })).value;
      theCategories = JSON.parse(categoriesString).js;
      theCategories.unshift(
        {
          id: 'FAVOURITE',
          title: 'Favourite'
        }
      );
      postMessage({ 
        message: 'progress', 
        data: { 
          total: theCategories.length,
          current: 0,
          step: 'FETCHING_CATEGORIES'
        }
      });
      await Promise.each(theCategories, async (category: any, index: number) => {
        await CategoriesTable.insert(category.id, category.title);
        postMessage({ 
          message: 'progress', 
          data: { 
            current: index + 1,
          }
        })
      });
      await ConfigTable.insertOrReplace('LAST_SYNC_CATEGORIES', moment().format('YYYY-MM-DD HH:mm:ss'));
    }

    postMessage({ 
      message: 'progress', 
      data: { 
        total: theCategories.length,
        current: 0,
        step: 'PROCESSING_CATEGRORIES'
      }
    });
    theCategories = await CategoriesTable.getAll();
    postMessage({ message: 'complete', data: theCategories });
}

It is working on the main thread but when I use it inside the Web Worker, No call is transferred to the Android Native. I got this error message too

Line 349 - Msg: Uncaught (in promise) Error: The jeep-sqlite element is not present in the DOM! Please check the @capacitor-community/sqlite documentation for instructions regarding the web platform.

My own plugin doesn't work either. So I guess that capacitor plugin are not working inside a Web Worker? Or I missed a configuration?

Thanks!

jepiqueau commented 1 year ago

@fegauthier from my understanding of Capacitor, capacitor plugins are not working on web worker but you can ask your question to the capacitor team by raisins an issue and you will have an official answer. Thanks anyhow for using the plugin.

fegauthier commented 1 year ago

@jepiqueau Thanks for your answer. That's what I did too.

https://github.com/ionic-team/capacitor/issues/6309

jepiqueau commented 1 year ago

@fegauthier Did you provide a Code reproduction to the Ionic team as requested

fegauthier commented 1 year ago

@jepiqueau I will do it as soon as I have a little bit of time. Thanks!

fegauthier commented 1 year ago

@jepiqueau I answered on the capacitorjs github. What I found is that the platform detected when using WebWorker is web instead of Android.

jepiqueau commented 1 year ago

@fegauthier means that you cannot use it for native platforms. Which seems pretty logical. I think you should close this issue

fegauthier commented 1 year ago

@fegauthier means that you cannot use it for native platforms. Which seems pretty logical. I think you should close this issue

Yeah! That's what i'm guessing too. Thanks for your time!