Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

… memory usage.
  • Loading branch information...
commit 48f4ecae2d489438827c167b5aa79ba69f55cbfb 1 parent fe893b8
@jacwright jacwright authored
View
14 README.md
@@ -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
19 lib/parser.js
@@ -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,12 +12,12 @@ 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;
@@ -25,13 +25,13 @@ function load(file, callback) {
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
3  lib/worker.js
@@ -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
46 lib/wurfl.js
@@ -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
8 package.json
@@ -1,9 +1,9 @@
{
"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"
},
@@ -11,7 +11,7 @@
"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"
Please sign in to comment.
Something went wrong with that request. Please try again.