Comparing version 1.0.1 to 1.0.2
95
index.js
@@ -1,7 +0,1 @@ | ||
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; | ||
}; | ||
(function (factory) { | ||
@@ -21,7 +15,2 @@ if (typeof module === "object" && typeof module.exports === "object") { | ||
const observedObjectsSymbol_1 = require("./observedObjectsSymbol"); | ||
/** | ||
* A Subject that will only memorize its subscribers | ||
* And remove them on unsubscribe. | ||
* Unsubscription will not close or stop the Subject itself. | ||
*/ | ||
class ReusableSubject extends rxjs_1.Subject { | ||
@@ -47,8 +36,5 @@ unsubscribe() { | ||
const handlers = Object.assign(optHandlers, { | ||
// Note for future: leave receiver as parameter even if not used | ||
// to keep args as the last and not include receiver in this one | ||
set: (obj, prop, value, receiver, ...args) => { | ||
let notificationChain; | ||
if (typeof value === "object" && !Array.isArray(value)) { | ||
// Creating the chain of properties that will be notified | ||
notificationChain = Object.assign({ | ||
@@ -63,13 +49,2 @@ [prop]: value, | ||
} | ||
/* | ||
* We when we set a property which will be an object | ||
* we set it as a Proxy and pass it | ||
* an edited SETTER with binded trailing keys to reach | ||
* this property. | ||
* E.g. if we have an object structure like x.y.z.w | ||
* x, x.y and x.y.z will be Proxies; each property | ||
* will receive a setter with the parent keys. | ||
* w property, will receive below (in else), | ||
* ["x", "y", "z"] as args. | ||
*/ | ||
obj[prop] = new Proxy(value, Object.assign(handlers, { | ||
@@ -80,14 +55,3 @@ set: bindLast(handlers.set, [...args, prop]), | ||
else { | ||
/* | ||
* We finalize the path of the keys passed in the above condition | ||
* to reach “object endpoint” (like "w" for the prev. example) | ||
* The path keys composition, let us subscribe to observables | ||
* with dot notation like x.y.z.w | ||
*/ | ||
if (obj[prop] === value) { | ||
/* | ||
* If the value is the same, we return true. | ||
* This cannot be considered as a fail. Also, failing would bring | ||
* a strict-mode script to throw a TypeError. | ||
*/ | ||
return true; | ||
@@ -109,3 +73,2 @@ } | ||
const value = notificationChain[keyPath]; | ||
// We want both single properties an complex objects to be notified when edited | ||
if (this[observedObjectsSymbol_1.observedObjects][keyPath]) { | ||
@@ -126,9 +89,2 @@ this[observedObjectsSymbol_1.observedObjects][keyPath].next(value); | ||
} | ||
/** | ||
* Registers a custom property to be observed. | ||
* | ||
* @param {string} prop - The property or object | ||
* property to subscribe to (e.g. `epsilon` | ||
* or `time.current`) | ||
*/ | ||
observe(prop) { | ||
@@ -140,28 +96,13 @@ if (!this[observedObjectsSymbol_1.observedObjects][prop]) { | ||
} | ||
/** | ||
* Unsubscribes from all the subscriptions in a specific pool | ||
* @param subscriptions | ||
*/ | ||
unsubscribeAll(subscriptions) { | ||
subscriptions.forEach(sub => sub.unsubscribe()); | ||
} | ||
/** | ||
* Returns the current object without proxies | ||
*/ | ||
snapshot() { | ||
return Object.assign({}, this); | ||
const snapshot = Object.assign({}, this); | ||
delete snapshot[observedObjectsSymbol_1.observedObjects]; | ||
return snapshot; | ||
} | ||
} | ||
_a = observedObjectsSymbol_1.observedObjects; | ||
__decorate([ | ||
nonEnumerable | ||
], _ObservableObject.prototype, _a, void 0); | ||
// Workaround to allow us to recognize T's props as part of ObservableObject | ||
// https://stackoverflow.com/a/54737176/2929433 | ||
exports.ObservableObject = _ObservableObject; | ||
/** | ||
* Builds the initial object-proxy composed of proxies objects | ||
* @param sourceObject | ||
* @param handlers | ||
*/ | ||
function buildInitialProxyChain(sourceObject, handlers, ...args) { | ||
@@ -181,11 +122,2 @@ let chain = {}; | ||
} | ||
/** | ||
* Builds the chain of properties that will be notified. | ||
* This is used when a property that is or will be | ||
* an object, is assigned. | ||
* The function will compose an object { "x.y.z": value } | ||
* for each key of each nested object. | ||
* @param source - Current object | ||
* @param args | ||
*/ | ||
function buildNotificationChain(source, ...args) { | ||
@@ -201,26 +133,5 @@ let chain = {}; | ||
} | ||
/** | ||
* Creates a function that accepts default arguments | ||
* with some other trailing arbitrary dev-defined arguments | ||
* | ||
* E.g. Setter receives the following arguments: obj, prop, value, receiver. | ||
* We wrap the original function in another one that adds the arguments; | ||
* | ||
* @param {Function} fn - the original function | ||
* @param {any[]} boundArgs - the arbitrary arguments | ||
*/ | ||
function bindLast(fn, ...boundArgs) { | ||
return (...args) => fn(...args, ...boundArgs); | ||
} | ||
/** | ||
* @enumerable Decorator to initialize a property to be writable but not enumerable | ||
* @param {boolean} value - new value | ||
*/ | ||
function nonEnumerable(target, propertyKey) { | ||
Object.defineProperty(target, propertyKey, { | ||
writable: true, | ||
enumerable: false | ||
}); | ||
} | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -14,2 +14,1 @@ (function (factory) { | ||
}); | ||
//# sourceMappingURL=observedObjectsSymbol.js.map |
{ | ||
"name": "roxe", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Observe object changes through proxies", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
48393
155