Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add DOM Storage getters

  • Loading branch information...
commit e491bce29e91e5cf60e34e16093f7c1a4ade99b3 1 parent 22ce35c
Félix Saparelli authored
Showing with 71 additions and 34 deletions.
  1. +2 −2 README.markdown
  2. +37 −13 dynworker.coffee
  3. +32 −19 dynworker.js
View
4 README.markdown
@@ -11,7 +11,7 @@ extra file you will ever have to load is `dynworker.js`.
DynWorker contains a few utilities to augment your worker. You can easily inject
functions into the worker, run arbitrary code, pass messages containing mixed
-data (everything is JSON-encoded), and access DOM storage (setters only atm).
+data (everything is JSON-encoded), and access DOM storage.
Future developments may include being able to clone workers, access the DOM asynchronously,
make full XHR requests (including XML parsing), access the indexedDB directly from
@@ -80,7 +80,7 @@ Inside the worker:
```javascript
$.localStorage.setItem("key", "data");
$.localStorage.getItem("key", function(data) {
- // Getters are not implemented yet
+ // Do something with that data
});
```
View
50 dynworker.coffee
@@ -61,6 +61,20 @@ DynWorker = (path = DynWorker.libpath) ->
# Use DOM Storage
store = (type, action, key, data) ->
switch action
+ when 'key'
+ val = if type is 'local'
+ window.localStorage.key key
+ else
+ window.sessionStorage.key key
+ cmd 'callback', data, [val]
+
+ when 'get'
+ val = if type is 'local'
+ window.localStorage.getItem key
+ else
+ window.sessionStorage.getItem key
+ cmd 'callback', data, [val]
+
when 'set'
if type is 'local'
window.localStorage.setItem key, data
@@ -102,6 +116,13 @@ DynWorker.path = (path) ->
InWorker = ->
+ # Hold callbacks here for doubly asynchronous processes
+ callbacks = []
+
+ # Run a callback stored above
+ deepCallback = (index, arguments_array) ->
+ callbacks[index].apply null, arguments_array
+
# Receive a message from the parent (main) thread
receive = (func) ->
callback = (e) ->
@@ -125,17 +146,18 @@ InWorker = ->
# Use DOM Storage from the worker. Everything is async,
# and the getters take a callback method instead of returning.
- store = {
- 'local': {
- setItem: (key, data) -> cmd 'domstorage', 'local', 'set', key, data
- removeItem: (key) -> cmd 'domstorage', 'local', 'remove', key
- clear: -> cmd 'domstorage', 'local', 'clear'
- }, 'session': {
- setItem: (key, data) -> cmd 'domstorage', 'session', 'set', key, data
- removeItem: (key) -> cmd 'domstorage', 'session', 'remove', key
- clear: -> cmd 'domstorage', 'session', 'clear'
+ store = (scope) ->
+ return {
+ key: (index, callback) ->
+ i = callbacks.push callback
+ cmd 'domstorage', scope, 'key', index, i-1
+ getItem: (key, callback) ->
+ i = callbacks.push callback
+ cmd 'domstorage', scope, 'get', key, i-1
+ setItem: (key, data) -> cmd 'domstorage', scope, 'set', key, data
+ removeItem: (key) -> cmd 'domstorage', scope, 'remove', key
+ clear: -> cmd 'domstorage', scope, 'clear'
}
- }
return {
@@ -143,8 +165,9 @@ InWorker = ->
receive: receive
send: send
cmd: cmd
- localStorage: store.local
- sessionStorage: store.session
+ callback: deepCallback
+ localStorage: store('local')
+ sessionStorage: store('session')
}
@@ -156,7 +179,8 @@ if typeof window == "undefined"
$.receive (e, data) ->
switch data['DynWorkerAction']
- when 'eval' then eval data['arg1']
+ when 'eval' then eval data.arg1
+ when 'callback' then $.callback data.arg1, data.arg2
else
# This is the main thread
window.DynWorker = DynWorker
View
51 dynworker.js
@@ -61,7 +61,14 @@
return weval("$.send($.ns['" + name + "'].apply(null, " + (JSON.stringify(args)) + "));");
};
store = function(type, action, key, data) {
+ var val;
switch (action) {
+ case 'key':
+ val = type === 'local' ? window.localStorage.key(key) : window.sessionStorage.key(key);
+ return cmd('callback', data, [val]);
+ case 'get':
+ val = type === 'local' ? window.localStorage.getItem(key) : window.sessionStorage.getItem(key);
+ return cmd('callback', data, [val]);
case 'set':
if (type === 'local') {
return window.localStorage.setItem(key, data);
@@ -107,7 +114,11 @@
}
};
InWorker = function() {
- var cmd, receive, send, store;
+ var callbacks, cmd, deepCallback, receive, send, store;
+ callbacks = [];
+ deepCallback = function(index, arguments_array) {
+ return callbacks[index].apply(null, arguments_array);
+ };
receive = function(func) {
var callback;
callback = function(e) {
@@ -135,37 +146,37 @@
}
return send(msg);
};
- store = {
- 'local': {
- setItem: function(key, data) {
- return cmd('domstorage', 'local', 'set', key, data);
+ store = function(scope) {
+ return {
+ key: function(index, callback) {
+ var i;
+ i = callbacks.push(callback);
+ return cmd('domstorage', scope, 'key', index, i - 1);
},
- removeItem: function(key) {
- return cmd('domstorage', 'local', 'remove', key);
+ getItem: function(key, callback) {
+ var i;
+ i = callbacks.push(callback);
+ return cmd('domstorage', scope, 'get', key, i - 1);
},
- clear: function() {
- return cmd('domstorage', 'local', 'clear');
- }
- },
- 'session': {
setItem: function(key, data) {
- return cmd('domstorage', 'session', 'set', key, data);
+ return cmd('domstorage', scope, 'set', key, data);
},
removeItem: function(key) {
- return cmd('domstorage', 'session', 'remove', key);
+ return cmd('domstorage', scope, 'remove', key);
},
clear: function() {
- return cmd('domstorage', 'session', 'clear');
+ return cmd('domstorage', scope, 'clear');
}
- }
+ };
};
return {
ns: {},
receive: receive,
send: send,
cmd: cmd,
- localStorage: store.local,
- sessionStorage: store.session
+ callback: deepCallback,
+ localStorage: store('local'),
+ sessionStorage: store('session')
};
};
if (typeof window === "undefined") {
@@ -174,7 +185,9 @@
$.receive(function(e, data) {
switch (data['DynWorkerAction']) {
case 'eval':
- return eval(data['arg1']);
+ return eval(data.arg1);
+ case 'callback':
+ return $.callback(data.arg1, data.arg2);
}
});
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.