Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda