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

Use indexedDB directly via idb-keyval #305

Merged
merged 11 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Authors@R: c(
person(family = "Ajax.org B.V.", role=c("ctb", "cph"), comment= "Ace library"),
person("Zeno", "Rocha", role = c("ctb", "cph"), comment = "clipboard.js library"),
person("Nick", "Payne", role = c("ctb", "cph"), comment = "Bootbox library"),
person(family = "Mozilla", role = c("ctb", "cph"), comment = "localforage library")
person("Jake", "Archibald", role = c("ctb", "cph"), comment = "idb-keyval library")
)
Description: Create interactive tutorials using R Markdown. Use a combination
of narrative, figures, videos, exercises, and quizzes to create self-paced
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ learnr 0.10.0 (unreleased)

* A version number has been added to `question_submission` events. This will help when using custom storage methods. ([#291](https://github.com/rstudio/learnr/pull/291))

* Tutorial storage on the browser is now executed directly on `indexedDB` using `idb-keyval` (dropping `localforage`). ([#305](https://github.com/rstudio/learnr/pull/305))
schloerke marked this conversation as resolved.
Show resolved Hide resolved

## Bug fixes

Expand Down
12 changes: 6 additions & 6 deletions R/html-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ html_dependency_src <- function(...) {
}
}


localforage_html_dependency <- function() {
idb_html_dependency <- function() {
htmltools::htmlDependency(
name = "localforage",
version = "1.5",
src = system.file("lib/localforage", package = "learnr"),
script = "localforage.min.js"
name = "idb-keyvalue",
version = "3.2.0",
src = system.file("lib/idb-keyval", package = "learnr"),
script = "idb-keyval-iife-compat.min.js",
all_files = FALSE
)
}

Expand Down
3 changes: 1 addition & 2 deletions R/initialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ initialize_tutorial <- function() {
rmarkdown::html_dependency_jquery(),
rmarkdown::html_dependency_font_awesome(),
bootbox_html_dependency(),
localforage_html_dependency(),
idb_html_dependency(),
tutorial_html_dependency(),
tutorial_autocompletion_html_dependency(),
tutorial_diagnostics_html_dependency()
Expand All @@ -45,4 +45,3 @@ dput_to_string <- function(x) {
dput(x, file = conn)
paste0(textConnectionValue(conn), collapse = "")
}

1 change: 1 addition & 0 deletions inst/lib/idb-keyval/idb-keyval-iife-compat.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions inst/lib/idb-keyval/idb-keyval-iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var idbKeyval = (function (exports) {
'use strict';

const promiseStore = (openreq, storeName) => new Promise((resolve, reject) => {
openreq.onerror = () => reject(openreq.error);
openreq.onsuccess = () => resolve(openreq.result);
// First time setup: create an empty object store
openreq.onupgradeneeded = () => {
openreq.result.createObjectStore(storeName);
};
});
class Store {
constructor(dbName = 'keyval-store', storeName = 'keyval') {
this.storeName = storeName;
this._dbp = promiseStore(indexedDB.open(dbName), storeName)
.then(db => {
if (db.objectStoreNames.contains(storeName))
return db;
db.close();
return promiseStore(indexedDB.open(dbName, db.version + 1), storeName);
});
}
_withIDBStore(type, callback) {
return this._dbp.then(db => new Promise((resolve, reject) => {
const transaction = db.transaction(this.storeName, type);
transaction.oncomplete = () => resolve();
transaction.onabort = transaction.onerror = () => reject(transaction.error);
callback(transaction.objectStore(this.storeName));
}));
}
}
let store;

function getDefaultStore() {
if (!store)
store = new Store();
return store;
}

function get(key, store = getDefaultStore()) {
let req;
return store._withIDBStore('readonly', store => {
req = store.get(key);
}).then(() => req.result);
}

function set(key, value, store = getDefaultStore()) {
return store._withIDBStore('readwrite', store => {
store.put(value, key);
});
}

function del(key, store = getDefaultStore()) {
return store._withIDBStore('readwrite', store => {
store.delete(key);
});
}

function clear(store = getDefaultStore()) {
return store._withIDBStore('readwrite', store => {
store.clear();
});
}

function keys(store = getDefaultStore()) {
const keys = [];
return store._withIDBStore('readonly', store => {
// This would be store.getAllKeys(), but it isn't supported by Edge or Safari.
// And openKeyCursor isn't supported by Safari.
(store.openKeyCursor || store.openCursor).call(store).onsuccess = function () {
if (!this.result)
return;
keys.push(this.result.key);
this.result.continue();
};
}).then(() => keys);
}

exports.Store = Store;
exports.get = get;
exports.set = set;
exports.del = del;
exports.clear = clear;
exports.keys = keys;

return exports;

}({}));
3 changes: 3 additions & 0 deletions inst/lib/idb-keyval/note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Original File Location](https://github.com/jakearchibald/idb-keyval/blob/master/dist/idb-keyval-iife-compat.min.js)
[New File Location](https://github.com/schloerke/idb-keyval/blob/idb-keyval/dist/idb-keyval-iife-compat.min.js)
[PR](https://github.com/jakearchibald/idb-keyval/pull/73)
7 changes: 0 additions & 7 deletions inst/lib/localforage/localforage.min.js

This file was deleted.

Loading