Browse Source

feature(hooks): add new .starting() and .started() hooks

Ryan Chandler 4 years ago
parent
commit
1beaeabbba
8 changed files with 47 additions and 0 deletions
  1. 0 0
      dist/spruce.js
  2. 0 0
      dist/spruce.js.map
  3. 0 0
      dist/spruce.module.js
  4. 0 0
      dist/spruce.module.js.map
  5. 0 0
      dist/spruce.umd.js
  6. 0 0
      dist/spruce.umd.js.map
  7. 16 0
      src/index.js
  8. 31 0
      tests/hooks.spec.js

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


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


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


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


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


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


+ 16 - 0
src/index.js

@@ -14,7 +14,13 @@ const Spruce = {
 
     disableReactivity: false,
 
+    startingCallbacks: [],
+
+    startedCallbacks: [],
+
     start() {
+        this.startingCallbacks.forEach(fn => fn())
+
         this.attach()
 
         this.stores = createObservable(this.stores, {
@@ -38,6 +44,16 @@ const Spruce = {
                 this.disableReactivity = false
             }
         })
+
+        this.startedCallbacks.forEach(fn => fn())
+    },
+
+    starting(callback) {
+        this.startingCallbacks.push(callback)
+    },
+
+    started(callback) {
+        this.startedCallbacks.push(callback)
     },
 
     attach() {

+ 31 - 0
tests/hooks.spec.js

@@ -0,0 +1,31 @@
+import Alpine from 'alpinejs'
+import Spruce from '../dist/spruce'
+
+beforeEach(() => {
+    Spruce.subscribers = []
+})
+
+beforeAll(() => {
+    window.Spruce = Spruce
+    window.Alpine = Alpine
+})
+
+test('.starting() > callbacks are executed', () => {
+    let fixture = 0;
+
+    Spruce.starting(() => fixture++)
+
+    Spruce.start()
+
+    expect(fixture).toEqual(1)
+})
+
+test('.started() > callbacks are executed', () => {
+    let fixture = 0;
+
+    Spruce.started(() => fixture++)
+
+    Spruce.start()
+
+    expect(fixture).toEqual(1)
+})

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