Skip to content

Commit b29f056

Browse files
committed
Merge pull request #1 from thgreasi/rollup
chore: use es6 & rollup
2 parents 44e4322 + c2653d6 commit b29f056

24 files changed

+982
-50
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [ "es2015-rollup" ]
3+
}

.eslintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "eslint:recommended",
4+
"env": {
5+
"node": true,
6+
"mocha": true
7+
},
8+
"globals": {
9+
"localforage": true,
10+
"Promise": true,
11+
"Observable": true,
12+
"self": true,
13+
"System": true
14+
}
15+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
build
12
bower_components
23
node_modules
34
components

.jscsrc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"esnext": true,
3+
"disallowSpacesInAnonymousFunctionExpression": {
4+
"beforeOpeningRoundBrace": true
5+
},
6+
"disallowTrailingComma": true,
7+
"requireBlocksOnNewline": true,
8+
"requireLineFeedAtFileEnd": true,
9+
"requireSpaceAfterKeywords": [
10+
"if",
11+
"else",
12+
"for",
13+
"while",
14+
"do",
15+
"switch",
16+
"return",
17+
"try",
18+
"catch"
19+
],
20+
"requireSpaceBeforeBlockStatements": true,
21+
"requireSpacesInConditionalExpression": true,
22+
"requireSpacesInFunctionExpression": {
23+
"beforeOpeningCurlyBrace": true
24+
},
25+
"safeContextKeyword": ["globalObject", "self"],
26+
"validateQuoteMarks": {
27+
"escape": true,
28+
"mark": "'"
29+
},
30+
"validateIndentation": 4
31+
}

.jshintrc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"asi": false,
3+
"bitwise": true,
4+
"browser": true,
5+
"curly": true,
6+
"eqeqeq": true,
7+
"eqnull": true,
8+
"esnext": true,
9+
"immed": true,
10+
"latedef": true,
11+
"newcap": true,
12+
"noarg": true,
13+
"nonew": true,
14+
"quotmark": false,
15+
"strict": false,
16+
"trailing": false,
17+
"undef": true,
18+
"unused": true,
19+
20+
"validthis": true,
21+
22+
"globals": {
23+
"console": true,
24+
"define": true,
25+
"localforage": true,
26+
"module": true,
27+
"Promise": true,
28+
"Observable": true,
29+
"require": true,
30+
"self": true,
31+
"System": true
32+
}
33+
}

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- "4.0"
4+
- "5.0"

bower.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

