@frui.ts/dirtycheck
Advanced tools
Comparing version 1.0.0-alpha.9 to 1.0.0-alpha.10
@@ -1,22 +0,16 @@ | ||
import type { PropertyName } from "@frui.ts/helpers"; | ||
import DirtyWatcherBase from "./dirtyWatcherBase"; | ||
export interface AutomaticDirtyWatcherParams<TEntity> { | ||
isVisible?: boolean; | ||
includedProperties?: PropertyName<TEntity>[]; | ||
excludedProperties?: PropertyName<TEntity>[]; | ||
import { PropertyName } from "@frui.ts/helpers"; | ||
import { DirtyPropertiesList, IDirtyWatcher } from "./types"; | ||
export interface DirtyWatchConfig<TTarget> { | ||
exclude?: PropertyName<TTarget>[]; | ||
} | ||
export default class AutomaticDirtyWatcher<TEntity = any> extends DirtyWatcherBase<TEntity> { | ||
export default class AutomaticDirtyWatcher<TTarget extends Record<string, any>> implements IDirtyWatcher<TTarget> { | ||
private target; | ||
private _includedProperties?; | ||
private _excludedProperties?; | ||
private _results; | ||
private _watchedProperties; | ||
constructor(target: TEntity, params?: AutomaticDirtyWatcherParams<TEntity>); | ||
checkDirty(): boolean; | ||
checkDirty(propertyName: PropertyName<TEntity>): boolean; | ||
getDirtyProperties(): Iterable<PropertyName<TEntity>>; | ||
private config?; | ||
isDirtyFlagVisible: boolean; | ||
dirtyProperties: DirtyPropertiesList<TTarget>; | ||
private checkedProperties; | ||
constructor(target: TTarget, isDirtyFlagVisible: boolean, config?: DirtyWatchConfig<TTarget> | undefined); | ||
get isDirty(): boolean; | ||
reset(): void; | ||
private shouldWatchProperty; | ||
} | ||
export declare function attachAutomaticDirtyWatcher<TEntity>(target: TEntity): AutomaticDirtyWatcher<TEntity>; | ||
export declare function attachAutomaticDirtyWatcher<TEntity>(target: TEntity, isVisible: boolean): AutomaticDirtyWatcher<TEntity>; | ||
export declare function attachAutomaticDirtyWatcher<TEntity>(target: TEntity, params: AutomaticDirtyWatcherParams<TEntity>): AutomaticDirtyWatcher<TEntity>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.attachAutomaticDirtyWatcher = void 0; | ||
const helpers_1 = require("@frui.ts/helpers"); | ||
const mobx_1 = require("mobx"); | ||
const dirtyWatcherBase_1 = __importDefault(require("./dirtyWatcherBase")); | ||
const utils_1 = require("./utils"); | ||
class AutomaticDirtyWatcher extends dirtyWatcherBase_1.default { | ||
constructor(target, params) { | ||
super(params === null || params === void 0 ? void 0 : params.isVisible); | ||
var helpers_1 = require("@frui.ts/helpers"); | ||
var mobx_1 = require("mobx"); | ||
var AutomaticDirtyWatcher = (function () { | ||
function AutomaticDirtyWatcher(target, isDirtyFlagVisible, config) { | ||
this.target = target; | ||
this._watchedProperties = []; | ||
if (typeof params === "object") { | ||
this._includedProperties = params.includedProperties; | ||
this._excludedProperties = params.excludedProperties; | ||
} | ||
this.config = config; | ||
this.isDirtyFlagVisible = isDirtyFlagVisible; | ||
this.reset(); | ||
} | ||
checkDirty(propertyName) { | ||
if (!this.isEnabled) { | ||
return false; | ||
Object.defineProperty(AutomaticDirtyWatcher.prototype, "isDirty", { | ||
get: function () { | ||
var _this = this; | ||
return this.checkedProperties.some(function (prop) { return !!mobx_1.get(_this.dirtyProperties, prop); }); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
AutomaticDirtyWatcher.prototype.reset = function () { | ||
this.dirtyProperties = {}; | ||
this.checkedProperties = []; | ||
var target = this.target; | ||
var _loop_1 = function (propertyName) { | ||
var _a; | ||
if (target.hasOwnProperty(propertyName) && this_1.shouldWatchProperty(propertyName)) { | ||
var originalValue_1 = target[propertyName]; | ||
helpers_1.ensureObservableProperty(target, propertyName, originalValue_1); | ||
this_1.checkedProperties.push(propertyName); | ||
mobx_1.extendObservable(this_1.dirtyProperties, (_a = {}, | ||
Object.defineProperty(_a, propertyName, { | ||
get: function () { | ||
return mobx_1.get(target, propertyName) !== originalValue_1; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}), | ||
_a)); | ||
} | ||
}; | ||
var this_1 = this; | ||
for (var propertyName in target) { | ||
_loop_1(propertyName); | ||
} | ||
if (propertyName) { | ||
return !!this._results[propertyName]; | ||
} | ||
else { | ||
for (const propertyName of this._watchedProperties) { | ||
if (this._results[propertyName]) { | ||
return true; | ||
} | ||
} | ||
}; | ||
AutomaticDirtyWatcher.prototype.shouldWatchProperty = function (propertyName) { | ||
var _a, _b; | ||
if ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.exclude) === null || _b === void 0 ? void 0 : _b.includes(propertyName)) { | ||
return false; | ||
} | ||
} | ||
*getDirtyProperties() { | ||
if (!this.isEnabled) { | ||
return; | ||
} | ||
for (const propertyName of this._watchedProperties) { | ||
if (this._results[propertyName]) { | ||
yield propertyName; | ||
} | ||
} | ||
} | ||
reset() { | ||
const resultsObject = {}; | ||
this._watchedProperties.length = 0; | ||
for (const name of Object.keys(this.target)) { | ||
const propertyName = name; | ||
if ((!this._includedProperties || this._includedProperties.includes(propertyName)) && | ||
(!this._excludedProperties || !this._excludedProperties.includes(propertyName))) { | ||
this._watchedProperties.push(propertyName); | ||
const entity = this.target; | ||
const originalValue = (0, mobx_1.get)(entity, propertyName); | ||
if ((0, mobx_1.isArrayLike)(originalValue)) { | ||
defineArrayDirtyWatchProperty(resultsObject, originalValue, propertyName, entity); | ||
} | ||
else if ((0, helpers_1.isSet)(originalValue) || (0, mobx_1.isObservableSet)(originalValue)) { | ||
defineSetDirtyCheckProperty(resultsObject, originalValue, propertyName, entity); | ||
} | ||
else { | ||
Object.defineProperty(resultsObject, propertyName, { | ||
get: () => { | ||
const currentValue = (0, mobx_1.get)(entity, propertyName); | ||
return originalValue !== currentValue; | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
this._results = (0, mobx_1.observable)(resultsObject); | ||
} | ||
} | ||
return true; | ||
}; | ||
__decorate([ | ||
mobx_1.observable, | ||
__metadata("design:type", Boolean) | ||
], AutomaticDirtyWatcher.prototype, "isDirtyFlagVisible", void 0); | ||
__decorate([ | ||
mobx_1.observable, | ||
__metadata("design:type", Object) | ||
], AutomaticDirtyWatcher.prototype, "dirtyProperties", void 0); | ||
__decorate([ | ||
mobx_1.computed, | ||
__metadata("design:type", Object), | ||
__metadata("design:paramtypes", []) | ||
], AutomaticDirtyWatcher.prototype, "isDirty", null); | ||
__decorate([ | ||
mobx_1.action, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], AutomaticDirtyWatcher.prototype, "reset", null); | ||
return AutomaticDirtyWatcher; | ||
}()); | ||
exports.default = AutomaticDirtyWatcher; | ||
function defineArrayDirtyWatchProperty(resultsObject, originalValue, propertyName, entity) { | ||
const arraySnapshot = originalValue.slice(); | ||
Object.defineProperty(resultsObject, propertyName, { | ||
get: () => { | ||
const currentValue = (0, mobx_1.get)(entity, propertyName); | ||
return (!currentValue || | ||
currentValue.length !== arraySnapshot.length || | ||
arraySnapshot.some((originalItem, index) => originalItem !== currentValue[index])); | ||
}, | ||
}); | ||
} | ||
function defineSetDirtyCheckProperty(resultsObject, originalValue, propertyName, entity) { | ||
const setSnapshot = new Set(originalValue); | ||
Object.defineProperty(resultsObject, propertyName, { | ||
get: () => { | ||
const currentValue = (0, mobx_1.get)(entity, propertyName); | ||
if (!currentValue || currentValue.size !== setSnapshot.size) { | ||
return true; | ||
} | ||
for (const item of setSnapshot.values()) { | ||
if (!currentValue.has(item)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
}); | ||
} | ||
function attachAutomaticDirtyWatcher(target, params) { | ||
const paramsObject = typeof params === "boolean" ? { isVisible: params } : params; | ||
const automaticDirtyWatcher = new AutomaticDirtyWatcher(target, paramsObject); | ||
(0, utils_1.attachDirtyWatcher)(target, automaticDirtyWatcher); | ||
return automaticDirtyWatcher; | ||
} | ||
exports.attachAutomaticDirtyWatcher = attachAutomaticDirtyWatcher; | ||
//# sourceMappingURL=automaticDirtyWatcher.js.map |
@@ -1,5 +0,4 @@ | ||
export { default as AutomaticDirtyWatcher, attachAutomaticDirtyWatcher } from "./automaticDirtyWatcher"; | ||
export { default as Configuration } from "./configuration"; | ||
export { default as ManualDirtyWatcher } from "./manualDirtyWatcher"; | ||
export { default as AutomaticDirtyWatcher } from "./automaticDirtyWatcher"; | ||
export * from "./helpers"; | ||
export { default as ManualDirtyWatcher, hasManualDirtyWatcher, setDirty } from "./manualDirtyWatcher"; | ||
export * from "./types"; | ||
export * from "./utils"; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ManualDirtyWatcher = exports.Configuration = exports.attachAutomaticDirtyWatcher = exports.AutomaticDirtyWatcher = void 0; | ||
var automaticDirtyWatcher_1 = require("./automaticDirtyWatcher"); | ||
Object.defineProperty(exports, "AutomaticDirtyWatcher", { enumerable: true, get: function () { return __importDefault(automaticDirtyWatcher_1).default; } }); | ||
Object.defineProperty(exports, "attachAutomaticDirtyWatcher", { enumerable: true, get: function () { return automaticDirtyWatcher_1.attachAutomaticDirtyWatcher; } }); | ||
var configuration_1 = require("./configuration"); | ||
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return __importDefault(configuration_1).default; } }); | ||
exports.AutomaticDirtyWatcher = automaticDirtyWatcher_1.default; | ||
__export(require("./helpers")); | ||
var manualDirtyWatcher_1 = require("./manualDirtyWatcher"); | ||
Object.defineProperty(exports, "ManualDirtyWatcher", { enumerable: true, get: function () { return __importDefault(manualDirtyWatcher_1).default; } }); | ||
__exportStar(require("./types"), exports); | ||
__exportStar(require("./utils"), exports); | ||
exports.ManualDirtyWatcher = manualDirtyWatcher_1.default; | ||
exports.hasManualDirtyWatcher = manualDirtyWatcher_1.hasManualDirtyWatcher; | ||
exports.setDirty = manualDirtyWatcher_1.setDirty; | ||
//# sourceMappingURL=index.js.map |
import { PropertyName } from "@frui.ts/helpers"; | ||
import DirtyWatcherBase from "./dirtyWatcherBase"; | ||
export default class ManualDirtyWatcher<TEntity = any> extends DirtyWatcherBase<TEntity> { | ||
protected _dirtyProperties: import("mobx").ObservableSet<PropertyName<TEntity>>; | ||
checkDirty(): boolean; | ||
checkDirty(propertyName: PropertyName<TEntity>): boolean; | ||
getDirtyProperties(): Iterable<PropertyName<TEntity>>; | ||
setDirty(propertyName: PropertyName<TEntity>, isDirty?: boolean): void; | ||
import { DirtyPropertiesList, IHasManualDirtyWatcher, IManualDirtyWatcher } from "./types"; | ||
export default class ManualDirtyWatcher<TTarget> implements IManualDirtyWatcher<TTarget> { | ||
isDirtyFlagVisible: boolean; | ||
dirtyProperties: DirtyPropertiesList<TTarget>; | ||
constructor(isDirtyFlagVisible: boolean); | ||
reset(): void; | ||
setDirty(propertyName: PropertyName<TTarget>, isDirty?: boolean): void; | ||
get isDirty(): boolean; | ||
} | ||
export declare function hasManualDirtyWatcher<TTarget>(target: any): target is IHasManualDirtyWatcher<TTarget>; | ||
export declare function setDirty<TTarget>(target: TTarget, propertyName: PropertyName<TTarget>, isDirty?: boolean): void; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
@@ -20,9 +8,2 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
@@ -32,53 +13,64 @@ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const mobx_1 = require("mobx"); | ||
const dirtyWatcherBase_1 = __importStar(require("./dirtyWatcherBase")); | ||
class ManualDirtyWatcher extends dirtyWatcherBase_1.default { | ||
constructor() { | ||
super(...arguments); | ||
this._dirtyProperties = mobx_1.observable.set(); | ||
var mobx_1 = require("mobx"); | ||
var ManualDirtyWatcher = (function () { | ||
function ManualDirtyWatcher(isDirtyFlagVisible) { | ||
this.dirtyProperties = {}; | ||
this.isDirtyFlagVisible = isDirtyFlagVisible; | ||
} | ||
checkDirty(propertyName) { | ||
if (!this.isEnabled) { | ||
return false; | ||
} | ||
if (propertyName) { | ||
return this._dirtyProperties.has(propertyName); | ||
} | ||
else { | ||
return this._dirtyProperties.size > 0; | ||
} | ||
ManualDirtyWatcher.prototype.reset = function () { | ||
var _this = this; | ||
mobx_1.keys(this.dirtyProperties).forEach(function (prop) { return mobx_1.remove(_this.dirtyProperties, prop); }); | ||
}; | ||
ManualDirtyWatcher.prototype.setDirty = function (propertyName, isDirty) { | ||
if (isDirty === void 0) { isDirty = true; } | ||
mobx_1.set(this.dirtyProperties, propertyName, isDirty); | ||
}; | ||
Object.defineProperty(ManualDirtyWatcher.prototype, "isDirty", { | ||
get: function () { | ||
return mobx_1.values(this.dirtyProperties).some(function (x) { return !!x; }); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
__decorate([ | ||
mobx_1.observable, | ||
__metadata("design:type", Boolean) | ||
], ManualDirtyWatcher.prototype, "isDirtyFlagVisible", void 0); | ||
__decorate([ | ||
mobx_1.observable, | ||
__metadata("design:type", Object) | ||
], ManualDirtyWatcher.prototype, "dirtyProperties", void 0); | ||
__decorate([ | ||
mobx_1.action, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], ManualDirtyWatcher.prototype, "reset", null); | ||
__decorate([ | ||
mobx_1.action, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [String, Object]), | ||
__metadata("design:returntype", void 0) | ||
], ManualDirtyWatcher.prototype, "setDirty", null); | ||
__decorate([ | ||
mobx_1.computed, | ||
__metadata("design:type", Object), | ||
__metadata("design:paramtypes", []) | ||
], ManualDirtyWatcher.prototype, "isDirty", null); | ||
return ManualDirtyWatcher; | ||
}()); | ||
exports.default = ManualDirtyWatcher; | ||
function hasManualDirtyWatcher(target) { | ||
return (!!target && | ||
target.__dirtycheck !== undefined && | ||
typeof target.__dirtycheck.setDirty === "function"); | ||
} | ||
exports.hasManualDirtyWatcher = hasManualDirtyWatcher; | ||
function setDirty(target, propertyName, isDirty) { | ||
if (isDirty === void 0) { isDirty = true; } | ||
if (hasManualDirtyWatcher(target)) { | ||
target.__dirtycheck.setDirty(propertyName, isDirty); | ||
} | ||
getDirtyProperties() { | ||
if (this.isEnabled) { | ||
return this._dirtyProperties; | ||
} | ||
else { | ||
return dirtyWatcherBase_1.emptyResults; | ||
} | ||
} | ||
setDirty(propertyName, isDirty = true) { | ||
if (isDirty) { | ||
this._dirtyProperties.add(propertyName); | ||
} | ||
else { | ||
this._dirtyProperties.delete(propertyName); | ||
} | ||
} | ||
reset() { | ||
this._dirtyProperties.clear(); | ||
} | ||
} | ||
__decorate([ | ||
mobx_1.action, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [String, Object]), | ||
__metadata("design:returntype", void 0) | ||
], ManualDirtyWatcher.prototype, "setDirty", null); | ||
__decorate([ | ||
mobx_1.action, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:returntype", void 0) | ||
], ManualDirtyWatcher.prototype, "reset", null); | ||
exports.default = ManualDirtyWatcher; | ||
exports.setDirty = setDirty; | ||
//# sourceMappingURL=manualDirtyWatcher.js.map |
@@ -1,13 +0,17 @@ | ||
import type { PropertyName } from "@frui.ts/helpers"; | ||
export interface EntityDirtyWatcher<TEntity> { | ||
isEnabled: boolean; | ||
isVisible: boolean; | ||
readonly isDirty: boolean; | ||
checkDirty(): boolean; | ||
checkDirty(propertyName: PropertyName<TEntity>): boolean; | ||
checkDirtyVisible(): boolean; | ||
checkDirtyVisible(propertyName: PropertyName<TEntity>): boolean; | ||
getDirtyProperties(): Iterable<PropertyName<TEntity>>; | ||
getDirtyVisibleProperties(): Iterable<PropertyName<TEntity>>; | ||
import { PropertyName } from "@frui.ts/helpers"; | ||
export declare type DirtyPropertiesList<TTarget> = Partial<Record<PropertyName<TTarget>, boolean>>; | ||
export interface IDirtyWatcher<TTarget> { | ||
isDirty: boolean; | ||
isDirtyFlagVisible: boolean; | ||
dirtyProperties: Readonly<DirtyPropertiesList<TTarget>>; | ||
reset(): void; | ||
} | ||
export interface IManualDirtyWatcher<TTarget> extends IDirtyWatcher<TTarget> { | ||
setDirty(propertyName: PropertyName<TTarget>, isDirty?: boolean): void; | ||
} | ||
export interface IHasDirtyWatcher<TTarget> { | ||
__dirtycheck: IDirtyWatcher<TTarget>; | ||
} | ||
export interface IHasManualDirtyWatcher<TTarget> extends IHasDirtyWatcher<TTarget> { | ||
__dirtycheck: IManualDirtyWatcher<TTarget>; | ||
} |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "1.0.0-alpha.9", | ||
"version": "1.0.0-alpha.10", | ||
"description": "Observable dirty checking", | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@frui.ts/helpers": "^1.0.0-alpha.9" | ||
"@frui.ts/helpers": "^1.0.0-alpha.10" | ||
}, | ||
@@ -45,3 +45,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "718298c96617555dd2121f87a2dfefb3ea16d039" | ||
"gitHead": "5454d007244bb9cdd28be5207a9f125f04a9ac43" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
1
133
32119
18
289
1