ui: Change vocab of ReopenableEventSource from 'reopen' to 'open' (#5973)

You can potentially close an EventSource before its first tick by
immediately setting the readyState to a non-open state. Therefore it
never opens.

Calling `open` will then open it.

'Open' fits better than 'reopen' when taking the above into account
This commit is contained in:
John Cowen 2019-06-20 09:35:30 +01:00 committed by John Cowen
parent 0dfae99a1b
commit 62e3c5605c
7 changed files with 22 additions and 21 deletions

View File

@ -11,7 +11,7 @@ import proxyFactory from 'consul-ui/utils/dom/event-source/proxy';
import firstResolverFactory from 'consul-ui/utils/dom/event-source/resolver'; import firstResolverFactory from 'consul-ui/utils/dom/event-source/resolver';
import CallableEventSourceFactory from 'consul-ui/utils/dom/event-source/callable'; import CallableEventSourceFactory from 'consul-ui/utils/dom/event-source/callable';
import ReopenableEventSourceFactory from 'consul-ui/utils/dom/event-source/reopenable'; import OpenableEventSourceFactory from 'consul-ui/utils/dom/event-source/openable';
import BlockingEventSourceFactory from 'consul-ui/utils/dom/event-source/blocking'; import BlockingEventSourceFactory from 'consul-ui/utils/dom/event-source/blocking';
import StorageEventSourceFactory from 'consul-ui/utils/dom/event-source/storage'; import StorageEventSourceFactory from 'consul-ui/utils/dom/event-source/storage';
@ -70,8 +70,8 @@ switch (env('CONSUL_UI_REALTIME_RUNNER')) {
// All The EventSource-i // All The EventSource-i
export const CallableEventSource = CallableEventSourceFactory(EventTarget, Promise, runner); export const CallableEventSource = CallableEventSourceFactory(EventTarget, Promise, runner);
export const ReopenableEventSource = ReopenableEventSourceFactory(CallableEventSource); export const OpenableEventSource = OpenableEventSourceFactory(CallableEventSource);
export const BlockingEventSource = BlockingEventSourceFactory(ReopenableEventSource); export const BlockingEventSource = BlockingEventSourceFactory(OpenableEventSource);
export const StorageEventSource = StorageEventSourceFactory(EventTarget, Promise); export const StorageEventSource = StorageEventSourceFactory(EventTarget, Promise);
// various utils // various utils

View File

@ -9,7 +9,7 @@ export default function(eventSource = EventSource) {
super(...arguments); super(...arguments);
this.configuration = configuration; this.configuration = configuration;
} }
reopen() { open() {
switch (this.readyState) { switch (this.readyState) {
case 3: // CLOSING case 3: // CLOSING
this.readyState = 1; this.readyState = 1;
@ -18,6 +18,7 @@ export default function(eventSource = EventSource) {
eventSource.apply(this, [this.source, this.configuration]); eventSource.apply(this, [this.source, this.configuration]);
break; break;
} }
return this;
} }
}; };
} }

View File

