UNPKG

@absinthe/socket

Version:
94 lines (69 loc) 2.9 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _toConsumableArray = _interopDefault(require('@babel/runtime/helpers/toConsumableArray')); var _objectSpread = _interopDefault(require('@babel/runtime/helpers/objectSpread')); var _objectWithoutProperties = _interopDefault(require('@babel/runtime/helpers/objectWithoutProperties')); var utilsArray = require('@jumpn/utils-array'); require('core-js/modules/es6.array.find-index'); var utilsComposite = require('@jumpn/utils-composite'); require('core-js/modules/es6.function.bind'); var _newArrowCheck = _interopDefault(require('@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(utilsComposite.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 utilsArray.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); module.exports = observe$1; //# sourceMappingURL=observe.js.map