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.
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!