Confirmed users
764
edits
(Updated for asynchronicity) |
m (→Example Usage) |
||
(21 intermediate revisions by 3 users not shown) | |||
Line 3: | Line 3: | ||
== JEP 11 - Simple Persistent Storage == | == JEP 11 - Simple Persistent Storage == | ||
* Author: | * Author: Drew Willcoxon, <adw at mozilla dot com> | ||
* | * Editors: Atul Varma <atul at mozilla dot com>, Aza Raskin <aza at mozilla dot com> | ||
* Status: | * Champion: Drew Willcoxon | ||
* Status: Implementing | |||
* Type: API Track | * Type: API Track | ||
* Created: 27 May 2009 | * Created: 27 May 2009 | ||
* Reference Implementation: None | * Reference Implementation: None | ||
* Relevant Bugs: {{bug|503466}}, {{bug|499871}}, {{bug|496694}} | |||
* [[Labs/Jetpack/JEPs|JEP Index]] | * [[Labs/Jetpack/JEPs|JEP Index]] | ||
=== Introduction and Rationale === | === Introduction and Rationale === | ||
This JEP describes a simple mechanism through which Jetpacks can persistently | This JEP describes a simple mechanism through which Jetpacks can persistently store JS primitives and blobs of JSON data. | ||
This proposal is favored over [https://developer.mozilla.org/Pt/DOM/DOM_Storage DOM Storage] because the latter only supports storing strings, which forces the developer to manually perform error-prone parsing tasks for almost any kind of use case. It should also be noted that the simple storage outlined in this proposal can be implemented on the web using DOM Storage; as such, this proposal should not be considered "breaking the web". | This proposal is favored over [https://developer.mozilla.org/Pt/DOM/DOM_Storage DOM Storage] because the latter only supports storing strings, which forces the developer to manually perform error-prone parsing tasks for almost any kind of use case. It should also be noted that the simple storage outlined in this proposal can be implemented on the web using DOM Storage; as such, this proposal should not be considered "breaking the web". | ||
Line 23: | Line 23: | ||
=== Proposal === | === Proposal === | ||
Simple, persistent storage will live at <code>jetpack.storage.simple</code>. The <code>jetpack.storage</code> namespace will provide access to any other available storage systems, such as SQLite, secure/password storage, and so on. The current <code>jetpack.sessionStorage</code> object, which allows arbitrary JS objects (they need not be JSON-able) to be stored between reloads of a Jetpack within the same Firefox session, will be renamed to <code>jetpack.storage.session</code>. | |||
Simple storage is really simple. <code>jetpack.storage.simple</code> is a single, persistent JavaScript object available and private to each Jetpack feature. For the most part this object is like any other JavaScript object, and a feature can set whatever properties it wants on it. To manipulate its persistent data, a feature therefore need only use the various [https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference standard JavaScript functions and operators]. | |||
<code> | The <code>jetpack.simple.storage</code> object is automatically flushed to disk. How and when it is flushed is an implementation detail -- either periodically and on onload or, perhaps in the future if using catch-alls, when a property is set on the object. Storage may be flushed manually, however, by calling <code>jetpack.storage.simple.sync()</code>. The object can be forced to reload its data from disk by calling <code>jetpack.storage.simple.open()</code>, although the data comes loaded automatically. | ||
==== Flushing Storage to Disk ==== | |||
<code> | As described above, the <code>jetpack.storage.simple</code> object is automatically written to disk, but a feature may force flush by calling: | ||
<pre class="brush:js;toolbar:false;"> | <pre class="brush:js;toolbar:false;"> | ||
jetpack.storage.simple.sync() | |||
jetpack.storage.simple. | |||
</pre> | </pre> | ||
''Arguments'' | ''Arguments'' | ||
This method takes no arguments. | |||
''Return value'' | ''Return value'' | ||
This | This method has no return value. | ||
==== | ==== Repopulating Storage ==== | ||
< | As described above, the <code>jetpack.storage.simple</code> object is automatically populated when a feature is loaded, but a feature may force the object to read from disk by calling: | ||
jetpack.storage.simple | |||
<pre class="brush:js;toolbar:false;"> | <pre class="brush:js;toolbar:false;"> | ||
jetpack.storage.simple. | jetpack.storage.simple.open() | ||
</pre> | </pre> | ||
Note that any properties already on the object will be overwritten, but no properties are deleted before loading. | |||
''Arguments'' | ''Arguments'' | ||
This method takes no arguments. | |||
''Return value'' | ''Return value'' | ||
This | This method has no return value. | ||
=== | === Example Usage === | ||
This code persistently stores some data: | |||
<pre class="brush:js;toolbar:false;"> | <pre class="brush:js;toolbar:false;"> | ||
var | jetpack.future.import("storage.simple"); | ||
jetpack.storage.simple. | var myStorage = jetpack.storage.simple; | ||
myStorage.fribblefrops = [1, 3, 3, 7]; | |||
myStorage.heimelfarbs = { bar: "baz" }; | |||
</pre> | </pre> | ||
And then to use these objects later: | |||
<pre class="brush:js;toolbar:false;"> | <pre class="brush:js;toolbar:false;"> | ||
var myStorage = jetpack.storage.simple; | |||
myStorage.fribblefrops.forEach(function (elt) console.log(elt)); | |||
var bar = myStorage.heimelfarbs.bar; | |||
</pre> | </pre> | ||
That's all there is to it! (These examples create a <code>myStorage</code> variable to emphasize the fact that <code>jetpack.storage.simple</code> is just a normal JavaScript object. We could have simply used <code>jetpack.storage.simple</code> directly.) |