-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): added trace function
- Loading branch information
1 parent
744ba80
commit a343a8a
Showing
11 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
export { default as Task } from './task'; | ||
export { default as Task } from './task/task'; | ||
|
||
export { default as clone } from './clone'; | ||
export { default as overwrite } from './overwrite'; | ||
export { default as clone } from './object/clone'; | ||
export { default as overwrite } from './object/overwrite'; | ||
export { default as trace } from './object/trace'; | ||
|
||
export { default as getType } from './type/get-type'; | ||
export { default as isArray } from './type/is-array'; | ||
export { default as isObject } from './type/is-object'; | ||
|
||
export * from './types'; |
4 changes: 2 additions & 2 deletions
4
packages/utilities/src/clone.ts → packages/utilities/src/object/clone.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import isArray from '../type/is-array'; | ||
import isObject from '../type/is-object'; | ||
import clone from './clone'; | ||
|
||
type TraceGate<TValue extends object> = keyof ProxyHandler<TValue>; | ||
type TraceCallback<TValue extends object> = (result: TraceResult<TValue>) => void; | ||
|
||
interface TraceOptions<TValue extends object> { | ||
gates: TraceGate<TValue>[]; | ||
paths: PropertyKey[]; | ||
hasGetGate: boolean; | ||
} | ||
|
||
interface TraceResult<TValue extends object> { | ||
gate: TraceGate<TValue>; | ||
paths: PropertyKey[]; | ||
oldValue: unknown; | ||
newValue: unknown; | ||
} | ||
|
||
type GateMap<TValue extends object = any> = { | ||
[TGate in TraceGate<TValue>]?: (callback: TraceCallback<TValue>, options: TraceOptions<TValue>) => ProxyHandler<TValue>[TGate]; | ||
} | ||
|
||
const GATE_MAP = { | ||
get: (callback, { hasGetGate, gates, paths }) => (target, prop, receiver) => { | ||
const value = Reflect.get(target, prop, receiver); | ||
|
||
if (hasGetGate) { | ||
defaultCallback(callback, 'get', paths, prop, value, value); | ||
} | ||
|
||
return deepTrace(value, callback, { | ||
gates, | ||
hasGetGate, | ||
paths: paths.concat(prop), | ||
}); | ||
}, | ||
set: (callback, { paths }) => (target, prop, value, receiver) => { | ||
defaultCallback(callback, 'set', paths, prop, target[prop], value); | ||
return Reflect.set(target, prop, value, receiver); | ||
}, | ||
deleteProperty: (callback, { paths }) => (target, prop) => { | ||
defaultCallback(callback, 'deleteProperty', paths, prop, target[prop]); | ||
return Reflect.deleteProperty(target, prop); | ||
}, | ||
} as GateMap; | ||
|
||
function defaultCallback<TValue extends object>(callback: TraceCallback<TValue>, gate: TraceGate<TValue>, paths: PropertyKey[], key: PropertyKey, oldValue: unknown, newValue?: unknown) { | ||
try { | ||
callback({ | ||
gate, | ||
newValue, | ||
paths: paths.concat(key), | ||
oldValue: newValue === oldValue ? oldValue : clone(oldValue), | ||
}); | ||
} catch { | ||
console.warn('Trace callback failed'); | ||
} | ||
} | ||
|
||
function deepTrace<TValue extends object>(value: TValue, callback: TraceCallback<TValue>, options: TraceOptions<TValue>): TValue { | ||
if (!isObject(value) && !isArray(value)) { | ||
return value; | ||
} | ||
|
||
const { | ||
gates, | ||
} = options; | ||
|
||
const handler = gates.reduce((output, gate) => { | ||
const gateHandler = (GATE_MAP as GateMap<TValue>)[gate]; | ||
|
||
if (gateHandler) { | ||
output[gate] = gateHandler(callback, options)?.bind(output) as any; | ||
} | ||
|
||
return output; | ||
}, {} as ProxyHandler<TValue>); | ||
|
||
return new Proxy(value, handler); | ||
} | ||
|
||
export default function trace<TValue extends object>(value: TValue, gates: TraceGate<TValue> | TraceGate<TValue>[], callback: TraceCallback<TValue>): TValue { | ||
const allGates = ([] as TraceGate<TValue>[]).concat(gates); | ||
const hasGetGate = allGates.includes('get'); | ||
|
||
return deepTrace(value, callback, { | ||
hasGetGate, | ||
gates: hasGetGate ? allGates : allGates.concat('get'), | ||
paths: [], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/utilities/src/get-type.ts → packages/utilities/src/type/get-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import getType from './get-type'; | ||
|
||
export default function isArray(value: unknown): value is unknown[] { | ||
return getType(value) === 'array'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import getType from './get-type'; | ||
|
||
export default function isObject(value: unknown): value is object { | ||
return getType(value) === 'object'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters