From 335c0f200fd0729c3dbb149529e63082d319d526 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Fri, 17 Aug 2018 16:04:14 +0530 Subject: [PATCH] feat: custom cache invalidation for hub calls In some cases, we know when to invalidate the cache. This feature allows to do just that. erpnext.hub namespace is now an EventEmitter instance. --- erpnext/public/js/hub/event_emitter.js | 31 +++++++++++++++++++++++ erpnext/public/js/hub/hub_call.js | 20 ++++++++++----- erpnext/public/js/hub/marketplace.js | 3 +++ erpnext/public/js/hub/pages/favourites.js | 8 +++--- erpnext/public/js/hub/pages/item.js | 3 ++- 5 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 erpnext/public/js/hub/event_emitter.js diff --git a/erpnext/public/js/hub/event_emitter.js b/erpnext/public/js/hub/event_emitter.js new file mode 100644 index 0000000000..1e7288191d --- /dev/null +++ b/erpnext/public/js/hub/event_emitter.js @@ -0,0 +1,31 @@ +/** + * Simple EventEmitter which uses jQuery's event system + */ +class EventEmitter { + init() { + this.jq = jQuery(this); + } + + trigger(evt, data) { + !this.jq && this.init(); + this.jq.trigger(evt, data); + } + + once(evt, handler) { + !this.jq && this.init(); + this.jq.one(evt, (e, data) => handler(data)); + } + + on(evt, handler) { + !this.jq && this.init(); + this.jq.bind(evt, (e, data) => handler(data)); + } + + off(evt, handler) { + !this.jq && this.init(); + this.jq.unbind(evt, (e, data) => handler(data)); + } +} + + +export default EventEmitter; \ No newline at end of file diff --git a/erpnext/public/js/hub/hub_call.js b/erpnext/public/js/hub/hub_call.js index 6bc1701551..d2eaab31b4 100644 --- a/erpnext/public/js/hub/hub_call.js +++ b/erpnext/public/js/hub/hub_call.js @@ -2,7 +2,7 @@ frappe.provide('hub'); frappe.provide('erpnext.hub'); erpnext.hub.cache = {}; -hub.call = function call_hub_method(method, args={}) { +hub.call = function call_hub_method(method, args={}, setup_cache_invalidation = invalidate_after_5_mins) { return new Promise((resolve, reject) => { // cache @@ -11,12 +11,9 @@ hub.call = function call_hub_method(method, args={}) { resolve(erpnext.hub.cache[key]); } - // cache invalidation after 5 minutes - const timeout = 5 * 60 * 1000; - - setTimeout(() => { - delete erpnext.hub.cache[key]; - }, timeout); + // cache invalidation + const clear_cache = () => delete erpnext.hub.cache[key]; + setup_cache_invalidation(clear_cache); frappe.call({ method: 'erpnext.hub_node.api.call_hub_method', @@ -42,3 +39,12 @@ hub.call = function call_hub_method(method, args={}) { .fail(reject) }); } + +function invalidate_after_5_mins(clear_cache) { + // cache invalidation after 5 minutes + const timeout = 5 * 60 * 1000; + + setTimeout(() => { + clear_cache(); + }, timeout); +} diff --git a/erpnext/public/js/hub/marketplace.js b/erpnext/public/js/hub/marketplace.js index 154324338d..ee21d4826a 100644 --- a/erpnext/public/js/hub/marketplace.js +++ b/erpnext/public/js/hub/marketplace.js @@ -14,10 +14,13 @@ import './pages/not_found'; // helpers import './hub_call'; +import EventEmitter from './event_emitter'; frappe.provide('hub'); frappe.provide('erpnext.hub'); +$.extend(erpnext.hub, EventEmitter.prototype); + erpnext.hub.Marketplace = class Marketplace { constructor({ parent }) { this.$parent = $(parent); diff --git a/erpnext/public/js/hub/pages/favourites.js b/erpnext/public/js/hub/pages/favourites.js index 6f2c67c01e..566f5b0e21 100644 --- a/erpnext/public/js/hub/pages/favourites.js +++ b/erpnext/public/js/hub/pages/favourites.js @@ -26,9 +26,11 @@ erpnext.hub.Favourites = class Favourites extends SubPage { } get_favourites() { - return hub.call('get_favourite_items_of_seller', { - hub_seller: hub.settings.company_email - }); + return hub.call( + 'get_favourite_items_of_seller', + { hub_seller: hub.settings.company_email }, + clear_cache => erpnext.hub.on('action:item_favourite', clear_cache) + ); } render(items) { diff --git a/erpnext/public/js/hub/pages/item.js b/erpnext/public/js/hub/pages/item.js index ee43391a22..a94d5845b0 100644 --- a/erpnext/public/js/hub/pages/item.js +++ b/erpnext/public/js/hub/pages/item.js @@ -79,9 +79,10 @@ erpnext.hub.Item = class Item extends SubPage { .then(() => { $(favourite_button).html('Saved'); frappe.show_alert(__('Saved to Favourites')); + erpnext.hub.trigger('action:item_favourite'); }) .catch(e => { - console.log(e); + console.error(e); }); }