//
//  CBLChangeListenerToken.h
//  CouchbaseLite
//
//  Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import <Foundation/Foundation.h>
#import "CBLListenerToken.h"

NS_ASSUME_NONNULL_BEGIN

/** This makes the parent object to release the owning reference of the token. */
@protocol CBLRemovableListenerToken <NSObject>

- (void) removeToken: (id)token;

@end


@interface CBLChangeListenerToken<ChangeType> : NSObject <CBLListenerToken>

/**
 Initialize with the given listener block and the dispatch queue.

 @param listener The listener block.
 @param queue The dispatch queue; if nil, the main queue will be used.
 @param delegate The delegate for removing the references from change notifier.
 @return The CBLChangeListenerToken object.
 */
- (instancetype) initWithListener: (void (^)(ChangeType))listener
                            queue: (nullable dispatch_queue_t)queue
                         delegate: (nullable id<CBLRemovableListenerToken>)delegate;

/** An arbitrary context that can be associated by the client, such as a documentID, c4queryObserver. */
@property (nonatomic, nullable) id context;

@property (nonatomic, weak, nullable) id<CBLRemovableListenerToken> delegate;

/**
 Posts an asynchronous change notification to the listener block on its dispatch queue.
 */
- (void) postChange: (ChangeType)change;

@end

NS_ASSUME_NONNULL_END
