const createStoreImpl:CreateStoreImpl=(createState)=>{
type TState = ReturnType<typeof createState>
type Listener=(state: TState, prevState: TState)=>voidlet state: TState
// Set 去重const listeners: Set<Listener>=newSet()const setState: StoreApi<TState>['setState']=(partial, replace)=>{// 判断 set 时传入的是对象还是函数const nextState =typeof partial ==='function'?(partial as(state: TState)=> TState)(state): partial
// 判断数据是否变化if(!Object.is(nextState, state)){const previousState = state
// 是否直接 replace 替换 state,还是 merge 合并 state
state =
replace ??(typeof nextState !=='object'|| nextState ===null)?(nextState as TState): Object.assign({}, state, nextState)// 数据变换后发布通知
listeners.forEach((listener)=>listener(state, previousState))}}// 直接获取保存的闭包 stateconst getState: StoreApi<TState>['getState']=()=> state
// 添加订阅const subscribe: StoreApi<TState>['subscribe']=(listener)=>{
listeners.add(listener)// Unsubscribereturn()=> listeners.delete(listener)}// 取消所有订阅直接调用 Set 的 clearconst destroy: StoreApi<TState>['destroy']=()=>{if(import.meta.env?.MODE!=='production'){
console.warn('[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected.',)}
listeners.clear()}const api ={ setState, getState, subscribe, destroy }// createState 是用户传入的函数,传入的函数会拿到 setState、getState 和 api 方法
state =createState(setState, getState, api)return api as any
}
const createImpl =<T>(createState: StateCreator<T,[],[]>)=>{if(import.meta.env?.MODE!=='production'&&typeof createState !=='function'){
console.warn("[DEPRECATED] Passing a vanilla store will be unsupported in a future version. Instead use `import { useStore } from 'zustand'`.",)}// 管理状态的实例(发布订阅)const api =typeof createState ==='function'?createStore(createState): createState
// 交给用户调用的 hook,通过 useStore 封装 useSyncExternalStoreWithSelectorconst useBoundStore:any=(selector?: any, equalityFn?: any)=>useStore(api, selector, equalityFn)// 添加额外 API
Object.assign(useBoundStore, api)return useBoundStore
}
immer 的中间件实现主要是拦截了发布订阅的 set 方法,通过immer去修改数据后,再调用原始 set 方法
const useBeeStore =create(immer((set)=>({
bees:0,addBees:(by)=>set((state)=>{
state.bees += by
}),})),)
const immerImpl:ImmerImpl=(initializer)=>(set, get, store)=>{
type T= ReturnType<typeof initializer>// 拦截了 set 方法
store.setState=(updater, replace,...a)=>{// 通过 immer 修改数据const nextState =(typeof updater ==='function'?produce(updater as any): updater
)as((s:T)=>T)|T| Partial<T>// 修改后再调用原始方法returnset(nextState as any, replace,...a)}returninitializer(store.setState, get, store)}exportconst immer = immerImpl as unknown as Immer
目录 介绍核心功能负载均衡启动两个支付服务订单模块引入依赖LoadBalanced 注解启动订单服务测试结果 负载均衡算法切换总结 介绍 Spring Cloud LoadBalancer 是 Spring Cloud 提供的客户端负载均衡解决方案,提供更现代化的 API 和更好的 Spring 生态系统集成。它支…
Android Coli 3 ImageView load two suit Bitmap thumb and formal,Kotlin(四) 对 Android Coli 3 ImageView load two suit Bitmap thumb and formal,Kotlin(三)-CSDN博客 进行完善,注意完善 …
摘要 Adam优化器(Adaptive Moment Estimation)是一种广泛应用于深度学习的优化算法,通过自适应学习率加速梯度下降过程。本文从Adam的定义、算法原理、优势与局限性、应用场景及变体等方面进行调研,结合学术文献和实践经验&#x…
Efficient Burst Raw Denoising with Stabilization and Multi-Frequency Denoising Network Burst Raw Denoising必要性Burst Raw Image Denoising流程Main Contributions具体方法介绍集成noise priorCMOS sensor 噪声建模噪声变换(Variance stabilization…
定义 设 P ( A ) > 0 P(A) > 0 P(A)>0,若在随机事件 A A A发生的条件下随机事件 B B B发生的概率记作 P ( B ∣ A ) P(B|A) P(B∣A),定义 P ( B ∣ A ) P ( A B ) P ( A ) P(B|A) \frac{P(AB)}{P(A)} P(B∣A)P(A)P(AB)
则称 P ( B ∣ A ) …