@web-atoms/core
Version:
215 lines (214 loc) • 7.04 kB
JavaScript
System.register(["./types"], function (_export, _context) {
"use strict";
var ArrayHelper, AtomBinder, symbolHandlers, symbolBindable;
_export("AtomBinder", void 0);
return {
setters: [function (_types) {
ArrayHelper = _types.ArrayHelper;
}],
execute: function () {
_export("symbolHandlers", symbolHandlers = Symbol.for("handlers"));
_export("symbolBindable", symbolBindable = Symbol.for("bindable"));
_export("AtomBinder", AtomBinder = class AtomBinder {
static refreshValue(target, key) {
const handlers = AtomBinder.get_WatchHandler(target, key);
if (handlers === undefined || handlers == null) {
return;
}
for (const item of handlers) {
item(target, key);
}
if (target.onPropertyChanged) {
target.onPropertyChanged(key);
}
}
static add_WatchHandler(target, key, handler) {
if (target == null) {
return;
}
const handlers = AtomBinder.get_WatchHandler(target, key);
handlers.push(handler);
if (Array.isArray(target)) {
return;
}
const pv = AtomBinder.getPropertyDescriptor(target, key);
if (pv && pv.get) {
return;
}
const tw = target;
let bindables = tw[symbolBindable];
if (bindables === undefined) {
bindables = {};
Object.defineProperty(tw, symbolBindable, {
value: bindables,
enumerable: false,
writable: true,
configurable: true
});
}
if (!bindables[key]) {
bindables[key] = 1;
const o = target[key];
const nk = Symbol.for(key);
target[nk] = o;
const set = function (v) {
const ov = this[nk];
if (ov === undefined ? ov === v : ov == v) {
return;
}
this[nk] = v;
AtomBinder.refreshValue(this, key);
};
const get = function () {
return this[nk];
};
if (pv) {
delete target[key];
Object.defineProperty(target, key, {
get,
set,
configurable: true,
enumerable: true
});
} else {
Object.defineProperty(target, key, {
get,
set,
enumerable: true,
configurable: true
});
}
}
}
static getPropertyDescriptor(target, key) {
const pv = Object.getOwnPropertyDescriptor(target, key);
if (!pv) {
const pt = Object.getPrototypeOf(target);
if (pt) {
return AtomBinder.getPropertyDescriptor(pt, key);
}
}
return pv;
}
static get_WatchHandler(target, key) {
if (target == null) {
return null;
}
let handlers = target[symbolHandlers];
if (handlers === undefined) {
handlers = {};
Object.defineProperty(target, symbolHandlers, {
value: handlers,
enumerable: false,
writable: true,
configurable: true
});
}
let handlersForKey = handlers[key];
if (handlersForKey === undefined || handlersForKey == null) {
handlersForKey = [];
handlers[key] = handlersForKey;
}
return handlersForKey;
}
static remove_WatchHandler(target, key, handler) {
if (target == null) {
return;
}
const handlers = target[symbolHandlers];
if (typeof handlers === "undefined") {
return;
}
const handlersForKey = target[symbolHandlers][key];
if (handlersForKey === undefined || handlersForKey == null) {
return;
}
ArrayHelper.remove(handlersForKey, f => f === handler);
if (!handlersForKey.length) {
handlers[key] = null;
delete handlers[key];
}
}
static invokeItemsEvent(target, mode, index, item, oldItem) {
const key = "_items";
const handlers = AtomBinder.get_WatchHandler(target, key);
if (!handlers) {
return;
}
for (const obj of handlers) {
obj(target, mode, index, item, oldItem);
}
AtomBinder.refreshValue(target, "length");
}
static refreshItems(ary) {
AtomBinder.invokeItemsEvent(ary, "refresh", -1, null);
}
static add_CollectionChanged(target, handler) {
if (target == null) {
throw new Error("Target Array to watch cannot be null");
}
if (handler == null) {
throw new Error("Target handle to watch an Array cannot be null");
}
const handlers = AtomBinder.get_WatchHandler(target, "_items");
handlers.push(handler);
return {
dispose: () => {
AtomBinder.remove_CollectionChanged(target, handler);
}
};
}
static remove_CollectionChanged(t, handler) {
if (t == null) {
return;
}
const target = t;
const handlers = target[symbolHandlers];
if (typeof handlers === "undefined") {
return;
}
const key = "_items";
const handlersForKey = handlers[key];
if (handlersForKey === undefined || handlersForKey == null) {
return;
}
ArrayHelper.remove(handlersForKey, f => f === handler);
if (!handlersForKey.length) {
handlers[key] = null;
delete handlers[key];
}
}
static watch(item, property, f) {
AtomBinder.add_WatchHandler(item, property, f);
return {
dispose: () => {
AtomBinder.remove_WatchHandler(item, property, f);
}
};
}
static clear(a) {
a.length = 0;
this.invokeItemsEvent(a, "refresh", -1, null);
AtomBinder.refreshValue(a, "length");
}
static addItem(a, item) {
const index = a.length;
a.push(item);
this.invokeItemsEvent(a, "add", index, item);
AtomBinder.refreshValue(a, "length");
}
static removeItem(a, item) {
const i = a.findIndex(x => x === item);
if (i === -1) {
return false;
}
a.splice(i, 1);
AtomBinder.invokeItemsEvent(a, "remove", i, item);
AtomBinder.refreshValue(a, "length");
return true;
}
});
}
};
});
//# sourceMappingURL=AtomBinder.js.map