From 0624c5c8ab93ed74387f7e522e7ea9b00e81d1b1 Mon Sep 17 00:00:00 2001 From: yuanchuan Date: Sun, 28 Nov 2021 18:10:42 +0800 Subject: [PATCH] Fix bad imports --- src/canvas.js | 6 +++--- src/index.js | 4 ++-- src/paint.js | 8 ++++---- src/shader.js | 6 +++--- src/utils/cache.js | 46 ++++++++++++++++++++++++---------------------- src/utils/memo.js | 4 ++-- 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 8db9c27..5beb968 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -1,8 +1,8 @@ import { un_entity } from './utils/index'; -import { setCache, getCache } from '~/utils/cache'; +import Cache from './utils/cache'; function draw_canvas(code, width, height, random) { - let result = getCache(code); + let result = Cache.get(code); if (result) { return Promise.resolve(result); } @@ -24,7 +24,7 @@ function draw_canvas(code, width, height, random) { } catch(e) { // ignore } - return Promise.resolve(setCache(code, canvas.toDataURL())); + return Promise.resolve(Cache.set(code, canvas.toDataURL())); } export { diff --git a/src/index.js b/src/index.js index 340a208..99d89b3 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ import { uniform_time } from './uniform'; import get_props from './utils/get-props'; import { get_variable, get_all_variables } from './utils/variables'; -import { clearCache } from './utils/cache'; +import Cache from './utils/cache'; import { make_tag_function, @@ -39,7 +39,7 @@ class Doodle extends HTMLElement { } update(styles) { - clearCache(); + Cache.clear(); let use = this.get_use(); if (!styles) styles = un_entity(this.innerHTML); this.innerHTML = styles; diff --git a/src/paint.js b/src/paint.js index 84ed1f2..ce92fe6 100644 --- a/src/paint.js +++ b/src/paint.js @@ -1,10 +1,10 @@ import { un_entity } from './utils/index'; -import { getCache, setCache } from '~/utils/cache'; +import Cache from './utils/cache'; let counter = 1; -function make_paint(code, random) { - let result = getCache(code); +function make_paint(code) { + let result = Cache.get(code); if (result) { return Promise.resolve(result); } @@ -22,7 +22,7 @@ function make_paint(code, random) { } } catch(e) {} - return Promise.resolve(setCache(code, `paint(${name})`)); + return Promise.resolve(Cache.set(code, `paint(${name})`)); } export { diff --git a/src/shader.js b/src/shader.js index 4610aea..ea45306 100644 --- a/src/shader.js +++ b/src/shader.js @@ -1,4 +1,4 @@ -import { setCache, getCache } from './utils/cache'; +import Cache from './utils/cache'; function create_shader(gl, type, source) { let shader = gl.createShader(type); @@ -56,7 +56,7 @@ function load_texture(gl, image, i) { } function draw_shader(shaders, width, height) { - let result = getCache(shaders); + let result = Cache.get(shaders); if (result) { return Promise.resolve(result); } @@ -110,7 +110,7 @@ function draw_shader(shaders, width, height) { gl.drawArrays(gl.TRIANGLES, 0, 6); // resolve image data in 72dpi :( - return Promise.resolve(setCache(shaders, canvas.toDataURL())); + return Promise.resolve(Cache.set(shaders, canvas.toDataURL())); } export { diff --git a/src/utils/cache.js b/src/utils/cache.js index 96937f6..d9abe6e 100644 --- a/src/utils/cache.js +++ b/src/utils/cache.js @@ -1,26 +1,28 @@ -import { hash, is_nil } from './index'; +import { hash, is_nil } from './index.js'; -let _cache = {}; - -export function clearCache() { - _cache = {}; -} - -export function setCache(input, value) { - if (is_nil(input)) { - return ''; +class CacheValue { + constructor() { + this.cache = {}; + } + clear() { + this.cache = {}; + } + set(input, value) { + if (is_nil(input)) { + return ''; + } + let key = this.getKey(input); + return this.cache[key] = value; + } + get(input) { + let key = this.getKey(input); + return this.cache[key]; + } + getKey(input) { + return (typeof input === 'string') + ? hash(input) + : hash(JSON.stringify(input)); } - let key = getKey(input); - return _cache[key] = value; } -export function getCache(input) { - let key = getKey(input); - return _cache[key]; -} - -function getKey(input) { - return (typeof input === 'string') - ? hash(input) - : hash(JSON.stringify(input)); -} +export default new CacheValue(); diff --git a/src/utils/memo.js b/src/utils/memo.js index 8630da9..a5f81bd 100644 --- a/src/utils/memo.js +++ b/src/utils/memo.js @@ -1,8 +1,8 @@ -import { getCache, setCache } from './cache'; +import Cache from './cache'; export default function(prefix, fn) { return (...args) => { let key = prefix + args.join('-');; - return getCache(key) || setCache(key, fn.apply(null, args)); + return Cache.get(key) || Cache.set(key, fn.apply(null, args)); } }