invent-framework / invent

Express yourself with code.
https://invent-framework.github.io/
Apache License 2.0
109 stars 12 forks source link

fix localstorage issues #50

Closed fpliger closed 1 year ago

WebReflection commented 1 year ago

This LGTM but I wonder if we should link also this paragraph to enable localStorage on occasions: https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/Using#accessing_a_users_cookies_in_an_embedded_cross-origin_iframe

ntoll commented 1 year ago

Fixed by #55

WebReflection commented 1 year ago

I often forget Python can act like a Proxy via classes and this PR kinda enlightened me around this topic.

As yesterday I was digging to provide a fallback I'd like, for history sake, to write down how I would personally shim the localStorage on the JS side when its' not available, forgetting in the process about persistent saves (it requires window.name shenanigans there to work persistently, but that still will be more like a sessionStorage instead).

const localStorage = new Proxy(new Map, {
  deleteProperty(map, key) {
    map.delete(key);
    return true;
  },
  get(map, name) {
    switch (name) {
      case 'clear': return () => { map.clear() };
      case 'getItem': return key => map.get(key) ?? null;
      case 'key': return i => [...map][i]?.[0];
      case 'removeItem': return key => { map.delete(key); };
      case 'setItem': return (key, value) => { map.set(key, value); };
      case 'length': return map.size;
      default: return map.get(name);
    }
  },
  set(map, key, value) {
    map.set(key, value);
  }
});

If we ever need this in the future, it's been also tested and it works as expected as 1:1 API replacement.

fpliger commented 1 year ago

This LGTM but I wonder if we should link also this paragraph to enable localStorage on occasions: https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/Using#accessing_a_users_cookies_in_an_embedded_cross-origin_iframe

Cool. Totally ignored that :)

If we ever need this in the future, it's been also tested and it works as expected as 1:1 API replacement.

TY @WebReflection !