Update framework
This commit is contained in:
12
framework/asset-manager/downloader.d.ts
vendored
Normal file
12
framework/asset-manager/downloader.d.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { Queue } from './queue';
|
||||
import { AssetStorage } from './storage';
|
||||
export declare class Downloader extends EventEmitter {
|
||||
private storage;
|
||||
private queue;
|
||||
private basePath;
|
||||
constructor(storage: AssetStorage, queue: Queue, basePath?: string);
|
||||
setBasePath(path: string): void;
|
||||
download(): Promise<any>;
|
||||
downloadItem(path: string): Promise<any>;
|
||||
}
|
50
framework/asset-manager/downloader.js
Normal file
50
framework/asset-manager/downloader.js
Normal file
@@ -0,0 +1,50 @@
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { buildPath } from './utils';
|
||||
export class Downloader extends EventEmitter {
|
||||
constructor(storage, queue, basePath = '') {
|
||||
super();
|
||||
this.storage = storage;
|
||||
this.queue = queue;
|
||||
this.basePath = basePath;
|
||||
}
|
||||
setBasePath(path) {
|
||||
this.basePath = this.basePath;
|
||||
}
|
||||
download() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const downloaded = new Map();
|
||||
let numDownloaded = 0;
|
||||
while (this.queue.length() > 0) {
|
||||
const path = this.queue.pop();
|
||||
const item = yield this.downloadItem(buildPath(this.basePath, path));
|
||||
downloaded.set(path, item);
|
||||
numDownloaded++;
|
||||
this.emit('download.progress', {
|
||||
downloaded: numDownloaded,
|
||||
remaining: this.queue.length()
|
||||
});
|
||||
}
|
||||
return downloaded;
|
||||
});
|
||||
}
|
||||
downloadItem(path) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const inCache = yield this.storage.get(path);
|
||||
if (inCache) {
|
||||
return inCache;
|
||||
}
|
||||
const response = yield fetch(path);
|
||||
this.storage.add(path, response);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
18
framework/asset-manager/index.d.ts
vendored
Normal file
18
framework/asset-manager/index.d.ts
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
import EventEmitter from 'eventemitter3';
|
||||
export declare class AssetManager extends EventEmitter {
|
||||
private name;
|
||||
private basePath;
|
||||
private downloader;
|
||||
private queue;
|
||||
private storage;
|
||||
private manifest;
|
||||
constructor(name: string, basePath: string);
|
||||
init(): Promise<boolean>;
|
||||
setManifest(path: string): Promise<any>;
|
||||
enqueue(path: string): void;
|
||||
download(): Promise<any>;
|
||||
downloadFromManifest(key: string): Promise<any>;
|
||||
downloadFile(path: string): Promise<any>;
|
||||
setBasePath(path: string): void;
|
||||
clearCache(): void;
|
||||
}
|
71
framework/asset-manager/index.js
Normal file
71
framework/asset-manager/index.js
Normal file
@@ -0,0 +1,71 @@
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
import { Downloader } from './downloader';
|
||||
import { Queue } from './queue';
|
||||
import { Manifest } from './manifest';
|
||||
import { AssetStorage } from './storage';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { buildPath } from './utils';
|
||||
export class AssetManager extends EventEmitter {
|
||||
constructor(name, basePath) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.basePath = basePath;
|
||||
this.queue = new Queue();
|
||||
this.storage = new AssetStorage(name);
|
||||
this.downloader = new Downloader(this.storage, this.queue, this.basePath);
|
||||
console.log(`Asset manager initialized`);
|
||||
}
|
||||
init() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield this.storage.init();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
setManifest(path) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
this.manifest = yield Manifest(`${this.basePath}/${path}`);
|
||||
this.storage.setManifest(this.manifest);
|
||||
return this.manifest;
|
||||
});
|
||||
}
|
||||
enqueue(path) {
|
||||
this.queue.add(path);
|
||||
}
|
||||
download() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const result = yield this.downloader.download();
|
||||
return result;
|
||||
});
|
||||
}
|
||||
downloadFromManifest(key) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const paths = this.manifest[key];
|
||||
paths.forEach((path) => this.enqueue(path));
|
||||
this.downloader.on('download.progress', (info) => this.emit('progress', info));
|
||||
const files = yield this.downloader.download();
|
||||
this.downloader.off('download.progress');
|
||||
return files;
|
||||
});
|
||||
}
|
||||
downloadFile(path) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const result = yield this.downloader.downloadItem(buildPath(this.basePath, path));
|
||||
return result;
|
||||
});
|
||||
}
|
||||
setBasePath(path) {
|
||||
this.basePath = this.basePath;
|
||||
this.downloader.setBasePath(this.basePath);
|
||||
}
|
||||
clearCache() {
|
||||
this.storage.clear();
|
||||
}
|
||||
}
|
2
framework/asset-manager/manifest.d.ts
vendored
Normal file
2
framework/asset-manager/manifest.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export declare function Manifest(manifestPath: string): Promise<any>;
|
||||
export declare function CheckManifest(manifest: any): boolean;
|
40
framework/asset-manager/manifest.js
Normal file
40
framework/asset-manager/manifest.js
Normal file
@@ -0,0 +1,40 @@
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
import * as yaml from 'yaml';
|
||||
export function Manifest(manifestPath) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
const response = yield fetch(manifestPath);
|
||||
console.log(response);
|
||||
const data = yield response.text();
|
||||
console.log(`Parsing: `, data);
|
||||
const manifest = yaml.parse(data);
|
||||
return manifest;
|
||||
}
|
||||
catch (error) {
|
||||
alert(`Error occured: ${error.toString()}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
export function CheckManifest(manifest) {
|
||||
const prevManifestStr = localStorage.getItem('manifest');
|
||||
if (!prevManifestStr) {
|
||||
localStorage.setItem('manifest', JSON.stringify(manifest));
|
||||
return false;
|
||||
}
|
||||
const prevManifest = JSON.parse(prevManifestStr);
|
||||
if (prevManifest.version === manifest.version) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
localStorage.setItem('manifest', manifest);
|
||||
return false;
|
||||
}
|
||||
}
|
8
framework/asset-manager/queue.d.ts
vendored
Normal file
8
framework/asset-manager/queue.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export declare class Queue {
|
||||
private items;
|
||||
constructor();
|
||||
add(file: string): string[];
|
||||
remove(file: string): string[];
|
||||
pop(): string;
|
||||
length(): number;
|
||||
}
|
19
framework/asset-manager/queue.js
Normal file
19
framework/asset-manager/queue.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export class Queue {
|
||||
constructor() {
|
||||
this.items = [];
|
||||
}
|
||||
add(file) {
|
||||
this.items.push(file);
|
||||
return this.items;
|
||||
}
|
||||
remove(file) {
|
||||
this.items = this.items.filter((item) => item !== file);
|
||||
return this.items;
|
||||
}
|
||||
pop() {
|
||||
return this.items.pop();
|
||||
}
|
||||
length() {
|
||||
return this.items.length;
|
||||
}
|
||||
}
|
11
framework/asset-manager/storage.d.ts
vendored
Normal file
11
framework/asset-manager/storage.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
export declare class AssetStorage {
|
||||
private id;
|
||||
private cache;
|
||||
private manifest;
|
||||
constructor(id: string);
|
||||
init(): Promise<void>;
|
||||
add(request: RequestInfo, response: Response): Promise<Boolean>;
|
||||
get(request: RequestInfo): Promise<Response>;
|
||||
setManifest(manifest: any): Promise<void>;
|
||||
clear(): Promise<void>;
|
||||
}
|
48
framework/asset-manager/storage.js
Normal file
48
framework/asset-manager/storage.js
Normal file
@@ -0,0 +1,48 @@
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
import { CheckManifest } from './manifest';
|
||||
export class AssetStorage {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
}
|
||||
init() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
this.cache = yield caches.open(this.id);
|
||||
});
|
||||
}
|
||||
add(request, response) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const result = yield this.cache.put(request, response);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
get(request) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const result = yield this.cache.match(request);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
setManifest(manifest) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
this.manifest = manifest;
|
||||
if (!CheckManifest(this.manifest)) {
|
||||
yield this.clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
clear() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const keys = yield this.cache.keys();
|
||||
keys.forEach((key) => __awaiter(this, void 0, void 0, function* () {
|
||||
const result = yield this.cache.delete(key);
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
1
framework/asset-manager/test/index.d.ts
vendored
Normal file
1
framework/asset-manager/test/index.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
5
framework/asset-manager/test/index.js
Normal file
5
framework/asset-manager/test/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const yaml = require('yaml');
|
||||
const fs = require('fs');
|
||||
const data = fs.readFileSync('manifest.yaml');
|
||||
const parsed = yaml.parse(data.toString());
|
||||
console.log(parsed);
|
1
framework/asset-manager/utils.d.ts
vendored
Normal file
1
framework/asset-manager/utils.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export declare function buildPath(basePath: string, path: string): string;
|
8
framework/asset-manager/utils.js
Normal file
8
framework/asset-manager/utils.js
Normal file
@@ -0,0 +1,8 @@
|
||||
export function buildPath(basePath, path) {
|
||||
if (!basePath) {
|
||||
return path;
|
||||
}
|
||||
else {
|
||||
return `${basePath}/${path}`;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user