Skip to content

Commit a75bd9f

Browse files
committed
Included experimental support with Deno
1 parent 474025c commit a75bd9f

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

packages/database/src/core/util/util.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,20 @@ export const setTimeoutNonBlocking = function (
618618
time: number
619619
): number | object {
620620
const timeout: number | object = setTimeout(fn, time);
621-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
622-
if (typeof timeout === 'object' && (timeout as any)['unref']) {
621+
// @ts-ignore Deno and unrefTimer are only defined in Deno environments.
622+
// Note: at the time of this comment, unrefTimer is under the unstable set of APIs. Run with --unstable to enable the API.
623+
if (
624+
typeof timeout === 'number' &&
625+
typeof Deno !== 'undefined' &&
626+
Deno['unrefTimer']
627+
) {
628+
// @ts-ignore Deno and unrefTimer are only defined in Deno environments.
629+
Deno.unrefTimer(timeout);
630+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
631+
} else if (typeof timeout === 'object' && (timeout as any)['unref']) {
623632
// eslint-disable-next-line @typescript-eslint/no-explicit-any
624633
(timeout as any)['unref']();
625634
}
635+
626636
return timeout;
627637
};

packages/database/test/deno.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect, use } from 'chai';
2+
import * as sinon from 'sinon';
3+
import sinonChai from 'sinon-chai';
4+
5+
import { setTimeoutNonBlocking } from '../src/core/util/util';
6+
use(sinonChai);
7+
describe('Deno tests', () => {
8+
let oldSetTimeout;
9+
beforeEach(() => {
10+
oldSetTimeout = globalThis.setTimeout;
11+
});
12+
afterEach(() => {
13+
globalThis.setTimeout = oldSetTimeout;
14+
});
15+
it('should call the deno unrefTimer() if in Deno', () => {
16+
// @ts-ignore override nodejs behavior
17+
global.Deno = {
18+
unrefTimer: sinon.spy()
19+
};
20+
// @ts-ignore override nodejs behavior
21+
global.setTimeout = () => 1;
22+
setTimeoutNonBlocking(() => {}, 0);
23+
expect(globalThis.Deno.unrefTimer).to.have.been.called;
24+
});
25+
it('should not call the deno unrefTimer() if not in Deno', () => {
26+
// @ts-ignore override nodejs behavior
27+
global.Deno2 = {
28+
unrefTimer: sinon.spy()
29+
};
30+
// @ts-ignore override node.js behavior
31+
global.setTimeout = () => 1;
32+
setTimeoutNonBlocking(() => {}, 0);
33+
expect(globalThis.Deno2.unrefTimer).to.not.have.been.called;
34+
});
35+
});

scripts/emulator-testing/emulators/database-emulator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as request from 'request';
1919

2020
import { Emulator } from './emulator';
2121

22-
import * as rulesJSON from '../../../config/database.rules.json';
22+
import rulesJSON from '../../../config/database.rules.json';
2323

2424
export class DatabaseEmulator extends Emulator {
2525
namespace: string;

0 commit comments

Comments
 (0)