How to use the @firebase/util.Deferred function in @firebase/util

To help you get started, we’ve selected a few @firebase/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github firebase / firebase-js-sdk / packages / database / src / api / Query.ts View on Github external
validateArgCount('Query.once', 1, 4, arguments.length);
    validateEventType('Query.once', 1, eventType, false);
    validateCallback('Query.once', 2, userCallback, true);

    const ret = Query.getCancelAndContextArgs_(
      'Query.once',
      cancelOrContext,
      context
    );

    // TODO: Implement this more efficiently (in particular, use 'get' wire protocol for 'value' event)
    // TODO: consider actually wiring the callbacks into the promise. We cannot do this without a breaking change
    // because the API currently expects callbacks will be called synchronously if the data is cached, but this is
    // against the Promise specification.
    let firstCall = true;
    const deferred = new Deferred();

    // A dummy error handler in case a user wasn't expecting promises
    deferred.promise.catch(() => {});

    const onceCallback = (snapshot: DataSnapshot) => {
      // NOTE: Even though we unsubscribe, we may get called multiple times if a single action (e.g. set() with JSON)
      // triggers multiple events (e.g. child_added or child_changed).
      if (firstCall) {
        firstCall = false;
        this.off(eventType, onceCallback);

        if (userCallback) {
          userCallback.bind(ret.context)(snapshot);
        }
        deferred.resolve(snapshot);
      }
github maierj / fastlane-action / node_modules / @firebase / component / dist / index.esm.js View on Github external
Provider.prototype.get = function (identifier) {
        if (identifier === void 0) { identifier = DEFAULT_ENTRY_NAME; }
        // if multipleInstances is not supported, use the default name
        var normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
        if (!this.instancesDeferred.has(normalizedIdentifier)) {
            var deferred = new Deferred();
            this.instancesDeferred.set(normalizedIdentifier, deferred);
            // If the service instance is available, resolve the promise with it immediately
            try {
                var instance = this.getOrInitializeService(normalizedIdentifier);
                if (instance) {
                    deferred.resolve(instance);
                }
            }
            catch (e) {
                // when the instance factory throws an exception during get(), it should not cause
                // a fatal error. We just return the unresolved promise in this case.
            }
        }
        return this.instancesDeferred.get(normalizedIdentifier).promise;
    };
    Provider.prototype.getImmediate = function (options) {
github firebase / firebase-js-sdk / packages / database / src / api / Query.ts View on Github external
validateArgCount('Query.once', 1, 4, arguments.length);
    validateEventType('Query.once', 1, eventType, false);
    validateCallback('Query.once', 2, userCallback, true);

    const ret = Query.getCancelAndContextArgs_(
      'Query.once',
      failureCallbackOrContext,
      context
    );

    // TODO: Implement this more efficiently (in particular, use 'get' wire protocol for 'value' event)
    // TODO: consider actually wiring the callbacks into the promise. We cannot do this without a breaking change
    // because the API currently expects callbacks will be called synchronously if the data is cached, but this is
    // against the Promise specification.
    let firstCall = true;
    const deferred = new Deferred();

    // A dummy error handler in case a user wasn't expecting promises
    deferred.promise.catch(() => {});

    const onceCallback = (snapshot: DataSnapshot) => {
      // NOTE: Even though we unsubscribe, we may get called multiple times if a single action (e.g. set() with JSON)
      // triggers multiple events (e.g. child_added or child_changed).
      if (firstCall) {
        firstCall = false;
        this.off(eventType, onceCallback);

        if (userCallback) {
          userCallback.bind(ret.context)(snapshot);
        }
        deferred.resolve(snapshot);
      }
github firebase / firebase-js-sdk / packages / database / src / api / Reference.ts View on Github external
'Reference.setWithPriority',
      1,
      newVal,
      this.path,
      false
    );
    validatePriority('Reference.setWithPriority', 2, newPriority, false);
    validateCallback('Reference.setWithPriority', 3, onComplete, true);

    if (this.getKey() === '.length' || this.getKey() === '.keys') {
      throw 'Reference.setWithPriority failed: ' +
        this.getKey() +
        ' is a read-only object.';
    }

    const deferred = new Deferred();
    this.repo.setWithPriority(
      this.path,
      newVal,
      newPriority,
      deferred.wrapCallback(onComplete)
    );
    return deferred.promise;
  }
github firebase / firebase-js-sdk / packages / ioc / src / container.ts View on Github external
private async _getFactory(name: string): Promise {
    if (this._factories[name]) {
      return this._factories[name];
    }

    if (this._pendingRegistration[name]) {
      return this._pendingRegistration[name].promise;
    }

    const deferred = new Deferred();
    this._pendingRegistration[name] = deferred;

    return deferred.promise;
  }
}
github firebase / firebase-js-sdk / packages / database / src / api / Reference.ts View on Github external
set(
    newVal: unknown,
    onComplete?: (a: Error | null) => void
  ): Promise {
    validateArgCount('Reference.set', 1, 2, arguments.length);
    validateWritablePath('Reference.set', this.path);
    validateFirebaseDataArg('Reference.set', 1, newVal, this.path, false);
    validateCallback('Reference.set', 2, onComplete, true);

    const deferred = new Deferred();
    this.repo.setWithPriority(
      this.path,
      newVal,
      /*priority=*/ null,
      deferred.wrapCallback(onComplete)
    );
    return deferred.promise;
  }
github firebase / firebase-js-sdk / packages / database / src / api / onDisconnect.ts View on Github external
set(value: unknown, onComplete?: (a: Error | null) => void): Promise {
    validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
    validateWritablePath('OnDisconnect.set', this.path_);
    validateFirebaseDataArg('OnDisconnect.set', 1, value, this.path_, false);
    validateCallback('OnDisconnect.set', 2, onComplete, true);
    const deferred = new Deferred();
    this.repo_.onDisconnectSet(
      this.path_,
      value,
      deferred.wrapCallback(onComplete)
    );
    return deferred.promise;
  }
github firebase / firebase-js-sdk / packages / database / src / api / onDisconnect.ts View on Github external
priority: number | string | null,
    onComplete?: (a: Error | null) => void
  ): Promise {
    validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
    validateWritablePath('OnDisconnect.setWithPriority', this.path_);
    validateFirebaseDataArg(
      'OnDisconnect.setWithPriority',
      1,
      value,
      this.path_,
      false
    );
    validatePriority('OnDisconnect.setWithPriority', 2, priority, false);
    validateCallback('OnDisconnect.setWithPriority', 3, onComplete, true);

    const deferred = new Deferred();
    this.repo_.onDisconnectSetWithPriority(
      this.path_,
      value,
      priority,
      deferred.wrapCallback(onComplete)
    );
    return deferred.promise;
  }
github firebase / firebase-js-sdk / packages / ioc / src / Container.ts View on Github external
const factory = await (async () => {
      if (this._factories[serviceName]) return this._factories[serviceName];

      /**
       * If it doesn't already exist, create a new deferred to asynchronously
       * handle the resolution of the `get`
       */
      if (!this._pendingRegistration[serviceName]) {
        this._pendingRegistration[serviceName] = new Deferred();
      }

      return this._pendingRegistration[serviceName].promise;
    })();