Description

Provides the base functionality for wrapper objects which emit events. Wrappers like GoogleMap, GoogleMapsPolygon, etc. should extend this class.

Extends

GoogleMapsNativeObjectWrapper

Implements

IGoogleMapsNativeObjectEmittingWrapper

Index

core/abstraction/base/google-maps-native-object-emitting-wrapper.ts

Properties
Methods

Constructor

constructor(api: GoogleMapsApiService, native: TNative)

Creates an instance of GoogleMapsNativeObjectEmittingWrapper.

⚠️ Implementors should weakly-type the constructor's native parameter as any. ⚠️ This is because strong typing the constructor with the relevant native type causes Angular's DI to emit decoration type metadata for the class. This metadata is then run when the app loads, before the lazy loader has a chance to take action, causing the app to crash as the metadata refers to a native type that doesn't yet exist (e.g. google.maps.Map, google.maps.Marker). Weakly typing the native argument as any causes the compiler to use Object as the type, allowing the decoration code to run correctly. Because the base class GoogleMapsNativeObjectWrapper still strong-types the native argument, it is safe to use any` here without losing type safety and intellisense.

Parameters:
Name Type Optional Description
api GoogleMapsApiService No

The instance of the maps api service.

native TNative No

The instantiated native object to be wrapped.

Properties

Public custom
Type: any
Public Readonly native
Type: TNative
The instantiated native object to be wrapped.

Methods

Public clearListeners
clearListeners()

Unregisters all handlers of any previously registered native event.

Returns: void
Public listenTo
listenTo(eventName: string, handleEvent: (args: any[]) => void)

Registers a handler to a specific event of the native object and takes care of executing the handler inside angular's zone.

Parameters:
Name Type Optional Description
eventName string No

The name of the native event to register the handler for.

handleEvent function No

The function to execute when the event is triggered by the native object.

Returns: void

An function for unregistering the handler from the event.

Public stopListeningTo
stopListeningTo(eventName: string)

Unregisters all handlers previously registered to handle a specific event.

Parameters:
Name Type Optional Description
eventName string No

The name of the native event for which to unregister all handlers.

Returns: void
Public setCustom
setCustom(custom: any)
Parameters:
Name Type Optional
custom any No
Returns: void
import { GoogleMapsApiService                   } from '../../api/google-maps-api.service';
import { IGoogleMapsNativeObject                } from '../native/i-google-maps-native-object';
import { IGoogleMapsNativeObjectEmittingWrapper } from './i-google-maps-native-object-emitting-wrapper';
import { GoogleMapsNativeObjectWrapper          } from './google-maps-native-object-wrapper';

/**
 * Provides the base functionality for wrapper objects which emit events.
 * Wrappers like GoogleMap, GoogleMapsPolygon, etc. should extend this class.
 * 
 * @abstract
 * @extends {GoogleMapsNativeObjectWrapper<TNative>}
 * @implements {IGoogleMapsNativeObjectEmittingWrapper<TNative>}
 * @template TNative The type of native object the wrapper will work with.
 */
export abstract class GoogleMapsNativeObjectEmittingWrapper<TNative extends IGoogleMapsNativeObject>
              extends GoogleMapsNativeObjectWrapper<TNative>
           implements IGoogleMapsNativeObjectEmittingWrapper<TNative>
{
    /**
     * Creates an instance of GoogleMapsNativeObjectEmittingWrapper.
     * 
     * ⚠️ Implementors should weakly-type the constructor's `native` parameter as `any`. ⚠️
     * This is because strong typing the constructor with the relevant native type causes Angular's DI to
     * emit decoration type metadata for the class. This metadata is then run when the app loads, before
     * the lazy loader has a chance to take action, causing the app to crash as the metadata refers to a
     * native type that doesn't yet exist (e.g. `google.maps.Map`, `google.maps.Marker`).
     * Weakly typing the `native` argument as `any` causes the compiler to use `Object` as the type, allowing
     * the decoration code to run correctly. Because the base class {@link GoogleMapsNativeObjectWrapper}
     * still strong-types the `native argument, it is safe to use `any` here without losing type safety
     * and intellisense.
     * 
     * @param {GoogleMapsApiService} api The instance of the maps api service.
     * @param {TNative} native The instantiated native object to be wrapped.
     */
    constructor(api: GoogleMapsApiService, native: TNative)
    {
        super(api, native);
    }

    /**
     * Registers a handler to a specific event of the native object and takes care of executing the handler inside angular's zone.
     *
     * @param {string} eventName The name of the native event to register the handler for.
     * @param {(...args: any[]) => void} handleEvent The function to execute when the event is triggered by the native object.
     * @returns {() => void} An function for unregistering the handler from the event.
     */
    public listenTo(eventName: string, handleEvent: (...args: any[]) => void): () => void
    {
        // The event is fired natively outside of angular, but the handler will work in the context of angular components.
        // This will bring execution to angular's zone.
        const handleEventInAngular = (...args: any[]) => this.api.runInsideAngular(() => handleEvent(args));

        // Register the handler and return the remove function
        const listener = google.maps.event.addListener(this.native, eventName, handleEventInAngular);

        // Flatten the object and bind the function to it, so it could execute correctly independently of the `this` value when it's called
        return listener.remove.bind(listener);
    }

    /**
     * Unregisters **all** handlers previously registered to handle a specific event.
     *
     * @param {string} eventName The name of the native event for which to unregister all handlers.
     */
    public stopListeningTo(eventName: string): void
    {
        return google.maps.event.clearListeners(this.native, eventName);
    }

    /**
     * Unregisters all handlers of any previously registered native event.
     */
    public clearListeners(): void
    {
        return google.maps.event.clearInstanceListeners(this.native);
    }
}

results matching ""

    No results matching ""