瀏覽代碼

feat(event bus)

Ryan Chandler 5 年之前
父節點
當前提交
df842f3348
共有 8 個文件被更改,包括 30 次插入4 次删除
  1. 1 1
      dist/spruce.js
  2. 0 0
      dist/spruce.js.map
  3. 1 1
      dist/spruce.module.js
  4. 0 0
      dist/spruce.module.js.map
  5. 1 1
      dist/spruce.umd.js
  6. 0 0
      dist/spruce.umd.js.map
  7. 19 0
      src/bus.js
  8. 8 1
      src/index.js

+ 1 - 1
dist/spruce.js

@@ -1,2 +1,2 @@
-var t=function(t){return null==t},e=function(r,o){return Object.keys(r).forEach(function(n){t(r[n])||Object.getPrototypeOf(r[n])!==Object.prototype||(r[n]=e(r[n],o))}),new Proxy(r,{get:function(t,e){return o.hasOwnProperty("get")&&o.get(e),t[e]},set:function(r,n,s){return t(s)||"object"!=typeof s||(s=e(s,o)),o.set(n,r[n]=s),!0}})},r={options:{globalStoreVariable:!1},stores:{},subscribers:[],start:function(){try{var t=this;return Promise.resolve(new Promise(function(t){"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})).then(function(){document.querySelectorAll("[x-subscribe]").forEach(function(t){t.setAttribute("x-init",function(t){var e="$store = Spruce.subscribe($el)";return t.hasAttribute("x-init")&&(e=e+"; "+t.getAttribute("x-init")),e}(t)),t.removeAttribute("x-subscribe")}),t.stores=e(t.stores,{set:function(e,r){t.updateSubscribers(e,r)}}),t.options.globalStoreVariable&&(document.querySelectorAll("[x-data]").forEach(function(e){t.subscribers.includes(e)||t.subscribers.push(e)}),window.$store=t.stores)})}catch(t){return Promise.reject(t)}},store:function(t,e){void 0===e&&(e={}),this.stores[t]||(this.stores[t]=e)},subscribe:function(t){return this.subscribers.push(t),this.stores},updateSubscribers:function(t,e){this.subscribers.forEach(function(t){void 0!==t.__x&&(t.__x.$data.spruce=e)})},config:function(t){void 0===t&&(t={}),this.options=Object.assign(this.options,t)}},o=window.deferLoadingAlpine||function(t){t()};window.deferLoadingAlpine=function(t){window.Spruce=r,window.Spruce.start(),o(t)},module.exports=r;
+var t=function(t){return null==t},e=function(n,s){return Object.keys(n).forEach(function(o){t(n[o])||Object.getPrototypeOf(n[o])!==Object.prototype||(n[o]=e(n[o],s))}),new Proxy(n,{get:function(t,e){return s.hasOwnProperty("get")&&s.get(e),t[e]},set:function(n,o,r){return t(r)||"object"!=typeof r||(r=e(r,s)),s.set(o,n[o]=r),!0}})},n={options:{globalStoreVariable:!1},events:{events:{},on:function(t,e){this.events.hasOwnProperty(t)||(this.events[t]=[]),this.events[t].push(e)},emit:function(t,e){void 0===e&&(e={}),this.events.hasOwnProperty(t)&&this.events[t].forEach(function(t){t(e)})}},stores:{},subscribers:[],start:function(){try{var t=this;return Promise.resolve(new Promise(function(t){"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})).then(function(){document.querySelectorAll("[x-subscribe]").forEach(function(t){t.setAttribute("x-init",function(t){var e="$store = Spruce.subscribe($el)";return t.hasAttribute("x-init")&&(e=e+"; "+t.getAttribute("x-init")),e}(t)),t.removeAttribute("x-subscribe")}),t.stores=e(t.stores,{set:function(e,n){t.updateSubscribers(e,n)}}),t.options.globalStoreVariable&&(document.querySelectorAll("[x-data]").forEach(function(e){t.subscribers.includes(e)||t.subscribers.push(e)}),window.$store=t.stores)})}catch(t){return Promise.reject(t)}},store:function(t,e){void 0===e&&(e={}),this.stores[t]||(this.stores[t]=e)},subscribe:function(t){return this.subscribers.push(t),this.stores},updateSubscribers:function(t,e){this.subscribers.forEach(function(t){void 0!==t.__x&&(t.__x.$data.spruce=e)})},config:function(t){void 0===t&&(t={}),this.options=Object.assign(this.options,t)},on:function(t,e){this.events.on(t,e)}},s=window.deferLoadingAlpine||function(t){t()};window.deferLoadingAlpine=function(t){window.Spruce=n,window.Spruce.start(),s(t)},module.exports=n;
 //# sourceMappingURL=spruce.js.map

File diff suppressed because it is too large
+ 0 - 0
dist/spruce.js.map


+ 1 - 1
dist/spruce.module.js

@@ -1,2 +1,2 @@
-var t=function(t){return null==t},e=function(r,n){return Object.keys(r).forEach(function(o){t(r[o])||Object.getPrototypeOf(r[o])!==Object.prototype||(r[o]=e(r[o],n))}),new Proxy(r,{get:function(t,e){return n.hasOwnProperty("get")&&n.get(e),t[e]},set:function(r,o,s){return t(s)||"object"!=typeof s||(s=e(s,n)),n.set(o,r[o]=s),!0}})},r={options:{globalStoreVariable:!1},stores:{},subscribers:[],start:function(){try{var t=this;return Promise.resolve(new Promise(function(t){"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})).then(function(){document.querySelectorAll("[x-subscribe]").forEach(function(t){t.setAttribute("x-init",function(t){var e="$store = Spruce.subscribe($el)";return t.hasAttribute("x-init")&&(e=e+"; "+t.getAttribute("x-init")),e}(t)),t.removeAttribute("x-subscribe")}),t.stores=e(t.stores,{set:function(e,r){t.updateSubscribers(e,r)}}),t.options.globalStoreVariable&&(document.querySelectorAll("[x-data]").forEach(function(e){t.subscribers.includes(e)||t.subscribers.push(e)}),window.$store=t.stores)})}catch(t){return Promise.reject(t)}},store:function(t,e){void 0===e&&(e={}),this.stores[t]||(this.stores[t]=e)},subscribe:function(t){return this.subscribers.push(t),this.stores},updateSubscribers:function(t,e){this.subscribers.forEach(function(t){void 0!==t.__x&&(t.__x.$data.spruce=e)})},config:function(t){void 0===t&&(t={}),this.options=Object.assign(this.options,t)}},n=window.deferLoadingAlpine||function(t){t()};window.deferLoadingAlpine=function(t){window.Spruce=r,window.Spruce.start(),n(t)};export default r;
+var t=function(t){return null==t},e=function(n,s){return Object.keys(n).forEach(function(r){t(n[r])||Object.getPrototypeOf(n[r])!==Object.prototype||(n[r]=e(n[r],s))}),new Proxy(n,{get:function(t,e){return s.hasOwnProperty("get")&&s.get(e),t[e]},set:function(n,r,o){return t(o)||"object"!=typeof o||(o=e(o,s)),s.set(r,n[r]=o),!0}})},n={options:{globalStoreVariable:!1},events:{events:{},on:function(t,e){this.events.hasOwnProperty(t)||(this.events[t]=[]),this.events[t].push(e)},emit:function(t,e){void 0===e&&(e={}),this.events.hasOwnProperty(t)&&this.events[t].forEach(function(t){t(e)})}},stores:{},subscribers:[],start:function(){try{var t=this;return Promise.resolve(new Promise(function(t){"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})).then(function(){document.querySelectorAll("[x-subscribe]").forEach(function(t){t.setAttribute("x-init",function(t){var e="$store = Spruce.subscribe($el)";return t.hasAttribute("x-init")&&(e=e+"; "+t.getAttribute("x-init")),e}(t)),t.removeAttribute("x-subscribe")}),t.stores=e(t.stores,{set:function(e,n){t.updateSubscribers(e,n)}}),t.options.globalStoreVariable&&(document.querySelectorAll("[x-data]").forEach(function(e){t.subscribers.includes(e)||t.subscribers.push(e)}),window.$store=t.stores)})}catch(t){return Promise.reject(t)}},store:function(t,e){void 0===e&&(e={}),this.stores[t]||(this.stores[t]=e)},subscribe:function(t){return this.subscribers.push(t),this.stores},updateSubscribers:function(t,e){this.subscribers.forEach(function(t){void 0!==t.__x&&(t.__x.$data.spruce=e)})},config:function(t){void 0===t&&(t={}),this.options=Object.assign(this.options,t)},on:function(t,e){this.events.on(t,e)}},s=window.deferLoadingAlpine||function(t){t()};window.deferLoadingAlpine=function(t){window.Spruce=n,window.Spruce.start(),s(t)};export default n;
 //# sourceMappingURL=spruce.module.js.map

File diff suppressed because it is too large
+ 0 - 0
dist/spruce.module.js.map


+ 1 - 1
dist/spruce.umd.js

@@ -1,2 +1,2 @@
-!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.spruce=e()}(this,function(){var t=function(t){return null==t},e=function(n,r){return Object.keys(n).forEach(function(o){t(n[o])||Object.getPrototypeOf(n[o])!==Object.prototype||(n[o]=e(n[o],r))}),new Proxy(n,{get:function(t,e){return r.hasOwnProperty("get")&&r.get(e),t[e]},set:function(n,o,i){return t(i)||"object"!=typeof i||(i=e(i,r)),r.set(o,n[o]=i),!0}})},n={options:{globalStoreVariable:!1},stores:{},subscribers:[],start:function(){try{var t=this;return Promise.resolve(new Promise(function(t){"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})).then(function(){document.querySelectorAll("[x-subscribe]").forEach(function(t){t.setAttribute("x-init",function(t){var e="$store = Spruce.subscribe($el)";return t.hasAttribute("x-init")&&(e=e+"; "+t.getAttribute("x-init")),e}(t)),t.removeAttribute("x-subscribe")}),t.stores=e(t.stores,{set:function(e,n){t.updateSubscribers(e,n)}}),t.options.globalStoreVariable&&(document.querySelectorAll("[x-data]").forEach(function(e){t.subscribers.includes(e)||t.subscribers.push(e)}),window.$store=t.stores)})}catch(t){return Promise.reject(t)}},store:function(t,e){void 0===e&&(e={}),this.stores[t]||(this.stores[t]=e)},subscribe:function(t){return this.subscribers.push(t),this.stores},updateSubscribers:function(t,e){this.subscribers.forEach(function(t){void 0!==t.__x&&(t.__x.$data.spruce=e)})},config:function(t){void 0===t&&(t={}),this.options=Object.assign(this.options,t)}},r=window.deferLoadingAlpine||function(t){t()};return window.deferLoadingAlpine=function(t){window.Spruce=n,window.Spruce.start(),r(t)},n});
+!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.spruce=e()}(this,function(){var t=function(t){return null==t},e=function(n,o){return Object.keys(n).forEach(function(s){t(n[s])||Object.getPrototypeOf(n[s])!==Object.prototype||(n[s]=e(n[s],o))}),new Proxy(n,{get:function(t,e){return o.hasOwnProperty("get")&&o.get(e),t[e]},set:function(n,s,r){return t(r)||"object"!=typeof r||(r=e(r,o)),o.set(s,n[s]=r),!0}})},n={options:{globalStoreVariable:!1},events:{events:{},on:function(t,e){this.events.hasOwnProperty(t)||(this.events[t]=[]),this.events[t].push(e)},emit:function(t,e){void 0===e&&(e={}),this.events.hasOwnProperty(t)&&this.events[t].forEach(function(t){t(e)})}},stores:{},subscribers:[],start:function(){try{var t=this;return Promise.resolve(new Promise(function(t){"loading"==document.readyState?document.addEventListener("DOMContentLoaded",t):t()})).then(function(){document.querySelectorAll("[x-subscribe]").forEach(function(t){t.setAttribute("x-init",function(t){var e="$store = Spruce.subscribe($el)";return t.hasAttribute("x-init")&&(e=e+"; "+t.getAttribute("x-init")),e}(t)),t.removeAttribute("x-subscribe")}),t.stores=e(t.stores,{set:function(e,n){t.updateSubscribers(e,n)}}),t.options.globalStoreVariable&&(document.querySelectorAll("[x-data]").forEach(function(e){t.subscribers.includes(e)||t.subscribers.push(e)}),window.$store=t.stores)})}catch(t){return Promise.reject(t)}},store:function(t,e){void 0===e&&(e={}),this.stores[t]||(this.stores[t]=e)},subscribe:function(t){return this.subscribers.push(t),this.stores},updateSubscribers:function(t,e){this.subscribers.forEach(function(t){void 0!==t.__x&&(t.__x.$data.spruce=e)})},config:function(t){void 0===t&&(t={}),this.options=Object.assign(this.options,t)},on:function(t,e){this.events.on(t,e)}},o=window.deferLoadingAlpine||function(t){t()};return window.deferLoadingAlpine=function(t){window.Spruce=n,window.Spruce.start(),o(t)},n});
 //# sourceMappingURL=spruce.umd.js.map

File diff suppressed because it is too large
+ 0 - 0
dist/spruce.umd.js.map


+ 19 - 0
src/bus.js

@@ -0,0 +1,19 @@
+export default {
+    events: {},
+
+    on(name, callback) {
+        if (! this.events.hasOwnProperty(name)) {
+            this.events[name] = []
+        }
+
+        this.events[name].push(callback)
+    },
+
+    emit(name, data = {}) {
+        if (this.events.hasOwnProperty(name)) {
+            this.events[name].forEach(callback => {
+                callback(data)
+            })
+        }
+    }
+}

+ 8 - 1
src/index.js

@@ -1,11 +1,14 @@
 import { domReady, buildInitExpression } from './utils'
 import { createObservable } from './observable'
+import EventBus from './bus'
 
 const Spruce = {
     options: {
         globalStoreVariable: false,
     },
 
+    events: EventBus,
+
     stores: {},
 
     subscribers: [],
@@ -57,7 +60,11 @@ const Spruce = {
 
     config(options = {}) {
         this.options = Object.assign(this.options, options)
-    }
+    },
+
+    on(name, callback) {
+        this.events.on(name, callback)
+    },
 }
 
 const deferrer = window.deferLoadingAlpine || function (callback) { callback() }

Some files were not shown because too many files changed in this diff