Skip to content

Commit

Permalink
Fix delayed localStorage load issue in Chrome OS
Browse files Browse the repository at this point in the history
  • Loading branch information
llaske committed Apr 3, 2018
1 parent dcb096c commit 5f0d7f0
Show file tree
Hide file tree
Showing 40 changed files with 1,119 additions and 479 deletions.
40 changes: 28 additions & 12 deletions activities/Abacus.activity/lib/sugar-web/datastore/sugarizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load text
DatastoreObject.prototype.loadAsText = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, text);
}
var that = this;
html5storage.waitEndOfLoad(function() {
var result = html5storage.getValue(datastorePrefix + that.objectId);
if (result != null) {
that.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + that.objectId);
that.setDataAsText(text);
}
that.toload = false;
callback_c(null, result.metadata, text);
}
});
};

// Set metadata
Expand Down Expand Up @@ -233,6 +236,19 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
};
html5storage.load();

// Wait for end of loading
html5storage.waitEndOfLoad = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (storageloadedcalls.length != 0) { // HACK: On Chrome OS, wait for the end of load of localStorage in memory
storageloadedcalls.push(then);
return;
};
}
if (then) {
then();
}
}

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand Down Expand Up @@ -287,7 +303,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
chrome.storage.local.remove(key);
} else {
window.localStorage.removeItem(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load text
DatastoreObject.prototype.loadAsText = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, text);
}
var that = this;
html5storage.waitEndOfLoad(function() {
var result = html5storage.getValue(datastorePrefix + that.objectId);
if (result != null) {
that.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + that.objectId);
that.setDataAsText(text);
}
that.toload = false;
callback_c(null, result.metadata, text);
}
});
};

// Set metadata
Expand Down Expand Up @@ -233,6 +236,19 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
};
html5storage.load();

// Wait for end of loading
html5storage.waitEndOfLoad = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (storageloadedcalls.length != 0) { // HACK: On Chrome OS, wait for the end of load of localStorage in memory
storageloadedcalls.push(then);
return;
};
}
if (then) {
then();
}
}

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand Down Expand Up @@ -287,7 +303,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
chrome.storage.local.remove(key);
} else {
window.localStorage.removeItem(key);
}
Expand Down
40 changes: 28 additions & 12 deletions activities/ActivityTemplate/lib/sugar-web/datastore/sugarizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load text
DatastoreObject.prototype.loadAsText = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, text);
}
var that = this;
html5storage.waitEndOfLoad(function() {
var result = html5storage.getValue(datastorePrefix + that.objectId);
if (result != null) {
that.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + that.objectId);
that.setDataAsText(text);
}
that.toload = false;
callback_c(null, result.metadata, text);
}
});
};

// Set metadata
Expand Down Expand Up @@ -233,6 +236,19 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
};
html5storage.load();

// Wait for end of loading
html5storage.waitEndOfLoad = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (storageloadedcalls.length != 0) { // HACK: On Chrome OS, wait for the end of load of localStorage in memory
storageloadedcalls.push(then);
return;
};
}
if (then) {
then();
}
}

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand Down Expand Up @@ -287,7 +303,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
chrome.storage.local.remove(key);
} else {
window.localStorage.removeItem(key);
}
Expand Down
40 changes: 28 additions & 12 deletions activities/Blockrain.activity/lib/sugar-web/datastore/sugarizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load text
DatastoreObject.prototype.loadAsText = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, text);
}
var that = this;
html5storage.waitEndOfLoad(function() {
var result = html5storage.getValue(datastorePrefix + that.objectId);
if (result != null) {
that.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + that.objectId);
that.setDataAsText(text);
}
that.toload = false;
callback_c(null, result.metadata, text);
}
});
};

// Set metadata
Expand Down Expand Up @@ -233,6 +236,19 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
};
html5storage.load();

// Wait for end of loading
html5storage.waitEndOfLoad = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (storageloadedcalls.length != 0) { // HACK: On Chrome OS, wait for the end of load of localStorage in memory
storageloadedcalls.push(then);
return;
};
}
if (then) {
then();
}
}

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand Down Expand Up @@ -287,7 +303,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
chrome.storage.local.remove(key);
} else {
window.localStorage.removeItem(key);
}
Expand Down
40 changes: 28 additions & 12 deletions activities/Calculate.activity/lib/sugar-web/datastore/sugarizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load text
DatastoreObject.prototype.loadAsText = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, text);
}
var that = this;
html5storage.waitEndOfLoad(function() {
var result = html5storage.getValue(datastorePrefix + that.objectId);
if (result != null) {
that.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + that.objectId);
that.setDataAsText(text);
}
that.toload = false;
callback_c(null, result.metadata, text);
}
});
};

// Set metadata
Expand Down Expand Up @@ -233,6 +236,19 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
};
html5storage.load();

// Wait for end of loading
html5storage.waitEndOfLoad = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (storageloadedcalls.length != 0) { // HACK: On Chrome OS, wait for the end of load of localStorage in memory
storageloadedcalls.push(then);
return;
};
}
if (then) {
then();
}
}

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand Down Expand Up @@ -287,7 +303,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
chrome.storage.local.remove(key);
} else {
window.localStorage.removeItem(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,20 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
// Load text
DatastoreObject.prototype.loadAsText = function(callback) {
var callback_c = datastore.callbackChecker(callback);
var result = html5storage.getValue(datastorePrefix + this.objectId);
if (result != null) {
this.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + this.objectId);
this.setDataAsText(text);
}
this.toload = false;
callback_c(null, result.metadata, text);
}
var that = this;
html5storage.waitEndOfLoad(function() {
var result = html5storage.getValue(datastorePrefix + that.objectId);
if (result != null) {
that.setMetadata(result.metadata);
var text = null;
if (result.text) {
text = html5storage.getValue(datastoreTextPrefix + that.objectId);
that.setDataAsText(text);
}
that.toload = false;
callback_c(null, result.metadata, text);
}
});
};

// Set metadata
Expand Down Expand Up @@ -233,6 +236,19 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
};
html5storage.load();

// Wait for end of loading
html5storage.waitEndOfLoad = function(then) {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
if (storageloadedcalls.length != 0) { // HACK: On Chrome OS, wait for the end of load of localStorage in memory
storageloadedcalls.push(then);
return;
};
}
if (then) {
then();
}
}

// Test if HTML5 storage is available
html5storage.test = function() {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime)
Expand Down Expand Up @@ -287,7 +303,7 @@ define(["sugar-web/bus", "sugar-web/env"], function(bus, env) {
try {
if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
this.values[key] = null;
chrome.store.remove(key);
chrome.storage.local.remove(key);
} else {
window.localStorage.removeItem(key);
}
Expand Down
Loading

0 comments on commit 5f0d7f0

Please sign in to comment.