This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
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.
feat: model now updates alll states successfully!
ready to release
- Loading branch information
1 parent
59a3df8
commit 7976391
Showing
7 changed files
with
174 additions
and
82 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
45 changes: 45 additions & 0 deletions
45
packages/cactus-sync-client/lib/src/abstract/StateModel.ts
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,45 @@ | ||
import { ApolloQueryResult, FetchResult } from '@apollo/client/core' | ||
import { Emitter } from 'mitt' | ||
import { Maybe } from './BasicTypes' | ||
|
||
export enum StateModelEvents { | ||
addUpdateStateModel = 'addUpdateStateModel', | ||
removeStateModel = 'removeStateModel', | ||
} | ||
|
||
export type StateModelChange<TModel> = { | ||
modelName: string | ||
item: TModel | ||
} | ||
export const validateStateModelResult = <TResult>( | ||
result: FetchResult<TResult> | ApolloQueryResult<TResult> | ||
): { isNotValid: boolean; data?: Maybe<TResult>; isValid: boolean } => { | ||
if (result.errors != null) return { isNotValid: true, isValid: false } | ||
const data = result.data | ||
if (typeof data != 'object') return { isNotValid: true, isValid: false } | ||
return { isNotValid: false, data, isValid: true } | ||
} | ||
export const notifyStateModelListeners = <TModel>({ | ||
remove, | ||
emitter, | ||
item, | ||
modelName, | ||
notifyListeners, | ||
}: { | ||
modelName: string | ||
notifyListeners: Maybe<boolean> | ||
item: TModel | ||
remove?: Maybe<boolean> | ||
emitter: Emitter | ||
}) => { | ||
if (notifyListeners) { | ||
const obj: StateModelChange<TModel> = { | ||
modelName: modelName, | ||
item, | ||
} | ||
const eventType = remove | ||
? StateModelEvents.removeStateModel | ||
: StateModelEvents.addUpdateStateModel | ||
emitter.emit(eventType, obj) | ||
} | ||
} |
Oops, something went wrong.