dist/localforage-observable.es6.js

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
var babelHelpers = {};
2+
babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
3+
return typeof obj;
4+
} : function (obj) {
5+
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
6+
};
7+
8+
babelHelpers.classCallCheck = function (instance, Constructor) {
9+
if (!(instance instanceof Constructor)) {
10+
throw new TypeError("Cannot call a class as a function");
11+
}
12+
};
13+
14+
babelHelpers.createClass = function () {
15+
function defineProperties(target, props) {
16+
for (var i = 0; i < props.length; i++) {
17+
var descriptor = props[i];
18+
descriptor.enumerable = descriptor.enumerable || false;
19+
descriptor.configurable = true;
20+
if ("value" in descriptor) descriptor.writable = true;
21+
Object.defineProperty(target, descriptor.key, descriptor);
22+
}
23+
}
24+
25+
return function (Constructor, protoProps, staticProps) {
26+
if (protoProps) defineProperties(Constructor.prototype, protoProps);
27+
if (staticProps) defineProperties(Constructor, staticProps);
28+
return Constructor;
29+
};
30+
}();
31+
32+
babelHelpers;
33+
34+
// thanks AngularJS
35+
function isDate(value) {
36+
return toString.call(value) === '[object Date]';
37+
}
38+
39+
function isFunction(value) {
40+
return typeof value === 'function';
41+
}
42+
43+
var isArray = function () {
44+
if (!isFunction(Array.isArray)) {
45+
return function (value) {
46+
return toString.call(value) === '[object Array]';
47+
};
48+
}
49+
return Array.isArray;
50+
}();
51+
52+
function isRegExp(value) {
53+
return toString.call(value) === '[object RegExp]';
54+
}
55+
56+
function equals(o1, o2) {
57+
if (o1 === o2) return true;
58+
if (o1 === null || o2 === null) return false;
59+
if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN
60+
var t1 = typeof o1 === 'undefined' ? 'undefined' : babelHelpers.typeof(o1),
61+
t2 = typeof o2 === 'undefined' ? 'undefined' : babelHelpers.typeof(o2),
62+
length,
63+
key,
64+
keySet;
65+
if (t1 == t2) {
66+
if (t1 == 'object') {
67+
if (isArray(o1)) {
68+
if (!isArray(o2)) return false;
69+
if ((length = o1.length) == o2.length) {
70+
for (key = 0; key < length; key++) {
71+
if (!equals(o1[key], o2[key])) return false;
72+
}
73+
return true;
74+
}
75+
} else if (isDate(o1)) {
76+
if (!isDate(o2)) return false;
77+
return isNaN(o1.getTime()) && isNaN(o2.getTime()) || o1.getTime() === o2.getTime();
78+
} else if (isRegExp(o1) && isRegExp(o2)) {
79+
return o1.toString() == o2.toString();
80+
} else {
81+
if (isArray(o2)) return false;
82+
keySet = {};
83+
for (key in o1) {
84+
if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
85+
if (!equals(o1[key], o2[key])) return false;
86+
keySet[key] = true;
87+
}
88+
for (key in o2) {
89+
if (!keySet.hasOwnProperty(key) && key.charAt(0) !== '$' && o2[key] !== undefined && !isFunction(o2[key])) return false;
90+
}
91+
return true;
92+
}
93+
}
94+
}
95+
return false;
96+
}
97+
98+
var ObservableLibraryMethods = ['clear',
99+
// 'getItem',
100+
// 'iterate',
101+
// 'key',
102+
// 'keys',
103+
// 'length',
104+
'removeItem', 'setItem'];
105+
106+
var LocalForageObservableWrapper = function () {
107+
function LocalForageObservableWrapper(options, subscriptionObserver) {
108+
babelHelpers.classCallCheck(this, LocalForageObservableWrapper);
109+
110+
this.options = options;
111+
this.subscriptionObserver = subscriptionObserver;
112+
}
113+
114+
babelHelpers.createClass(LocalForageObservableWrapper, [{
115+
key: 'hasMethodFilterOptions',
116+
value: function hasMethodFilterOptions() {
117+
if (this.options) {
118+
for (var i = 0, methodName; methodName = ObservableLibraryMethods[i]; i++) {
119+
if (this.options[methodName]) {
120+
return true;
121+
}
122+
}
123+
}
124+
return false;
125+
}
126+
}, {
127+
key: 'publish',
128+
value: function publish(publishObject) {
129+
if (publishObject.success && typeof this.subscriptionObserver.next === 'function') {
130+
try {
131+
this.subscriptionObserver.next(publishObject);
132+
} catch (e) {/* */}
133+
} else if (publishObject.fail && typeof this.subscriptionObserver.error === 'function') {
134+
try {
135+
this.subscriptionObserver.error(publishObject);
136+
} catch (e) {/* */}
137+
}
138+
}
139+
}]);
140+
return LocalForageObservableWrapper;
141+
}();
142+
143+
function processObserverList(list, changeArgs) {
144+
for (var i = 0, observableWrapper; observableWrapper = list[i]; i++) {
145+
var itemOptions = observableWrapper.options;
146+
if (!itemOptions || (!itemOptions.key || itemOptions.key === changeArgs.key) && (itemOptions[changeArgs.methodName] === true || !observableWrapper.hasMethodFilterOptions())) {
147+
observableWrapper.publish(changeArgs);
148+
}
149+
}
150+
}
151+
152+
function handleMethodCall(localforageInstance, methodName, args) {
153+
return localforageInstance.ready().then(function () {
154+
var changeArgs = {
155+
key: args[0],
156+
methodName: methodName,
157+
oldValue: null,
158+
newValue: null
159+
};
160+
161+
if (methodName === 'setItem' && args[1] !== undefined) {
162+
changeArgs.newValue = args[1];
163+
}
164+
165+
// if change detection is enabled to at least one active observable
166+
// and an applicable method is called then we should retrieve the old value
167+
var detectChanges = (methodName === 'setItem' || methodName === 'removeItem') && !!localforageInstance._observables.changeDetection.length;
168+
169+
var getOldValuePromise = detectChanges ? localforageInstance.getItem(changeArgs.key).then(function (value) {
170+
changeArgs.oldValue = value;
171+
// don't return anything
172+
}) : Promise.resolve();
173+
174+
var promise = getOldValuePromise.then(function () {
175+
return localforageInstance._baseMethods[methodName].apply(localforageInstance, args);
176+
}) /*.then(function() {
177+
return getDriverPromise(localforageInstance, localforageInstance.driver());
178+
}).then(function(driver) {
179+
return driver[methodName].apply(localforageInstance, args);
180+
})*/;
181+
182+
// don't return this promise so that the observers
183+
// get notified after the method invoker
184+
promise.then(function () {
185+
changeArgs.success = true;
186+
}).catch(function (error) {
187+
changeArgs.fail = true;
188+
changeArgs.error = error;
189+
}).then(function () {
190+
if (detectChanges && !equals(changeArgs.oldValue, changeArgs.newValue)) {
191+
processObserverList(localforageInstance._observables.changeDetection, changeArgs);
192+
}
193+
processObserverList(localforageInstance._observables.callDetection, changeArgs);
194+
});
195+
196+
return promise;
197+
});
198+
}
199+
200+
function wireUpMethods(localforageInstance) {
201+
function wireUpMethod(localforageInstance, methodName) {
202+
localforageInstance._baseMethods = localforageInstance._baseMethods || {};
203+
localforageInstance._baseMethods[methodName] = localforageInstance[methodName];
204+
localforageInstance[methodName] = function () {
205+
return handleMethodCall(this, methodName, arguments);
206+
};
207+
}
208+
209+
for (var i = 0, len = ObservableLibraryMethods.length; i < len; i++) {
210+
var methodName = ObservableLibraryMethods[i];
211+
wireUpMethod(localforageInstance, methodName);
212+
}
213+
}
214+
215+
function setup(localforageInstance) {
216+
if (!localforageInstance._observables) {
217+
localforageInstance._observables = {
218+
callDetection: [],
219+
changeDetection: []
220+
};
221+
222+
wireUpMethods(localforageInstance);
223+
}
224+
}
225+
226+
function localforageObservable(options) {
227+
var localforageInstance = this;
228+
setup(localforageInstance);
229+
230+
var localforageObservablesList = options && options.changeDetection === false ? localforageInstance._observables.callDetection : localforageInstance._observables.changeDetection;
231+
232+
var observable = localforageObservable.factory(function (observer) {
233+
var observableWrapper = new LocalForageObservableWrapper(options, observer);
234+
localforageObservablesList.push(observableWrapper);
235+
236+
return function () {
237+
var index = localforageObservablesList.indexOf(observableWrapper);
238+
if (index >= 0) {
239+
return localforageObservablesList.splice(index, 1);
240+
}
241+
};
242+
});
243+
244+
return observable;
245+
}
246+
247+
// In case the user want to override the used Observables
248+
// eg: with RxJS or ES-Observable
249+
localforageObservable.factory = function (subscribeFn) {
250+
return new Observable(subscribeFn);
251+
};
252+
253+
function extendPrototype(localforage) {
254+
try {
255+
var localforagePrototype = Object.getPrototypeOf(localforage);
256+
if (localforagePrototype) {
257+
localforagePrototype.newObservable = localforageObservable;
258+
return true;
259+
}
260+
} catch (e) {/* */}
261+
return false;
262+
}
263+
264+
var extendPrototypeResult = extendPrototype(localforage);
265+
266+
export { localforageObservable, extendPrototype, extendPrototypeResult };

0 commit comments

Comments
 (0)