Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a "cache lazy lookup results" flag #84

Open
Pomax opened this issue Sep 9, 2020 · 0 comments
Open

Adding a "cache lazy lookup results" flag #84

Pomax opened this issue Sep 9, 2020 · 0 comments
Labels
enhancement Making working code work better. help welcome Want to help out? Have a look at issues tagged with this label.

Comments

@Pomax
Copy link
Owner

Pomax commented Sep 9, 2020

Updating the lazy loader to this:

function lazy(object, property, getter, cache=false) {
    if (cache) {
        // one-time getter, overwriting the property binding with
        // the actual value on first lookup.
        return Object.defineProperty(object, property, {
            get: () => {
                object[property] = getter();
                return object[property];
            },
            set: (v) => {
                delete object[property];
                object[property] = v;
            },
            configurable: true
        });
    }

    // permanent lazy loader
    let val;
    Object.defineProperty(object, property, {
        get: () => {
            if (val) return val;
            val = getter();
            return val;
        }
    });
};

means that its possible to either do pure lazy loading, always consulting the byte buffer, or one-time lazy loading, where the result gets cached to the property that was lazy loaded. Both have advantages and disadvantages, and it would be nice if there were a way to tell a font which caching policy to for which datatypes.

I'd imagine this as a dedicated cache list object that can be imported:

const cache = {};

function enableCachingFor(...classDotField) {
  classDotField.forEach(entry => {
    let [className, fieldName] = entry.split(`.`);
    if (!cache [className]) cache [className] = [];
    cache [className].push(fieldName);
  });
}

function shouldCache(classInstance, fieldName) {
  let className = classInstance.constructor.name;
  if (!cache[className]) return false;
  return cache [className].includes(fieldName};
}

export { enableCachingFor, shouldCache };

Which can then be specified in someone's code:

import { Font } from "Font.js";

const font = new Font("lol");

font.enableCachingFor(
  `NameRecord.value`,
  `VORG.vertORiginYMetrics`,
  ...
);

font.onerror = ... 
font.onload = ...
font.src = "somefontlollercakes.otf";

And that the lazy loader can consult when invoked:

import { shouldCache } from "cache-o-lator.js";

export default function lazy(object, property, getter, cache=false) {
  if (shouldCache(object, property)) {
    return Object.defineProperty(...)
  }

  // implicit else
  return Object.defineProperty(...)
}
@Pomax Pomax added help welcome Want to help out? Have a look at issues tagged with this label. enhancement Making working code work better. labels Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making working code work better. help welcome Want to help out? Have a look at issues tagged with this label.
Projects
None yet
Development

No branches or pull requests

1 participant