react-context-store
Advanced tools
Comparing version 1.0.0-beta.5 to 1.0.0-beta.6
@@ -10,2 +10,6 @@ # Changelog | ||
## [1.0.0-beta.6] - 2021-03-01 | ||
- Fixed simultaneous create for IndexableContextStore and StatefulIndexableContextStore | ||
## [1.0.0-beta.5] - 2021-02-28 | ||
@@ -12,0 +16,0 @@ |
/// <reference types="react" /> | ||
import { Stateful, statefulStates } from "../../shared"; | ||
import { IndexableContextStore, IndexableContextStoreValue, IndexableContextStoreKey } from "../interfaces"; | ||
export declare function getCreateOneContextData<Params, TContextStore extends IndexableContextStore<any>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, dataHandlers: { | ||
export declare function getCreateOneContextData<Params, TContextStore extends IndexableContextStore<any>>(contextDataAtTimeOfExecution: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, dataHandlers: { | ||
action: (params: Params) => Promise<IndexableContextStoreValue<TContextStore>>; | ||
@@ -10,3 +10,3 @@ error?: (params: Params) => Promise<IndexableContextStoreValue<TContextStore> | null>; | ||
}): Promise<IndexableContextStoreValue<TContextStore>>; | ||
export declare function setContextDataForCreateOne<Params, TContextStore extends IndexableContextStore<any>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<IndexableContextStoreValue<TContextStore>>): Promise<IndexableContextStoreValue<TContextStore> | null>; | ||
export declare function setContextDataForCreateOne<Params, TContextStore extends IndexableContextStore<any>>(setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<IndexableContextStoreValue<TContextStore>>): Promise<IndexableContextStoreValue<TContextStore> | null>; | ||
export declare function getUpdatedContextDataForCreateOne<TContextStore extends IndexableContextStore<unknown>>(store: TContextStore, index: IndexableContextStoreKey<TContextStore>, value: null | IndexableContextStoreValue<TContextStore>, state: keyof typeof statefulStates): TContextStore; |
@@ -57,46 +57,54 @@ "use strict"; | ||
var shared_1 = require("../../shared"); | ||
function getCreateOneContextData(contextData, setContextData, params, dataHandlers) { | ||
function getCreateOneContextData(contextDataAtTimeOfExecution, setContextData, params, dataHandlers) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var action, error, getIndex, preload, value, e_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var action, error, getIndex, preload, value, e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
action = dataHandlers.action, error = dataHandlers.error, getIndex = dataHandlers.getIndex, preload = dataHandlers.preload; | ||
value = null; | ||
_c.label = 1; | ||
_d.label = 1; | ||
case 1: | ||
_c.trys.push([1, 4, , 5]); | ||
return [4, setContextDataForCreateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
_d.trys.push([1, 4, , 11]); | ||
return [4, setContextDataForCreateOne(setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
case 2: | ||
value = | ||
(_a = (_c.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForCreateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
(_a = (_d.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForCreateOne(setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
case 3: | ||
value = | ||
(_b = (_c.sent())) !== null && _b !== void 0 ? _b : value; | ||
if (value == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.actionReturnedNull)]; | ||
(_b = (_d.sent())) !== null && _b !== void 0 ? _b : value; | ||
return [2, Promise.resolve(value)]; | ||
case 4: | ||
e_1 = _d.sent(); | ||
_d.label = 5; | ||
case 5: | ||
_d.trys.push([5, 9, , 10]); | ||
if (!error) return [3, 7]; | ||
return [4, setContextDataForCreateOne(setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
case 6: | ||
_d.sent(); | ||
return [3, 8]; | ||
case 7: | ||
setContextData(function (contextData) { | ||
return __assign(__assign({}, contextData), { state: shared_1.statefulStates.error }); | ||
}); | ||
_d.label = 8; | ||
case 8: | ||
if (typeof e_1 === "string") { | ||
return [2, Promise.reject(e_1)]; | ||
} | ||
else { | ||
return [2, Promise.resolve(value)]; | ||
return [2, Promise.reject(e_1.message || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
} | ||
return [3, 5]; | ||
case 4: | ||
e_1 = _c.sent(); | ||
try { | ||
setContextData(__assign(__assign({}, contextData), { state: shared_1.statefulStates.error })); | ||
if (typeof e_1 === "string") { | ||
return [2, Promise.reject(e_1)]; | ||
} | ||
else { | ||
return [2, Promise.reject(e_1.message || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
} | ||
} | ||
catch (_d) { | ||
setContextData(__assign(__assign({}, contextData), { state: shared_1.statefulStates.error })); | ||
return [2, Promise.reject(shared_1.errorMessages.errorCallbackRejected)]; | ||
} | ||
return [3, 5]; | ||
case 5: return [2]; | ||
return [3, 10]; | ||
case 9: | ||
_c = _d.sent(); | ||
setContextData(function (contextData) { | ||
return __assign(__assign({}, contextData), { state: shared_1.statefulStates.error }); | ||
}); | ||
return [2, Promise.reject(shared_1.errorMessages.errorCallbackRejected)]; | ||
case 10: return [3, 11]; | ||
case 11: return [2]; | ||
} | ||
@@ -107,3 +115,3 @@ }); | ||
exports.getCreateOneContextData = getCreateOneContextData; | ||
function setContextDataForCreateOne(contextData, setContextData, params, getIndex, state, action) { | ||
function setContextDataForCreateOne(setContextData, params, getIndex, state, action) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -125,3 +133,5 @@ var index, value, _a; | ||
value = _a; | ||
setContextData(getUpdatedContextDataForCreateOne(contextData, index, value, state)); | ||
setContextData(function (contextData) { | ||
return getUpdatedContextDataForCreateOne(contextData, index, value, state); | ||
}); | ||
return [2, value]; | ||
@@ -128,0 +138,0 @@ } |
/// <reference types="react" /> | ||
import { Stateful, statefulStates } from "../../shared"; | ||
import { IndexableContextStore, IndexableContextStoreKey, IndexableContextStoreValue } from "../interfaces"; | ||
export declare function getDeleteOneContextData<Params, TContextStore extends IndexableContextStore<any>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, dataHandlers: { | ||
export declare function getDeleteOneContextData<Params, TContextStore extends IndexableContextStore<any>>(contextDataAtTimeOfExecution: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, dataHandlers: { | ||
action: (params: Params) => Promise<unknown>; | ||
@@ -10,3 +9,1 @@ error?: (params: Params) => Promise<IndexableContextStoreValue<TContextStore> | null>; | ||
}): Promise<IndexableContextStoreValue<TContextStore>>; | ||
export declare function setContextDataForDeleteOne<Params, TContextStore extends IndexableContextStore<unknown>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<IndexableContextStoreValue<TContextStore> | null>, deleteIfNull?: boolean): Promise<import("../interfaces").IndexableContextStoreData<unknown>[IndexableContextStoreKey<TContextStore>]>; | ||
export declare function getUpdatedContextDataForDeleteOne<TContextStore extends IndexableContextStore<unknown>>(store: TContextStore, index: IndexableContextStoreKey<TContextStore>, value: null | IndexableContextStoreValue<TContextStore>, state: keyof typeof statefulStates): TContextStore; |
@@ -55,44 +55,36 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getUpdatedContextDataForDeleteOne = exports.setContextDataForDeleteOne = exports.getDeleteOneContextData = void 0; | ||
exports.getDeleteOneContextData = void 0; | ||
var shared_1 = require("../../shared"); | ||
var get_update_one_context_data_1 = require("./get-update-one-context-data"); | ||
function getDeleteOneContextData(contextData, setContextData, params, dataHandlers) { | ||
var _a, _b; | ||
function getDeleteOneContextData(contextDataAtTimeOfExecution, setContextData, params, dataHandlers) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var action, error, getIndex, preload, value, e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var action, error, getIndex, preload, index, oldData, e_1, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
action = dataHandlers.action, error = dataHandlers.error, getIndex = dataHandlers.getIndex, preload = dataHandlers.preload; | ||
value = null; | ||
_d.label = 1; | ||
index = getIndex(params); | ||
oldData = contextDataAtTimeOfExecution.data[index]; | ||
if (oldData == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
_b.label = 1; | ||
case 1: | ||
_d.trys.push([1, 4, , 9]); | ||
return [4, setContextDataForDeleteOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.loading, preload, false)]; | ||
_b.trys.push([1, 4, , 9]); | ||
return [4, setContextDataForDeleteOne(setContextData, params, getIndex, shared_1.statefulStates.loading, preload, false)]; | ||
case 2: | ||
value = | ||
(_a = (_d.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForDeleteOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
_b.sent(); | ||
return [4, setContextDataForDeleteOne(setContextData, params, getIndex, shared_1.statefulStates.success, action, true)]; | ||
case 3: | ||
value = | ||
(_b = (_d.sent())) !== null && _b !== void 0 ? _b : value; | ||
if (value == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
else { | ||
return [2, Promise.resolve(value)]; | ||
} | ||
return [3, 9]; | ||
_b.sent(); | ||
return [2, Promise.resolve(oldData)]; | ||
case 4: | ||
e_1 = _d.sent(); | ||
_d.label = 5; | ||
e_1 = _b.sent(); | ||
_b.label = 5; | ||
case 5: | ||
_d.trys.push([5, 7, , 8]); | ||
return [4, setContextDataForDeleteOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.error, error, false)]; | ||
_b.trys.push([5, 7, , 8]); | ||
return [4, setContextDataForDeleteOne(setContextData, params, getIndex, shared_1.statefulStates.error, error, false)]; | ||
case 6: | ||
value = _d.sent(); | ||
if (value == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
else if (typeof e_1 === "string") { | ||
_b.sent(); | ||
if (typeof e_1 === "string") { | ||
return [2, Promise.reject(e_1)]; | ||
@@ -105,4 +97,6 @@ } | ||
case 7: | ||
_c = _d.sent(); | ||
setContextData(__assign(__assign({}, contextData), { state: shared_1.statefulStates.error })); | ||
_a = _b.sent(); | ||
setContextData(function (contextData) { | ||
return __assign(__assign({}, contextData), { state: shared_1.statefulStates.error }); | ||
}); | ||
return [2, Promise.reject(shared_1.errorMessages.errorCallbackRejected)]; | ||
@@ -116,6 +110,6 @@ case 8: return [3, 9]; | ||
exports.getDeleteOneContextData = getDeleteOneContextData; | ||
function setContextDataForDeleteOne(contextData, setContextData, params, getIndex, state, action, deleteIfNull) { | ||
if (deleteIfNull === void 0) { deleteIfNull = true; } | ||
function setContextDataForDeleteOne(setContextData, params, getIndex, state, action, deleteIfActionUndefined) { | ||
if (deleteIfActionUndefined === void 0) { deleteIfActionUndefined = true; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var index, oldValue, defaultValue, value, _a, newStore; | ||
var index, newValue, _a; | ||
return __generator(this, function (_b) { | ||
@@ -125,4 +119,2 @@ switch (_b.label) { | ||
index = getIndex(params); | ||
oldValue = contextData.data[index]; | ||
defaultValue = deleteIfNull ? null : oldValue; | ||
if (!action) return [3, 2]; | ||
@@ -134,9 +126,19 @@ return [4, action(params)]; | ||
case 2: | ||
_a = defaultValue; | ||
_a = null; | ||
_b.label = 3; | ||
case 3: | ||
value = _a; | ||
newStore = getUpdatedContextDataForDeleteOne(contextData, index, value, state); | ||
setContextData(newStore); | ||
return [2, oldValue]; | ||
newValue = _a; | ||
setContextData(function (contextData) { | ||
var oldValue = contextData.data[index]; | ||
var value = newValue; | ||
if (action == null) { | ||
value = deleteIfActionUndefined ? null : oldValue; | ||
} | ||
else { | ||
value = newValue !== null && newValue !== void 0 ? newValue : null; | ||
} | ||
var newStore = getUpdatedContextDataForDeleteOne(contextData, index, value, state); | ||
return newStore; | ||
}); | ||
return [2]; | ||
} | ||
@@ -146,3 +148,2 @@ }); | ||
} | ||
exports.setContextDataForDeleteOne = setContextDataForDeleteOne; | ||
function getUpdatedContextDataForDeleteOne(store, index, value, state) { | ||
@@ -164,2 +165,1 @@ if (value != null) { | ||
} | ||
exports.getUpdatedContextDataForDeleteOne = getUpdatedContextDataForDeleteOne; |
/// <reference types="react" /> | ||
import { Stateful, statefulStates } from "../../shared"; | ||
import { IndexableContextStore, IndexableContextStoreKey, IndexableContextStoreValue } from "../interfaces"; | ||
export declare function getUpdateOneContextData<Params, TContextStore extends IndexableContextStore<any>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, dataHandlers: { | ||
export declare function getUpdateOneContextData<Params, TContextStore extends IndexableContextStore<any>>(contextDataAtTimeOfExecution: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, dataHandlers: { | ||
action: (params: Params) => Promise<Partial<IndexableContextStoreValue<TContextStore>>>; | ||
@@ -10,3 +10,3 @@ error?: (params: Params) => Promise<Partial<IndexableContextStoreValue<TContextStore>> | null>; | ||
}): Promise<IndexableContextStoreValue<TContextStore>>; | ||
export declare function setContextDataForUpdateOne<Params, TContextStore extends IndexableContextStore<any>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<Partial<IndexableContextStoreValue<TContextStore>> | null>): Promise<import("../interfaces").IndexableContextStoreData<any>[IndexableContextStoreKey<TContextStore>]>; | ||
export declare function setContextDataForUpdateOne<Params, TContextStore extends IndexableContextStore<any>>(setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<Partial<IndexableContextStoreValue<TContextStore>> | null>): Promise<IndexableContextStoreValue<TContextStore>>; | ||
export declare function getUpdatedContextDataForUpdateOne<TContextStore extends IndexableContextStore<any>>(store: TContextStore, index: IndexableContextStoreKey<TContextStore>, value: null | Partial<IndexableContextStoreValue<TContextStore>>, state: keyof typeof statefulStates): TContextStore; |
@@ -57,6 +57,6 @@ "use strict"; | ||
var shared_1 = require("../../shared"); | ||
function getUpdateOneContextData(contextData, setContextData, params, dataHandlers) { | ||
function getUpdateOneContextData(contextDataAtTimeOfExecution, setContextData, params, dataHandlers) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var action, error, getIndex, preload, value, e_1, _c; | ||
var action, error, getIndex, preload, index, oldData, value, e_1, _c; | ||
return __generator(this, function (_d) { | ||
@@ -66,2 +66,7 @@ switch (_d.label) { | ||
action = dataHandlers.action, error = dataHandlers.error, getIndex = dataHandlers.getIndex, preload = dataHandlers.preload; | ||
index = getIndex(params); | ||
oldData = contextDataAtTimeOfExecution.data[index]; | ||
if (oldData == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
value = null; | ||
@@ -71,33 +76,24 @@ _d.label = 1; | ||
_d.trys.push([1, 4, , 9]); | ||
return [4, setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
return [4, setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
case 2: | ||
value = | ||
(_a = (_d.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
return [4, setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
case 3: | ||
value = | ||
(_b = (_d.sent())) !== null && _b !== void 0 ? _b : value; | ||
if (value == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.actionReturnedNull)]; | ||
} | ||
else { | ||
return [2, Promise.resolve(value)]; | ||
} | ||
return [3, 9]; | ||
return [2, Promise.resolve(value)]; | ||
case 4: | ||
e_1 = _d.sent(); | ||
if (e_1.message === shared_1.errorMessages.indexNotFound) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
_d.label = 5; | ||
case 5: | ||
_d.trys.push([5, 7, , 8]); | ||
return [4, setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
return [4, setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
case 6: | ||
value = _d.sent(); | ||
if (value == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
if (typeof e_1 === "string") { | ||
return [2, Promise.reject(e_1)]; | ||
} | ||
else { | ||
return [2, Promise.resolve(value)]; | ||
return [2, Promise.reject(e_1.message || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
} | ||
@@ -107,3 +103,5 @@ return [3, 8]; | ||
_c = _d.sent(); | ||
setContextData(__assign(__assign({}, contextData), { state: shared_1.statefulStates.error })); | ||
setContextData(function (contextData) { | ||
return __assign(__assign({}, contextData), { state: shared_1.statefulStates.error }); | ||
}); | ||
return [2, Promise.reject(shared_1.errorMessages.errorCallbackRejected)]; | ||
@@ -117,5 +115,5 @@ case 8: return [3, 9]; | ||
exports.getUpdateOneContextData = getUpdateOneContextData; | ||
function setContextDataForUpdateOne(contextData, setContextData, params, getIndex, state, action) { | ||
function setContextDataForUpdateOne(setContextData, params, getIndex, state, action) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var index, value, _a, newStore; | ||
var index, value, _a; | ||
return __generator(this, function (_b) { | ||
@@ -135,5 +133,9 @@ switch (_b.label) { | ||
value = _a; | ||
newStore = getUpdatedContextDataForUpdateOne(contextData, index, value, state); | ||
setContextData(newStore); | ||
return [2, newStore.data[index]]; | ||
return [2, new Promise(function (resolve) { | ||
setContextData(function (contextData) { | ||
var newStore = getUpdatedContextDataForUpdateOne(contextData, index, value, state); | ||
resolve(newStore.data[index]); | ||
return newStore; | ||
}); | ||
})]; | ||
} | ||
@@ -148,5 +150,2 @@ }); | ||
var oldValue = store.data[index]; | ||
if (oldValue == null) { | ||
throw new Error(shared_1.errorMessages.indexNotFound); | ||
} | ||
var newValue = __assign(__assign({}, oldValue), value); | ||
@@ -153,0 +152,0 @@ if (Array.isArray(data)) { |
@@ -10,3 +10,3 @@ /// <reference types="react" /> | ||
}): Promise<IndexableStatefulContextStoreValueData<TContextStore>>; | ||
export declare function setContextDataForCreateOne<Params, TContextStore extends IndexableStatefulContextStore<any>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableStatefulContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<IndexableStatefulContextStoreValueData<TContextStore>>): Promise<IndexableStatefulContextStoreValueData<TContextStore> | null>; | ||
export declare function setContextDataForCreateOne<Params, TContextStore extends IndexableStatefulContextStore<any>>(setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableStatefulContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<IndexableStatefulContextStoreValueData<TContextStore>>): Promise<IndexableStatefulContextStoreValueData<TContextStore> | null>; | ||
export declare function getUpdatedContextDataForCreateOne<TContextStore extends IndexableStatefulContextStore<unknown>>(store: TContextStore, index: IndexableStatefulContextStoreKey<TContextStore>, value: null | IndexableStatefulContextStoreValueData<TContextStore>, state: keyof typeof statefulStates): TContextStore; |
@@ -60,19 +60,19 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var action, error, getIndex, preload, value, e_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var action, error, getIndex, preload, value, e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
action = dataHandlers.action, error = dataHandlers.error, getIndex = dataHandlers.getIndex, preload = dataHandlers.preload; | ||
value = null; | ||
_c.label = 1; | ||
_d.label = 1; | ||
case 1: | ||
_c.trys.push([1, 4, , 8]); | ||
return [4, setContextDataForCreateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
_d.trys.push([1, 4, , 11]); | ||
return [4, setContextDataForCreateOne(setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
case 2: | ||
value = | ||
(_a = (_c.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForCreateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
(_a = (_d.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForCreateOne(setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
case 3: | ||
value = | ||
(_b = (_c.sent())) !== null && _b !== void 0 ? _b : value; | ||
(_b = (_d.sent())) !== null && _b !== void 0 ? _b : value; | ||
if (value == null) { | ||
@@ -84,14 +84,23 @@ return [2, Promise.reject(shared_1.errorMessages.actionReturnedNull)]; | ||
} | ||
return [3, 8]; | ||
return [3, 11]; | ||
case 4: | ||
e_1 = _c.sent(); | ||
if (!error) return [3, 6]; | ||
return [4, setContextDataForCreateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
e_1 = _d.sent(); | ||
if (!error) return [3, 9]; | ||
_d.label = 5; | ||
case 5: | ||
_c.sent(); | ||
return [3, 7]; | ||
_d.trys.push([5, 7, , 8]); | ||
return [4, setContextDataForCreateOne(setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
case 6: | ||
setContextData(__assign(__assign({}, contextData), { state: shared_1.statefulStates.success })); | ||
_c.label = 7; | ||
_d.sent(); | ||
return [3, 8]; | ||
case 7: | ||
_c = _d.sent(); | ||
return [2, Promise.reject(shared_1.errorMessages.errorCallbackRejected)]; | ||
case 8: return [3, 10]; | ||
case 9: | ||
setContextData(function (contextData) { | ||
return __assign(__assign({}, contextData), { state: shared_1.statefulStates.success }); | ||
}); | ||
_d.label = 10; | ||
case 10: | ||
if (typeof e_1 === "string") { | ||
@@ -103,4 +112,4 @@ return [2, Promise.reject(e_1)]; | ||
} | ||
return [3, 8]; | ||
case 8: return [2]; | ||
return [3, 11]; | ||
case 11: return [2]; | ||
} | ||
@@ -111,3 +120,3 @@ }); | ||
exports.getCreateOneContextData = getCreateOneContextData; | ||
function setContextDataForCreateOne(contextData, setContextData, params, getIndex, state, action) { | ||
function setContextDataForCreateOne(setContextData, params, getIndex, state, action) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -129,3 +138,5 @@ var index, value, _a; | ||
value = _a; | ||
setContextData(getUpdatedContextDataForCreateOne(contextData, index, value, state)); | ||
setContextData(function (contextData) { | ||
return getUpdatedContextDataForCreateOne(contextData, index, value, state); | ||
}); | ||
return [2, value]; | ||
@@ -132,0 +143,0 @@ } |
@@ -61,19 +61,24 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var action, error, getIndex, preload, value, e_1, e2_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var action, error, getIndex, preload, value, index, oldData, e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
action = dataHandlers.action, error = dataHandlers.error, getIndex = dataHandlers.getIndex, preload = dataHandlers.preload; | ||
value = null; | ||
_c.label = 1; | ||
index = getIndex(params); | ||
oldData = contextData.data[index]; | ||
if (oldData == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
_d.label = 1; | ||
case 1: | ||
_c.trys.push([1, 4, , 10]); | ||
_d.trys.push([1, 4, , 10]); | ||
return [4, setContextDataForDeleteOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.loading, preload, false)]; | ||
case 2: | ||
value = | ||
(_a = (_c.sent())) !== null && _a !== void 0 ? _a : value; | ||
(_a = (_d.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForDeleteOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
case 3: | ||
value = | ||
(_b = (_c.sent())) !== null && _b !== void 0 ? _b : value; | ||
(_b = (_d.sent())) !== null && _b !== void 0 ? _b : value; | ||
if (value == null) { | ||
@@ -87,9 +92,9 @@ return [2, Promise.reject(shared_1.errorMessages.actionReturnedNull)]; | ||
case 4: | ||
e_1 = _c.sent(); | ||
_c.label = 5; | ||
e_1 = _d.sent(); | ||
_d.label = 5; | ||
case 5: | ||
_c.trys.push([5, 7, , 9]); | ||
return [4, get_update_one_context_data_1.setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
_d.trys.push([5, 7, , 9]); | ||
return [4, get_update_one_context_data_1.setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
case 6: | ||
value = _c.sent(); | ||
value = _d.sent(); | ||
if (typeof e_1 === "string") { | ||
@@ -99,11 +104,11 @@ return [2, Promise.reject(e_1)]; | ||
else { | ||
throw e_1; | ||
return [2, Promise.reject(e_1.message || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
} | ||
return [3, 9]; | ||
case 7: | ||
e2_1 = _c.sent(); | ||
return [4, get_update_one_context_data_1.setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.error)]; | ||
_c = _d.sent(); | ||
return [4, get_update_one_context_data_1.setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.error)]; | ||
case 8: | ||
_c.sent(); | ||
return [2, Promise.reject(e2_1.message || shared_1.errorMessages.errorCallbackRejected)]; | ||
_d.sent(); | ||
return [2, Promise.reject(shared_1.errorMessages.errorCallbackRejected)]; | ||
case 9: return [3, 10]; | ||
@@ -119,7 +124,6 @@ case 10: return [2]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var index, oldValue, defaultValue, value, _a, newStore, newStore, e_2; | ||
var index, oldValue, defaultValue, value, _a, newStore; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 4, , 5]); | ||
index = getIndex(params); | ||
@@ -138,20 +142,19 @@ oldValue = contextData.data[index].data; | ||
value = _a; | ||
if (value != null) { | ||
newStore = get_update_one_context_data_1.getUpdatedContextDataForUpdateOne(contextData, index, value, state); | ||
setContextData(newStore); | ||
return [2, newStore.data[index].data]; | ||
} | ||
else { | ||
newStore = getUpdatedContextDataForDeleteOne(contextData, index); | ||
setContextData(newStore); | ||
return [2, oldValue]; | ||
} | ||
return [3, 5]; | ||
if (!(value != null)) return [3, 5]; | ||
return [4, new Promise(function (resolve) { | ||
setContextData(function (contextData) { | ||
var newStore = get_update_one_context_data_1.getUpdatedContextDataForUpdateOne(contextData, index, value, state); | ||
resolve(newStore); | ||
return newStore; | ||
}); | ||
})]; | ||
case 4: | ||
e_2 = _b.sent(); | ||
if (typeof e_2 === "string") { | ||
return [2, Promise.reject(e_2)]; | ||
} | ||
return [2, Promise.reject((e_2 === null || e_2 === void 0 ? void 0 : e_2.message) || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
case 5: return [2]; | ||
newStore = _b.sent(); | ||
return [2, newStore.data[index].data]; | ||
case 5: | ||
setContextData(function (contextData) { | ||
var newStore = getUpdatedContextDataForDeleteOne(contextData, index); | ||
return newStore; | ||
}); | ||
return [2, oldValue]; | ||
} | ||
@@ -158,0 +161,0 @@ }); |
@@ -10,3 +10,3 @@ /// <reference types="react" /> | ||
}): Promise<IndexableStatefulContextStoreValueData<TContextStore>>; | ||
export declare function setContextDataForUpdateOne<Params, TContextStore extends IndexableStatefulContextStore<unknown>>(contextData: TContextStore, setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableStatefulContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<Partial<IndexableStatefulContextStoreValueData<TContextStore>> | null>): Promise<unknown>; | ||
export declare function setContextDataForUpdateOne<Params, TContextStore extends IndexableStatefulContextStore<unknown>>(setContextData: React.Dispatch<React.SetStateAction<TContextStore>>, params: Params, getIndex: (params: Params) => IndexableStatefulContextStoreKey<TContextStore>, state: Stateful["state"], action?: (params: Params) => Promise<Partial<IndexableStatefulContextStoreValueData<TContextStore>> | null>): Promise<unknown>; | ||
export declare function getUpdatedContextDataForUpdateOne<TContextStore extends IndexableStatefulContextStore<unknown>>(store: TContextStore, index: IndexableStatefulContextStoreKey<TContextStore>, value: null | Partial<IndexableStatefulContextStoreValueData<TContextStore>>, state: keyof typeof statefulStates): TContextStore; |
@@ -60,19 +60,24 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var action, error, getIndex, preload, value, e1_1, e2_1; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var action, error, getIndex, preload, value, index, oldData, e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
action = dataHandlers.action, error = dataHandlers.error, getIndex = dataHandlers.getIndex, preload = dataHandlers.preload; | ||
value = null; | ||
_c.label = 1; | ||
index = getIndex(params); | ||
oldData = contextData.data[index]; | ||
if (oldData == null) { | ||
return [2, Promise.reject(shared_1.errorMessages.indexNotFound)]; | ||
} | ||
_d.label = 1; | ||
case 1: | ||
_c.trys.push([1, 4, , 9]); | ||
return [4, setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
_d.trys.push([1, 4, , 9]); | ||
return [4, setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.loading, preload)]; | ||
case 2: | ||
value = | ||
(_a = (_c.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
(_a = (_d.sent())) !== null && _a !== void 0 ? _a : value; | ||
return [4, setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.success, action)]; | ||
case 3: | ||
value = | ||
(_b = (_c.sent())) !== null && _b !== void 0 ? _b : value; | ||
(_b = (_d.sent())) !== null && _b !== void 0 ? _b : value; | ||
if (value == null) { | ||
@@ -86,22 +91,21 @@ return [2, Promise.reject(shared_1.errorMessages.actionReturnedNull)]; | ||
case 4: | ||
e1_1 = _c.sent(); | ||
_c.label = 5; | ||
e_1 = _d.sent(); | ||
_d.label = 5; | ||
case 5: | ||
_c.trys.push([5, 7, , 8]); | ||
return [4, setContextDataForUpdateOne(contextData, setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
_d.trys.push([5, 7, , 8]); | ||
return [4, setContextDataForUpdateOne(setContextData, params, getIndex, shared_1.statefulStates.error, error)]; | ||
case 6: | ||
value = _c.sent(); | ||
if (typeof e1_1 === "string") { | ||
return [2, Promise.reject(e1_1)]; | ||
value = _d.sent(); | ||
if (typeof e_1 === "string") { | ||
return [2, Promise.reject(e_1)]; | ||
} | ||
else { | ||
return [2, Promise.reject(e1_1.message || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
return [2, Promise.reject(e_1.message || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
} | ||
return [3, 8]; | ||
case 7: | ||
e2_1 = _c.sent(); | ||
setContextData(__assign(__assign({}, contextData), { state: shared_1.statefulStates.error })); | ||
if (typeof e2_1 === "string") { | ||
return [2, Promise.reject(e2_1)]; | ||
} | ||
_c = _d.sent(); | ||
setContextData(function (contextData) { | ||
return __assign(__assign({}, contextData), { state: shared_1.statefulStates.error }); | ||
}); | ||
return [2, Promise.reject(shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
@@ -115,9 +119,8 @@ case 8: return [3, 9]; | ||
exports.getUpdateOneContextData = getUpdateOneContextData; | ||
function setContextDataForUpdateOne(contextData, setContextData, params, getIndex, state, action) { | ||
function setContextDataForUpdateOne(setContextData, params, getIndex, state, action) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var index, value, _a, newStore, d, a, e_1; | ||
var index, value, _a, newStore, d, a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 4, , 5]); | ||
index = getIndex(params); | ||
@@ -134,11 +137,14 @@ if (!action) return [3, 2]; | ||
value = _a; | ||
newStore = getUpdatedContextDataForUpdateOne(contextData, index, value, state); | ||
setContextData(newStore); | ||
return [4, new Promise(function (resolve) { | ||
setContextData(function (contextData) { | ||
var newStore = getUpdatedContextDataForUpdateOne(contextData, index, value, state); | ||
resolve(newStore); | ||
return newStore; | ||
}); | ||
})]; | ||
case 4: | ||
newStore = _b.sent(); | ||
d = newStore.data; | ||
a = d[index]; | ||
return [2, a.data]; | ||
case 4: | ||
e_1 = _b.sent(); | ||
return [2, Promise.reject((e_1 === null || e_1 === void 0 ? void 0 : e_1.message) || shared_1.errorMessages.unknownPreloadOrActionReject)]; | ||
case 5: return [2]; | ||
} | ||
@@ -153,5 +159,2 @@ }); | ||
var oldValue = data[index]; | ||
if (oldValue == null) { | ||
throw new Error(shared_1.errorMessages.indexNotFound); | ||
} | ||
var newValue = { | ||
@@ -158,0 +161,0 @@ data: __assign(__assign({}, oldValue.data), value), |
@@ -15,3 +15,3 @@ { | ||
"enzyme-adapter-react-16": "1.15.6", | ||
"husky": "5.1.1", | ||
"husky": "5.1.2", | ||
"jest": "26.6.3", | ||
@@ -46,3 +46,3 @@ "lint-staged": "10.5.4", | ||
"sideEffects": false, | ||
"version": "1.0.0-beta.5" | ||
"version": "1.0.0-beta.6" | ||
} |
103152
1647