Suddenly the Store.Conn is no longer available. Any suggestion?
Here is working fine.
Me, too. (Conn is missing).
It looks like this webpackJsonp is triggered too soon before all module (maybe with Promise) resolve/retrieve necessary values.
webpackJsonp([], {'parasite': (x, y, z) => getStore(z)}, 'parasite');
So I just move instantiate observe new messages, give it a lil time (2-3 secs) before executing webpackJsonp and then followed by observing newMsg.
if (!window.Store) {
(function() {
function getStore(modules) {
: # Nothing is changed here;
: # Snippet stay the same
}
let observeNewMsg = function(){
if (window.WAPI._observerInitialized)
return !1;
if (!('undefined' != typeof window.Store && window.Store.Msg)){
setTimeout(function(){
observeNewMsg()
}, 2 * 1000);
return !1;
}
// flag been triggered
window.WAPI._observerInitialized = true;
/**
* New messages observable functions.
*/
window.WAPI._newMessagesQueue = [];
window.WAPI._newMessagesBuffer = (sessionStorage.getItem('saved_msgs') != null) ?
JSON.parse(sessionStorage.getItem('saved_msgs')) : [];
window.WAPI._newMessagesDebouncer = null;
window.WAPI._newMessagesCallbacks = [];
window.Store.Msg.off('add');
sessionStorage.removeItem('saved_msgs');
window.WAPI._newMessagesListener = window.Store.Msg.on('add', (newMessage) => {
if (newMessage && newMessage.isNewMsg && !newMessage.isSentByMe) {
let message = window.WAPI.processMessageObj(newMessage, false, false);
if (message) {
window.WAPI._newMessagesQueue.push(message);
window.WAPI._newMessagesBuffer.push(message);
}
// Starts debouncer time to don't call a callback for each message if more than one message arrives
// in the same second
if(!window.WAPI._newMessagesDebouncer && window.WAPI._newMessagesQueue.length > 0) {
window.WAPI._newMessagesDebouncer = setTimeout(() => {
window.WAPI._newMessagesDebouncer = null;
let queuedMessages = window.WAPI._newMessagesQueue;
window.WAPI._newMessagesQueue = [];
let removeCallbacks = [];
window.WAPI._newMessagesCallbacks.forEach(function(callbackObj) {
if(callbackObj.callback !== undefined) {
callbackObj.callback(queuedMessages);
}
if(callbackObj.rmAfterUse === true) {
removeCallbacks.push(callbackObj);
}
});
// Remove removable callbacks.
removeCallbacks.forEach(function(rmCallbackObj) {
let callbackIndex = window.WAPI._newMessagesCallbacks.indexOf(rmCallbackObj);
window.WAPI._newMessagesCallbacks.splice(callbackIndex, 1);
});
}, 1000);
}
}
});
window.WAPI._unloadInform = (event) => {
// Save in the buffer the ungot unreaded messages
window.WAPI._newMessagesBuffer.forEach((message) => {
Object.keys(message).forEach(key => message[key] === undefined ? delete message[key] : '');
});
sessionStorage.setItem("saved_msgs", JSON.stringify(window.WAPI._newMessagesBuffer));
// Inform callbacks that the page will be reloaded.
window.WAPI._newMessagesCallbacks.forEach(function(callbackObj) {
if(callbackObj.callback !== undefined) {
callbackObj.callback({status: -1, message: 'page will be reloaded, wait and register callback again.'});
}
});
};
window.addEventListener("unload", window.WAPI._unloadInform, false);
window.addEventListener("beforeunload", window.WAPI._unloadInform, false);
window.addEventListener("pageunload", window.WAPI._unloadInform, false);
/**
* Registers a callback to be called when a new message arrives the WAPI.
* @param rmCallbackAfterUse - Boolean - Specify if the callback need to be executed only once
* @param done - function - Callback function to be called when a new message arrives.
* @returns {boolean}
*/
window.WAPI.waitNewMessages = function(rmCallbackAfterUse = true, done) {
window.WAPI._newMessagesCallbacks.push({callback: done, rmAfterUse: rmCallbackAfterUse});
return true;
};
/**
* Reads buffered new messages.
* @param done - function - Callback function to be called contained the buffered messages.
* @returns {Array}
*/
window.WAPI.getBufferedNewMessages = function(done) {
let bufferedMessages = window.WAPI._newMessagesBuffer;
window.WAPI._newMessagesBuffer = [];
if(done !== undefined) {
done(bufferedMessages);
}
return bufferedMessages;
};
/** End new messages observable functions **/
console.log('_initObserve triggered.');
};
setTimeout(function(){
if (!(window.WAPI && window.WAPI._observerInitialized)){
webpackJsonp([], {'parasite': (x, y, z) => getStore(z)}, 'parasite');
observeNewMsg();
}
}, 3 * 1000);
})();
}
I know this may looks trivial, relative to something like poor connection; hope there's another way to identify the condition, like when is the right time to inject parasite.
~cmiiw
Problem was fixed. My mistake. The code is correct! :)
Most helpful comment
Here is working fine.