Pārlūkot izejas kodu

tests(Spruce.watch)

Ryan Chandler 5 gadi atpakaļ
vecāks
revīzija
031ad14f03

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/spruce.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/spruce.js.map


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/spruce.module.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/spruce.module.js.map


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/spruce.umd.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/spruce.umd.js.map


+ 5 - 1
src/bus.js

@@ -37,6 +37,10 @@ export default {
     runWatchers(stores, target, key) {
         const self = this
 
+        if (self.watchers[key]) {
+            return self.watchers[key].forEach(callback => callback(target[key]))
+        }
+
         Object.keys(self.watchers)
             .filter(watcher => watcher.includes('.'))
             .forEach(fullDotNotationKey => {
@@ -45,7 +49,7 @@ export default {
                 if (key !== dotNotationParts[dotNotationParts.length - 1]) return
 
                 dotNotationParts.reduce((comparison, part) => {
-                    if (Object.is(target, comparison)) {
+                    if (comparison[key] === target[key] || Object.is(target, comparison)) {
                         self.watchers[fullDotNotationKey].forEach(callback => callback(target[key]))
                     }
 

+ 0 - 5
src/index.js

@@ -4,7 +4,6 @@ import EventBus from './bus'
 
 const Spruce = {
     options: {
-        enableUpdateEvents: true,
         globalStoreVariable: false,
     },
 
@@ -27,10 +26,6 @@ const Spruce = {
                 this.events.runWatchers(this.stores, target, key)
 
                 this.updateSubscribers(key, value)
-
-                if (this.options.enableUpdateEvents) {
-                    this.emit('updated')
-                }
             }
         })
 

+ 20 - 0
tests/bus.spec.js

@@ -64,4 +64,24 @@ test('.emit() > will dispatch browser event to window with spruce: prefix', asyn
     await waitFor(() => {
         expect(document.querySelector('span').innerText).toEqual('car')
     })
+})
+
+test('.watch() > can listen for changes to property', async () => {
+    let fixture = undefined
+    
+    Spruce.store('example', {
+        cool: 'stuff'
+    })
+
+    Spruce.watch('example.cool', (value) => {
+        fixture = value
+    })
+
+    await Spruce.start()
+
+    expect(fixture).toBeUndefined()
+
+    Spruce.stores.example.cool = 'amazing'
+
+    expect(fixture).toEqual('amazing')
 })

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels