摘要:命名一個(gè)要一個(gè)只讀創(chuàng)建一些列改變需要一個(gè)監(jiān)聽(tīng)改變每一次改變狀態(tài)的時(shí)候執(zhí)行調(diào)用返回狀態(tài)優(yōu)化想要第一次監(jiān)聽(tīng)的時(shí)候返回當(dāng)前狀態(tài)監(jiān)聽(tīng)改變狀態(tài)統(tǒng)一使用調(diào)用法打印日志信息監(jiān)聽(tīng)改變之前的狀態(tài)監(jiān)聽(tīng)改變之后的狀態(tài)可以少寫好多個(gè)優(yōu)化想要第一次監(jiān)聽(tīng)的時(shí)候返回當(dāng)前
Step01.命名一個(gè)storeService要一個(gè)只讀state
var storeService = new Object({ _state: { age: 1 }, get state() { return this._state; }, set state(v) { throw new Error(" is read only.") } });
Step02.創(chuàng)建一些列mutation改變state
var storeService = new Object({ _state: { age: 1 }, get state() { return this._state; }, set state(v) { throw new Error(" is read only.") }, mutationModifyAge(newAge) { state.age = newAge; }, mutationModifyAge2(newAge) { state.age = newAge; }, mutationModifyAge3(newAge) { state.age = newAge; } });
Step03.需要一個(gè)watch監(jiān)聽(tīng)改變,每一次改變狀態(tài)的時(shí)候(執(zhí)行mutation)調(diào)用listenCallback 返回狀態(tài)
var storeService = new Object({ _listeners: [], _state: { age: 1 }, get state() { return this._state; }, set state(v) { throw new Error(" is read only.") }, mutationModifyAge(newAge) { state.age = newAge; this._dealCallback(); }, mutationModifyAge2(newAge) { state.age = newAge; this._dealCallback(); }, mutationModifyAge3(newAge) { state.age = newAge; this._dealCallback(); }, _dealCallback() { this._listeners.forEach(listener => listener(this.state)) }, watch(listener) { // 優(yōu)化想要第一次監(jiān)聽(tīng)的時(shí)候返回當(dāng)前狀態(tài) listener(this.state); this._listeners.push(listener); } }); // 監(jiān)聽(tīng)store storeService.watch(function (state) { console.log("callback", state); }) // 改變狀態(tài) storeService.mutationModifyAge(2)
Step04.統(tǒng)一使用dispatch調(diào)用mutation法
var storeService = new Object({ _listeners: [], _state: { age: 1 }, get state() { return this._state; }, set state(v) { throw new Error(" is read only.") }, dispatch(type, ...paylod) { // 打印日志信息 console.log(type ? type : "監(jiān)聽(tīng)", "改變之前的狀態(tài)", this.state); if (this._mutation[type]) { this._mutation[type](this.state, ...paylod); } console.log(type ? type : "監(jiān)聽(tīng)", "改變之后的狀態(tài)", this.state); // 可以少寫好多個(gè)listenCallback this._dealCallback(); }, _mutation: { mutationModifyAge(state, newAge) { state.age = newAge; }, mutationModifyAge2(state, newAge) { state.age = newAge; }, mutationModifyAge3(state, newAge) { state.age = newAge; } }, _dealCallback() { this._listeners.forEach(listener => listener(this.state)) }, watch(listener) { // 優(yōu)化想要第一次監(jiān)聽(tīng)的時(shí)候返回當(dāng)前狀態(tài) // listener(this.state); // 優(yōu)化讓第一次監(jiān)聽(tīng)也可以記錄歷史日志 this.dispatch(); this._listeners.push(listener); } }); // 監(jiān)聽(tīng)store storeService.watch(function (state) { console.log("callback", state); }) // 改變狀態(tài) storeService.dispatch("mutationModifyAge", 2)
以上內(nèi)容為創(chuàng)建一個(gè)簡(jiǎn)單的store。
以下開(kāi)始按照Vuex0.3.0開(kāi)始優(yōu)化
Step05.vuex dispatcher調(diào)用的方法是mutation,
mutation是唯一操作state的方法,
mutation不可包含任何副作用,
mutation對(duì)外暴露dispatch調(diào)用將上面的mutation 改為mutation
var storeService = new Object({ _listeners: [], _state: { age: 1 }, get state() { return this._state; }, set state(v) { throw new Error(" is read only.") }, dispatch(type, ...paylod) { // 打印日志信息 console.log(type ? type : "監(jiān)聽(tīng)", "改變之前的狀態(tài)", this.state); if (this._mutation[type]) { this._mutation[type](this.state, ...paylod); } console.log(type ? type : "監(jiān)聽(tīng)", "改變之后的狀態(tài)", this.state); // 可以少寫好多個(gè)listenCallback this._dealCallback(); }, _mutation: { mutationModifyAge(state, newAge) { state.age = newAge; }, mutationModifyAge2(state, newAge) { state.age = newAge; }, mutationModifyAge3(state, newAge) { state.age = newAge; } }, _dealCallback() { this._listeners.forEach(listener => listener(this.state)) }, watch(listener) { // 優(yōu)化想要第一次監(jiān)聽(tīng)的時(shí)候返回當(dāng)前狀態(tài) // listener(this.state); // 優(yōu)化讓第一次監(jiān)聽(tīng)也可以記錄歷史日志 this.dispatch(); this._listeners.push(listener); } }); // 監(jiān)聽(tīng)store storeService.watch(function (state) { console.log("callback", state); }) // 改變狀態(tài) storeService.dispatch("mutationModifyAge", 2)
Step05.vuex中mutation為純函數(shù),不可包含任何副作用,添加action來(lái)處理一系列副作用,最終還是調(diào)用dispatch 去改變state
var storeService = new Object({ _listeners: [], _state: { age: 1 }, get state() { return this._state; }, set state(v) { throw new Error(" is read only.") }, dispatch(type, ...paylod) { // 打印日志信息 console.log(type ? type : "監(jiān)聽(tīng)", "改變之前的狀態(tài)", this.state); if (this._mutation[type]) { this._mutation[type](this.state, ...paylod); } console.log(type ? type : "監(jiān)聽(tīng)", "改變之后的狀態(tài)", this.state); // 可以少寫好多個(gè)listenCallback this._dealCallback(); }, _mutation: { mutationModifyAge(state, newAge) { state.age = newAge; }, mutationModifyAge2(state, newAge) { state.age = newAge; }, mutationModifyAge3(state, newAge) { state.age = newAge; } }, _dealCallback() { this._listeners.forEach(listener => listener(this.state)) }, watch(listener) { // 優(yōu)化想要第一次監(jiān)聽(tīng)的時(shí)候返回當(dāng)前狀態(tài) // listener(this.state); // 優(yōu)化讓第一次監(jiān)聽(tīng)也可以記錄歷史日志 this.dispatch(); this._listeners.push(listener); }, bindAction(actions) { this.actions = Object.create(null); this._actions = Object.create(null); function createAction(action, store) { if (typeof action === "function") { return (...payload) => action(store, ...payload) } } Object.keys(actions).forEach(name => { console.log(actions[name]); this._actions[name] = createAction(actions[name], this) if (!this.actions[name]) { this.actions[name] = (...args) => this._actions[name](...args) } }) } }); var actions = { // 包含副作用的action asyncModifyAge(store, ...paylod) { setTimeout(() => { store.dispatch("mutationModifyAge", ...paylod) }, 2000); }, // 多次調(diào)用dispatch asyncMulModifyAge(store, ...paylod) { setTimeout(() => { store.dispatch("mutationModifyAge", ...paylod) }, 2000); store.dispatch("mutationModifyAge2", ...paylod) } } storeService.bindAction(actions); // 監(jiān)聽(tīng)store storeService.watch(function (state) { console.log("callback", state); }) // 改變狀態(tài) storeService.dispatch("mutationModifyAge", 2) storeService.actions.asyncMulModifyAge(21);
Step06 封裝成一個(gè)類hKeeper
var defaultOption = { state: {}, actions: {}, mutations: {} }; class hKeeper { constructor(options) { options = Object.assign({}, defaultOption, options); this._state = options.state; this.listeners = []; const dispatch = this.dispatch; this.dispatch = (...args) => { dispatch.apply(this, args); }; this.actions = Object.create(null); this._setupMutations(options.mutations); this._setupActions(options.actions); } get state() { return this._state } set state(v) { throw new Error("[hKeeper] hKeeper state is read only.") } dispatch(type, ...payload) { const mutation = this._mutations[type] const state = this.state if (mutation) { mutation(state, ...payload) } this.listeners.forEach(listener => listener(this.state)) } _setupMutations(mutations) { this._mutations = mutations; } // 設(shè)置action _setupActions(actions) { function createAction(action, store) { if (typeof action === "function") { return (...payload) => action(store, ...payload) } } this._actions = Object.create(null) Object.keys(actions).forEach(name => { this._actions[name] = createAction(actions[name], this) if (!this.actions[name]) { this.actions[name] = (...args) => this._actions[name](...args) } }) } watch(listener) { listener(this.state); this.listeners.push(listener) } } var storeService = new hKeeper( { state: { age: 1 }, actions: { // 包含副作用的action asyncModifyAge(store, ...paylod) { setTimeout(() => { store.dispatch("mutationModifyAge", ...paylod) }, 2000); }, // 多次調(diào)用dispatch asyncMulModifyAge(store, ...paylod) { setTimeout(() => { store.dispatch("mutationModifyAge", ...paylod) }, 2000); store.dispatch("mutationModifyAge2", ...paylod) } }, mutations: { mutationModifyAge(state, newAge) { state.age = newAge; }, mutationModifyAge2(state, newAge) { state.age = newAge; }, mutationModifyAge3(state, newAge) { state.age = newAge; } } } ) // 監(jiān)聽(tīng)store storeService.watch(function (state) { console.log("callback", state); }) // 改變狀態(tài) storeService.dispatch("mutationModifyAge", 2) storeService.actions.asyncMulModifyAge(21);
參考Vuex0.3.0版本 applyMiddleware 參考Redux
https://github.com/HereSincer...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/90674.html
摘要:本文旨在對(duì)鎖相關(guān)源碼本文中的源碼來(lái)自使用場(chǎng)景進(jìn)行舉例,為讀者介紹主流鎖的知識(shí)點(diǎn),以及不同的鎖的適用場(chǎng)景。中,關(guān)鍵字和的實(shí)現(xiàn)類都是悲觀鎖。自適應(yīng)意味著自旋的時(shí)間次數(shù)不再固定,而是由前一次在同一個(gè)鎖上的自旋時(shí)間及鎖的擁有者的狀態(tài)來(lái)決定。 前言 Java提供了種類豐富的鎖,每種鎖因其特性的不同,在適當(dāng)?shù)膱?chǎng)景下能夠展現(xiàn)出非常高的效率。本文旨在對(duì)鎖相關(guān)源碼(本文中的源碼來(lái)自JDK 8)、使用場(chǎng)景...
摘要:方法首先初始化一個(gè)回調(diào)函數(shù),這是當(dāng)一個(gè)成為之后就會(huì)調(diào)用的一個(gè)用于初始化一系列變量的方法,包括拓?fù)淙绾卧诩荷戏峙?,拓?fù)錉顟B(tài)更新,清除函數(shù),還有監(jiān)控線程等。 寫在前面的話,筆者第一次閱讀框架源碼,所以可能有些地方理解錯(cuò)誤或者沒(méi)有詳細(xì)解釋,如果在閱讀過(guò)程發(fā)現(xiàn)錯(cuò)誤很歡迎在文章下面評(píng)論指出。文章后續(xù)會(huì)陸續(xù)更新,可以關(guān)注或者收藏,轉(zhuǎn)發(fā)請(qǐng)先私信我,謝謝。對(duì)了,筆者看的是2.2.1這個(gè)版本 概述 ?...
摘要:起飛指南作者元瀟方凳雅集出品目前放出來(lái)了個(gè)內(nèi)置,但僅僅基于以下兩個(gè),就能做很多事情。行代碼實(shí)現(xiàn)一個(gè)全局元瀟根組件掛上即可子組件調(diào)用隨時(shí)隨地實(shí)現(xiàn)一個(gè)局部元瀟的本質(zhì)是的一個(gè)語(yǔ)法糖,感興趣可以閱讀一下的類型定義和實(shí)現(xiàn)。 React Hook起飛指南 作者:元瀟 方凳雅集出品 16.8目前放出來(lái)了10個(gè)內(nèi)置hook,但僅僅基于以下兩個(gè)API,就能做很多事情。所以這篇文章不會(huì)講很多API,...
摘要:原文聊一聊應(yīng)用緩存導(dǎo)讀是提供的一種應(yīng)用緩存機(jī)制基于它應(yīng)用可以實(shí)現(xiàn)離線訪問(wèn)為此瀏覽器還提供了應(yīng)用緩存的雖然的技術(shù)已被標(biāo)準(zhǔn)廢棄但這不影響我們嘗試去了解它也正是因?yàn)榈膽?yīng)用緩存機(jī)制如此誘人餓了么和郵箱等都還在使用著它描述對(duì)熟悉的同學(xué)可以跳過(guò)此 原文: 聊一聊H5應(yīng)用緩存-Manifest 導(dǎo)讀 Manifest 是 H5提供的一種應(yīng)用緩存機(jī)制, 基于它web應(yīng)用可以實(shí)現(xiàn)離線訪問(wèn)(offline...
摘要:是的架構(gòu)的實(shí)現(xiàn)。是在年提出的一種前端架構(gòu),主要用來(lái)處理復(fù)雜的邏輯的一致性問(wèn)題當(dāng)時(shí)是為了解決頁(yè)面的消息通知問(wèn)題。 去年10月底來(lái)到了新公司,剛開(kāi)始接手 Android 項(xiàng)目時(shí),發(fā)現(xiàn)該項(xiàng)目真的是一團(tuán)遭,項(xiàng)目開(kāi)發(fā)上沒(méi)有任何架構(gòu)可言,開(kāi)發(fā)人員連簡(jiǎn)單的 MVC、MVP 都不了解,Activity 及其臃腫,業(yè)務(wù)邊界也不明確,因此我決定重新分析一下當(dāng)前主流的幾種開(kāi)發(fā)架構(gòu),選出適合當(dāng)前項(xiàng)目的架構(gòu)形式...
閱讀 2554·2023-04-25 19:47
閱讀 3399·2019-08-29 17:18
閱讀 862·2019-08-29 15:26
閱讀 3372·2019-08-29 14:17
閱讀 1156·2019-08-26 13:49
閱讀 3350·2019-08-26 13:22
閱讀 3035·2019-08-26 10:44
閱讀 2703·2019-08-23 16:51