@adpt/cloud
Version:
AdaptJS cloud component library
83 lines • 2.95 kB
JavaScript
;
/*
* Copyright 2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@adpt/core");
const lodash_1 = tslib_1.__importDefault(require("lodash"));
function proxyToHandle(hand, prop) {
if (!lodash_1.default.isString(prop))
return true;
if (Object.hasOwnProperty.call(hand, prop))
return true;
const propDesc = Object.getOwnPropertyDescriptor(hand, prop);
if (propDesc && propDesc.get)
return true;
const proto = Object.getPrototypeOf(hand);
if (Object.hasOwnProperty.call(proto, prop))
return true;
const protoPropDesc = Object.getOwnPropertyDescriptor(proto, prop);
if (protoPropDesc && protoPropDesc.get)
return true;
return false;
}
function computeDefault(elem, prop) {
if (elem == null)
return undefined;
const defsObj = elem.componentType.defaults;
return defsObj && defsObj[prop];
}
/** @alpha */
function extendedHandle() {
const wrap = core_1.handle();
return new Proxy(wrap, {
get: (hand, prop, _rx) => {
if (proxyToHandle(hand, prop))
return hand[prop];
if (!lodash_1.default.isString(prop)) {
throw new Error(`Internal error. Non-string property should ` +
`have been proxied to Handle`);
}
if (hand.origTarget === undefined) {
throw new Error(`Cannot access method '${prop}' on Handle ` +
`because the Handle has not been associated to any Element`);
}
const defVal = computeDefault(hand.origTarget, prop);
return (...args) => {
return core_1.useMethod(hand, defVal, prop, ...args);
};
}
});
}
exports.extendedHandle = extendedHandle;
/** @alpha */
function handles() {
const ret = ({
// tslint:disable-next-line:no-object-literal-type-assertion
create: new Proxy({}, {
get: (_target, prop, _rx) => {
if (!lodash_1.default.isString(prop))
return undefined;
const hand = extendedHandle();
ret[prop] = hand;
return hand;
}
})
});
return ret;
}
exports.handles = handles;
//# sourceMappingURL=handles.js.map