Sha256: 686642e9459896ad96b5e6ca591bafb20b65c2a5be55f67f370928a1a675f26b

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

import { sendEvent } from './events';
/**
  ObserverSet is a data structure used to keep track of observers
  that have been deferred.

  It ensures that observers are called in the same order that they
  were initially triggered.

  It also ensures that observers for any object-key pairs are called
  only once, even if they were triggered multiple times while
  deferred. In this case, the order that the observer is called in
  will depend on the first time the observer was triggered.

  @private
  @class ObserverSet
*/
export default class ObserverSet {
    constructor() {
        this.added = new Map();
        this.queue = [];
    }
    add(object, key, event) {
        let keys = this.added.get(object);
        if (keys === undefined) {
            keys = new Set();
            this.added.set(object, keys);
        }
        if (!keys.has(key)) {
            this.queue.push(object, key, event);
            keys.add(key);
        }
    }
    flush() {
        // The queue is saved off to support nested flushes.
        let queue = this.queue;
        this.added.clear();
        this.queue = [];
        for (let i = 0; i < queue.length; i += 3) {
            let object = queue[i];
            let key = queue[i + 1];
            let event = queue[i + 2];
            if (object.isDestroying || object.isDestroyed) {
                continue;
            }
            sendEvent(object, event, [object, key]);
        }
    }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
discourse-ember-source-3.6.0.0 dist/es/@ember/-internals/metal/lib/observer_set.js
discourse-ember-source-3.5.1.1 dist/es/ember-metal/lib/observer_set.js
discourse-ember-source-3.5.1.0 dist/dist/es/ember-metal/lib/observer_set.js