Michael Klein 03a1a86dfe
ui: chore - upgrade ember and friends (#14518)
* v3.20.2...v3.24.0

* Fix handle undefined outlet in route component

* Don't use template helper for optional modal.open

Using the optional-helper here will trigger a computation
in the same runloop error. This is because we are setting
the `modal`-property when the `<Ref>` component gets
rendered which will update the `this.modal`-property which
will then recompute the `optional`-helper leading to this
error.

Instead we will create an action that will call the `open`-method
on the modal when it is defined. This gets rid of the double
computation error as we will not access the modal property
twice in the same runloop when `modal` is getting set.

* Fix - fn needs to be passed function tab-nav

We create functions in the component file instead
so that fn-helper stops complaining about the
need to pass a function.

* Update ember-exam to 6.1 version

"Makes it compatible" with ember-qunit v5

* scheduleOnce setMaxHeight paged-collection

We need to schedule to get around double-computation error.

* Fix - model.data is removed from ember-data

This has been private API all along - we need to
work around the removal.

Reference: https://github.com/emberjs/data/pull/7338/files#diff-9a8746fc5c86fd57e6122f00fef3155f76f0f3003a24b53fb7c4621d95dcd9bfL1310

* Fix `propContains` instead of `deepEqual` policy

Recent model.data works differently than iterating attributes.
We use `propContains` instead of `deepEqual`. We are only
interested in the properties we assert against and match
the previous behavior with this change.

* Fix `propContains` instead of `deepEqual` token

* Better handling single-records repo test-helper

`model.data` has been removed we need to handle proxies and
model instances differently.

* Fix remaining repository tests with propContains

We don't want to match entire objects - we don't care
about properties we haven't defined in the assertion.

* Don't use template helper for optional modal.open

Using a template helper will give us a recomputation error -
we work around it by creating an explicit action on
the component instead.

* Await `I $verb the $pageObject object` step

* Fix no more customization ember-can

No need to customize, the helper handles destruction
fine on its own.

* Fix - don't pass `optional` functions to fn

We will declare the functions on the component instead.
This gives us the same behavior but no error from
`fn`, which expects a function to be passed.

* Fix - handle `undefined` state on validate modifier

StateChart can yield out an undefined `state` we need
to handle that in the validate modifier

* Fix linting errors tests directory

* Warn / turn off new ember linting issues

We will tackle them one by one and don't want to
autofix issues that could be dangerous to auto-fix.

* Auto-fix linting issues

* More linting configuration

* Fix remaining linting issues

* Fix linting issues new files after rebase

* ui: Remove ember-cli-uglify config now we are using terser (#14574)

Co-authored-by: John Cowen <johncowen@users.noreply.github.com>
2022-09-15 09:43:17 +01:00

156 lines
5.9 KiB
JavaScript

import { get } from '@ember/object';
const pause = 2000;
// native EventSource retry is ~3s wait
// any specified errors here will mean that the blocking query will attempt
// a reconnection every 3s until it reconnects to Consul
export const createErrorBackoff = function (ms = 3000, P = Promise, wait = setTimeout) {
// This expects an ember-data like error
return function (err) {
// expect and ember-data error or a http-like error (e.statusCode)
let status = get(err, 'errors.firstObject.status') || get(err, 'statusCode');
if (typeof status !== 'undefined') {
// ember-data errors are strings, http errors are numbers
status = status.toString();
switch (true) {
// Any '5xx' (not 500) errors should back off and try again
case status.indexOf('5') === 0 && status.length === 3 && status !== '500':
// fallsthrough
case status === '0':
// TODO: Move this to the view layer so we can show a connection error
// and reconnection success to the user
// Any 0 aborted connections should back off and try again
return new P(function (resolve) {
wait(function () {
resolve(err);
}, ms);
});
}
}
// any other errors should throw to be picked up by an error listener/catch
throw err;
};
};
export const validateCursor = function (current, prev = null) {
let cursor = parseInt(current);
if (!isNaN(cursor)) {
// if cursor is less than the current cursor, reset to zero
if (prev !== null && cursor < prev) {
cursor = 0;
}
// if cursor is less than 0, its always safe to use 1
return Math.max(cursor, 1);
}
};
const throttle = function (configuration, prev, current) {
return function (obj) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(obj);
}, configuration.interval || pause);
});
};
};
const defaultCreateEvent = function (result, configuration) {
return {
type: 'message',
data: result,
};
};
/**
* Wraps an EventSource with functionality to add native EventSource-like functionality
*
* @param {Class} [CallableEventSource] - CallableEventSource Class
* @param {Function} [backoff] - Default backoff function for all instances, defaults to createErrorBackoff
*/
export default function (EventSource, backoff = createErrorBackoff()) {
/**
* An EventSource implementation to add native EventSource-like functionality with just callbacks (`cursor` and 5xx backoff)
*
* This includes:
* 1. 5xx backoff support (uses a 3 second reconnect like native implementations). You can add to this via `Promise.catch`
* 2. A `cursor` configuration value. Current `cursor` is taken from the `meta` property of the event (i.e. `event.data.meta.cursor`)
* 3. Event data can be customized by adding a `configuration.createEvent`
*
* @param {Function} [source] - Promise returning function that resolves your data
* @param {Object} [configuration] - Plain configuration object:
* `cursor` - Cursor position of the EventSource
* `createEvent` - A data filter, giving you the opportunity to filter or replace the event data, such as removing/replacing records
*/
const BlockingEventSource = function (source, configuration = {}) {
const { currentEvent, ...config } = configuration;
EventSource.apply(this, [
(configuration) => {
const { createEvent, ...superConfiguration } = configuration;
return source
.apply(this, [superConfiguration, this])
.catch(backoff)
.then((result) => {
if (result instanceof Error) {
return result;
}
const _createEvent =
typeof createEvent === 'function' ? createEvent : defaultCreateEvent;
let event = _createEvent(result, configuration);
// allow custom types, but make a default of `message`, ideally this would check for CustomEvent
// but keep this flexible for the moment
if (!event.type) {
event = {
type: 'message',
data: event,
};
}
// meta is also configurable by using createEvent
const meta = get(event.data || {}, 'meta');
if (meta) {
// pick off the `cursor` from the meta and add it to configuration
// along with cursor validation
configuration.cursor = validateCursor(meta.cursor, configuration.cursor);
configuration.cacheControl = meta.cacheControl;
configuration.interval = meta.interval;
}
if ((configuration.cacheControl || '').indexOf('no-store') === -1) {
this.currentEvent = event;
}
this.dispatchEvent(event);
const throttledResolve = throttle(configuration, event, this.previousEvent);
this.previousEvent = this.currentEvent;
return throttledResolve(result);
});
},
config,
]);
if (typeof currentEvent !== 'undefined') {
this.currentEvent = currentEvent;
}
// only on initialization
// if we already have an currentEvent set via configuration
// dispatch the event so things are populated immediately
this.addEventListener('open', (e) => {
const currentEvent = e.target.getCurrentEvent();
if (typeof currentEvent !== 'undefined') {
this.dispatchEvent(currentEvent);
}
});
};
BlockingEventSource.prototype = Object.assign(
Object.create(EventSource.prototype, {
constructor: {
value: EventSource,
configurable: true,
writable: true,
},
}),
{
// if we are having these props, at least make getters
getCurrentEvent: function () {
return this.currentEvent;
},
getPreviousEvent: function () {
return this.previousEvent;
},
}
);
return BlockingEventSource;
}