@xailabs/altx
Version:
Flux flavor based on alt.js
55 lines (44 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setAltInstance = setAltInstance;
exports.getAltInstance = getAltInstance;
var _alt = require('alt');
var _alt2 = _interopRequireDefault(_alt);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var instance = void 0; /**
* This is the alt instance that holds the state of the entire application.
* All stores and actions are created on this instance.
*/
setAltInstance(new _alt2.default());
function setAltInstance(alt) {
if (instance) teardown(instance);
setup(alt);
}
function getAltInstance() {
return instance;
}
var root = global || window;
function setup(alt) {
instance = alt;
if (process.env.NODE_ENV !== 'production') {
// Debugging with chrome devtools
// @see https://github.com/goatslacker/alt-devtool
require('alt-utils/lib/chromeDebug')(instance);
// sometimes, chromeDebug just doesn't update until an action is dispatched.
// we use a dummy action to do just that
var refreshAction = instance.generateActions('__refresh__').__refresh__;
instance.handleMessage = function (e) {
if (e.data && e.data.type === 'ALT' && e.data.source === 'alt-devtools') {
refreshAction.defer();
}
};
root && root.addEventListener && root.addEventListener('message', instance.handleMessage);
}
}
function teardown(alt) {
if (process.env.NODE_ENV !== 'production') {
root && root.removeEventListener && root.removeEventListener('message', instance.handleMessage);
}
}