@absinthe/socket
Version:
90 lines (67 loc) • 2.65 kB
JavaScript
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _objectSpread from '@babel/runtime/helpers/objectSpread';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { replace } from '@jumpn/utils-array';
import 'core-js/modules/es6.array.find-index';
import { hasIn } from '@jumpn/utils-composite';
import 'core-js/modules/es6.function.bind';
import _newArrowCheck from '@babel/runtime/helpers/newArrowCheck';
var _this = undefined;
var observe = function observe(_ref, observer) {
var activeObservers = _ref.activeObservers,
rest = _objectWithoutProperties(_ref, ["activeObservers"]);
_newArrowCheck(this, _this);
return _objectSpread({}, rest, {
activeObservers: _toConsumableArray(activeObservers).concat([observer]),
isActive: true
});
}.bind(undefined);
var _this$1 = undefined;
var findIndex = function findIndex(notifiers, key, value // $FlowFixMe: flow is having some troubles to match hasIn signature (curry)
) {
_newArrowCheck(this, _this$1);
return notifiers.findIndex(hasIn([key], value));
}.bind(undefined);
var _this$2 = undefined;
var refresh = function refresh(notifier) {
var _this2 = this;
_newArrowCheck(this, _this$2);
return function (notifiers) {
_newArrowCheck(this, _this2);
return replace(findIndex(notifiers, "request", notifier.request), [notifier], notifiers);
}.bind(this);
}.bind(undefined);
var _this$3 = undefined;
var updateNotifiers = function updateNotifiers(absintheSocket, updater) {
_newArrowCheck(this, _this$3);
absintheSocket.notifiers = updater(absintheSocket.notifiers);
return absintheSocket;
}.bind(undefined);
var _this$4 = undefined;
var refreshNotifier = function refreshNotifier(absintheSocket, notifier) {
_newArrowCheck(this, _this$4);
updateNotifiers(absintheSocket, refresh(notifier));
return notifier;
}.bind(undefined);
var _this$5 = undefined;
/**
* Observes given notifier using the provided observer
*
* @example
* import * as withAbsintheSocket from "@absinthe/socket"
*
* const logEvent = eventName => (...args) => console.log(eventName, ...args);
*
* const updatedNotifier = withAbsintheSocket.observe(absintheSocket, notifier, {
* onAbort: logEvent("abort"),
* onError: logEvent("error"),
* onStart: logEvent("open"),
* onResult: logEvent("result")
* });
*/
var observe$1 = function observe$$1(absintheSocket, notifier, observer) {
_newArrowCheck(this, _this$5);
return refreshNotifier(absintheSocket, observe(notifier, observer));
}.bind(undefined);
export default observe$1;
//# sourceMappingURL=observe.js.map