Labs/Jetpack/JEP/11: Difference between revisions

m
(→‎Introduction and Rationale: added notes on implementation)
 
(28 intermediate revisions by 4 users not shown)
Line 3: Line 3:
== JEP 11 - Simple Persistent Storage ==
== JEP 11 - Simple Persistent Storage ==


* Author: Atul Varma <atul at mozilla dot com>
* Author: Drew Willcoxon, <adw at mozilla dot com>
* Champion: Atul Varma <atul at mozilla dot com>
* Editors: Atul Varma <atul at mozilla dot com>, Aza Raskin <aza at mozilla dot com>
* Status: Draft
* 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]]


Line 15: Line 17:
This JEP describes a simple mechanism through which Jetpacks can persistently store JS primitives and blobs of JSON data.
This JEP describes a simple mechanism through which Jetpacks can persistently store JS primitives and blobs of JSON data.


The [https://developer.mozilla.org/Pt/DOM/DOM_Storage DOM Storage] specification is not favorable because it only supports storing strings, which forces the developer to manually perform parsing tasks.
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".
 
The API of this proposal is constrained by what is possible to implement using JavaScript 1.8, since distributing binary components with the Jetpack extension is nontrivial.


Part of this proposal involves adding a <code>jetpack.storage</code> namespace.
Part of this proposal involves adding a <code>jetpack.storage</code> namespace.
Line 23: Line 23:
=== Proposal ===
=== Proposal ===


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, 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>.


==== Storing Values ====
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].
 
<pre class="brush:js;toolbar:false;">
jetpack.storage.simple.set(key, value)
</pre>


''Arguments''
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.


<code>key</code>: A string uniquely identifying the data to be placed in persistent storage.
==== Flushing Storage to Disk ====


<code>value</code>: A JS primitive or JSON-able JS object that represents the data to be stored in persistent storage. If some other data with the given <code>key</code> is already being stored, it is overwritten by <code>value</code>.
As described above, the <code>jetpack.storage.simple</code> object is automatically written to disk, but a feature may force flush by calling:
 
''Return value''
 
This function has no return value.
 
==== Retrieving Values ====


<pre class="brush:js;toolbar:false;">
<pre class="brush:js;toolbar:false;">
jetpack.storage.simple.get(key)
jetpack.storage.simple.sync()
</pre>
</pre>


''Arguments''
''Arguments''


<code>key</code>: A string uniquely identifying the data to be retrieved from persistent storage.
This method takes no arguments.


''Return value''
''Return value''


A JS primitive or JSON-able JS object that represents the corresponding value for <code>key</code>.
This method has no return value.


==== Removing Values ====
==== 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:


<pre class="brush:js;toolbar:false;">
<pre class="brush:js;toolbar:false;">
jetpack.storage.simple.remove(key)
jetpack.storage.simple.open()
</pre>
</pre>


Removes the key and its associated data from persistent storage. Calling <code>jetpack.storage.simple.remove(key)</code> is the same as calling <code>jetpack.storage.simple.set(key, undefined)</code>.
Note that any properties already on the object will be overwritten, but no properties are deleted before loading.


''Arguments''
''Arguments''


<code>key</code>: The string uniquely identifying the data to be removed from persistent storage.
This method takes no arguments.


''Return value''
''Return value''


This function has no return value.
This method has no return value.


==== Other Notes ====
=== Example Usage ===


The values passed into <code>set()</code> and returned from <code>get()</code> are all serialized/de-serialized at the time of calling; this means that, for instance, given the following code:
This code persistently stores some data:


<pre class="brush:js;toolbar:false;">
<pre class="brush:js;toolbar:false;">
var original = {foo: 'bar'};
jetpack.future.import("storage.simple");
jetpack.storage.simple.set('test', original);
var myStorage = jetpack.storage.simple;
myStorage.fribblefrops = [1, 3, 3, 7];
myStorage.heimelfarbs = { bar: "baz" };
</pre>
</pre>


the following statement will not be true:
And then to use these objects later:


<pre class="brush:js;toolbar:false;">
<pre class="brush:js;toolbar:false;">
jetpack.storage.simple.get('test') == original;
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.)
Confirmed users
764

edits