@pandorajs/hub
Version:
pandora.js messenge hub
77 lines • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultObjectProxyBehaviour = void 0;
const IntrospectionUtils_1 = require("./IntrospectionUtils");
const const_1 = require("../const");
const hostWeakExtMap = new WeakMap();
exports.DefaultObjectProxyBehaviour = {
host: {
invoke(host, method, params) {
return host[method](...params);
},
getProperty(host, name) {
return host[name];
},
introspect(host) {
return IntrospectionUtils_1.IntrospectionUtils.introspect(host);
},
async subscribe(hub, objectDescription, host, register) {
if (!hostWeakExtMap.has(host)) {
hostWeakExtMap.set(host, new Map());
}
const hostExt = hostWeakExtMap.get(host);
const { countKey, cbKey } = getSubscribeKeys(register);
if (!hostExt.has(countKey)) {
hostExt.set(countKey, 0);
}
const cnt = hostExt.get(countKey) + 1;
hostExt.set(countKey, cnt);
if (cnt === 1) {
const cb = async (...params) => {
await hub.multipleInvoke({
objectTag: objectDescription.tag,
objectName: objectDescription.name + '@subscriber',
}, const_1.OBJECT_ACTION_INVOKE, {
propertyName: 'callback',
data: [register, params],
});
};
hostExt.set(cbKey, cb);
await host.subscribe(register, cb);
}
return true;
},
async unsubscribe(hub, objectDescription, host, register) {
const map = hostWeakExtMap.get(host);
const { countKey, cbKey } = getSubscribeKeys(register);
const cnt = map.get(countKey) - 1;
map.set(countKey, cnt);
if (cnt === 0) {
await host.unsubscribe(register, map.get(cbKey));
map.delete(countKey);
map.delete(cbKey);
}
return true;
},
},
proxy: {
invoke(proxy, consumer, method, params) {
return consumer.invoke(method, params);
},
subscribe(proxy, consumer, register, fn) {
return consumer.subscribe(register, fn);
},
unsubscribe(proxy, consumer, register, fn) {
return consumer.unsubscribe(register, fn);
},
getProperty(proxy, consumer, name) {
return consumer.getProperty(name);
},
},
};
function getSubscribeKeys(register) {
const countKey = register + '@count';
const cbKey = register + '@cb';
return { countKey, cbKey };
}
//# sourceMappingURL=DefaultObjectProxyBehaviour.js.map