Labs/Jetpack/JEP/11: Difference between revisions
(Updated for asynchronicity) |
(Updated for bug 496694 patch v2.) |
||
Line 27: | Line 27: | ||
==== Callbacks ==== | ==== Callbacks ==== | ||
Because of the asynchronous nature of this API, its operations communicate their statuses and results to callers via callbacks. In the context of simple storage, a callback is an object that defines the methods <code>onResult</code> and <code>onError</code>. | Because of the asynchronous nature of this API, its operations communicate their statuses and results to callers via callbacks. In the context of simple storage, a callback is either a function or an object that defines the methods <code>onResult</code> and <code>onError</code>. If a callback is a function, it is called when its associated asynchronous operation successfully completes. If a callback is an object, it must define the method <code>onResult</code> and, optionally, the method <code>onError</code>. <code>onResult</code> is called when the operation successfully completes, and <code>onError</code> is called when an error occurs during the operation. This allows callers to provide a simple function in the common case; only if callers wish to be notified of errors do they need to use the more verbose object form. | ||
In this proposal, <code>onResult</code> refers to either the function form or the <code>object.onResult</code> form. Since there is only one form for errors, <code>onError</code> refers to the <code>object.onError</code> form. | |||
<code>onResult</code> is called when the callback's associated asynchronous operation successfully completes: | <code>onResult</code> is called when the callback's associated asynchronous operation successfully completes: | ||
<pre class="brush:js;toolbar:false;"> | <pre class="brush:js;toolbar:false;"> | ||
onResult(key, [value]) | |||
</pre> | </pre> | ||
Line 38: | Line 40: | ||
<pre class="brush:js;toolbar:false;"> | <pre class="brush:js;toolbar:false;"> | ||
onError(key, [value,] errorMessage) | |||
</pre> | </pre> | ||
Line 57: | Line 59: | ||
<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>. If <code>value</code> is not a JS primitive or a JSON-able JS object, an exception is thrown. | <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>. If <code>value</code> is not a JS primitive or a JSON-able JS object, an exception is thrown. | ||
<code>callback</code>: A callback | <code>callback</code>: A callback. <code>onResult</code> and <code>onError</code> will be passed <code>key</code> and <code>value</code>. | ||
''Return value'' | ''Return value'' | ||
Line 73: | Line 75: | ||
<code>key</code>: A string uniquely identifying the data to be retrieved from persistent storage. If <code>key</code> is not a string, an exception is thrown. | <code>key</code>: A string uniquely identifying the data to be retrieved from persistent storage. If <code>key</code> is not a string, an exception is thrown. | ||
<code>callback</code>: A callback | <code>callback</code>: A callback. <code>onResult</code> will be passed <code>key</code> and a value. If <code>key</code> exists in the store, then the value will be <code>key</code>'s associated data. If no such key exists, then the value will be <code>undefined</code>. <code>onError</code> will be passed only <code>key</code>. | ||
''Return value'' | ''Return value'' | ||
Line 91: | Line 93: | ||
<code>key</code>: The string uniquely identifying the data to be removed from persistent storage. If <code>key</code> is not a string, an exception is thrown. | <code>key</code>: The string uniquely identifying the data to be removed from persistent storage. If <code>key</code> is not a string, an exception is thrown. | ||
<code>callback</code>: A callback | <code>callback</code>: A callback. <code>onResult</code> and <code>onError</code> will be passed only <code>key</code>. | ||
''Return value'' | ''Return value'' |
Revision as of 19:48, 11 June 2009
JEP 11 - Simple Persistent Storage
- Author: Atul Varma <atul at mozilla dot com>
- Champion: Atul Varma <atul at mozilla dot com>
- Status: Draft
- Type: API Track
- Created: 27 May 2009
- Reference Implementation: None
- JEP Index
Introduction and Rationale
This JEP describes a simple mechanism through which Jetpacks can persistently and asynchronously store JS primitives and blobs of JSON data. Asynchronicity is desirable because a Jetpack's usage of persistent storage should not block the browser's front-end.
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.
This proposal is favored over 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".
Part of this proposal involves adding a jetpack.storage
namespace.
Proposal
Persistent storage will live at jetpack.storage.simple
. The jetpack.storage
namespace will provide access to any other available storage systems, such as sqlite, secure/password storage, and so on. The current jetpack.sessionStorage
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 jetpack.storage.session
.
Callbacks
Because of the asynchronous nature of this API, its operations communicate their statuses and results to callers via callbacks. In the context of simple storage, a callback is either a function or an object that defines the methods onResult
and onError
. If a callback is a function, it is called when its associated asynchronous operation successfully completes. If a callback is an object, it must define the method onResult
and, optionally, the method onError
. onResult
is called when the operation successfully completes, and onError
is called when an error occurs during the operation. This allows callers to provide a simple function in the common case; only if callers wish to be notified of errors do they need to use the more verbose object form.
In this proposal, onResult
refers to either the function form or the object.onResult
form. Since there is only one form for errors, onError
refers to the object.onError
form.
onResult
is called when the callback's associated asynchronous operation successfully completes:
onResult(key, [value])
onError
is called when an error occurs during the operation:
onError(key, [value,] errorMessage)
The definitions of key
and value
vary according to the simple storage operation associated with the callback, and depending on the context value
may not be passed at all. Details follow in the sections below.
In the interest of promoting proper coding practices, callbacks are never optional.
Storing Values
jetpack.storage.simple.set(key, value, callback)
Arguments
key
: A string uniquely identifying the data to be placed in persistent storage. If key
is not a string, an exception is thrown.
value
: 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 key
is already being stored, it is overwritten by value
. If value
is not a JS primitive or a JSON-able JS object, an exception is thrown.
callback
: A callback. onResult
and onError
will be passed key
and value
.
Return value
This function has no return value.
Retrieving Values
jetpack.storage.simple.get(key, callback)
Arguments
key
: A string uniquely identifying the data to be retrieved from persistent storage. If key
is not a string, an exception is thrown.
callback
: A callback. onResult
will be passed key
and a value. If key
exists in the store, then the value will be key
's associated data. If no such key exists, then the value will be undefined
. onError
will be passed only key
.
Return value
This function has no return value.
Removing Values
jetpack.storage.simple.remove(key, callback)
Removes the key and its associated data from persistent storage. Calling jetpack.storage.simple.remove(key)
is the same as calling jetpack.storage.simple.set(key, undefined)
.
Arguments
key
: The string uniquely identifying the data to be removed from persistent storage. If key
is not a string, an exception is thrown.
callback
: A callback. onResult
and onError
will be passed only key
.
Return value
This function has no return value.
Other Notes
Equivalence vs. Equality
The values passed into set()
and returned from get()
are all serialized/de-serialized at the time of calling; this means that, for instance, given the following code:
var original = {foo: 'bar'}; jetpack.storage.simple.set('test', original);
the following condition will hold:
assert(jetpack.storage.simple.get('test') != original);