Skip to content

Commit 2d55342

Browse files
committed
Added util function
1 parent b4771e2 commit 2d55342

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

common/api-review/util.api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ export interface Subscribe<T> {
417417
(observer: PartialObserver<T>): Unsubscribe;
418418
}
419419

420+
// Warning: (ae-missing-release-tag) "timeoutResolve" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
421+
//
422+
// @public
423+
export function timeoutResolve<T>(promise: Promise<T>, timeInMS?: number): Promise<T>;
424+
420425
// Warning: (ae-missing-release-tag) "Unsubscribe" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
421426
//
422427
// @public (undocumented)

packages/database-compat/test/query.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { timeoutResolve } from '@firebase/util';
1819
import { expect, use } from 'chai';
1920
import chaiAsPromised from 'chai-as-promised';
2021
import * as _ from 'lodash';
@@ -29,7 +30,7 @@ import {
2930
} from '../../database/test/helpers/EventAccumulator';
3031
import { DataSnapshot, Query, Reference } from '../src/api/Reference';
3132

32-
import { getFreshRepo, getPath, getRandomNode, pause, timeoutResolve } from './helpers/util';
33+
import { getFreshRepo, getPath, getRandomNode, pause } from './helpers/util';
3334

3435
use(chaiAsPromised);
3536

packages/database/test/exp/integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// eslint-disable-next-line import/no-extraneous-dependencies
1919
import { initializeApp, deleteApp } from '@firebase/app';
20-
import { Deferred } from '@firebase/util';
20+
import { Deferred, timeoutResolve } from '@firebase/util';
2121
import { expect, use } from 'chai';
2222
import chaiAsPromised from 'chai-as-promised';
2323

@@ -33,7 +33,7 @@ import {
3333
runTransaction
3434
} from '../../src/index';
3535
import { EventAccumulatorFactory } from '../helpers/EventAccumulator';
36-
import { DATABASE_ADDRESS, DATABASE_URL, timeoutResolve } from '../helpers/util';
36+
import { DATABASE_ADDRESS, DATABASE_URL } from '../helpers/util';
3737

3838
use(chaiAsPromised);
3939

packages/database/test/helpers/util.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { Deferred } from '@firebase/util';
19-
2018
import { ConnectionTarget } from '../../src/api/test_access';
2119

2220
// eslint-disable-next-line @typescript-eslint/no-require-imports
@@ -72,11 +70,3 @@ export function shuffle(arr, randFn = Math.random) {
7270
arr[j] = tmp;
7371
}
7472
}
75-
76-
export function timeoutResolve<T>(promise: Promise<T>, timeInMS = 2000) {
77-
const deferredPromise = new Deferred<T>();
78-
setTimeout(() => deferredPromise.reject('timeout!'), timeInMS);
79-
promise.then(deferredPromise.resolve, deferredPromise.reject);
80-
return deferredPromise.promise;
81-
}
82-

packages/util/index.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export * from './src/errors';
3131
export * from './src/json';
3232
export * from './src/jwt';
3333
export * from './src/obj';
34+
export * from './src/promise';
3435
export * from './src/query';
3536
export * from './src/sha1';
3637
export * from './src/subscribe';

packages/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export * from './src/errors';
2626
export * from './src/json';
2727
export * from './src/jwt';
2828
export * from './src/obj';
29+
export * from './src/promise';
2930
export * from './src/query';
3031
export * from './src/sha1';
3132
export * from './src/subscribe';

packages/util/src/promise.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { Deferred } from "./deferred";
19+
20+
/*
21+
Rejects if the given promise doesn't resolve in timeInMS milliseconds.
22+
@internal
23+
*/
24+
export function timeoutResolve<T>(promise: Promise<T>, timeInMS = 2000) {
25+
const deferredPromise = new Deferred<T>();
26+
setTimeout(() => deferredPromise.reject('timeout!'), timeInMS);
27+
promise.then(deferredPromise.resolve, deferredPromise.reject);
28+
return deferredPromise.promise;
29+
}

0 commit comments

Comments
 (0)