use non-zero injection point for 'start of script array'. clean up module loading a bit

This commit is contained in:
Stewart Allen 2022-02-17 15:04:36 -05:00
commit 5a82a7c746
4 changed files with 29 additions and 19 deletions

17
app.js
View file

@ -243,16 +243,17 @@ function initModule(mod, file, dir) {
getCookieValue: cookieValue,
logger: log.new
},
inject: (code, file, options) => {
if (!script[code]) {
inject: (code, file, opt = {}) => {
let codelist = script[code];
if (!codelist) {
return logger.log(`inject missing target "${code}"`);
}
const opt = options || {};
const path = `${dir}/${file}`;
if (opt.end) {
script[code].push(path);
codelist.push(path);
} else {
script[code].splice(0, 0, path);
let io = Math.max(0, codelist.indexOf('@inject'));
codelist.splice(io, 0, path);
}
if (opt.cachever) {
cachever[path] = opt.cachever;
@ -306,6 +307,7 @@ const script = {
"main/gapp",
"moto/license",
"main/kiri",
"@inject",
"ext/three",
"ext/three-bgu",
"ext/three-svg",
@ -377,6 +379,7 @@ const script = {
"main/gapp",
"moto/license",
"main/kiri",
"@inject",
"ext/three",
"ext/pngjs",
"ext/jszip",
@ -431,6 +434,7 @@ const script = {
"main/gapp",
"moto/license",
"main/kiri",
"@inject",
"ext/clip2",
"ext/earcut",
"add/array",
@ -458,6 +462,7 @@ const script = {
"main/gapp",
"moto/license",
"main/kiri",
"@inject",
"ext/clip2",
"data/local",
"ext/three",
@ -675,6 +680,8 @@ function concatCode(array) {
let direct = array.filter(f => f.charAt(0) !== '@');
let inject = array.filter(f => f.charAt(0) === '@').map(f => f.substring(1));
synth.inject = "/* injection point */";
// in debug mode, the script should load dependent
// scripts instead of serving a complete bundle
if (debug) {

View file

@ -1,7 +1,6 @@
// ## License
//
// Copyright (c) 2011 Evan Wallace (http://madebyevan.com/), under the MIT license.
// Modified for GridApps by Stewart Allen (sa@grid.space)
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
(function() {

View file

@ -3337,14 +3337,8 @@
// prevent safari from exiting full screen mode
DOC.onkeydown = function (evt) { if (evt.keyCode == 27) evt.preventDefault() }
// run optional module functions NOW before kiri-init has run
if (Array.isArray(self.kirimod)) {
kirimod.forEach(function(mod) { mod(kiri.api) });
}
// new module loading
kiri.loader.forEach(mod => { mod(kiri.api)} );
kiri.load = (mod) => { mod(kiri.api) };
// complete module loading
kiri.load_exec();
// upon restore, seed presets
api.event.emit('preset', api.conf.dbo());

View file

@ -5,11 +5,21 @@
(function () {
if (!self.kiri) {
let modfns = self.kirimod = [];
let kiri = self.kiri = {
beta: 3301,
driver: {}, // driver modules
loader: [], // module loading: array of functions
load: (fn) => kiri.loader.push(fn)
loader: modfns, // module loading: array of functions
load(fn) {
kiri.loader.push(fn);
},
load_exec() {
// complete module loading
modfns.forEach(modfn => { modfn(kiri.api)} );
// rewrite load() to be immediate post-finalize
kiri.load = (modfn) => { modfn(kiri.api) };
}
};
}