Эх сурвалжийг харах

Merge pull request #48 from ryangjchandler/remove/event-bus

remove(event bus)
Ryan Chandler 4 жил өмнө
parent
commit
15f9fcf649

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.js.map


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.module.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.module.js.map


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.umd.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/spruce.umd.js.map


+ 0 - 72
src/bus.js

@@ -1,72 +0,0 @@
-export default {
-    watchers: {},
-
-    events: {},
-
-    on(name, callback, once = false) {
-        if (! this.events[name]) {
-            this.events[name] = []
-        }
-
-        this.events[name].push({ callback, once })
-
-        return () => this.off(name, callback)
-    },
-
-    once(name, callback) {
-        this.on(name, callback, true)
-    },
-
-    off(name, callback) {
-        this.events[name] = this.events[name].filter(registerCallback => {
-            return registerCallback.callback !== callback && registerCallback.once !== true
-        })
-    },
-
-    emit(name, data = {}) {
-        (this.events[name] || []).forEach(callback => {
-            callback.callback(data)
-
-            if (callback.once) {
-                this.off(name, callback)
-            }
-        })
-
-        window.dispatchEvent(new CustomEvent(`spruce:${name}`, {
-            detail: data,
-            bubbles: true
-        }))
-    },
-
-    watch(dotNotation, callback) {
-        if (! this.watchers[dotNotation]) {
-            this.watchers[dotNotation] = []
-        }
-
-        this.watchers[dotNotation].push(callback)
-    },
-
-    runWatchers(stores, target, key, value) {
-        const self = this
-
-        if (self.watchers[key]) {
-            return self.watchers[key].forEach(callback => callback(value))
-        }
-
-        Object.keys(self.watchers)
-            .filter(watcher => watcher.includes('.'))
-            .forEach(fullDotNotationKey => {
-                let dotNotationParts = fullDotNotationKey.split('.')
-
-                if (key !== dotNotationParts[dotNotationParts.length - 1]) return
-
-                dotNotationParts.reduce((comparison, part) => {
-                    if (comparison[key] === target[key] || Object.is(target, comparison)) {
-                        self.watchers[fullDotNotationKey].forEach(callback => callback(value))
-                    }
-
-                    return comparison[part]
-                }, stores)
-            })
-    }
-}

+ 1 - 30
src/index.js

@@ -1,29 +1,20 @@
 import { domReady } from './utils'
 import { createObservable } from './observable'
-import EventBus from './bus'
 
 const Spruce = {
-    events: EventBus,
-
     stores: {},
 
     subscribers: [],
 
     async start() {
         await domReady()
-
-        this.emit('init')
         
         this.attach()
 
         document.addEventListener('turbolinks:render', this.attach)
 
         this.stores = createObservable(this.stores, {
-            set: (target, key, value) => {
-                this.events.runWatchers(this.stores, target, key, value)
-
-                this.updateSubscribers()
-            }
+            set: this.updateSubscribers.bind(this)
         })
     },
 
@@ -65,26 +56,6 @@ const Spruce = {
         this.subscribers.filter(el => !! el.__x).forEach(el => {
             el.__x.updateElements(el)
         })
-    },
-
-    on(name, callback) {
-        return this.events.on(name, callback)
-    },
-
-    once(name, callback) {
-        return this.events.once(name, callback)
-    },
-
-    off(name, callback) {
-        this.events.off(name, callback)
-    },
-
-    emit(name, data = {}) {
-        this.events.emit(name, { ...data, store: this.stores })
-    },
-
-    watch(dotNotation, callback) {
-        this.events.watch(dotNotation, callback)
     }
 }
 

+ 0 - 10
src/utils.js

@@ -8,16 +8,6 @@ export const domReady = () => {
     })
 }
 
-export const buildInitExpression = el => {
-    let expression = "$store = Spruce.subscribe($el)"
-
-    if (el.hasAttribute('x-init')) {
-        expression = `${expression}; ${el.getAttribute('x-init')}`
-    }
-
-    return expression
-}
-
 export const isNullOrUndefined = value => {
     return value === null || value === undefined
 }

+ 0 - 144
tests/bus.spec.js

@@ -1,144 +0,0 @@
-import Spruce from '../dist/spruce'
-import Alpine from 'alpinejs'
-import { waitFor } from '@testing-library/dom'
-
-beforeEach(() => {
-    Spruce.subscribers = []
-})
-
-beforeAll(() => {
-    window.Spruce = Spruce
-    window.Alpine = Alpine
-})
-
-/* Spruce.on() */
-
-test('.on() > can be used to register event listeners', () => {
-    Spruce.on('example', (event) => {})
-
-    expect(Spruce.events.events.hasOwnProperty('example')).toBeTruthy()
-})
-
-test('.on() > listener can access store', () => {
-    Spruce.store('foo', {
-        bar: 'bob'
-    })
-
-    let fixture = undefined;
-
-    Spruce.on('example', (detail) => {
-        fixture = detail
-    })
-
-    Spruce.emit('example')
-
-    expect(fixture.store).toEqual(Spruce.stores)
-})
-
-/* Spruce.once() */
-
-test('.once() > can be used to register event listeners', () => {
-    Spruce.once('once', (event) => {})
-
-    expect(Spruce.events.events.hasOwnProperty('once')).toBeTruthy()
-})
-
-test('.once() > will unhook listener after first run', () => {
-    let fixture = 0
-
-    Spruce.once('once', (event) => {
-        fixture += 10
-    })
-
-    expect(fixture).toEqual(0)
-
-    Spruce.emit('once')
-
-    expect(fixture).toEqual(10)
-
-    Spruce.emit('once')
-
-    expect(fixture).toEqual(10)
-})
-
-/* Spruce.emit() */
-
-test('.emit() > will run registered listeners', () => {
-    let fixture = 0;
-
-    Spruce.on('example', (detail) => {
-        fixture =+ detail.inc
-    })
-
-    Spruce.emit('example', { inc: 10 })
-
-    expect(fixture).toEqual(10)
-})
-
-test('.emit() > will dispatch browser event to window with spruce: prefix', async () => {
-    document.body.innerHTML = `
-        <div x-data="{ foo: 'bar' }" @spruce:example.window="foo = $event.detail.foo">
-            <span x-text="foo"></span>
-        </div>
-    `
-
-    Alpine.start()
-
-    expect(document.querySelector('span').innerText).toEqual('bar')
-
-    Spruce.emit('example', { foo: 'car' })
-
-    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')
-})
-
-test('.off() > can unregister listener', () => {
-    let fixture = undefined;
-
-    const callback = () => {
-        fixture = 0
-    }
-
-    Spruce.on('fixture-event', callback)
-    
-    Spruce.off('fixture-event', callback)
-
-    Spruce.emit('fixture-event')
-
-    expect(fixture).toBeUndefined()
-})
-
-test('.off() > returned when calling .on()', () => {
-    let fixture = undefined;
-
-    let off = Spruce.on('fixture-event', () => {
-        fixture = 0
-    })
-
-    off()
-
-    Spruce.emit('fixture-event')
-
-    expect(fixture).toBeUndefined()
-})

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно