-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Leland-Kwong/develop
Refactor and improve lifecycle management
- Loading branch information
Showing
30 changed files
with
970 additions
and
406 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
import type { AtomRef } from './types'; | ||
export type { AtomRef } from './types'; | ||
export { useIsNew } from './utils'; | ||
export { AtomDevTools } from './AtomDevTools'; | ||
export { AtomRoot } from './AtomRoot'; | ||
export declare function atomRef<T>({ key, defaultState }: { | ||
key: AtomRef<T>['key']; | ||
defaultState: AtomRef<T>['defaultState']; | ||
}): Readonly<AtomRef<T>>; | ||
export declare function atomRef<T>({ key, defaultState, resetOnInactive }: AtomRef<T>): Readonly<AtomRef<T>>; | ||
export declare function useRead<T, SelectorValue = T>(atomRef: AtomRef<T>, selector: (state: T) => SelectorValue): SelectorValue; | ||
export declare function useSend<T>(atomRef: AtomRef<T>): <Payload>(mutationFn: (oldState: T, payload: Payload) => T, payload: Payload) => Promise<[void, void]>; | ||
export declare function useReset<T>(atomRef: AtomRef<T>): () => Promise<[void, void]>; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-cache'; |
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,17 @@ | ||
"use strict"; | ||
/* | ||
* Extras are modules exported for external usage as | ||
* additional utilities that are separate from core. | ||
* */ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./use-cache"), exports); |
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,8 @@ | ||
/** | ||
* Returns a new function that compares the old return value | ||
* and new return value. If they are the same, then it will | ||
* return what was previously returned. This is useful for | ||
* determining if two different objects are equal to prevent | ||
* unecessary rerenders. | ||
*/ | ||
export declare function useIsNew<X, Y>(fn: (x: X) => Y, isNewValue?: (prev: Y, next: Y) => boolean): (x: X) => Y; |
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,44 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useIsNew = void 0; | ||
var react_1 = require("react"); | ||
function shallowCompare(cache, value) { | ||
var maybeNewValue = value !== cache; | ||
if (maybeNewValue) { | ||
var shouldShallowCompare = typeof cache === 'object'; | ||
if (shouldShallowCompare) { | ||
for (var _i = 0, _a = Object.keys(value); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
var prev = cache[key]; | ||
var next = value[key]; | ||
var isNewValue = prev !== next; | ||
if (isNewValue) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
return cache !== value; | ||
} | ||
/** | ||
* Returns a new function that compares the old return value | ||
* and new return value. If they are the same, then it will | ||
* return what was previously returned. This is useful for | ||
* determining if two different objects are equal to prevent | ||
* unecessary rerenders. | ||
*/ | ||
function useIsNew(fn, isNewValue) { | ||
if (isNewValue === void 0) { isNewValue = shallowCompare; } | ||
var cache = (0, react_1.useRef)(null); | ||
return function (x) { | ||
var next = fn(x); | ||
var shouldUpdateCache = cache.current === null || | ||
isNewValue(cache.current, next); | ||
if (shouldUpdateCache) { | ||
cache.current = next; | ||
} | ||
return cache.current; | ||
}; | ||
} | ||
exports.useIsNew = useIsNew; |
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,2 +1,3 @@ | ||
export * from './core'; | ||
export * from './extras'; | ||
export * as examples from './examples'; |
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,2 +1,7 @@ | ||
import type { AtomRef, Db } from './types'; | ||
export declare function useLifeCycle(db: Db<any>, atomRef: AtomRef<any>): void; | ||
import type { AtomRef, Db, LifeCycleEventData } from './types'; | ||
export declare function useLifeCycle(atomRef: AtomRef<any>, hookType: keyof Db<any>['activeHooks']): void; | ||
export declare function useOnLifeCycle<T>(atomRef: AtomRef<T>, fn: (data: { | ||
type: string; | ||
activeHooks: Db<T>['activeHooks']; | ||
state: Db<T>['state']; | ||
}) => void, predicate?: (data: LifeCycleEventData, atomRef: AtomRef<T>) => boolean): import("emittery").UnsubscribeFn; |
Oops, something went wrong.