Skip to content

Instantly share code, notes, and snippets.

@dantman
Last active September 26, 2017 14:56
Show Gist options
  • Save dantman/39683a3966c3c7b026d0615e1eb49ca0 to your computer and use it in GitHub Desktop.
Save dantman/39683a3966c3c7b026d0615e1eb49ca0 to your computer and use it in GitHub Desktop.
VRUB Exposed classes/modules API idea
using VRUB;
[ExposedClass]
class PersistentStore : ExposedClass { // base class could use a different name
Dictionary<string, string> _temporaryStore = new Dictionary<string, string>();
Dictionary<string, string> _persistentStore = new Dictionary<string, string>();
[ExposedMethod]
public string Get(string key, bool temporary = false) {
Dictionary<string, string> store = temporary ? _temporaryStore : _persistentStore;
return store.ContainsKey(key) ? store[key] : null;
}
[ExposedMethod]
public void Set(string key, string data, bool temporary = false) {
Dictionary<string, string> store = temporary ? _temporaryStore : _persistentStore;
// @todo We shouldn't parse the JSON here, but we should at least validate it
store[key] = data;
if (!temporary) {
QueueSave();
}
}
// ...
}
(function() {
'use strict';
const native = (...args) => VRUB.InteropPipe.Call('PersistentStore', ...args);
class PersistentStore {
constructor(temporary=false) {
this.temporary = temporary;
}
get(key) {
const data = native('Get', key, this.temporary);
if ( data !== null ) {
data = JSON.parse(data);
}
return data;
}
set(key, value) {
native('Set', key, JSON.stringify(value), this.temporary);
}
// ...
}
// Expose it on VRUB for now
window.VRUB = window.VRUB || {};
VRUB.PersistentStore = new PersistentStore(false);
VRUB.TemporaryStore = new PersistentStore(true);
// @todo Fake the browser.storage API here
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment