generated from freckle/npm-package-template
-
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.
- Loading branch information
Showing
16 changed files
with
423 additions
and
56 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 +1,2 @@ | ||
export { process } from './process'; | ||
export type { ResourceStatusT } from './resource-status'; | ||
export { fromMaybeResourceData, maybeResourceData, isFetching, updateResource } from './resource-status'; |
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,5 +1,8 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.process = void 0; | ||
var process_1 = require("./process"); | ||
Object.defineProperty(exports, "process", { enumerable: true, get: function () { return process_1.process; } }); | ||
exports.updateResource = exports.isFetching = exports.maybeResourceData = exports.fromMaybeResourceData = void 0; | ||
var resource_status_1 = require("./resource-status"); | ||
Object.defineProperty(exports, "fromMaybeResourceData", { enumerable: true, get: function () { return resource_status_1.fromMaybeResourceData; } }); | ||
Object.defineProperty(exports, "maybeResourceData", { enumerable: true, get: function () { return resource_status_1.maybeResourceData; } }); | ||
Object.defineProperty(exports, "isFetching", { enumerable: true, get: function () { return resource_status_1.isFetching; } }); | ||
Object.defineProperty(exports, "updateResource", { enumerable: true, get: function () { return resource_status_1.updateResource; } }); |
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,8 @@ | ||
// @flow | ||
declare export { process } from "./process"; | ||
export type { ResourceStatusT } from "./resource-status"; | ||
declare export { | ||
fromMaybeResourceData, | ||
maybeResourceData, | ||
isFetching, | ||
updateResource, | ||
} from "./resource-status"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
export type ResourceStatusT<R> = { | ||
status: 'idle'; | ||
} | { | ||
status: 'loading'; | ||
} | { | ||
status: 'reloading'; | ||
data: R; | ||
} | { | ||
status: 'error'; | ||
error: unknown; | ||
} | { | ||
status: 'complete'; | ||
data: R; | ||
hasUpdated: boolean; | ||
} | { | ||
status: 'updating'; | ||
data: R; | ||
} | { | ||
status: 'updating-error'; | ||
data: R; | ||
error: unknown; | ||
}; | ||
export declare function fromMaybeResourceData<R>(resource: ResourceStatusT<R>, defaultData: R): R; | ||
export declare function maybeResourceData<R>(resource: ResourceStatusT<R>): R | undefined | null; | ||
export declare function isFetching<R>(resource: ResourceStatusT<R>): boolean; | ||
export declare const updateResource: <R>(resource: ResourceStatusT<R>, update: (data: R) => R) => ResourceStatusT<R>; |
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.updateResource = exports.isFetching = exports.maybeResourceData = exports.fromMaybeResourceData = void 0; | ||
const maybe_1 = require("@freckle/maybe"); | ||
const exhaustive_js_1 = require("@freckle/exhaustive-js"); | ||
function fromMaybeResourceData(resource, defaultData) { | ||
var _a; | ||
return (_a = maybeResourceData(resource)) !== null && _a !== void 0 ? _a : defaultData; | ||
} | ||
exports.fromMaybeResourceData = fromMaybeResourceData; | ||
function maybeResourceData(resource) { | ||
switch (resource.status) { | ||
case 'idle': | ||
return null; | ||
case 'loading': | ||
return null; | ||
case 'reloading': | ||
return resource.data; | ||
case 'error': | ||
return null; | ||
case 'complete': | ||
return resource.data; | ||
case 'updating': | ||
return resource.data; | ||
case 'updating-error': | ||
return resource.data; | ||
default: | ||
return (0, exhaustive_js_1.exhaustive)(resource); | ||
} | ||
} | ||
exports.maybeResourceData = maybeResourceData; | ||
function isFetching(resource) { | ||
return resource.status === 'loading' || resource.status === 'reloading'; | ||
} | ||
exports.isFetching = isFetching; | ||
const updateResource = (resource, update) => { | ||
const mData = maybeResourceData(resource); | ||
return (0, maybe_1.maybe)(() => resource, data => ({ | ||
status: 'complete', | ||
data: update(data), | ||
hasUpdated: false // This is used for async updates, e.g. from a fetch response | ||
}), mData); | ||
}; | ||
exports.updateResource = updateResource; |
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,42 @@ | ||
// @flow | ||
export type ResourceStatusT<R> = | ||
| {| | ||
status: "idle", | ||
|} | ||
| {| | ||
status: "loading", | ||
|} | ||
| {| | ||
status: "reloading", | ||
data: R, | ||
|} | ||
| {| | ||
status: "error", | ||
error: mixed, | ||
|} | ||
| {| | ||
status: "complete", | ||
data: R, | ||
hasUpdated: boolean, | ||
|} | ||
| {| | ||
status: "updating", | ||
data: R, | ||
|} | ||
| {| | ||
status: "updating-error", | ||
data: R, | ||
error: mixed, | ||
|}; | ||
declare export function fromMaybeResourceData<R>( | ||
resource: ResourceStatusT<R>, | ||
defaultData: R | ||
): R; | ||
declare export function maybeResourceData<R>( | ||
resource: ResourceStatusT<R> | ||
): R | void | null; | ||
declare export function isFetching<R>(resource: ResourceStatusT<R>): boolean; | ||
declare export var updateResource: <R>( | ||
resource: ResourceStatusT<R>, | ||
update: (data: R) => R | ||
) => ResourceStatusT<R>; |
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
Oops, something went wrong.