Monday, May 27, 2024
HomeIOS Developmentjavascript - Make iOS Capacitor CAPPluginCall notifyListener to attend for reactjs async...

javascript – Make iOS Capacitor CAPPluginCall notifyListener to attend for reactjs async indexdb “put” name


I’ve a capacitor plugin to bridge Swift and Reactjs, principally to run a PWA on an iOS machine, however some a part of the code is written in Swift on this app, thus once I change from Reactjs to Swift, the javascript major thread is blocked by swiftui screens, and all of the background async process in javascript involves a halt. Right here in Swift, I’ve outlined some listeners utilizing the capacitor notifyListener operate, which sends notifications to Reactjs when some occasion happens in Swift, one such listener is answerable for setting some information within the Reactjs indexedDB, however because the major thread is blocked (in accordance with my information), that indexedDB updation async name is thrown into some queue, and I don’t get any response again from the DB, however once I shut the swiftui screens and are available again to the Reactjs code, the updation occurs and I get the specified response.

So my query right here is, is there a way by which I can wait in Swiftui, until that DB async name finishes and I can get the outcome on the similar time with out closing the Swiftui screens and coming again once more?

Right here is the pattern code to your reference. Any assist can be appreciated.

Listener in swift –

do {
      strive activationScheduler?.startActivationTimer()
      self.notifyListeners(self.onTripStart, information: ["transactionId": options.booking.id])
   } catch {
      debugPrint("Failed to begin activation timer: (error)")
   }

Code in Reactjs which runs when notification is raised from swift –

useEffect(() => {
    const activationFlowTripStartListener = ActivationFlowPlugin.addListener('onTripStart', (eventData) => {
      handleSetPassAndTicketWithDB(eventData);
    });

    return () => {
      activationFlowTripStartListener?.take away();
    };
  }, [userId]);

const handleSetPassAndTicketWithDB = useCallback(async (passData) => {
    const decryptkey = getKeyForEncryptAndDecrypt(userId);
    const { get } = IndexDB(objectStoreKeys.myPassHistory);
    let go = await get(passData?.transactionId, decryptkey);
    const newCachedPassData = {
      ...go,
      ...go?.cachedPass,
      isContactLessValidationVisited: true,
      isRidePresent: true,
    };
    await setUserPass(newCachedPassData);
  }, [userId, setUserPass]);

Additionally another factor as you possibly can see right here, I’m “getting” and “setting” information within the IndexedDB. The get operate is working accurately, the difficulty arises solely once I use the set operate of the IndexedDB object.

The await setUserPass operate name above calls this operate to replace the indexedDB –

const setHistoryData = useCallback(async (newData) => {
    if (newData?.transactionId) {
      const decryptkey = getKeyForEncryptAndDecrypt(phoneNumber);
      const { set } = IndexDB(objectStoreKeys.myPassHistory);
      await set(undefined, { newData }, decryptkey);
    }
    return 0;
  }, [phoneNumber]);

And right here is the set async operate of the IndexedDB object –

set: async operate set(key, val, encryptKey) {
    const encryptData = encryptObject(val, encryptKey);
    return (await dbPromise).put(`${objectStore}`, encryptData, key);
  },
get: async operate get(key, decryptKey) {
    const information = await (await dbPromise).get(`${objectStore}`, key);
    return decryptObject(information, decryptKey);
  },

RELATED ARTICLES

Most Popular

Recent Comments