@airma/core
Advanced tools
Comparing version 15.2.3 to 15.3.0
@@ -66,21 +66,53 @@ // src/libs/tools.ts | ||
}; | ||
function isObject(data) { | ||
return data && typeof data === "object"; | ||
} | ||
function shallowEqual(prev, current) { | ||
if (Object.is(prev, current)) { | ||
return true; | ||
} | ||
if (!isObject(prev) || !isObject(current)) { | ||
return false; | ||
} | ||
const prevKeys = Object.keys(prev); | ||
const currentKeys = Object.keys(current); | ||
if (prevKeys.length !== currentKeys.length) { | ||
return false; | ||
} | ||
const pre = prev; | ||
const curr = current; | ||
const hasDiffKey = prevKeys.some((key) => !Object.prototype.hasOwnProperty.call(curr, key)); | ||
if (hasDiffKey) { | ||
return false; | ||
} | ||
const hasDiffValue = currentKeys.some((key) => { | ||
const currentValue = curr[key]; | ||
const prevValue = pre[key]; | ||
return !Object.is(currentValue, prevValue); | ||
}); | ||
return !hasDiffValue; | ||
} | ||
// src/libs/reducer.ts | ||
function rebuildDispatchMethod(connection, type) { | ||
if (connection.cacheMethods[type]) { | ||
return connection.cacheMethods[type]; | ||
function rebuildDispatchMethod(updater, type) { | ||
if (updater.cacheMethods[type]) { | ||
return updater.cacheMethods[type]; | ||
} | ||
const newMethod = function newMethod2(...args) { | ||
const method = connection.current[type]; | ||
function dispatch(action) { | ||
const { dispatches } = updater; | ||
const dispatchCallbacks = [...dispatches]; | ||
dispatchCallbacks.forEach((callback) => { | ||
callback(action); | ||
}); | ||
} | ||
const method = updater.current[type]; | ||
const result = method(...args); | ||
const { dispatch, reducer } = connection; | ||
connection.current = reducer(result); | ||
connection.cacheState = result; | ||
if (typeof dispatch !== "function") { | ||
return result; | ||
} | ||
const { reducer } = updater; | ||
updater.current = reducer(result); | ||
updater.cacheState = result; | ||
dispatch({ type, state: result }); | ||
return result; | ||
}; | ||
connection.cacheMethods[type] = newMethod; | ||
updater.cacheMethods[type] = newMethod; | ||
return newMethod; | ||
@@ -90,44 +122,98 @@ } | ||
const defaultModel = reducer(defaultState); | ||
const connection = { | ||
const updater = { | ||
current: defaultModel, | ||
reducer, | ||
dispatch: null, | ||
dispatches: [], | ||
cacheMethods: {}, | ||
cacheState: defaultState | ||
}; | ||
const actualReducer = function actualReducer2(state, action) { | ||
if (typeof connection.current[action.type] === "function" || action.type === "@@AIR_STATE_UPDATE") { | ||
return action.state; | ||
} | ||
return state; | ||
}; | ||
actualReducer.agent = createProxy(defaultModel, { | ||
get(target, p) { | ||
const value = connection.current[p]; | ||
if (connection.current.hasOwnProperty(p) && typeof value === "function") { | ||
return rebuildDispatchMethod(connection, p); | ||
return { | ||
agent: createProxy(defaultModel, { | ||
get(target, p) { | ||
const value = updater.current[p]; | ||
if (updater.current.hasOwnProperty(p) && typeof value === "function") { | ||
return rebuildDispatchMethod(updater, p); | ||
} | ||
return value; | ||
} | ||
return value; | ||
}), | ||
getCacheState() { | ||
return updater.cacheState; | ||
}, | ||
update(updateReducer, outState) { | ||
const { cacheState } = updater; | ||
updater.reducer = updateReducer; | ||
updater.cacheState = outState ? outState.state : cacheState; | ||
updater.current = updateReducer(updater.cacheState); | ||
}, | ||
connect(dispatchCall) { | ||
const { dispatches } = updater; | ||
if (dispatches.some((d) => d === dispatchCall) || !dispatchCall) { | ||
return; | ||
} | ||
dispatches.push(dispatchCall); | ||
}, | ||
disconnect(dispatchCall) { | ||
if (!dispatchCall) { | ||
updater.dispatches = []; | ||
return; | ||
} | ||
const { dispatches } = updater; | ||
const index = dispatches.indexOf(dispatchCall); | ||
if (index < 0) { | ||
return; | ||
} | ||
dispatches.splice(index, 1); | ||
} | ||
}); | ||
actualReducer.update = function update(updateReducer, uncontrolled) { | ||
const { cacheState } = connection; | ||
connection.reducer = updateReducer; | ||
connection.cacheState = uncontrolled ? uncontrolled.state : cacheState; | ||
connection.current = updateReducer(connection.cacheState); | ||
}; | ||
actualReducer.connect = function connect(dispatchCall) { | ||
connection.dispatch = null; | ||
connection.dispatch = dispatchCall || null; | ||
} | ||
var requiredModelKey = "@airma/core/requiredModels"; | ||
function createRequiredModels(requireFn) { | ||
const modelList = []; | ||
const hold = function hold2(reducer, defaultState) { | ||
const replaceModel = function replaceModel2(state) { | ||
return reducer(state); | ||
}; | ||
replaceModel.creation = function creation() { | ||
const model = createModel(replaceModel, defaultState); | ||
return { | ||
...model | ||
}; | ||
}; | ||
modelList.push(replaceModel); | ||
return replaceModel; | ||
}; | ||
actualReducer.disconnect = function disconnect() { | ||
connection.dispatch = null; | ||
const models = requireFn(hold); | ||
models[requiredModelKey] = function active() { | ||
return modelList.map((model) => { | ||
return [model, model.creation()]; | ||
}); | ||
}; | ||
return actualReducer; | ||
return models; | ||
} | ||
function activeRequiredModels(factory) { | ||
const active = factory[requiredModelKey]; | ||
if (typeof active !== "function") { | ||
throw new Error("This is a invalid model collection or function"); | ||
} | ||
const pairs = active(); | ||
const instances = new Map(pairs); | ||
return { | ||
get(reducer) { | ||
return instances.get(reducer); | ||
}, | ||
destroy() { | ||
pairs.forEach(([, padding]) => padding.disconnect()); | ||
} | ||
}; | ||
} | ||
export { | ||
activeRequiredModels, | ||
createModel, | ||
createProxy, | ||
createRequiredModels, | ||
isFunctionModel, | ||
shallowEqual, | ||
useSimpleProxy | ||
}; |
export declare type Action = { | ||
type: string; | ||
state?: any; | ||
prevState?: any; | ||
params?: any[]; | ||
type: string; | ||
state?: any; | ||
prevState?: any; | ||
params?: any[]; | ||
}; | ||
@@ -11,52 +11,78 @@ | ||
export declare type AirModel<S> = { | ||
state: S; | ||
[key: string]: any; | ||
state: S; | ||
[key: string]: any; | ||
}; | ||
export declare interface AirModelInstance { | ||
[key: string]: any; | ||
[key: number]: any; | ||
[key: string]: any; | ||
[key: number]: any; | ||
} | ||
declare type ValidInstance<S,T extends AirModelInstance>={ | ||
[K in keyof T]:T[K] extends ((...args: any[]) => S)?T[K]:T[K] extends ((...args: any[]) => any)?never:T[K] | ||
declare type ValidInstance<S, T extends AirModelInstance> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => S | ||
? T[K] | ||
: T[K] extends (...args: any[]) => any | ||
? never | ||
: T[K]; | ||
}; | ||
export declare type AirReducer<S, T extends AirModelInstance> = ( | ||
state: S | ||
) => ValidInstance<S,T>; | ||
state: S | ||
) => ValidInstance<S, T>; | ||
export declare type Reducer<S, A> = (state: S, action: A) => S; | ||
export declare interface Connection< | ||
S = any, | ||
T extends AirModelInstance = AirModelInstance | ||
> { | ||
agent: T; | ||
export declare interface ReducerPadding<S = any, T extends AirModelInstance = AirModelInstance> { | ||
agent: T; | ||
update:(reducer:AirReducer<S, T>,outState?:{state:S})=>void; | ||
connect: (dispatch?: Dispatch) => void; | ||
disconnect: () => void; | ||
getCacheState(): S; | ||
update: (reducer: AirReducer<S, T>, outState?: { state: S }) => void; | ||
connect: (dispatch?: Dispatch) => void; | ||
disconnect: (dispatch?: Dispatch) => void; | ||
} | ||
export declare type ActualReducer<S = any, T extends AirModelInstance = any> = Reducer< | ||
S, | ||
Action | ||
> & | ||
ReducerPadding<S, T>; | ||
export declare function createModel<S, T extends AirModelInstance, D extends S>( | ||
reducer: AirReducer<S, T>, | ||
defaultState:D | ||
): ActualReducer<S, T>; | ||
reducer: AirReducer<S, T>, | ||
defaultState: D | ||
): Connection<S, T>; | ||
declare type HoldCallback = < | ||
S = any, | ||
T extends AirModelInstance = any, | ||
D extends S = any | ||
>( | ||
reducer: AirReducer<S, T>, | ||
defaultState?: D | ||
) => typeof reducer; | ||
export declare function createRequiredModels< | ||
T extends Array<any> | ((...args: any) => any) | Record<string, any> | ||
>( | ||
requireFn: (hold: HoldCallback) => T | ||
): T; | ||
export declare function activeRequiredModels< | ||
T extends Array<any> | ((...args: any) => any) | Record<string, any> | ||
>( | ||
models: T | ||
): { | ||
get(reducer: AirReducer<any, any>): Connection | undefined; | ||
destroy(): void; | ||
}; | ||
export declare function useSimpleProxy<T extends Record<string, unknown>>( | ||
target: T, | ||
handler: ProxyHandler<T>, | ||
target: T, | ||
handler: ProxyHandler<T> | ||
): T; | ||
export declare function createProxy<T extends Record<string, any>>( | ||
target: T, | ||
handler: ProxyHandler<T>, | ||
target: T, | ||
handler: ProxyHandler<T> | ||
): T; | ||
export function isFunctionModel<S, T extends AirModel<S>>( | ||
model: T | { new (): T } | ((state: S) => T) | ||
export declare function isFunctionModel<S, T extends AirModel<S>>( | ||
model: T | { new (): T } | ((state: S) => T) | ||
): model is (state: S) => T; | ||
export declare function shallowEqual<R>(prev: R, current: R): boolean; |
@@ -7,3 +7,3 @@ { | ||
"name": "@airma/core", | ||
"version": "15.2.3", | ||
"version": "15.3.0", | ||
"description": "This is the core for agent-reducer and airma", | ||
@@ -10,0 +10,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9857
288