2019-02-21 10:36:15 +00:00
|
|
|
import ObjectProxy from '@ember/object/proxy';
|
|
|
|
import ArrayProxy from '@ember/array/proxy';
|
|
|
|
|
|
|
|
import createListeners from 'consul-ui/utils/dom/create-listeners';
|
|
|
|
|
|
|
|
import EventTarget from 'consul-ui/utils/dom/event-target/rsvp';
|
|
|
|
|
|
|
|
import cacheFactory from 'consul-ui/utils/dom/event-source/cache';
|
|
|
|
import proxyFactory from 'consul-ui/utils/dom/event-source/proxy';
|
|
|
|
import firstResolverFactory from 'consul-ui/utils/dom/event-source/resolver';
|
|
|
|
|
|
|
|
import CallableEventSourceFactory from 'consul-ui/utils/dom/event-source/callable';
|
2019-06-20 08:35:30 +00:00
|
|
|
import OpenableEventSourceFactory from 'consul-ui/utils/dom/event-source/openable';
|
2019-02-21 10:36:15 +00:00
|
|
|
import BlockingEventSourceFactory from 'consul-ui/utils/dom/event-source/blocking';
|
|
|
|
import StorageEventSourceFactory from 'consul-ui/utils/dom/event-source/storage';
|
|
|
|
|
2019-06-04 14:51:10 +00:00
|
|
|
import EmberObject from '@ember/object';
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
|
2020-01-21 17:52:40 +00:00
|
|
|
import { env } from 'consul-ui/env';
|
2019-06-04 14:51:10 +00:00
|
|
|
|
|
|
|
let runner;
|
|
|
|
switch (env('CONSUL_UI_REALTIME_RUNNER')) {
|
|
|
|
case 'ec':
|
2022-09-15 08:43:17 +00:00
|
|
|
runner = function (target, configuration, isClosed) {
|
2019-06-04 14:51:10 +00:00
|
|
|
return EmberObject.extend({
|
|
|
|
task: task(function* run() {
|
|
|
|
while (!isClosed(target)) {
|
|
|
|
yield target.source.bind(target)(configuration);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.create()
|
|
|
|
.get('task')
|
|
|
|
.perform();
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'generator':
|
2022-09-15 08:43:17 +00:00
|
|
|
runner = async function (target, configuration, isClosed) {
|
|
|
|
const run = function* () {
|
2019-06-04 14:51:10 +00:00
|
|
|
while (!isClosed(target)) {
|
|
|
|
yield target.source.bind(target)(configuration);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let step = run().next();
|
|
|
|
let res;
|
|
|
|
while (!step.done) {
|
|
|
|
res = await step.value;
|
|
|
|
step = run().next();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'async':
|
2022-09-15 08:43:17 +00:00
|
|
|
runner = async function (target, configuration, isClosed) {
|
|
|
|
const run = function () {
|
2019-06-04 14:51:10 +00:00
|
|
|
return target.source.bind(target)(configuration);
|
|
|
|
};
|
|
|
|
let res;
|
|
|
|
while (!isClosed(target)) {
|
|
|
|
res = await run();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// use the default runner
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:36:15 +00:00
|
|
|
// All The EventSource-i
|
2019-06-04 14:51:10 +00:00
|
|
|
export const CallableEventSource = CallableEventSourceFactory(EventTarget, Promise, runner);
|
2019-06-20 08:35:30 +00:00
|
|
|
export const OpenableEventSource = OpenableEventSourceFactory(CallableEventSource);
|
|
|
|
export const BlockingEventSource = BlockingEventSourceFactory(OpenableEventSource);
|
2019-02-21 10:36:15 +00:00
|
|
|
export const StorageEventSource = StorageEventSourceFactory(EventTarget, Promise);
|
|
|
|
|
|
|
|
// various utils
|
2019-05-01 18:09:29 +00:00
|
|
|
export const proxy = proxyFactory(ObjectProxy, ArrayProxy, createListeners);
|
2019-02-21 10:36:15 +00:00
|
|
|
export const resolve = firstResolverFactory(Promise);
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
export const source = function (source) {
|
2019-02-21 10:36:15 +00:00
|
|
|
// create API needed for conventional promise blocked, loading, Routes
|
|
|
|
// i.e. resolve/reject on first response
|
2022-09-15 08:43:17 +00:00
|
|
|
return resolve(source, createListeners()).then(function (data) {
|
2019-02-21 10:36:15 +00:00
|
|
|
// create API needed for conventional DD/computed and Controllers
|
2019-05-01 18:09:29 +00:00
|
|
|
return proxy(source, data);
|
2019-02-21 10:36:15 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
export const cache = cacheFactory(source, BlockingEventSource, Promise);
|
2019-05-01 18:09:29 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
const errorEvent = function (e) {
|
2019-05-01 18:09:29 +00:00
|
|
|
return new ErrorEvent('error', {
|
|
|
|
error: e,
|
|
|
|
message: e.message,
|
|
|
|
});
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const fromPromise = function (promise) {
|
|
|
|
return new CallableEventSource(function (configuration) {
|
2019-05-01 18:09:29 +00:00
|
|
|
const dispatch = this.dispatchEvent.bind(this);
|
|
|
|
const close = () => {
|
|
|
|
this.close();
|
|
|
|
};
|
|
|
|
return promise
|
2022-09-15 08:43:17 +00:00
|
|
|
.then(function (result) {
|
2019-05-01 18:09:29 +00:00
|
|
|
close();
|
|
|
|
dispatch({ type: 'message', data: result });
|
|
|
|
})
|
2022-09-15 08:43:17 +00:00
|
|
|
.catch(function (e) {
|
2019-05-01 18:09:29 +00:00
|
|
|
close();
|
|
|
|
dispatch(errorEvent(e));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const toPromise = function (target, cb, eventName = 'message', errorName = 'error') {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2019-05-01 18:09:29 +00:00
|
|
|
// TODO: e.target.data
|
2022-09-15 08:43:17 +00:00
|
|
|
const message = function (e) {
|
2019-05-01 18:09:29 +00:00
|
|
|
resolve(e.data);
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const error = function (e) {
|
2019-05-01 18:09:29 +00:00
|
|
|
reject(e.error);
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const remove = function () {
|
2019-05-01 18:09:29 +00:00
|
|
|
if (typeof target.close === 'function') {
|
|
|
|
target.close();
|
|
|
|
}
|
|
|
|
target.removeEventListener(eventName, message);
|
|
|
|
target.removeEventListener(errorName, error);
|
|
|
|
};
|
|
|
|
target.addEventListener(eventName, message);
|
|
|
|
target.addEventListener(errorName, error);
|
|
|
|
cb(remove);
|
|
|
|
});
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
export const once = function (cb, configuration, Source = OpenableEventSource) {
|
|
|
|
return new Source(function (configuration, source) {
|
2020-03-19 10:28:21 +00:00
|
|
|
return cb(configuration, source)
|
2022-09-15 08:43:17 +00:00
|
|
|
.then(function (data) {
|
2020-03-19 10:28:21 +00:00
|
|
|
source.dispatchEvent({ type: 'message', data: data });
|
|
|
|
source.close();
|
|
|
|
})
|
2022-09-15 08:43:17 +00:00
|
|
|
.catch(function (e) {
|
2020-03-19 10:28:21 +00:00
|
|
|
source.dispatchEvent({ type: 'error', error: e });
|
|
|
|
source.close();
|
|
|
|
});
|
|
|
|
}, configuration);
|
|
|
|
};
|