Browse Source

wip(start building support for persistence)

Ryan Chandler 4 năm trước cách đây
mục cha
commit
f7a985916f

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 1
dist/spruce.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/spruce.js.map


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 1
dist/spruce.module.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/spruce.module.js.map


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 1
dist/spruce.umd.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/spruce.umd.js.map


+ 15 - 2
examples/index.html

@@ -6,12 +6,25 @@
     <body>
         <div x-data>
             <span x-text="$store.application.name"></span>
-            <button @click="$store.application.name = 'Example'">Click</button>
+            <button @click="$store.application.toggle()">Click</button>
         </div>
+
+        <div x-data>
+            <code>Persisted</code>
+            <input type="text" x-model="$store.persisted.example">
+        </div>
+
         <script>
             Spruce.store('application', {
-                name: 'Application'
+                name: 'Application',
+                toggle() {
+                    this.name = ['Application', 'Example'][Math.floor(Math.random() * 2)]
+                }
             })
+
+            Spruce.store('persisted', {
+                example: 'Hello'
+            }, true)
         </script>
     </body>
 </html>

+ 53 - 2
src/index.js

@@ -4,8 +4,12 @@ import { createObservable } from './observable'
 const Spruce = {
     stores: {},
 
+    persisted: [],
+
     subscribers: [],
 
+    disableReactivity: false,
+
     async start() {
         await domReady()
         
@@ -14,7 +18,19 @@ const Spruce = {
         document.addEventListener('turbolinks:render', this.attach)
 
         this.stores = createObservable(this.stores, {
-            set: this.updateSubscribers.bind(this)
+            set: () => {
+                if (this.disableReactivity) {
+                    return
+                }
+
+                this.updateSubscribers()
+
+                this.disableReactivity = true
+
+                this.persisted.forEach(this.updateLocalStorage.bind(this))
+
+                this.disableReactivity = false
+            }
         })
     },
 
@@ -32,7 +48,13 @@ const Spruce = {
         })
     },
 
-    store(name, state) {
+    store(name, state, persist = false) {
+        if (persist) {
+            this.stores[name] = this.retrieveFromLocalStorage(name)
+
+            this.persisted.push(name)
+        }
+
         if (! this.stores[name]) {
             this.stores[name] = state
         }
@@ -56,6 +78,35 @@ const Spruce = {
         this.subscribers.filter(el => !! el.__x).forEach(el => {
             el.__x.updateElements(el)
         })
+    },
+
+    retrieveFromLocalStorage(name) {
+        const storage = JSON.parse(window.localStorage.getItem(`__spruce:${name}`))
+
+        if (! storage) {
+            return null
+        }
+
+        return storage
+    },
+
+    updateLocalStorage(name) {
+        window.localStorage.setItem(
+            `__spruce:${name}`,
+            JSON.stringify(this.store(name))
+        )
+    },
+
+    persist(name) {
+        if (! this.persisted.includes(name)) {
+            this.persisted.push(name)
+        }
+
+        this.updateLocalStorage(name)
+    },
+
+    dontPersist(name) {
+        this.persisted = this.persisted.filter(_ => name !== _)
     }
 }
 

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác