UNPKG

@mamba-le/auth

Version:
211 lines (181 loc) 5.11 kB
/** * @description 认证模块 * @version 0.3.7 * @author lengyingxin <lengyingxin8966@gmail.com> (https://github.com/LengYXin) */ import _isEmpty from 'lodash/isEmpty'; import _isEqual from 'lodash/isEqual'; import { __awaiter, __generator, __decorate } from 'tslib'; import { BindAll } from 'lodash-decorators'; import { isObservable, toJS, observable, computed, action } from 'mobx'; import { persist } from 'mobx-persist'; import { Subject, lastValueFrom } from 'rxjs'; import { AuthOptions } from './options'; var InfoController = /** @class */ function () { function InfoController(options) { if (options === void 0) { options = { StorageKey: 'mamba-auth-info' }; } this.options = options; /** * 持久化初始化完成 Subject * @type {Promise<any>} * @memberof ControllerUser */ this.HydrateSubject = new Subject(); /** * 数据存储对象 * @protected * @type {T} * @memberof InfoController */ this._value = undefined; /** * 最后一次更新 值 HydrateisStopped 未完成前 存储 用于对比变化 * @protected * @memberof AuthController */ this.lastValue = undefined; this.createHydrate(); } Object.defineProperty(InfoController.prototype, "HydrateAsync", { /** * 持久化初始化完成 Promise * @type {Promise<any>} * @memberof ControllerUser */ get: function get() { return lastValueFrom(this.HydrateSubject, { defaultValue: undefined }); }, enumerable: false, configurable: true }); Object.defineProperty(InfoController.prototype, "HydrateisStopped", { /** * 异步 HydrateSubject 已经完成 * @readonly * @memberof PortalAuthController */ get: function get() { return this.HydrateSubject.isStopped; }, enumerable: false, configurable: true }); Object.defineProperty(InfoController.prototype, "StorageKey", { get: function get() { return "_Auth_" + this.options.StorageKey; }, enumerable: false, configurable: true }); Object.defineProperty(InfoController.prototype, "value", { /** * 外部可访问对象 * @readonly * @type {T} * @memberof InfoController */ get: function get() { return this._value; }, enumerable: false, configurable: true }); /** * 保存 Token * @param _AccessToken * @returns */ InfoController.prototype.onSave = function (value) { try { AuthOptions.writeCheck(); if (_isEqual(value, this._value)) { return AuthOptions.log('Save Value 无值改变', value, '=>', this._value); } if (isObservable(value)) { value = toJS(value); } if (!this.HydrateisStopped) { this.lastValue = value; } this._value = value; } catch (error) { AuthOptions.log('error', error); } }; /** * 清理所有的登录信息 * @return {*} * @memberof AuthController */ InfoController.prototype.onClear = function () { this.onSave(undefined); }; /** * 创建持久化存储 * @memberof BaseModel */ InfoController.prototype.createHydrate = function () { return __awaiter(this, void 0, void 0, function () { var Hydrate, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2,, 3]); if (!AuthOptions.browser) { throw '非浏览器环境 不进行 Hydrate'; } Hydrate = AuthOptions.createHydrate(); persist({ _value: { type: 'object' } })(this); AuthOptions.log("Info " + this.StorageKey, this); return [4 /*yield*/ , Hydrate(this.StorageKey, this)]; case 1: _a.sent(); if (!_isEmpty(this.lastValue) && !_isEqual(this._value, this.lastValue)) { AuthOptions.log("Info " + this.StorageKey + " LastValue", this, this.lastValue); this.onSave(this.lastValue); } this.HydrateSubject.next(true); this.HydrateSubject.complete(); return [3 /*break*/ , 3]; case 2: error_1 = _a.sent(); if (AuthOptions.browser) { AuthOptions.log('error', error_1); } this.HydrateSubject.next(false); this.HydrateSubject.complete(); return [3 /*break*/ , 3]; case 3: return [2 /*return*/ ]; } }); }); }; __decorate([observable], InfoController.prototype, "_value", void 0); __decorate([computed], InfoController.prototype, "value", null); __decorate([action], InfoController.prototype, "onSave", null); InfoController = __decorate([BindAll()], InfoController); return InfoController; }(); export { InfoController }; //# sourceMappingURL=info.js.map