Exemple #1
0
exports["test weak observer unsubscribe"] = function*(assert) {
  const loader = Loader(module);
  const { Observer, observe, subscribe, unsubscribe } = loader.require("sdk/core/observer");
  const { WeakReference } = loader.require("sdk/core/reference");

  let sawNotification = false;
  let firstWait = defer();
  let secondWait = defer();

  const WeakObserver = Class({
    extends: Observer,
    implements: [WeakReference],
    observe: function() {
      sawNotification = true;
      firstWait.resolve();
    }
  });

  const StrongObserver = Class({
    extends: Observer,
    observe: function() {
      secondWait.resolve();
    }
  });

  observe.define(Observer, (x, ...rest) => x.observe(...rest));

  let weakObserver = new WeakObserver;
  let strongObserver = new StrongObserver();
  subscribe(weakObserver, "test-topic");
  subscribe(strongObserver, "test-wait");

  notifyObservers(null, "test-topic", null);
  yield firstWait.promise;

  assert.ok(sawNotification, "Should have seen notification before GC");
  sawNotification = false;

  yield loader.require("sdk/test/memory").gc();

  notifyObservers(null, "test-topic", null);
  notifyObservers(null, "test-wait", null);
  yield secondWait.promise;

  assert.ok(sawNotification, "Should have seen notification after GC");
  sawNotification = false;

  try {
    unsubscribe(weakObserver, "test-topic");
    unsubscribe(strongObserver, "test-wait");
    assert.pass("Should not have seen an exception");
  }
  catch (e) {
    assert.fail("Should not have seen an exception");
  }

  loader.unload();
};
Exemple #2
0
exports["test subscribe unsubscribe"] = assert => {
  const topic = Date.now().toString(32);
  const Subscriber = Class({
    extends: Observer,
    initialize: function(observe) {
      this.observe = observe;
    }
  });
  observe.define(Subscriber, (x, subject, _, data) =>
                                x.observe(subject.wrappedJSObject.x));

  let xs = [];
  const x = Subscriber((...rest) => xs.push(...rest));

  let ys = [];
  const y = Subscriber((...rest) => ys.push(...rest));

  const publish = (topic, data) =>
    notifyObservers(message(data), topic, null);

  publish({x:0});

  subscribe(x, topic);

  publish(topic, {x:1});

  subscribe(y, topic);

  publish(topic, {x:2});
  publish(topic + "!", {x: 2.5});

  unsubscribe(x, topic);

  publish(topic, {x:3});

  subscribe(y, topic);

  publish(topic, {x:4});

  subscribe(x, topic);

  publish(topic, {x:5});

  unsubscribe(x, topic);
  unsubscribe(y, topic);

  publish(topic, {x:6});

  assert.deepEqual(xs, [1, 2, 5]);
  assert.deepEqual(ys, [2, 3, 4, 5]);
}