Permalink
Browse files

Added option to specify specific groups to include in order to reduce…

… memory usage.
  • Loading branch information...
1 parent fe893b8 commit 48f4ecae2d489438827c167b5aa79ba69f55cbfb @jacwright jacwright committed Oct 11, 2011
Showing with 62 additions and 28 deletions.
  1. +14 −0 README.md
  2. +12 −7 lib/parser.js
  3. +2 −1 lib/worker.js
  4. +30 −16 lib/wurfl.js
  5. +4 −4 package.json
View
@@ -75,6 +75,20 @@ wurfl.watch(myWurflFile, function() {
});
```
+If you want to reduce memory usage even further, you can specify the the groups you are interested in, the rest will be left out completely. For example, if you only care about the "display" capabilities:
+
+```javascript
+wurfl.loadSync({ groups: [ 'display' ] });
+```
+
+When specifying groups the "file" parameter needs to be an options hash with `groups` and optionally `file`. You can use this in loadSync(options), load(options, callback), and watch(options, callback).
+
+```javascript
+var options = { file: __dirname + '/wurfl.xml', groups: [ 'display' ] };
+wurfl.loadSync(options);
+wurfl.watch(options);
+```
+
The most common use-case is to load the file up when your script starts synchronously since you need it to exist before you start querying against it, but then add a watch to asynchronously update it later:
```javascript
View
@@ -3,7 +3,7 @@ var path = require('path');
var expat = require('node-expat');
-function watch(file, options, callback) {
+function watch(file, groups, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
@@ -12,26 +12,26 @@ function watch(file, options, callback) {
fs.watchFile(file, options, function(curr, prev) {
if (curr.mtime === prev.mtime) return;
- load(file, callback);
+ load(file, groups, callback);
});
}
-function load(file, callback) {
+function load(file, groups, callback) {
if (typeof file === 'function') {
callback = file;
file = null;
}
fs.readFile(file, function(err, contents) {
if (err) return console.error('Could not load wurfl.xml file:', err);
- var devices = parse(contents);
+ var devices = parse(contents, groups);
if (callback) callback(devices);
});
}
-function parse(contents) {
+function parse(contents, groups) {
var parser = new expat.Parser();
var devices = [];
@@ -45,8 +45,13 @@ function parse(contents) {
device.groups = {};
devices.push(device);
} else if (name === 'group') {
- device.groups[attr.id] = group = {};
- } else if (name === 'capability') {
+ // if groups is provided, don't parse groups not in the array
+ if (groups && groups.indexOf(attr.id) === -1) {
+ group = null;
+ } else {
+ device.groups[attr.id] = group = {};
+ }
+ } else if (name === 'capability' && group) {
var value = attr.value;
if (parseFloat(value).toString() === value) {
value = parseFloat(value);
View
@@ -5,11 +5,12 @@ onmessage = function(event) {
var data = event.data;
var method = data.method;
var file = data.file;
+ var groups = data.groups;
if (!method) throw new TypeError('Message does not contain method');
if (!file) throw new TypeError('Message does not contain file');
if (!parser.hasOwnProperty(method)) throw new ReferenceError('Method ' + method + ' is not valid');
- parser[method](file, function(devices) {
+ parser[method](file, groups, function(devices) {
if (!devices) return;
postMessage({
method: method,
View
@@ -43,43 +43,57 @@ function getWorker() {
}
-function watch(file, callback) {
- if (file && !options && typeof file !== 'string') {
- options = file;
- file = undefined;
+function watch(options, callback) {
+ if (typeof options === 'function') {
+ callback = file;
+ options = undefined;
+ }
+ if (typeof options === 'string') {
+ options = { file: options };
}
- file = file || wurflFile;
+ options = options || { file: options };
+ options.file = options.file || wurflFile;
if (callback) callbacks.watch[file] = callback;
getWorker().postMessage({
method: 'watch',
- file: file
+ file: file,
+ groups: options.groups
});
}
-function load(file, callback) {
- if (typeof file === 'function') {
+function load(options, callback) {
+ if (typeof options === 'function') {
callback = file;
- file = undefined;
+ options = undefined;
}
- file = file || wurflFile;
+ if (typeof options === 'string') {
+ options = { file: options };
+ }
+ options = options || { file: options };
+ options.file = options.file || wurflFile;
- if (callback) callbacks.load[file] = callback;
+ if (callback) callbacks.load[options.file] = callback;
getWorker().postMessage({
method: 'load',
- file: file
+ file: options.file,
+ groups: options.groups
});
}
-function loadSync(file) {
- file = file || wurflFile;
+function loadSync(options) {
+ if (typeof options === 'string') {
+ options = { file: options };
+ }
+ options = options || { file: options };
+ options.file = options.file || wurflFile;
- var contents = fs.readFileSync(file);
- packageDevices(parser.parse(contents));
+ var contents = fs.readFileSync(options.file);
+ packageDevices(parser.parse(contents, options.groups));
}
View
@@ -1,17 +1,17 @@
{
"name": "wurfl",
"author": "Jacob Wright <jacwright@gmail.com> (jacwright.com)",
- "version": "0.1.0",
- "main": "./lib/wurfl"
- "description": "NodeJS library for loading Wireless Universal Resource File (wurfl) xml file efficiently into memory for use in applications that need to lookup mobile device capabilities by their user agent."
+ "version": "0.2.0",
+ "main": "./lib/wurfl",
+ "description": "NodeJS library for loading Wireless Universal Resource File (wurfl) xml file efficiently into memory for use in applications that need to lookup mobile device capabilities by their user agent.",
"scripts": {
"test": "node test/test.js"
},
"dependencies": {
"node-expat": "1.4.0",
"webworker": "0.8.4"
},
- "homepage": "https://github.com/touchads/node-wurfl-api"
+ "homepage": "https://github.com/touchads/node-wurfl-api",
"repository": {
"type": "git",
"url": "git://github.com/touchads/node-wurfl-api.git"

0 comments on commit 48f4eca

Please sign in to comment.