@aliyun-obv/app-loader
Advanced tools
Comparing version 0.0.17 to 0.2.0
@@ -51,41 +51,55 @@ import React, { forwardRef, useState, useCallback, useEffect, Suspense, useRef } from "react"; | ||
}; | ||
let cache$1 = {}; | ||
function getEntryPoint(id) { | ||
return cache$1[id]; | ||
function createCacheLoader() { | ||
const cache = {}; | ||
function getCache(id) { | ||
return cache[id]; | ||
} | ||
function setCache(id, cacheInfo) { | ||
cache[id] = cacheInfo; | ||
} | ||
async function load(id, run) { | ||
let cacheInfo = getCache(id); | ||
if (cacheInfo && cacheInfo.loaded) { | ||
return cacheInfo.value; | ||
} | ||
if (!cacheInfo || !cacheInfo.loaded) { | ||
if (cacheInfo) { | ||
return cacheInfo.promise; | ||
} else { | ||
const promise = new Promise((resolve, reject) => { | ||
cacheInfo = { | ||
resolve, | ||
reject, | ||
value: null, | ||
loading: true, | ||
loaded: false | ||
// promise: promise, | ||
}; | ||
setCache(id, cacheInfo); | ||
}); | ||
cacheInfo.promise = promise; | ||
} | ||
} | ||
const value = await run(); | ||
cacheInfo.value = value; | ||
cacheInfo.loaded = true; | ||
cacheInfo.loading = false; | ||
cacheInfo.resolve(value); | ||
return value; | ||
} | ||
return { | ||
load | ||
}; | ||
} | ||
function setEntryPoint(id, entryPointInfo) { | ||
cache$1[id] = entryPointInfo; | ||
} | ||
const loader$1 = createCacheLoader(); | ||
const loadEntryPoint = async (appInfo, entrypoint, name) => { | ||
const id = `${appInfo.id}@${name}`; | ||
let entryPointInfo = getEntryPoint(id); | ||
if (entryPointInfo && entryPointInfo.loaded) { | ||
return; | ||
} | ||
if (!entryPointInfo || !entryPointInfo.loaded) { | ||
if (entryPointInfo) { | ||
return entryPointInfo.promise; | ||
} else { | ||
const promise = new Promise(function(resolve, reject) { | ||
entryPointInfo = { | ||
resolve, | ||
reject, | ||
loading: true, | ||
loaded: false | ||
}; | ||
setEntryPoint(id, entryPointInfo); | ||
}); | ||
entryPointInfo.promise = promise; | ||
} | ||
} | ||
try { | ||
const { js, css } = entrypoint; | ||
for (var index = 0; index < js.length; index++) { | ||
await systemRequire(formatUrl(js[index], appInfo.manifest)); | ||
} | ||
addStyles(css, appInfo.manifest); | ||
entryPointInfo.loaded = true; | ||
entryPointInfo.loading = false; | ||
entryPointInfo.resolve(null); | ||
return; | ||
return loader$1.load(id, async () => { | ||
const { js, css } = entrypoint; | ||
for (let index = 0; index < js.length; index++) { | ||
await systemRequire(formatUrl(js[index], appInfo.manifest)); | ||
} | ||
addStyles(css, appInfo.manifest); | ||
}); | ||
} catch (error) { | ||
@@ -96,41 +110,11 @@ console.error(`load entrypoint error: ${id}`); | ||
}; | ||
let cache = {}; | ||
function getAppManifest(id) { | ||
return cache[id]; | ||
} | ||
function setAppManifest(id, manifestInfo) { | ||
cache[id] = manifestInfo; | ||
} | ||
const loader = createCacheLoader(); | ||
const getManifest = async (url) => { | ||
let manifestInfo = getAppManifest(url); | ||
if (manifestInfo && manifestInfo.loaded) { | ||
return manifestInfo.manifest; | ||
} | ||
if (!manifestInfo || !manifestInfo.loaded) { | ||
if (manifestInfo) { | ||
return manifestInfo.promise; | ||
} else { | ||
const promise = new Promise(function(resolve, reject) { | ||
manifestInfo = { | ||
resolve, | ||
reject, | ||
manifest: null, | ||
loading: true, | ||
loaded: false | ||
// promise: promise, | ||
}; | ||
setAppManifest(url, manifestInfo); | ||
}); | ||
manifestInfo.promise = promise; | ||
} | ||
} | ||
try { | ||
const response = await fetch(url); | ||
const text = await response.text(); | ||
const manifest = JSON.parse(text); | ||
manifestInfo.manifest = manifest; | ||
manifestInfo.loaded = true; | ||
manifestInfo.loading = false; | ||
manifestInfo.resolve(manifest); | ||
return manifest; | ||
return loader.load(url, async () => { | ||
const response = await fetch(url); | ||
const text = await response.text(); | ||
const manifest = JSON.parse(text); | ||
return manifest; | ||
}); | ||
} catch (error) { | ||
@@ -137,0 +121,0 @@ console.error(`get manifest error: ${url}`); |
import { EntryPoint, IOBSApp } from './types'; | ||
interface EntryPointInfo { | ||
resolve: (value: any) => Promise<any> | void; | ||
reject: (reason?: any) => Promise<any> | void; | ||
promise: Promise<any>; | ||
loaded: boolean; | ||
loading: boolean; | ||
} | ||
export declare function getEntryPoint(id: string): EntryPointInfo; | ||
export declare function setEntryPoint(id: string, entryPointInfo: EntryPointInfo): void; | ||
export declare const loadEntryPoint: (appInfo: IOBSApp, entrypoint: EntryPoint, name: string) => Promise<any>; | ||
export {}; | ||
export declare const loadEntryPoint: (appInfo: IOBSApp, entrypoint: EntryPoint, name: string) => Promise<unknown>; |
import { AppManifest } from './types'; | ||
interface ManifestInfo { | ||
resolve: (value: AppManifest | PromiseLike<AppManifest>) => Promise<AppManifest> | void; | ||
reject: (reason?: any) => Promise<AppManifest> | void; | ||
promise: Promise<AppManifest>; | ||
loaded: boolean; | ||
loading: boolean; | ||
manifest: any; | ||
} | ||
/** | ||
* get application cache by app id | ||
* @param id | ||
*/ | ||
export declare function getAppManifest(id: string): ManifestInfo; | ||
/** | ||
* set application cache by app id | ||
* @param id | ||
*/ | ||
export declare function setAppManifest(id: string, manifestInfo: ManifestInfo): void; | ||
export declare const getManifest: (url: string) => Promise<any>; | ||
export {}; | ||
export declare const getManifest: (url: string) => Promise<AppManifest>; |
@@ -9,2 +9,3 @@ export interface IOBSApp { | ||
external?: Record<string, any>; | ||
slsEnv?: boolean; | ||
} | ||
@@ -11,0 +12,0 @@ export interface IOBSAppProps { |
{ | ||
"name": "@aliyun-obv/app-loader", | ||
"version": "0.0.17", | ||
"version": "0.2.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.cjs", |
Sorry, the diff of this file is not supported yet
19
44604
706