@ -44,8 +44,8 @@ export default function(ObjProxy, ArrProxy, createListeners) {
close: function() { close: function() {
return source.close(...arguments); return source.close(...arguments);
}, },
reopen: function() { open: function() {
return source.reopen(...arguments); return source.open(...arguments);
}, },
willDestroy: function() { willDestroy: function() {
this.listeners.remove(); this.listeners.remove();

View File

@ -7,7 +7,7 @@ export default function(P = Promise) {
if (current != null) { if (current != null) {
// immediately resolve if we have previous cached data // immediately resolve if we have previous cached data
return P.resolve(current.data).then(function(cached) { return P.resolve(current.data).then(function(cached) {
source.reopen(); source.open();
return cached; return cached;
}); });
} }

View File

@ -15,7 +15,7 @@ export default function(EventTarget, P = Promise) {
this.configuration = configuration; this.configuration = configuration;
this.configuration.cursor = 1; this.configuration.cursor = 1;
this.dispatcher = configuration.dispatcher; this.dispatcher = configuration.dispatcher;
this.reopen(); this.open();
} }
dispatchEvent() { dispatchEvent() {
if (this.readyState === 1) { if (this.readyState === 1) {

View File

@ -4,7 +4,7 @@ import {
cache, cache,
resolve, resolve,
CallableEventSource, CallableEventSource,
ReopenableEventSource, OpenableEventSource,
BlockingEventSource, BlockingEventSource,
StorageEventSource, StorageEventSource,
} from 'consul-ui/utils/dom/event-source/index'; } from 'consul-ui/utils/dom/event-source/index';
@ -16,7 +16,7 @@ module('Unit | Utility | dom/event source/index');
test('it works', function(assert) { test('it works', function(assert) {
// All The EventSource // All The EventSource
assert.ok(typeof CallableEventSource === 'function'); assert.ok(typeof CallableEventSource === 'function');
assert.ok(typeof ReopenableEventSource === 'function'); assert.ok(typeof OpenableEventSource === 'function');
assert.ok(typeof BlockingEventSource === 'function'); assert.ok(typeof BlockingEventSource === 'function');
assert.ok(typeof StorageEventSource === 'function'); assert.ok(typeof StorageEventSource === 'function');

View File

@ -1,8 +1,8 @@
import domEventSourceReopenable from 'consul-ui/utils/dom/event-source/reopenable'; import domEventSourceOpenable from 'consul-ui/utils/dom/event-source/openable';
import { module } from 'qunit'; import { module } from 'qunit';
import test from 'ember-sinon-qunit/test-support/test'; import test from 'ember-sinon-qunit/test-support/test';
module('Unit | Utility | dom/event-source/reopenable'); module('Unit | Utility | dom/event-source/openable');
const createEventSource = function() { const createEventSource = function() {
return class { return class {
@ -17,30 +17,30 @@ const createEventSource = function() {
close() {} close() {}
}; };
}; };
test('it creates an Reopenable class implementing EventSource', function(assert) { test('it creates an Openable class implementing EventSource', function(assert) {
const EventSource = createEventSource(); const EventSource = createEventSource();
const ReopenableEventSource = domEventSourceReopenable(EventSource); const OpenableEventSource = domEventSourceOpenable(EventSource);
assert.ok(ReopenableEventSource instanceof Function); assert.ok(OpenableEventSource instanceof Function);
const source = new ReopenableEventSource(function() {}); const source = new OpenableEventSource(function() {});
assert.ok(source instanceof EventSource); assert.ok(source instanceof EventSource);
}); });
test('it reopens the event source when reopen is called', function(assert) { test('it reopens the event source when reopen is called', function(assert) {
const callable = this.stub(); const callable = this.stub();
const EventSource = createEventSource(); const EventSource = createEventSource();
const ReopenableEventSource = domEventSourceReopenable(EventSource); const OpenableEventSource = domEventSourceOpenable(EventSource);
const source = new ReopenableEventSource(callable); const source = new OpenableEventSource(callable);
assert.equal(source.readyState, 1); assert.equal(source.readyState, 1);
// first automatic EventSource `open` // first automatic EventSource `open`
assert.ok(callable.calledOnce); assert.ok(callable.calledOnce);
source.readyState = 3; source.readyState = 3;
source.reopen(); source.open();
// still only called once as it hasn't completely closed yet // still only called once as it hasn't completely closed yet
// therefore is just opened by resetting the readyState // therefore is just opened by resetting the readyState
assert.ok(callable.calledOnce); assert.ok(callable.calledOnce);
assert.equal(source.readyState, 1); assert.equal(source.readyState, 1);
// properly close the source // properly close the source
source.readyState = 2; source.readyState = 2;
source.reopen(); source.open();
// this time it is reopened via a recall of the callable // this time it is opened via a recall of the callable
assert.ok(callable.calledTwice); assert.ok(callable.calledTwice);
}); });