-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmemojs.js
More file actions
110 lines (92 loc) · 2.85 KB
/
memojs.js
File metadata and controls
110 lines (92 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Generated by CoffeeScript 1.7.1
(function() {
var Memo, memo,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Memo = (function() {
function Memo() {
this._fireChange = __bind(this._fireChange, this);
this._storageKeyEventListeners = {};
}
Memo.prototype.set = function(key, value) {
var serializedValue;
if (value !== void 0) {
serializedValue = JSON.stringify(value);
window.localStorage[key] = serializedValue;
return value;
}
};
Memo.prototype.get = function(key) {
var value;
if (value = localStorage[key]) {
return this._parseJSONSilently(value);
} else {
return null;
}
};
Memo.prototype.keys = function() {
return Object.keys(localStorage);
};
Memo.prototype["delete"] = function(key) {
return localStorage.removeItem(key);
};
Memo.prototype.clear = function() {
return localStorage.clear();
};
Memo.prototype.all = function() {
var object;
object = {};
this.keys().map((function(_this) {
return function(key) {
return object[key] = _this.get(key);
};
})(this));
return object;
};
Memo.prototype.addListener = function(key, handler) {
var handlers;
if (handlers = this._storageKeyEventListeners[key]) {
return handlers.push(handler);
} else {
return this._storageKeyEventListeners[key] = [handler];
}
};
Memo.prototype._fireChange = function(storageEvent) {
var eventObject, listener, listeners, _i, _len, _results;
if (!(this._listenersForEvent(storageEvent) && this._storageValueChanged(storageEvent))) {
return;
}
listeners = this._listenersForEvent(storageEvent) || [];
eventObject = {
key: storageEvent.key,
oldValue: this._parseJSONSilently(storageEvent.oldValue),
newValue: this._parseJSONSilently(storageEvent.newValue),
url: storageEvent.url
};
_results = [];
for (_i = 0, _len = listeners.length; _i < _len; _i++) {
listener = listeners[_i];
_results.push(listener(eventObject));
}
return _results;
};
Memo.prototype._listenersForEvent = function(storageEvent) {
return this._storageKeyEventListeners[storageEvent.key];
};
Memo.prototype._storageValueChanged = function(storageEvent) {
return storageEvent.oldValue !== storageEvent.newValue;
};
Memo.prototype._parseJSONSilently = function(jsonString) {
var error;
try {
return JSON.parse(jsonString);
} catch (_error) {
error = _error;
return jsonString;
}
};
return Memo;
})();
memo = new Memo;
window.addEventListener('storage', memo._fireChange, false);
window.memo = memo;
}).call(this);