Parcourir la source

Merge pull request #77 from ryangjchandler/feature/store-callbacks

feature: closure generated stores
Ryan Chandler il y a 4 ans
Parent
commit
a1f9d6687a

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
dist/spruce.js


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
dist/spruce.js.map


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
dist/spruce.module.js


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
dist/spruce.module.js.map


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
dist/spruce.umd.js


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
dist/spruce.umd.js.map


+ 4 - 0
src/index.js

@@ -53,6 +53,10 @@ const Spruce = {
     },
 
     store(name, state, persist = false) {
+        if (typeof state === 'function') {
+            state = state()
+        }
+        
         if (persist) {
             try {
                 this.stores[name] = this.retrieveFromLocalStorage(name, getMethods(state))

+ 22 - 0
tests/store.spec.js

@@ -29,6 +29,28 @@ test('Spruce.store() > existing namespace will not be overwritten', () => {
         bob: 'car'
     })
 
+    expect(Spruce.store('testing')).toEqual({
+        foo: 'bar'
+    })
+})
+
+test('Spruce.store() > can be used with functions that generate stores', () => {
+    Spruce.store('testing', function () {
+        return {
+            foo: 'bar'
+        }
+    })
+
+    expect(Spruce.store('testing')).toEqual({
+        foo: 'bar'
+    })
+
+    Spruce.store('testing', function () {
+        return {
+            bar: 'car'
+        }
+    })
+
     expect(Spruce.store('testing')).toEqual({
         foo: 'bar'
     })

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff