Skip to content

Commit

Permalink
Enum -> singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Jan 19, 2022
1 parent 9973a77 commit 54204bc
Show file tree
Hide file tree
Showing 28 changed files with 2,002 additions and 3,519 deletions.
8 changes: 4 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "react-native-get-random-values";
export { AbsentSyncableStateEnum } from "./react-native/types/AbsentSyncableStateEnum";
export { AbsentSyncableStateSingleton } from "./react-native/types/AbsentSyncableStateSingleton";
export { Aligned } from "./react-native/components/Aligned";
export { AwaitingPullSyncableStateCollectionItem } from "./react-native/types/AwaitingPullSyncableStateCollectionItem";
export { AwaitingPushSyncableStateCollectionItem } from "./react-native/types/AwaitingPushSyncableStateCollectionItem";
Expand Down Expand Up @@ -92,7 +92,7 @@ export { PictureHelperInterface } from "./react-native/types/PictureHelperInterf
export { PreflightResponse } from "./react-native/types/PreflightResponse";
export { PreflightResponseCollection } from "./react-native/types/PreflightResponseCollection";
export { PreflightResponseCollectionItem } from "./react-native/types/PreflightResponseCollectionItem";
export { PreflightResponseEnum } from "./react-native/types/PreflightResponseEnum";
export { PreflightResponseSingleton } from "./react-native/types/PreflightResponseSingleton";
export { PushingSyncableStateCollectionItem } from "./react-native/types/PushingSyncableStateCollectionItem";
export { QueryParameter } from "./react-native/types/QueryParameter";
export { QueryParameters } from "./react-native/types/QueryParameters";
Expand Down Expand Up @@ -124,7 +124,7 @@ export { SyncableSchema } from "./react-native/types/SyncableSchema";
export { SyncableState } from "./react-native/types/SyncableState";
export { SyncableStateCollection } from "./react-native/types/SyncableStateCollection";
export { SyncableStateCollectionItem } from "./react-native/types/SyncableStateCollectionItem";
export { SyncableStateEnum } from "./react-native/types/SyncableStateEnum";
export { SyncableStateSingleton } from "./react-native/types/SyncableStateSingleton";
export { SyncableStateHelper } from "./react-native/services/SyncableStateHelper";
export { SyncConfiguration } from "./react-native/types/SyncConfiguration";
export { SyncConfigurationCollection } from "./react-native/types/SyncConfigurationCollection";
Expand All @@ -143,7 +143,7 @@ export { UnderlinedTopTabBarStyle } from "./react-native/types/UnderlinedTopTabB
export { UnderlinedTopTabBarStyleState } from "./react-native/types/UnderlinedTopTabBarStyleState";
export { unwrapRenderedFunctionComponent } from "./react-native/utilities/unwrapRenderedFunctionComponent";
export { UpToDateSyncableStateCollectionItem } from "./react-native/types/UpToDateSyncableStateCollectionItem";
export { UpToDateSyncableStateEnum } from "./react-native/types/UpToDateSyncableStateEnum";
export { UpToDateSyncableStateSingleton } from "./react-native/types/UpToDateSyncableStateSingleton";
export { useBackButton } from "./react-native/hooks/useBackButton";
export { useEventRefresh } from "./react-native/hooks/useEventRefresh";
export { useMeasure } from "./react-native/hooks/useMeasure";
Expand Down
79 changes: 42 additions & 37 deletions react-native/services/Sync/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import type { SyncConfigurationCollection } from "../../types/SyncConfigurationC
import type { FileStoreInterface } from "../../types/FileStoreInterface";
import type { SyncState } from "../../types/SyncState";
import type { SyncInterface } from "../../types/SyncInterface";
import type { PreflightResponseEnum, SyncableStateEnum } from "../../..";
import type { SyncableStateSingleton } from "../../types/SyncableStateSingleton";
import type { PreflightResponseSingleton } from "../../types/PreflightResponseSingleton";

const camelCaseToKebabCase = (camelCase: string): string =>
camelCase.replace(/([A-Z])/g, `-$1`).toLowerCase();
Expand Down Expand Up @@ -510,7 +511,7 @@ export class Sync<
execute(): Promise<boolean>;
}
| {
readonly type: `enum`;
readonly type: `singleton`;
readonly beforeLogMessage: string;
execute(): Promise<boolean>;
}
Expand All @@ -522,68 +523,70 @@ export class Sync<

for (const step of this.syncConfiguration.order) {
switch (step.type) {
case `enum`: {
const stateEnum = state.enums[
case `singleton`: {
const stateSingleton = state.singletons[
step.key
] as SyncableStateEnum<Json>;
] as SyncableStateSingleton<Json>;

const preflightEnum = preflightResponse.value.enums[
const preflightSingleton = preflightResponse.value.singletons[
step.key
] as PreflightResponseEnum;
] as PreflightResponseSingleton;

const kebabCasedEnumKey = camelCaseToKebabCase(step.key);
const kebabCasedSingletonKey = camelCaseToKebabCase(step.key);

switch (stateEnum.type) {
switch (stateSingleton.type) {
case `absent`:
this.logger.information(
`New enum "${step.key}" will be pulled.`
`New singleton "${step.key}" will be pulled.`
);

pulls.push({
type: `enum`,
beforeLogMessage: `Pulling enum "${step.key}"...`,
type: `singleton`,
beforeLogMessage: `Pulling singleton "${step.key}"...`,
execute: async () => {
const response = await this.request.returningJson<{
"200": SyncPullResponse<Json>;
}>(
`GET`,
`sync/${kebabCasedEnumKey}`,
`sync/${kebabCasedSingletonKey}`,
{ type: `empty` },
{},
abortSignal,
[`200`]
);
if (response.value.version === preflightEnum.version) {
if (
response.value.version === preflightSingleton.version
) {
if (this.stateStore.get() === state) {
state = {
...state,
enums: {
...state.enums,
singletons: {
...state.singletons,
[step.key]: {
type: `upToDate`,
version: preflightEnum.version,
values: response.value.data,
version: preflightSingleton.version,
value: response.value.data,
},
},
};

this.stateStore.set(state);

this.logger.information(
`Successfully pulled new enum "${step.key}".`
`Successfully pulled new singleton "${step.key}".`
);

return true;
} else {
this.logger.warning(
`The state store changed during pull of enum "${step.key}"; sync has been interrupted and will need to run again.`
`The state store changed during pull of singleton "${step.key}"; sync has been interrupted and will need to run again.`
);

return false;
}
} else {
this.logger.warning(
`The version of enum "${step.key}" changed from "${preflightEnum.version}" at the time of preflight to "${response.value.version}" at the time of pull; sync has been interrupted and will need to run again.`
`The version of singleton "${step.key}" changed from "${preflightSingleton.version}" at the time of preflight to "${response.value.version}" at the time of pull; sync has been interrupted and will need to run again.`
);

return false;
Expand All @@ -593,60 +596,62 @@ export class Sync<
break;

case `upToDate`:
if (preflightEnum.version === stateEnum.version) {
if (preflightSingleton.version === stateSingleton.version) {
this.logger.debug(
`No pull required of enum "${step.key}" as preflight and state store versions match ("${preflightEnum.version}").`
`No pull required of singleton "${step.key}" as preflight and state store versions match ("${preflightSingleton.version}").`
);
} else {
this.logger.information(
`Previously pulled enum "${step.key}" will be pulled again as versions do not match between preflight ("${preflightEnum.version}") and state store ("${stateEnum.version}").`
`Previously pulled singleton "${step.key}" will be pulled again as versions do not match between preflight ("${preflightSingleton.version}") and state store ("${stateSingleton.version}").`
);

pulls.push({
type: `enum`,
beforeLogMessage: `Pulling enum "${step.key}"...`,
type: `singleton`,
beforeLogMessage: `Pulling singleton "${step.key}"...`,
execute: async () => {
const response = await this.request.returningJson<{
"200": SyncPullResponse<Json>;
}>(
`GET`,
`sync/${kebabCasedEnumKey}`,
`sync/${kebabCasedSingletonKey}`,
{ type: `empty` },
{},
abortSignal,
[`200`]
);
if (response.value.version === preflightEnum.version) {
if (
response.value.version === preflightSingleton.version
) {
if (this.stateStore.get() === state) {
state = {
...state,
enums: {
...state.enums,
singletons: {
...state.singletons,
[step.key]: {
type: `upToDate`,
version: preflightEnum.version,
values: response.value.data,
version: preflightSingleton.version,
value: response.value.data,
},
},
};

this.stateStore.set(state);

this.logger.information(
`Successfully pulled update of enum "${step.key}".`
`Successfully pulled update of singleton "${step.key}".`
);

return true;
} else {
this.logger.warning(
`The state store changed during pull of enum "${step.key}"; sync has been interrupted and will need to run again.`
`The state store changed during pull of singleton "${step.key}"; sync has been interrupted and will need to run again.`
);

return false;
}
} else {
this.logger.warning(
`The version of enum "${step.key}" changed from "${preflightEnum.version}" at the time of preflight to "${response.value.version}" at the time of pull; sync has been interrupted and will need to run again.`
`The version of singleton "${step.key}" changed from "${preflightSingleton.version}" at the time of preflight to "${response.value.version}" at the time of pull; sync has been interrupted and will need to run again.`
);

return false;
Expand Down Expand Up @@ -983,9 +988,9 @@ export class Sync<
this.logger.information(pull.beforeLogMessage);

switch (pull.type) {
case `enum`:
case `singleton`:
this.setState({
type: `pullingEnum`,
type: `pullingSingleton`,
completedSteps: completedPulls,
totalSteps: pulls.length,
});
Expand Down
Loading

0 comments on commit 54204bc

Please sign in to comment.