Skip to content

Commit

Permalink
v1.0.33
Browse files Browse the repository at this point in the history
  • Loading branch information
ahadrt committed Aug 6, 2018
1 parent 177ac1b commit ff916ca
Show file tree
Hide file tree
Showing 4 changed files with 517 additions and 179 deletions.
226 changes: 120 additions & 106 deletions lib/AsService/AsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); } }
Expand All @@ -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",
Expand All @@ -95,60 +93,78 @@ 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;
});
}
}, {
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;
});
}
}, {
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);
Expand All @@ -168,44 +184,55 @@ 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);
}
}, {
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) {
Expand All @@ -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";
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Loading

0 comments on commit ff916ca

Please sign in to comment.