Browse Source

chore: adds a "waitAsync" utility

e22m4u 2 years ago
parent
commit
b79bba989b
4 changed files with 62 additions and 4 deletions
  1. 3 4
      src/mongodb-adapter.js
  2. 1 0
      src/utils/index.js
  3. 21 0
      src/utils/wait-async.js
  4. 37 0
      src/utils/wait-async.spec.js

+ 3 - 4
src/mongodb-adapter.js

@@ -1,6 +1,7 @@
 /* eslint no-unused-vars: 0 */
 import {ObjectId} from 'mongodb';
 import {MongoClient} from 'mongodb';
+import {waitAsync} from './utils/index.js';
 import {isObjectId} from './utils/index.js';
 import {Adapter} from '@e22m4u/js-repository';
 import {DataType} from '@e22m4u/js-repository';
@@ -155,8 +156,7 @@ export class MongodbAdapter extends Adapter {
    */
   async connect() {
     if (this._connecting) {
-      const tryAgainAfter = 500;
-      await new Promise(r => setTimeout(() => r(), tryAgainAfter));
+      await waitAsync(500);
       return this.connect();
     }
 
@@ -207,8 +207,7 @@ export class MongodbAdapter extends Adapter {
    */
   async disconnect() {
     if (this._connecting) {
-      const tryAgainAfter = 500;
-      await new Promise(r => setTimeout(() => r(), tryAgainAfter));
+      await waitAsync(500);
       return this.disconnect();
     }
     if (!this._connected) return;

+ 1 - 0
src/utils/index.js

@@ -1,3 +1,4 @@
+export * from './wait-async.js';
 export * from './is-object-id.js';
 export * from './create-mongodb-url.js';
 export * from './transform-values-deep.js';

+ 21 - 0
src/utils/wait-async.js

@@ -0,0 +1,21 @@
+import {InvalidArgumentError} from '@e22m4u/js-repository';
+
+/**
+ * Wait.
+ *
+ * @example
+ * ```ts
+ * await wait(1000); // 1sec
+ * ```
+ *
+ * @param {number} ms Milliseconds
+ * @returns {Promise<undefined>}
+ */
+export function waitAsync(ms) {
+  if (typeof ms !== 'number')
+    throw new InvalidArgumentError(
+      'The first argument of "waitAsync" must be' + ' a Number, but %v given.',
+      ms,
+    );
+  return new Promise(r => setTimeout(() => r(), ms));
+}

+ 37 - 0
src/utils/wait-async.spec.js

@@ -0,0 +1,37 @@
+import {expect} from 'chai';
+import {format} from '@e22m4u/js-format';
+import {waitAsync} from './wait-async.js';
+
+describe('wait', function () {
+  it('requires the first argument as a number', function () {
+    const throwable = v => () => waitAsync(v);
+    const error = v =>
+      format(
+        'The first argument of "waitAsync" must be a Number, but %s given.',
+        v,
+      );
+    expect(throwable('string')).to.throw(error('"string"'));
+    expect(throwable('')).to.throw(error('""'));
+    expect(throwable(true)).to.throw(error('true'));
+    expect(throwable(false)).to.throw(error('false'));
+    expect(throwable([])).to.throw(error('Array'));
+    expect(throwable({})).to.throw(error('Object'));
+    expect(throwable(undefined)).to.throw(error('undefined'));
+    expect(throwable(null)).to.throw(error('null'));
+    throwable(10)();
+    throwable(0)();
+  });
+
+  it('returns a promise that resolves after given milliseconds', async function () {
+    const startTime = new Date();
+    const delayMs = 15;
+    const accuracyMs = 5;
+    const promise = waitAsync(delayMs);
+    expect(promise).to.be.instanceof(Promise);
+    await promise;
+    const endTime = new Date();
+    const duration = endTime - startTime;
+    expect(duration).to.be.gte(delayMs - accuracyMs);
+    expect(duration).to.be.lte(delayMs + accuracyMs);
+  });
+});