// Paytm's turbo churged Publish - Subscribe Library
export const PaytmConnect = (() => {
const topics = {};
return {
subscribe: (topic, listener) => {
// Create the topic's object if not yet created
if (!topics.hasOwnProperty(topic)) topics[topic] = [];
// Add the listener to queue
const index = topics[topic].push(listener) - 1;
// Provide handle back for removal of topic
return {
remove:() => {
delete topics[topic][index];
}
};
},
publish: (topic, info) => {
// If the topic doesn't exist, or there's no listeners in queue, just leave
if (!topics.hasOwnProperty(topic)) return;
const allProperty = Object.getOwnPropertyNames(topic);
allProperty.forEach((property) => {
if (property.match(topic)) {
// Cycle through topics queue, fire!
topics[topic].forEach((item) => {
item(info !== undefined ? info : {});
});
}
});
}
};
})();
당신은 당신의 요구 사항을 만족시키기 위해 코드 if (property.match(topic)) {
를 조금 수정해야합니다.
이 접근법은 의미가 있으며 내가 고려한 것입니다. 그러나 더 효율적으로 수행 할 수있는 방법이 있는지 궁금합니다. 구독자의 수에 따라 실제로는 비효율적 일 수 있습니다. 이미 해결책이 있었다면이 비효율을 해결할 수 있기를 바랬습니다. –