blob: 41f0fe23ef146e2e9b4515e2183fb1053390b07a [file] [log] [blame]
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'types.dart';
/// Callback that receives updates to the camera position.
///
/// This callback is triggered when the platform Google Map
/// registers a camera movement.
///
/// This is used in [GoogleMap.onCameraMove].
typedef CameraPositionCallback = void Function(CameraPosition position);
/// Callback function taking a single argument.
typedef ArgumentCallback<T> = void Function(T argument);
/// Mutable collection of [ArgumentCallback] instances, itself an [ArgumentCallback].
///
/// Additions and removals happening during a single [call] invocation do not
/// change who gets a callback until the next such invocation.
///
/// Optimized for the singleton case.
class ArgumentCallbacks<T> {
final List<ArgumentCallback<T>> _callbacks = <ArgumentCallback<T>>[];
/// Callback method. Invokes the corresponding method on each callback
/// in this collection.
///
/// The list of callbacks being invoked is computed at the start of the
/// method and is unaffected by any changes subsequently made to this
/// collection.
void call(T argument) {
final int length = _callbacks.length;
if (length == 1) {
_callbacks[0].call(argument);
} else if (0 < length) {
for (final ArgumentCallback<T> callback
in List<ArgumentCallback<T>>.from(_callbacks)) {
callback(argument);
}
}
}
/// Adds a callback to this collection.
void add(ArgumentCallback<T> callback) {
_callbacks.add(callback);
}
/// Removes a callback from this collection.
///
/// Does nothing, if the callback was not present.
void remove(ArgumentCallback<T> callback) {
_callbacks.remove(callback);
}
/// Whether this collection is empty.
bool get isEmpty => _callbacks.isEmpty;
/// Whether this collection is non-empty.
bool get isNotEmpty => _callbacks.isNotEmpty;
}