-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
45 lines (37 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use strict";
var wrapConsole = require('./tools/wrapConsole.js');
var buildConfig = require('./tools/buildConfig.js');
var buildRoutes = require('./tools/buildRoutes.js');
var request = require('./tools/request.js');
var watch = require('./watch.js');
global.kojiCallbacks;
function pageLoad() {
wrapConsole();
window.addEventListener('message', ({ data }) => {
// Global context injection
if (data.action === 'injectGlobal') {
const { scope, key, value } = data.payload;
var temp = JSON.parse(window.localStorage.getItem('koji'));
temp[scope][key] = value;
exports.config[scope][key] = value;
exports.routes = buildRoutes(exports.config);
window.localStorage.setItem('koji', JSON.stringify(temp));
// update our hooks for an onchange event.
if(typeof global.kojiCallbacks['change'] === 'function') global.kojiCallbacks['change'](scope, key, value);
}
}, false);
window.localStorage.setItem('koji', JSON.stringify(getConfig()));
exports.config = getConfig();
exports.routes = buildRoutes(exports.config);
}
function getConfig() {
return require('./config.json').config;
}
function on(event, callback) {
if(!global.kojiCallbacks) global.kojiCallbacks = {};
global.kojiCallbacks[event] = callback;
}
exports.pageLoad = pageLoad;
exports.watch = watch;
exports.request = request;
exports.on = on;