Skip to content

Commit

Permalink
cache methods - instant from memory loading
Browse files Browse the repository at this point in the history
  • Loading branch information
sentivate committed Mar 17, 2022
1 parent 47ffc5c commit a2388a5
Show file tree
Hide file tree
Showing 10 changed files with 624 additions and 407 deletions.
432 changes: 252 additions & 180 deletions uwBridge/system/compile/build/front/bundle.js

Large diffs are not rendered by default.

432 changes: 252 additions & 180 deletions uwBridge/system/compile/build/front/coreBundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions uwBridge/system/compile/build/worker/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
reconnect() {
const thisContext = this;
if (!hasValue(thisContext.connectInterval)) {
this.socket.close();
thisContext.connectInterval = setInterval(() => {
thisContext.connect();
}, 2000);
Expand Down
1 change: 1 addition & 0 deletions uwBridge/system/compile/build/worker/coreBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
reconnect() {
const thisContext = this;
if (!hasValue(thisContext.connectInterval)) {
this.socket.close();
thisContext.connectInterval = setInterval(() => {
thisContext.connect();
}, 2000);
Expand Down
Empty file.
3 changes: 2 additions & 1 deletion uwBridge/system/compile/source/front/core/component/view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import app from '../app';
const {
demand,
demandJs,
utility: {
assign,
each,
Expand Down Expand Up @@ -54,7 +55,7 @@ view.on({
});
app.importComponent = async (componentName, importURL, type = 'dynamic') => {
if (importURL) {
await demand(importURL);
await demandJs(importURL);
}
await view.set(`@shared.components.${type}.${componentName}`, true);
await view.update('@shared.components.${type}');
Expand Down
112 changes: 69 additions & 43 deletions uwBridge/system/compile/source/front/core/demand.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import app from './app.js';
import { fetchFile } from './fetchFile.js';
import languagePath from './language.js';
import { watch, Watcher } from './watchers.js';
const {
utility: {
assign,
Expand All @@ -13,44 +11,25 @@ const {
isPlainObject,
each,
cnsl,
initialString,
getFileExtension,
isArray,
isNumber
isNumber,
mapAsync
},
imported,
crate
crate,
checksumData,
hotloadJS
} = app;
const commaString = ',';
const buildFilePath = (itemArg) => {
let item = itemArg;
if (item[0] !== '/') {
item = `/${item}`;
}
if (!hasDot(item)) {
if (initialString(item, -9) === 'language/') {
item = languagePath(item);
} else if (last(item) === '/') {
if (last(item) === '/') {
item += 'index.js';
} else if (initialString(item, -3) === 'js/') {
item += '.js';
} else if (initialString(item, -4) === 'css/') {
item += '.css';
}
// app.log(item);
}
if (getFileExtension(item) === 'js') {
// app.log(item, watch);
if (!Watcher.containerPrimary[item]) {
watch(item, (thing) => {
if (app.debug) {
console.log('Live Reload', thing);
}
crate.removeItem(thing.name);
crate.removeItem(`cs-${thing.name}`);
});
}
}
if (item[0] !== '/') {
item = `/${item}`;
}
return item;
};
const singleDemand = (items) => {
Expand Down Expand Up @@ -127,8 +106,51 @@ export const demand = async (files, options) => {
console.log(results, demandType, compiledImports, localImport, remoteImport);
return demandType(compiledImports, arrayToObjectMap);
};
const demandTypeMethod = (type, optionsFunction) => {
return function(filesArg, options) {
function buildFilePathType(item, type) {
let filePath = item;
if (filePath[0] !== '/') {
filePath = `/${filePath}`;
}
if (!hasDot(filePath)) {
if (last(filePath) === '/') {
filePath += `index.${type}`;
} else {
filePath += `.${type}`;
}
}
return filePath;
}
async function getCacheFromLocal(filePath, type) {
const crateCache = crate.storage.items[filePath];
if (type.match(/css|html/)) {
if (crateCache) {
return crateCache;
} else {
const cacheTimeElapsed = checksumData(filePath);
if (cacheTimeElapsed) {
const timeElapsed = Date.now() - cacheTimeElapsed.time;
if (timeElapsed >= app.cacheExpire) {
const localstoredCache = crate.getItem(filePath);
return localstoredCache;
}
}
}
} else {
const localstoredCache = crate.getItem(filePath);
console.log(filePath, localstoredCache);
if (localstoredCache && isString(localstoredCache)) {
const cacheTimeElapsed = checksumData(filePath);
if (cacheTimeElapsed) {
const hotModule = await hotloadJS(localstoredCache, filePath);
if (hotModule) {
return hotModule;
}
}
}
}
}
function demandTypeMethod(type, optionsFunction) {
return async function(filesArg, options) {
let files = filesArg;
if (optionsFunction) {
optionsFunction(options);
Expand All @@ -137,44 +159,49 @@ const demandTypeMethod = (type, optionsFunction) => {
if (imported[files]) {
return imported[files];
}
files = (hasDot(files)) ? files : `${files}${last(files) === '/' && 'index' || ''}.${type}`;
files = buildFilePathType(files, type);
if (imported[files]) {
return imported[files];
} else {
const localstoredCache = await getCacheFromLocal(files, type);
if (localstoredCache) {
return localstoredCache;
}
}
} else {
files = map(files, (item) => {
files = await mapAsync(files, async (item) => {
if (imported[item]) {
return imported[item];
}
const itemHasExt = hasDot(item);
const compiledFileName = (itemHasExt) ? item : `${item}${last(item) === '/' && 'index' || ''}.${type}`;
const compiledFileName = buildFilePathType(item, type);
if (imported[compiledFileName]) {
return imported[compiledFileName];
} else {
const localstoredCache = await getCacheFromLocal(compiledFileName, type);
if (localstoredCache) {
return localstoredCache;
}
}
app.log('Demand Type', type, compiledFileName);
return compiledFileName;
});
}
return demand(files, options);
};
};
}
export const demandCss = demandTypeMethod('css', (appendCSS) => {
return {
appendCSS
};
});
const demandJs = demandTypeMethod('js');
const demandHtml = demandTypeMethod('html');
const demandLang = (fileList) => {
const files = (isString(fileList)) ? fileList.split(commaString) : fileList;
return demand(map(files, languagePath));
};
assign(app.events, {
async ready(data) {
cnsl('Worker is Ready', 'notify');
app.systemLanguage = data.language;
try {
await demand('app/index.js');
await demandJs('/app/index.js');
} catch (error) {
console.log(error);
crate.clear();
Expand All @@ -187,5 +214,4 @@ assign(app, {
demandCss,
demandHtml,
demandJs,
demandLang,
});
47 changes: 45 additions & 2 deletions uwBridge/system/compile/source/front/core/fetchFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@ const iJson = (contents) => {
return {};
};
const isLibRegex = /(^js\/lib\/)|(\.min\.js)/;
const checksumData = (item) => {
const checksumString = crate.getItem(`cs-${item}`);
if (checksumString) {
const checksum = jsonParse(checksumString);
if (checksum) {
return checksum;
}
}
};
app.checksumData = checksumData;
const checksumReturn = (item) => {
return crate.getItem(`cs-${item}`);
const checksumString = crate.getItem(`cs-${item}`);
if (checksumString) {
const checksum = jsonParse(checksumString);
if (checksum?.cs) {
return checksum.cs;
}
}
};
const constructStyleTagThenAppendToHead = (text, filePath) => {
const node = styleNode.cloneNode(false);
Expand Down Expand Up @@ -51,6 +67,30 @@ const checkIfCompleted = (config) => {
getCompleted(config);
}
};
async function hotloadJS(fileContents, filepath) {
const lastSlash = filepath.lastIndexOf('/') + 1;
const filename = filepath.substring(lastSlash);
const dirname = filepath.substring(0, lastSlash);
const emulateImport = `Object.assign(import.meta, {path:'${dirname}',filename:'${filename}'});\n`;
let scriptRaw = new File([emulateImport, fileContents], filename, {
type: 'text/javascript'
});
let fileBlob = URL.createObjectURL(scriptRaw);
const moduleExports = assign({}, await import(fileBlob));
URL.revokeObjectURL(fileBlob);
imported[filename] = moduleExports;
fileBlob = null;
scriptRaw = null;
return moduleExports;
}
app.hotloadJS = hotloadJS;
async function hotloadLocalJS(dirname) {
const fileContents = crate.getItem(dirname);
if (fileContents) {
return hotloadJS(fileContents, dirname);
}
}
app.hotloadLocalJS = hotloadLocalJS;
const saveCompleted = async (json, config) => {
const {
file,
Expand All @@ -76,7 +116,10 @@ const saveCompleted = async (json, config) => {
if (app.debug) {
console.log('SAVE FILE TO LOCAL', fileContents);
}
crate.setItem(`cs-${filename}`, cs);
crate.setItem(`cs-${filename}`, {
cs,
time: Date.now()
});
crate.setItem(filename, fileContents);
}
if (!hasValue(imported[filename]) || fileContents !== true) {
Expand Down
1 change: 1 addition & 0 deletions uwBridge/system/compile/source/worker/core/ClientSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class ClientSocket {
reconnect() {
const thisContext = this;
if (!hasValue(thisContext.connectInterval)) {
this.socket.close();
thisContext.connectInterval = setInterval(() => {
thisContext.connect();
}, 2000);
Expand Down
2 changes: 1 addition & 1 deletion uwBridge/system/filesystem/system/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = async (uwApp) => {
const char = contents.charCodeAt(i);
hash = (hash ^ ((char + hash) << char));
}
return hash;
return hash.toString();
}
set(cacheName, item, checksum) {
const computedChecksum = checksum || this.checksum(item);
Expand Down

0 comments on commit a2388a5

Please sign in to comment.