( parent: DatabaseReference, value?: unknown )
| 597 | * but can be used immediately as the `Reference` to the child location. |
| 598 | */ |
| 599 | export function push( |
| 600 | parent: DatabaseReference, |
| 601 | value?: unknown |
| 602 | ): ThenableReference { |
| 603 | parent = getModularInstance(parent); |
| 604 | validateWritablePath('push', parent._path); |
| 605 | validateFirebaseDataArg('push', value, parent._path, true); |
| 606 | const now = repoServerTime(parent._repo); |
| 607 | const name = nextPushId(now); |
| 608 | |
| 609 | // push() returns a ThennableReference whose promise is fulfilled with a |
| 610 | // regular Reference. We use child() to create handles to two different |
| 611 | // references. The first is turned into a ThennableReference below by adding |
| 612 | // then() and catch() methods and is used as the return value of push(). The |
| 613 | // second remains a regular Reference and is used as the fulfilled value of |
| 614 | // the first ThennableReference. |
| 615 | const thenablePushRef: Partial<ThenableReferenceImpl> = child( |
| 616 | parent, |
| 617 | name |
| 618 | ) as ReferenceImpl; |
| 619 | const pushRef = child(parent, name) as ReferenceImpl; |
| 620 | |
| 621 | let promise: Promise<ReferenceImpl>; |
| 622 | if (value != null) { |
| 623 | promise = set(pushRef, value).then(() => pushRef); |
| 624 | } else { |
| 625 | promise = Promise.resolve(pushRef); |
| 626 | } |
| 627 | |
| 628 | thenablePushRef.then = promise.then.bind(promise); |
| 629 | thenablePushRef.catch = promise.then.bind(promise, undefined); |
| 630 | return thenablePushRef as ThenableReferenceImpl; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Removes the data at this Database location. |
no test coverage detected