-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(restore): fix
store.restore
not working with ref
.
- Loading branch information
Showing
5 changed files
with
63 additions
and
11 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
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
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,10 +1,32 @@ | ||
import { createObjectFromPrototype, isObject } from '../utils' | ||
|
||
const internal_refSet = new WeakSet() | ||
|
||
export function ref<T extends object>(o: T): T { | ||
internal_refSet.add(o) | ||
return o as T | ||
return o | ||
} | ||
|
||
export function hasRef<T extends object>(k: T) { | ||
export function isRef<T extends object>(k: T): boolean { | ||
return internal_refSet.has(k) | ||
} | ||
|
||
export function deepCloneWithRef<State extends object>(initialState: State): State { | ||
const cloned: State = createObjectFromPrototype(initialState) | ||
|
||
for (const key in initialState) { | ||
const value = initialState[key as keyof State] | ||
|
||
if (isObject(value)) { | ||
if (isRef(value)) { | ||
cloned[key as keyof State] = value | ||
} else { | ||
cloned[key as keyof State] = deepCloneWithRef(value) | ||
} | ||
} else { | ||
cloned[key as keyof State] = value | ||
} | ||
} | ||
|
||
return cloned | ||
} |