From 94f6c3eb929c2ff68b45a486271cf0772e46a48d Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 28 Apr 2016 19:55:49 +0300 Subject: [PATCH] fix: fix bad lib commit of an older file --- lib/localforageObservable.js | 389 ++++++++++------------------------ lib/localforageObservable_.js | 140 ------------ 2 files changed, 116 insertions(+), 413 deletions(-) delete mode 100644 lib/localforageObservable_.js diff --git a/lib/localforageObservable.js b/lib/localforageObservable.js index a134280..120136a 100644 --- a/lib/localforageObservable.js +++ b/lib/localforageObservable.js @@ -1,297 +1,140 @@ -(function() { - 'use strict'; - - // Promises! - var Promise = (typeof module !== 'undefined' && module.exports) ? - require('promise') : this.Promise; - - var globalObject = this; - var serializer = null; - - var ModuleType = { - DEFINE: 1, - EXPORT: 2, - WINDOW: 3 - }; - - // Attaching to window (i.e. no module loader) is the assumed, - // simple default. - var moduleType = ModuleType.WINDOW; - - // Find out what kind of module setup we have; if none, we'll just attach - // localForage to the main window. - if (typeof define === 'function' && define.amd) { - moduleType = ModuleType.DEFINE; - } else if (typeof module !== 'undefined' && module.exports) { - moduleType = ModuleType.EXPORT; - } - - function getDriverPromise(localForageInstance, driverName) { - getDriverPromise.result = getDriverPromise.result || {}; - if (getDriverPromise.result[driverName]) { - return getDriverPromise.result[driverName]; - } - if (!localForageInstance || typeof localForageInstance.getDriver !== 'function') { - Promise.reject(new Error( - 'localforage.getDriver() was not available! ' + - 'localforage-observable requires localforage v1.4+')); +import { equals as isEqual } from './utils'; +import { ObservableLibraryMethods } from './ObservableLibraryMethods'; +import { LocalForageObservableWrapper } from './LocalForageObservableWrapper'; + +function processObserverList(list, changeArgs) { + for (var i = 0, observableWrapper; (observableWrapper = list[i]); i++) { + var itemOptions = observableWrapper.options; + if (!itemOptions || ( + (!itemOptions.key || itemOptions.key === changeArgs.key) && + (itemOptions[changeArgs.methodName] === true || + !observableWrapper.hasMethodFilterOptions()) + )) { + observableWrapper.publish(changeArgs); } - getDriverPromise.result[driverName] = localForageInstance.getDriver(driverName); - return getDriverPromise.result[driverName]; - } - - - var ObservableLibraryMethods = [ - 'clear', - // 'getItem', - // 'iterate', - // 'key', - // 'keys', - // 'length', - 'removeItem', - 'setItem' - ]; - - function LocalForageObservableWrapper (options, subscriptionObserver) { - this.options = options; - this.subscriptionObserver = subscriptionObserver; } +} + +function handleMethodCall(localforageInstance, methodName, args) { + return localforageInstance.ready() + .then(function () { + var changeArgs = { + key: args[0], + methodName: methodName, + oldValue: null, + newValue: null + }; - LocalForageObservableWrapper.prototype.hasMethodFilterOptions = function () { - if (this.options) { - for (var i = 0, methodName; (methodName = ObservableLibraryMethods[i]); i++) { - if (this.options[methodName]) { - return true; - } + if (methodName === 'setItem' && args[1] !== undefined) { + changeArgs.newValue = args[1]; } - } - return false; - }; - LocalForageObservableWrapper.prototype.publish = function (publishObject) { - if (publishObject.success && typeof this.subscriptionObserver.next === 'function') { - try { - this.subscriptionObserver.next(publishObject); - } catch (e) { } - } else if (publishObject.fail && typeof this.subscriptionObserver.error === 'function') { - try { - this.subscriptionObserver.error(publishObject); - } catch (e) { } - } - }; - - function isSubscriptionObject(subscriptionObject) { - return subscriptionObject && - (typeof subscriptionObject.next === 'function' || - typeof subscriptionObject.error === 'function' || - typeof subscriptionObject.complete === 'function'); - } - - function localforageObservable(options) { - var localforageInstance = this; - setup(localforageInstance); - - var localforageObservablesList = options && options.changeDetection === false ? - localforageInstance._observables.callDetection : - localforageInstance._observables.changeDetection; - - var observable = localforageObservable.factory(function(observer) { - var observableWrapper = new LocalForageObservableWrapper(options, observer); - localforageObservablesList.push(observableWrapper); - - return function() { - var index = localforageObservablesList.indexOf(observableWrapper); - if (index >= 0) { - return localforageObservablesList.splice(index, 1); + // if change detection is enabled to at least one active observable + // and an applicable method is called then we should retrieve the old value + var detectChanges = (methodName === 'setItem' || methodName === 'removeItem') && + !!localforageInstance._observables.changeDetection.length; + + var getOldValuePromise = detectChanges ? + localforageInstance.getItem(changeArgs.key).then(function(value) { + changeArgs.oldValue = value; + // don't return anything + }) : + Promise.resolve(); + + var promise = getOldValuePromise.then(function() { + return localforageInstance._baseMethods[methodName].apply(localforageInstance, args); + })/*.then(function() { + return getDriverPromise(localforageInstance, localforageInstance.driver()); + }).then(function(driver) { + return driver[methodName].apply(localforageInstance, args); + })*/; + + // don't return this promise so that the observers + // get notified after the method invoker + promise.then(function() { + changeArgs.success = true; + }).catch(function(error) { + changeArgs.fail = true; + changeArgs.error = error; + }).then(function() { + if (detectChanges && !isEqual(changeArgs.oldValue, changeArgs.newValue)) { + processObserverList( + localforageInstance._observables.changeDetection, + changeArgs); } - }; - }); + processObserverList( + localforageInstance._observables.callDetection, + changeArgs); + }); - return observable; + return promise; + }); +} + +function wireUpMethods(localforageInstance) { + function wireUpMethod(localforageInstance, methodName) { + localforageInstance._baseMethods = localforageInstance._baseMethods || {}; + localforageInstance._baseMethods[methodName] = localforageInstance[methodName]; + localforageInstance[methodName] = function () { + return handleMethodCall(this, methodName, arguments); + }; } - // In case the user want to override the used Observables - // eg: with RxJS or ES-Observable - localforageObservable.factory = function (subscribeFn) { - return new Observable(subscribeFn); - }; - - function processObserverList(list, changeArgs) { - for (var i = 0, observableWrapper; (observableWrapper = list[i]); i++) { - var itemOptions = observableWrapper.options; - if (!itemOptions || ( - (!itemOptions.key || itemOptions.key === changeArgs.key) && - (itemOptions[changeArgs.methodName] === true || - !observableWrapper.hasMethodFilterOptions()) - )) { - observableWrapper.publish(changeArgs); - } - } + for (var i = 0, len = ObservableLibraryMethods.length; i < len; i++) { + var methodName = ObservableLibraryMethods[i]; + wireUpMethod(localforageInstance, methodName); } +} - var isEqual = (function() { - // thanks AngularJS - function equals(o1, o2) { - if (o1 === o2) return true; - if (o1 === null || o2 === null) return false; - if (o1 !== o1 && o2 !== o2) return true; // NaN === NaN - var t1 = typeof o1, t2 = typeof o2, length, key, keySet; - if (t1 == t2) { - if (t1 == 'object') { - if (isArray(o1)) { - if (!isArray(o2)) return false; - if ((length = o1.length) == o2.length) { - for(key=0; key= 0) { + return localforageObservablesList.splice(index, 1); + } + }; + }); - function wireUpMethods(localforageInstance) { - function wireUpMethod(localforageInstance, methodName) { - localforageInstance._baseMethods = localforageInstance._baseMethods || {}; - localforageInstance._baseMethods[methodName] = localforageInstance[methodName]; - localforageInstance[methodName] = function () { - return handleMethodCall(this, methodName, arguments); - }; - } + return observable; +} - for (var i = 0, len = ObservableLibraryMethods.length; i < len; i++) { - var methodName = ObservableLibraryMethods[i]; - wireUpMethod(localforageInstance, methodName); - } - } +// In case the user want to override the used Observables +// eg: with RxJS or ES-Observable +localforageObservable.factory = function (subscribeFn) { + return new Observable(subscribeFn); +}; - function extendPrototype(localforage) { +export function extendPrototype(localforage) { + try { var localforagePrototype = Object.getPrototypeOf(localforage); if (localforagePrototype) { localforagePrototype.newObservable = localforageObservable; + return true; } - } + } catch (e) { /* */ } + return false; +} - extendPrototype(localforage); - - if (moduleType === ModuleType.DEFINE) { - define('localforageObservable', function() { - return localforageObservable; - }); - } else if (moduleType === ModuleType.EXPORT) { - module.exports = localforageObservable; - } else { - this.localforageObservable = localforageObservable; - } -}).call(window); +export var extendPrototypeResult = extendPrototype(localforage); diff --git a/lib/localforageObservable_.js b/lib/localforageObservable_.js deleted file mode 100644 index 120136a..0000000 --- a/lib/localforageObservable_.js +++ /dev/null @@ -1,140 +0,0 @@ -import { equals as isEqual } from './utils'; -import { ObservableLibraryMethods } from './ObservableLibraryMethods'; -import { LocalForageObservableWrapper } from './LocalForageObservableWrapper'; - -function processObserverList(list, changeArgs) { - for (var i = 0, observableWrapper; (observableWrapper = list[i]); i++) { - var itemOptions = observableWrapper.options; - if (!itemOptions || ( - (!itemOptions.key || itemOptions.key === changeArgs.key) && - (itemOptions[changeArgs.methodName] === true || - !observableWrapper.hasMethodFilterOptions()) - )) { - observableWrapper.publish(changeArgs); - } - } -} - -function handleMethodCall(localforageInstance, methodName, args) { - return localforageInstance.ready() - .then(function () { - var changeArgs = { - key: args[0], - methodName: methodName, - oldValue: null, - newValue: null - }; - - if (methodName === 'setItem' && args[1] !== undefined) { - changeArgs.newValue = args[1]; - } - - // if change detection is enabled to at least one active observable - // and an applicable method is called then we should retrieve the old value - var detectChanges = (methodName === 'setItem' || methodName === 'removeItem') && - !!localforageInstance._observables.changeDetection.length; - - var getOldValuePromise = detectChanges ? - localforageInstance.getItem(changeArgs.key).then(function(value) { - changeArgs.oldValue = value; - // don't return anything - }) : - Promise.resolve(); - - var promise = getOldValuePromise.then(function() { - return localforageInstance._baseMethods[methodName].apply(localforageInstance, args); - })/*.then(function() { - return getDriverPromise(localforageInstance, localforageInstance.driver()); - }).then(function(driver) { - return driver[methodName].apply(localforageInstance, args); - })*/; - - // don't return this promise so that the observers - // get notified after the method invoker - promise.then(function() { - changeArgs.success = true; - }).catch(function(error) { - changeArgs.fail = true; - changeArgs.error = error; - }).then(function() { - if (detectChanges && !isEqual(changeArgs.oldValue, changeArgs.newValue)) { - processObserverList( - localforageInstance._observables.changeDetection, - changeArgs); - } - processObserverList( - localforageInstance._observables.callDetection, - changeArgs); - }); - - return promise; - }); -} - -function wireUpMethods(localforageInstance) { - function wireUpMethod(localforageInstance, methodName) { - localforageInstance._baseMethods = localforageInstance._baseMethods || {}; - localforageInstance._baseMethods[methodName] = localforageInstance[methodName]; - localforageInstance[methodName] = function () { - return handleMethodCall(this, methodName, arguments); - }; - } - - for (var i = 0, len = ObservableLibraryMethods.length; i < len; i++) { - var methodName = ObservableLibraryMethods[i]; - wireUpMethod(localforageInstance, methodName); - } -} - -function setup(localforageInstance) { - if (!localforageInstance._observables) { - localforageInstance._observables = { - callDetection: [], - changeDetection: [] - }; - - wireUpMethods(localforageInstance); - } -} - -export function localforageObservable(options) { - var localforageInstance = this; - setup(localforageInstance); - - var localforageObservablesList = options && options.changeDetection === false ? - localforageInstance._observables.callDetection : - localforageInstance._observables.changeDetection; - - var observable = localforageObservable.factory(function(observer) { - var observableWrapper = new LocalForageObservableWrapper(options, observer); - localforageObservablesList.push(observableWrapper); - - return function() { - var index = localforageObservablesList.indexOf(observableWrapper); - if (index >= 0) { - return localforageObservablesList.splice(index, 1); - } - }; - }); - - return observable; -} - -// In case the user want to override the used Observables -// eg: with RxJS or ES-Observable -localforageObservable.factory = function (subscribeFn) { - return new Observable(subscribeFn); -}; - -export function extendPrototype(localforage) { - try { - var localforagePrototype = Object.getPrototypeOf(localforage); - if (localforagePrototype) { - localforagePrototype.newObservable = localforageObservable; - return true; - } - } catch (e) { /* */ } - return false; -} - -export var extendPrototypeResult = extendPrototype(localforage);