Labs/Ubiquity/Writing Noun Types: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 3: Line 3:
{{draft}}
{{draft}}


Author: mitcho (Michael Yoshitaka Erlewine)
Authors: mitcho (Michael Yoshitaka Erlewine), brandon (Brandon Pung)


= Introduction =
= Introduction =
Line 151: Line 151:


=== Making the most of noun caching ===
=== Making the most of noun caching ===
Ubiquity has a caching feature which allows it to remember the suggestions that a noun type gave for a particular input. The tricky part of this caching is in deciding when to flush the cached results for each noun type. The length of time that a noun types results remain accurate can differ greatly depending on the nature of the noun type. For instance, a noun type based on your open tabs may only remain accurate for a few seconds, while a noun type for known street addresses will probably remain accurate for at least weeks/months, and a noun type that is strictly regex based will be accurate forever. There are two ways for a noun type to declare when it should be flushed: the cacheTime property and and the registerCacheObserver method.
'''The cacheTime property'''
By default, a noun types results will be cached for one day. However, the user can specify a different duration for the cache by adding the cacheTime property to their noun type with some integer value that represents the length of time to keep results cached in seconds. A cacheTime of 0 will result in no caching, while a cacheTime of -1 will result in the results being cached forever.
<pre>
// Example: Cache results for 1 minute
var noun_type_example = {
  label: "example",
  cacheTime: 60,
  suggest: function (text, html, callback, selectionIndices) {
    .......
  }
};
</pre>
'''The registerCacheObserver method'''
While the cacheTime property allows noun types to specify a fixed amount of time between flushings of the cache, for some noun types it makes more sense to have the cache flushed on certain events. For example, noun_type_tab flushes it's cache every time a new tab is opened or an old tab is closed. In order to register observers for flushing, simply add the registerCacheObserver property to your noun type. This property should be a function which takes a single argument that is a flush function. You can call this flush function anytime you want to flush the cache of this noun type.
<pre>
// Example
var noun_type_example = {
  label: "example",
  registerCacheObserver: function (flush) {
    //register observers here
    thing.addEventListener("someAction", flush, false);
  }
  suggest: function (text, html, callback, selectionIndices) {
    .......
  }
};
</pre>


=== Best practices ===
=== Best practices ===
52

edits

Navigation menu