Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
how to create a runner function in Typescript to call realm write only if it's not within a transaction?

I'm using multiple providers to deal with my realm so they need to make sure non of the other providers does actually manipulate the realm.

Such a check can look like follows:

  const createNode = (
    type: NodeType,
    name?: string,
    value?: string,
  ): Node & Realm.Object => {
    const realm = realmRef.current;
     
    (function writeItemLoop(i) {
      setTimeout((): Node & Realm.Object => {
        if (!realm.isInTransaction) {
          let node: Node & Realm.Object;
          realm.write(() => {
            node = realm.create<Node>('Node', {
              type: type,
              name: name,
              value: value,
            });
          });
          return node;
        } else {
          console.log(`realm was busy ${i}`);
          if (++i <= 10) {
            writeItemLoop(i);
          } else {
            console.log('canceling transaction');
            realm.cancelTransaction();
            i = 1;
            writeItemLoop(i);
          }
        }
      }, 1000);
    })(1);
  };

1.th question is: how to tell Typescript to ignore the warning:

Not all code paths return a value.ts(7030)

since once the realm is out of its transaction the result will be as declared.

  1. How to write a wrapper function with generics to wrap the inner transaction as callback as follows:

Inner callback:

let node: Node & Realm.Object;
realm.write(() => {
    node = realm.create<Node>('Node', {
    type: type,
    name: name,
    value: value,
  });
});
return Node;

Wrapper:

const realm = realmRef.current;  
(function writeItemLoop(j) {
  setTimeout((): Node & Realm.Object => {
    if (!realm.isInTransaction) {
      return callback(...args);
    } else {
      console.log(`realm was busy ${i}`);
      if (++i <= 10) {
        writeItemLoop(i);
      } else {
        console.log('canceling transaction');
        realm.cancelTransaction();
        i = 1;
        writeItemLoop(i);
      }
    }
  }, 1000);
})(1);

Thanks for any advices!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!