diff --git a/lib/AsService/AsService.js b/lib/AsService/AsService.js index 4a959b7..9acadcd 100644 --- a/lib/AsService/AsService.js +++ b/lib/AsService/AsService.js @@ -9,6 +9,14 @@ var _rxjs = _interopRequireDefault(require("rxjs")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } + +function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } + +function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } + +function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } + function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } @@ -24,66 +32,56 @@ var btoa = btoa || function (str) { var AsService = /*#__PURE__*/ function () { - function AsService(loader, mapper, autoload) { + function AsService(loader, mapper, autoload, paramcount, forceSourceLoad) { _classCallCheck(this, AsService); + _defineProperty(this, "_forceSourceLoad", false); + + _defineProperty(this, "_paramCount", 0); + _defineProperty(this, "_subs", {}); - _defineProperty(this, "_lastParams", null); + _defineProperty(this, "_lastParams", []); _defineProperty(this, "_sub", new _rxjs.default.BehaviorSubject()); _defineProperty(this, "_errorSub", new _rxjs.default.BehaviorSubject()); - _defineProperty(this, "_isLoading", false); - - _defineProperty(this, "ready", false); - if (!loader) { - console.log("barte error:", "btService", "loader is not set"); - return undefined; + console.log("barte error:", "btService", "loader is not set"); //return undefined; } - if (loader.$$isAsService) { - this._loader = loader.getLoader(); - var sourceMapper = loader.getMapper(); - - if (sourceMapper) { - if (mapper) { - this._mapper = function (data) { - for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { - params[_key - 1] = arguments[_key]; - } - - return mapper.apply(void 0, [sourceMapper.apply(void 0, [data].concat(params))].concat(params)); - }; - } else { - this._mapper = function () { - return sourceMapper.apply(void 0, arguments); - }; - } - - console.log(175, sourceMapper, this._mapper); - } + if (loader !== undefined && loader.$$isAsService) { + this._source = loader; + this._paramCount = paramcount; + this._forceSourceLoad = forceSourceLoad !== undefined; } else { - this._loader = typeof loader === "function" ? loader : function () { - return loader; - }; - this._mapper = mapper; + if (typeof loader === "function") { + this._loader = loader; + this._paramCount = loader.length; + } else { + this._loader = function () { + return loader; + }; + } + + if (autoload === true) this._reload(); } + this._mapper = mapper; this._autoload = autoload ? true : false; - if (autoload === true) this._reload(); this.$$isAsService = true; } _createClass(AsService, [{ key: "map", - value: function map(mapper) { + value: function map(mapper, forceLoadSource) { + console.log(1177, mapper); + if (!mapper || typeof mapper !== "function") { console.log("AsService Error: mapper function not set"); return undefined; - } else return new AsService(this, mapper, this._autoload); + } else return new AsService(this, mapper, this._autoload, mapper.length - 1, forceLoadSource); } }, { key: "getMapper", @@ -95,24 +93,10 @@ function () { value: function getLoader() { return this._loader; } - }, { - key: "publishNull", - value: function publishNull() { - for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { - params[_key2] = arguments[_key2]; - } - - var subfor = this.getSub(params); - subfor.sub.next(null); - } }, { key: "ErrorObservable", value: function ErrorObservable() { - for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { - params[_key3] = arguments[_key3]; - } - - var subfor = this.getSub(params); + var subfor = this.getSub.apply(this, arguments); return subfor.errorSub.filter(function (a) { return a !== undefined; }); @@ -120,11 +104,7 @@ function () { }, { key: "Observable", value: function Observable() { - for (var _len4 = arguments.length, params = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { - params[_key4] = arguments[_key4]; - } - - var subfor = this.getSub(params); + var subfor = this.getSub.apply(this, arguments); return subfor.sub.filter(function (a) { return a !== undefined; }); @@ -132,23 +112,59 @@ function () { }, { key: "refresh", value: function refresh() { - return this._reload(this._lastParams); + return this.forceLaod.apply(this, _toConsumableArray(this._lastParams)); + } + }, { + key: "forceLoad", + value: function forceLoad() { + var _this = this; + + for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) { + params[_key] = arguments[_key]; + } + + this._lastParams = params; + + if (this._source) { + var _this$_source; + + return (_this$_source = this._source).load.apply(_this$_source, params).then(function (a) { + return _this._mapper ? _this._mapper.apply(_this, [a].concat(params)) : a; + }).catch(function (e) { + return e; + }); + } else return this._reload.apply(this, params); } }, { key: "load", value: function load() { - return this._reload.apply(this, arguments); + var _this2 = this; + + for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { + params[_key2] = arguments[_key2]; + } + + this._lastParams = params; + + if (this._source) { + if (this._forceSourceLoad) { + return this.forceLoad.apply(this, params); + } else { + var _this$_source2; + + return (_this$_source2 = this._source).get.apply(_this$_source2, params).then(function (a) { + return _this2._mapper ? _this2._mapper.apply(_this2, [a].concat(params)) : a; + }).catch(function (e) { + return e; + }); + } + } else return this._reload.apply(this, params); } }, { key: "get", value: function get() { - for (var _len5 = arguments.length, params = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { - params[_key5] = arguments[_key5]; - } - - console.log(179, params); - var subfor = this.getSub(params); - if (subfor.state === "start") return this._reload.apply(this, params);else { + var subfor = this.getSub.apply(this, arguments); + if (subfor.state === "start") return this.load.apply(this, arguments);else { var ret = new Promise(function (res, rej) { var subs = subfor.sub.subscribe(function (a) { res(a); @@ -168,11 +184,7 @@ function () { }, { key: "publish", value: function publish() { - for (var _len6 = arguments.length, params = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) { - params[_key6] = arguments[_key6]; - } - - var sub = this.getSub(params); + var sub = this.getSub.apply(this, arguments); sub.next(this.$data); this._sub.next(this.$data); @@ -180,32 +192,47 @@ function () { }, { key: "getSub", value: function getSub() { - for (var _len7 = arguments.length, params = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) { - params[_key7] = arguments[_key7]; + var _this3 = this; + + for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { + params[_key3] = arguments[_key3]; } - var tmp = btoa(encodeURIComponent(params)); + var pars = params.slice(0, this._paramCount); + var tmp = btoa(encodeURIComponent(pars)); if (!this._subs[tmp]) { + var _this$_source3, _this$_source4; + this._subs[tmp] = { - sub: new _rxjs.default.BehaviorSubject(), - errorSub: new _rxjs.default.BehaviorSubject(), - state: "start" + sub: this._source === undefined ? new _rxjs.default.BehaviorSubject() : (_this$_source3 = this._source).Observable.apply(_this$_source3, params).map(function (a) { + return _this3._mapper.apply(_this3, [a].concat(_toConsumableArray(pars))); + }), + errorSub: this._source === undefined ? new _rxjs.default.BehaviorSubject() : (_this$_source4 = this._source).ErrorObservable.apply(_this$_source4, params).map(function (a) { + return _this3._mapper.apply(_this3, [a].concat(_toConsumableArray(pars))); + }), + state: this._source === undefined ? "start" : "idle" }; } return this._subs[tmp]; } + }, { + key: "getState", + value: function getState() { + var sub = this.getSub.apply(this, arguments); + return sub.state; + } }, { key: "_reload", value: function _reload() { - var _this = this; + var _this4 = this; - for (var _len8 = arguments.length, params = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) { - params[_key8] = arguments[_key8]; + for (var _len4 = arguments.length, params = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { + params[_key4] = arguments[_key4]; } - var subfor = this.getSub(params); + var subfor = this.getSub.apply(this, params); if (subfor.state === "loading") { var _ret = new Promise(function (res, rej) { @@ -220,66 +247,53 @@ function () { }); return _ret; - } // } - + } var ret = new Promise(function (res, rej) { subfor.state = "loading"; - var fnret = _this._loader.apply(_this, params); + var fnret = _this4._loader.apply(_this4, params); - var r = fnret; //if (r instanceof Rx.Observable) { + var r = fnret; if (r._isScalar !== undefined) { if (subfor.sourceObservable !== undefined) subfor.sourceObservable.unsubscribe(); subfor.sourceObservable = r.subscribe(function (b) { - var ret = _this._mapper ? _this._mapper.apply(_this, [b].concat(params)) : b; + var ret = _this4._mapper ? _this4._mapper.apply(_this4, [b].concat(params)) : b; subfor.sub.next(ret); - console.log("xxxxxxxxxx", ret); - _this._sub.next(ret); - - _this._isLoading = false; - _this._loaded = true; + _this4._sub.next(ret); }); } else if (r instanceof Promise) { fnret.then(function (d) { - _this.ready = true; subfor.state = "idle"; - var ret = _this._mapper ? _this._mapper.apply(_this, [d].concat(params)) : d; - _this.$data = ret; + var ret = _this4._mapper ? _this4._mapper.apply(_this4, [d].concat(params)) : d; + _this4.$data = ret; res(ret); subfor.sub.next(ret); - _this._sub.next(ret); - - _this._isLoading = false; - _this._loaded = true; + _this4._sub.next(ret); }).catch(function (e) { - subfor.state = "idle"; + subfor.state = "start"; try { res(ret); } catch (e) { rej(e); - } // this ._subs[tmp].sub .error(e); - + } subfor.errorSub.next(e); - _this._errorSub.next(e); // this._sub.error(e); - + _this4._errorSub.next(e); }); } else { - var _ret2 = _this._mapper ? _this._mapper.apply(_this, [r].concat(params)) : r; + var _ret2 = _this4._mapper ? _this4._mapper.apply(_this4, [r].concat(params)) : r; res(_ret2); subfor.sub.next(_ret2); - _this._sub.next(_ret2); + _this4._sub.next(_ret2); - _this._isLoading = false; - _this._loaded = true; subfor.state = "idle"; } }); diff --git a/package.json b/package.json index f88f08c..32ab7cd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@barteh/as-service", - "version": "1.0.29", + "version": "1.0.33", "description": "observable data service based on rxjs using axios for conunicate with server via http (XHR). ", "main": "index.js", "scripts": { diff --git a/src/AsService/AsService.1.js b/src/AsService/AsService.1.js new file mode 100644 index 0000000..2ac1458 --- /dev/null +++ b/src/AsService/AsService.1.js @@ -0,0 +1,298 @@ +import Rx from 'rxjs'; + +var btoa = btoa || function (str) { + return new Buffer(str).toString('base64'); +}; +export default class AsService { + + constructor(loader, mapper, autoload) { + if (!loader) { + console.log("barte error:", "btService", "loader is not set"); + return undefined; + } + + if (loader.$$isAsService) { + this._loader = loader.getLoader(); + let sourceMapper = loader.getMapper(); + if (sourceMapper) { + if (mapper) { + + this._mapper = (data, ...params) => mapper(sourceMapper(data, ...params), ...params); + } else { + + this._mapper = (...params) => { + return sourceMapper(...params); + } + + } + console.log(175, sourceMapper, this._mapper); + } + + } else { + this._loader = typeof loader === "function" + ? loader + : () => loader; + this._mapper = mapper; + } + + this._autoload = autoload + ? true + : false; + if (autoload === true) + this._reload(); + + this.$$isAsService = true; + } + + map(mapper) { + if (!mapper || typeof mapper !== "function") { + console.log("AsService Error: mapper function not set") + return undefined; + } else + return new AsService(this, mapper, this._autoload); + } + + getMapper() { + return this._mapper; + } + getLoader() { + return this._loader; + } + + _subs = {}; + _lastParams = null; + _sub = new Rx.BehaviorSubject(); + _errorSub = new Rx.BehaviorSubject(); + + get ObservableAll() { + return this + ._sub + .filter(a => a !== undefined); + } + + get ErrorObservableAll() { + return this + ._errorSub + .filter(a => a !== undefined); + } + + publishNull(...params) { + let subfor = this.getSub(params); + subfor + .sub + .next(null); + } + + ErrorObservable(...params) { + + let subfor = this.getSub(params) + return subfor + .errorSub + .filter(a => a !== undefined); + + } + Observable(...params) { + let subfor = this.getSub(params); + return subfor + .sub + .filter(a => a !== undefined); + + } + + refresh() { + return this._reload(this._lastParams); + } + + load(...params) { + + return this._reload(...params); + } + + get(...params) { + console.log(179, params); + let subfor = this.getSub(params); + + if (subfor.state === "start") + return this._reload(...params); + else { + let ret = new Promise((res, rej) => { + let subs = subfor + .sub + .subscribe(a => { + res(a); + subs.unsubscribe(); + }); + let esubs = subfor + .errorSub + .subscribe(e => { + rej(e); + esubs.unsubscribe(); + }); + + }); + + return ret; + } + } + + publishAll() {} + publish(...params) { + let sub = this.getSub(params); + sub.next(this.$data); + this + ._sub + .next(this.$data); + } + + _isLoading = false; + + getSub(...params) { + let tmp = btoa(encodeURIComponent(params)); + + if (!this._subs[tmp]) { + + this._subs[tmp] = { + sub: new Rx.BehaviorSubject(), + errorSub: new Rx.BehaviorSubject(), + state: "start" + + }; + } + return this._subs[tmp]; + + } + + ready = false; + + _reload(...params) { + + let subfor = this.getSub(params); + if (subfor.state === "loading") { + + let ret = new Promise((res, rej) => { + let subs = subfor + .sub + .subscribe(a => { + res(a); + subs.unsubscribe(); + }); + let esubs = subfor + .errorSub + .subscribe(e => { + rej(e); + esubs.unsubscribe(); + }); + + }); + + return ret; + } + + // } + + let ret = new Promise((res, rej) => { + + subfor.state = "loading"; + + let fnret = this._loader(...params); + + const r = fnret; + + //if (r instanceof Rx.Observable) { + if (r._isScalar !== undefined) { + if (subfor.sourceObservable !== undefined) + subfor.sourceObservable.unsubscribe(); + subfor.sourceObservable = r.subscribe(b => { + + let ret = this._mapper + ? this._mapper(b, ...params) + : b; + subfor + .sub + .next(ret); + console.log("xxxxxxxxxx", ret); + + this + ._sub + .next(ret); + + this._isLoading = false; + this._loaded = true; + + }) + + } else if (r instanceof Promise) { + fnret.then(d => { + this.ready = true; + subfor.state = "idle"; + + let ret = this._mapper + ? this._mapper(d, ...params) + : d; + + this.$data = ret; + + res(ret); + + subfor + .sub + .next(ret); + + this + ._sub + .next(ret); + + this._isLoading = false; + this._loaded = true; + + }).catch(e => { + subfor.state = "idle"; + + try { + res(ret); + + } catch (e) { + rej(e); + } + + // this ._subs[tmp].sub .error(e); + subfor + .errorSub + .next(e); + + this + ._errorSub + .next(e); + + // this._sub.error(e); + + }); + + } else { + + let ret = this._mapper + ? this._mapper(r, ...params) + : r; + res(ret); + subfor + .sub + .next(ret); + + this + ._sub + .next(ret); + + this._isLoading = false; + this._loaded = true; + subfor.state = "idle"; + + } + + }); + + return ret; + } + + _getFromPromise() {} + +} \ No newline at end of file diff --git a/src/AsService/AsService.js b/src/AsService/AsService.js index 3b29a64..625f7fa 100644 --- a/src/AsService/AsService.js +++ b/src/AsService/AsService.js @@ -5,51 +5,46 @@ var btoa = btoa || function (str) { }; export default class AsService { - constructor(loader, mapper, autoload) { + constructor(loader, mapper, autoload, paramcount, forceSourceLoad) { if (!loader) { console.log("barte error:", "btService", "loader is not set"); - return undefined; + //return undefined; } - if (loader.$$isAsService) { - this._loader = loader.getLoader(); - let sourceMapper = loader.getMapper(); - if (sourceMapper) { - if (mapper) { - - this._mapper = (data, ...params) => mapper(sourceMapper(data, ...params), ...params); - } else { - - this._mapper = (...params) => { - return sourceMapper(...params); - } - - } - console.log(175, sourceMapper, this._mapper); - } - + if (loader!==undefined && loader.$$isAsService) { + this._source = loader; + this._paramCount = paramcount; + this._forceSourceLoad = forceSourceLoad !== undefined; + } else { - this._loader = typeof loader === "function" - ? loader - : () => loader; - this._mapper = mapper; - } + if (typeof loader === "function") { + this._loader = loader; + this._paramCount = loader.length; + } else { + this._loader = () => loader; + } + if (autoload === true) + this._reload(); + } + + this._mapper = mapper; this._autoload = autoload ? true : false; - if (autoload === true) - this._reload(); this.$$isAsService = true; } + _forceSourceLoad = false; + _paramCount = 0; + map(mapper, forceLoadSource) { + console.log(1177, mapper); - map(mapper) { if (!mapper || typeof mapper !== "function") { console.log("AsService Error: mapper function not set") return undefined; } else - return new AsService(this, mapper, this._autoload); + return new AsService(this, mapper, this._autoload, mapper.length - 1, forceLoadSource); } getMapper() { @@ -60,7 +55,7 @@ export default class AsService { } _subs = {}; - _lastParams = null; + _lastParams = []; _sub = new Rx.BehaviorSubject(); _errorSub = new Rx.BehaviorSubject(); @@ -76,23 +71,19 @@ export default class AsService { .filter(a => a !== undefined); } - publishNull(...params) { - let subfor = this.getSub(params); - subfor - .sub - .next(null); - } + ErrorObservable(...params) { - let subfor = this.getSub(params) + let subfor = this.getSub(...params) return subfor .errorSub .filter(a => a !== undefined); } Observable(...params) { - let subfor = this.getSub(params); + let subfor = this.getSub(...params); + return subfor .sub .filter(a => a !== undefined); @@ -100,20 +91,52 @@ export default class AsService { } refresh() { - return this._reload(this._lastParams); + return this.forceLaod(...this._lastParams); } + forceLoad(...params) { + this._lastParams=params; + if (this._source) { + + return this + ._source + .load(...params) + .then(a => this._mapper + ? this._mapper(a, ...params) + : a) + .catch(e => e); + + } else + return this._reload(...params); + } + load(...params) { +this._lastParams=params; + if (this._source) { + if (this._forceSourceLoad) { + return this.forceLoad(...params); + } else { - return this._reload(...params); - } + return this + ._source + .get(...params) + .then(a => this._mapper + ? this._mapper(a, ...params) + : a) + .catch(e => e); + } + } else + return this._reload(...params); + } + get(...params) { - console.log(179, params); - let subfor = this.getSub(params); + + + let subfor = this.getSub(...params); if (subfor.state === "start") - return this._reload(...params); + return this.load(...params); else { let ret = new Promise((res, rej) => { let subs = subfor @@ -137,36 +160,51 @@ export default class AsService { publishAll() {} publish(...params) { - let sub = this.getSub(params); + let sub = this.getSub(...params); sub.next(this.$data); this ._sub .next(this.$data); } - _isLoading = false; - getSub(...params) { - let tmp = btoa(encodeURIComponent(params)); + + const pars = params.slice(0, this._paramCount); + let tmp = btoa(encodeURIComponent(pars)); if (!this._subs[tmp]) { this._subs[tmp] = { - sub: new Rx.BehaviorSubject(), - errorSub: new Rx.BehaviorSubject(), - state: "start" - + sub: this._source === undefined + ? new Rx.BehaviorSubject() + : this + ._source + .Observable(...params) + .map(a => this._mapper(a, ...pars)), + errorSub: this._source === undefined + ? new Rx.BehaviorSubject() + : this + ._source + .ErrorObservable(...params) + .map(a => this._mapper(a, ...pars)), + state: this._source === undefined + ? "start" + : "idle" }; + } return this._subs[tmp]; } - ready = false; + getState(...params){ + const sub=this.getSub(...params); + return sub.state; + } _reload(...params) { - let subfor = this.getSub(params); + let subfor = this.getSub(...params); if (subfor.state === "loading") { let ret = new Promise((res, rej) => { @@ -188,7 +226,6 @@ export default class AsService { return ret; } - // } let ret = new Promise((res, rej) => { @@ -198,12 +235,10 @@ export default class AsService { const r = fnret; - //if (r instanceof Rx.Observable) { + if (r._isScalar !== undefined) { - if(subfor.sourceObservable!==undefined) - subfor - .sourceObservable - .unsubscribe(); + if (subfor.sourceObservable !== undefined) + subfor.sourceObservable.unsubscribe(); subfor.sourceObservable = r.subscribe(b => { let ret = this._mapper @@ -212,20 +247,16 @@ export default class AsService { subfor .sub .next(ret); - console.log("xxxxxxxxxx", ret); this ._sub .next(ret); - this._isLoading = false; - this._loaded = true; - }) } else if (r instanceof Promise) { fnret.then(d => { - this.ready = true; + subfor.state = "idle"; let ret = this._mapper @@ -244,11 +275,8 @@ export default class AsService { ._sub .next(ret); - this._isLoading = false; - this._loaded = true; - }).catch(e => { - subfor.state = "idle"; + subfor.state = "start"; try { res(ret); @@ -257,7 +285,7 @@ export default class AsService { rej(e); } - // this ._subs[tmp].sub .error(e); + subfor .errorSub .next(e); @@ -266,7 +294,7 @@ export default class AsService { ._errorSub .next(e); - // this._sub.error(e); + }); @@ -284,8 +312,6 @@ export default class AsService { ._sub .next(ret); - this._isLoading = false; - this._loaded = true; subfor.state = "idle"; }