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.
This commit is contained in:
parent
c1fe1c45c7
commit
335c0f200f
31
erpnext/public/js/hub/event_emitter.js
Normal file
31
erpnext/public/js/hub/event_emitter.js
Normal file
@ -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;
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -79,9 +79,10 @@ erpnext.hub.Item = class Item extends SubPage {
|
||||
.then(() => {
|
||||
$(favourite_button).html('Saved');
|
||||
frappe.show_alert(__('Saved to <b><a href="#marketplace/favourites">Favourites</a></b>'));
|
||||
erpnext.hub.trigger('action:item_favourite');
|
||||
})
|
||||
.catch(e => {
|
||||
console.log(e);
|
||||
console.error(e);
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user