Skip to content

[RFC] Proposal for a 2.0 version of this library #842

Open
@usefulthink

Description

@usefulthink

I think, given the very limited scope of what this library is supposed to do,
it can be significantly simplified for a 2.0 version when effectively rewriting it around the ideas of the dynamic library loading.

The library as it is now seems unnecessarily complicated.

  • the exported Loader class is a de-facto singleton - without being
    implemented as one. This makes it needlessly complicated to use.
  • for a lot of tasks, there are several different ways to achieve the same
    result, where we can just provide one
  • there are lots of deprecated functions and other details that are
    outdated or not really needed (e.g. functions to remove the API,
    which – with newer versions – can't be done completely anyway)

Proposed API

The proposed API exposes just two main parts of functionality: a (static)
method to configure the options and the importLibrary function (which is
exported independently for convenience).

import ApiLoader from "@googlemaps/js-api-loader";

// initial configuration
ApiLoader.setOptions({ apiKey: "...", version: "weekly" });

// loading a library
const { Map } = ApiLoader.importLibrary("maps");
// anywhere else in the application the shorthand-version can be used as well
import { importLibrary } from "@googlemaps/js-api-loader";

// `importLibrary` is an alias for `ApiLoader.importLibrary`.
const { Map } = await importLibrary("maps");

To get status-information and error messages, the ApiLoader can
implement the EventTarget interface (add/removeEventListener):

// status could be initialized, loading, loaded, error, or auth-failure
ApiLoader.addEventListener("status-changed", (ev) => {
  console.log("new status:", ev.status);
});

ApiLoader.addEventListener("error", (ev) => {});

Notes on internal behavior

  • the ApiLoader doesn't do anything (except for storing the options) until
    the importLibrary function is called for the first time. This allows
    users to configure the loader in a central place of their application
    even if the maps API isn't needed on most pages

  • Once the importLibrary function is called, the options are frozen and
    attempts to modify them will throw an Error (or maybe just log an
    error-message to the console?)

  • the first call to importLibrary initiates the bootstrapping, once the
    maps API is loaded, importLibrary will directly forward to the
    google.maps.importLibrary function.

  • if an attempt to load the API fails, the loader will resolve all pending
    importLibrary promises with an error and will retry loading with the next
    importLibrary call.

  • if the library was already loaded by other means than the ApiLoader, a
    warning is logged to the console

Metadata

Metadata

Assignees

No one assigned

    Labels

    next major: breaking changethis is a change that we should wait to bundle into the next major versiontype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions