aws-crt
Version:
NodeJS bindings to the aws-c-* libraries
62 lines • 2.07 kB
JavaScript
"use strict";
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. 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.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
class BufferedEvent {
constructor(event, ...args) {
this.event = event;
this.args = args;
}
}
/**
* Provides buffered event emitting semantics, similar to many Node-style streams.
* Subclasses will override {@link BufferedEventEmitter#on} and trigger uncorking.
* Note that uncorking should always be done next tick, not during the on() call!
*/
class BufferedEventEmitter extends events_1.EventEmitter {
constructor() {
super();
this.corked = false;
}
cork() {
this.corked = true;
}
uncork() {
this.corked = false;
while (this.eventQueue) {
const event = this.eventQueue;
super.emit(event.event, ...event.args);
this.eventQueue = this.eventQueue.next;
}
}
emit(event, ...args) {
if (this.corked) {
// queue requests in order
let last = this.lastQueuedEvent;
this.lastQueuedEvent = new BufferedEvent(event, args);
if (last) {
last.next = this.lastQueuedEvent;
}
else {
this.eventQueue = this.lastQueuedEvent;
}
return this.listeners(event).length > 0;
}
return super.emit(event, ...args);
}
}
exports.BufferedEventEmitter = BufferedEventEmitter;
//# sourceMappingURL=event.js.map