Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: storeName can be used in createInstance and instance functions. #119

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Iterate supports early exit by returning non `undefined` value inside `iteratorC
Resulting value will be passed to the promise as the result of iteration.
You can use this to make a search in your data:
```js
$localForage.iterate(function(value, key) {
$localForage.iterate(function(value, key, iterationNumber) {
if(angular.isInt(value) && value > 10) {
return key;
}
Expand Down Expand Up @@ -156,13 +156,33 @@ You can use multiple instances of localForage at the same time. To create a new
name: '2nd',
driver: 'localStorageWrapper'
});

var lf3 = $localForage.createInstance({
name: '3rd',
storeName: 'kvpairs'
});
```

The parameters will inherit the default parameters that you might have configured in the config phase of your application (See [above](#configure-the-provider-) for details), but the new config object will overwrite them.
It means that you can have one instance using localStorage, and one instance using indexedDB/WebSQL, at the same time !
The instance will take the name that you will define in the config object. You can get an instance previously created by using the `instance` method:
```js
// DEPRECATED
var lf2 = $localForage.instance('2nd');

// NEW USAGE
var lf2 = $localForage.instance({
name: '2nd'
});

var lf3 = $localForage.instance({
name: '3rd',
storeName: 'kvpairs'
});

var lf4 = $localForage.instance({
storeName: 'example_store'
});
```

The `instance` method will return the default instance if you don't give a name parameter.
Expand Down
46 changes: 35 additions & 11 deletions src/angular-localForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
angularLocalForage.provider('$localForage', function() {
var lfInstances = {},
defaultConfig = {
name: 'lf'
name: 'lf',
storeName: 'keyvaluepairs',
},
// Send signals for each of the following actions ?
notify = {
Expand Down Expand Up @@ -55,28 +56,51 @@
LocalForageInstance.prototype.createInstance = function createInstance(config) {
if(angular.isObject(config)) { // create new instance
config = angular.extend({}, defaultConfig, config);
if(angular.isDefined(lfInstances[config.name])) {
throw new Error('A localForage instance with the name ' + config.name + ' is already defined.');
var lfInstanceName = config.name + '#' + config.storeName;
if(angular.isDefined(lfInstances[lfInstanceName])) {
throw new Error('A localForage instance with the name ' +
config.name + ' and storeName ' +
config.storeName + ' is already defined.');
}

lfInstances[config.name] = new LocalForageInstance(config);
return lfInstances[config.name];
lfInstances[lfInstanceName] = new LocalForageInstance(config);
return lfInstances[lfInstanceName];
} else {
throw new Error('The parameter should be a config object.')
throw new Error('The parameter should be a config object.');
}
};

LocalForageInstance.prototype.instance = function instance(name) {
if(angular.isUndefined(name)) {
return lfInstances[defaultConfig.name];
return lfInstances[defaultConfig.name + '#' + defaultConfig.storeName];
} else if(angular.isString(name)) {
if(angular.isDefined(lfInstances[name])) {
return lfInstances[name];
var lfInstanceName = name + '#' + defaultConfig.storeName;
if(angular.isDefined(lfInstances[lfInstanceName])) {
return lfInstances[lfInstanceName];
} else {
throw new Error('No localForage instance of that name exists.')
throw new Error('No localForage instance of that name exists.');
}
} else if(angular.isObject(name)) {
// if it is an object with {name, storeName} properties,
// return corresponding instance
var instanceObj = name;
if(!angular.isDefined(instanceObj.name) &&
!angular.isDefined(instanceObj.storeName)) {
throw new Error('instance parameter object needs to contain name or storeName');
}
var n = (angular.isDefined(instanceObj.name)
? instanceObj.name
: defaultConfig.name),
sn = (angular.isDefined(instanceObj.storeName)
? instanceObj.storeName
: defaultConfig.storeName);
if(angular.isDefined(lfInstances[n + '#' + sn])) {
return lfInstances[n + '#' + sn];
} else {
throw new Error('No localForage instance of that name exists.');
}
} else {
throw new Error('The parameter should be a string.')
throw new Error('The parameter should be a string or object.');
}
};

Expand Down
102 changes: 102 additions & 0 deletions tests/angular-localForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ describe('Module: LocalForageModule', function() {
}, done);
});

it('should have an iterationNumber with a 1-index', function(done) {
var count;

$localForage.iterate(function(value, key, iterationNumber) {
count = iterationNumber;
}).then(function() {
stopDigests(interval);
expect(count).toEqual(3);
done();
}, done);
})

it('key/value filter should work', function(done) {
//test key filter
$localForage.iterate(function(value, key) {
Expand Down Expand Up @@ -465,4 +477,94 @@ describe('Module: LocalForageModule', function() {
}, done);
});
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this added code is indented 4 spaces, but the rest of the code base is 2-space indent. Please conform, to keep a consistent style.

describe("createInstance", function () {
beforeEach(function () {
$localForage.createInstance({
name: 'DUPLICATE_INSTANCE_NAME'
});
});
it('should create a new instance', function () {
expect($localForage.createInstance({
name: 'TEST_INSTANCE'
})).toBeDefined();
});

it('should throw error if trying to create duplicate instance.',
function () {
expect($localForage.createInstance.bind($localForage, {
name: 'DUPLICATE_INSTANCE_NAME'
})).toThrowError(/already defined/);
});

it('should create instance with same name, different storeName',
function () {
expect($localForage.createInstance.bind($localForage, {
name: 'DUPLICATE_INSTANCE_NAME',
storeName: 'DIFFERENT_STORE_NAME'
})).not.toThrowError(/already defined/);
});
});

describe("instance", function () {
var $q, interval;
beforeEach(function () {
$localForage.createInstance({
name: 'TEST_INSTANCE_1'
});
$localForage.createInstance({
name: 'TEST_INSTANCE_2',
storeName: 'TEST_STORE_NAME_1'
});
$localForage.createInstance({
name: 'TEST_INSTANCE_2',
storeName: 'TEST_STORE_NAME_2'
});
inject(function (_$q_) {
$q = _$q_;
});
interval = triggerDigests();
});

afterEach(function () {
stopDigests(interval);
});

it('should get instance by name', function () {
expect($localForage.instance({
name: 'TEST_INSTANCE_1'
})).toBeDefined();
});

it('should throw exception if instance not exists', function () {
expect($localForage.instance.bind($localForage, {
name: 'NON_EXISTENT'
})).toThrowError();
});

it('should get instances with same name, different storeNames',
function (done) {
var instance1 = $localForage.instance({
name: 'TEST_INSTANCE_2',
storeName: 'TEST_STORE_NAME_1'
});
var instance2 = $localForage.instance({
name: 'TEST_INSTANCE_2',
storeName: 'TEST_STORE_NAME_2'
});
$q.all([
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to exercise setItem and getItem here to ensure that they're different. A simple:

expect(instance1).not.toBe(instance2)

will suffice to show that they are two separate references since they are reference types, which will clean up the $q stuff below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, they are the same LocalForageInstance references. but the createInstance behavior in localforage library drove me to write such tests because;
var inst1 = localforage.createInstance({name: 'foo', storeName: 'bar'});
var inst2 = localforage.createInstance({name: 'foo', storeName: 'bar'});
console.log('same ref:', inst1 === inst2);
// same ref: false

But you're right, in that case that is not a problem.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. From my reading of the code you submitted, the second var would throw an error, is that right?

I would say as long as:

var fooBar = { name: 'foo', storeName: 'bar' };
expect($localForage.instance(fooBar)).toBe($localForage.instance(fooBar));

is true, let's just simplify the test case to make it a little easier to understand what instance does in isolation from the other methods.

Might even be worth adding that as a test case, just to show that we're passing the instance by reference and expect that instance is (somewhat) referentially transparent?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already applied your suggestion and changed the test to not.toBe(instance).

my example was not for the $localForage, but for localforage itself.

in the localforage library, there is no instance function, just createInstance and my example just works fine (and two same instances are different references too.). I think in localforage module, createInstance name is a bit confusing, needs to be instance instead. because it also takes care of createInstance. in angular-localForage, the similar behavior can be produced with a function;

function instance(opts) {
    try {
        return $localForage.createInstance(opts);
    } catch(e) {
        return $localForage.instance(opts);
    }
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I followed that example you posted. Thanks for the clarification!

instance1.setItem('key', 'val1'),
instance2.setItem('key', 'val2')
]).then(function () {
return $q.all([
instance1.getItem('key').then(function (val) {
expect(val).toEqual('val1');
}),
instance2.getItem('key').then(function (val) {
expect(val).toEqual('val2');
})
]);
}).then(done);
});
});
});