Skip to content

Commit

Permalink
update types for internal / external subscription messages; minor upd…
Browse files Browse the repository at this point in the history
…ate to datastore 'observe'
  • Loading branch information
david-mcafee committed Mar 29, 2022
1 parent 8d2500c commit eb8a29d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
35 changes: 18 additions & 17 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
SchemaModel,
SchemaNamespace,
SchemaNonModel,
InternalSubscriptionMessage,
SubscriptionMessage,
DataStoreSnapshot,
SyncConflict,
Expand Down Expand Up @@ -1128,23 +1129,23 @@ class DataStore {
handle = this.storage
.observe(modelConstructor, predicate)
.filter(({ model }) => namespaceResolver(model) === USER)
.map((event: SubscriptionMessage<T>): SubscriptionMessage<T> => {
// The `element` returned by storage only contains updated fields.
// Intercept the event to send the `savedElement` so that the first
// snapshot returned to the consumer contains all fields.
// In the event of a delete we return `element`, as `savedElement`
// here is undefined.
const { opType, model, condition, element, savedElement } = event;

const updated = {
opType,
element: savedElement || element,
model,
condition,
};

return updated;
})
.map(
(event: InternalSubscriptionMessage<T>): SubscriptionMessage<T> => {
// The `element` returned by storage only contains updated fields.
// Intercept the event to send the `savedElement` so that the first
// snapshot returned to the consumer contains all fields.
// In the event of a delete we return `element`, as `savedElement`
// here is undefined.
const { opType, model, condition, element, savedElement } = event;

return {
opType,
element: savedElement || element,
model,
condition,
};
}
)
.subscribe(observer);
})();

Expand Down
8 changes: 4 additions & 4 deletions packages/datastore/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
PredicatesGroup,
QueryOne,
SchemaNamespace,
InternalSubscriptionMessage,
SubscriptionMessage,
isTargetNameAssociation,
} from '../types';
Expand All @@ -29,7 +30,7 @@ import { Adapter } from './adapter';
import getDefaultAdapter from './adapter/getDefaultAdapter';

export type StorageSubscriptionMessage<T extends PersistentModel> =
SubscriptionMessage<T> & {
InternalSubscriptionMessage<T> & {
mutator?: Symbol;
};

Expand Down Expand Up @@ -134,9 +135,8 @@ class StorageClass implements StorageFacade {

const element = updateMutationInput || savedElement;

const modelConstructor = (
Object.getPrototypeOf(savedElement) as Object
).constructor as PersistentModelConstructor<T>;
const modelConstructor = (Object.getPrototypeOf(savedElement) as Object)
.constructor as PersistentModelConstructor<T>;

this.pushStream.next({
model: modelConstructor,
Expand Down
7 changes: 6 additions & 1 deletion packages/datastore/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,12 @@ export enum OpType {
DELETE = 'DELETE',
}

export type SubscriptionMessage<T extends PersistentModel> = {
export type SubscriptionMessage<T extends PersistentModel> = Pick<
InternalSubscriptionMessage<T>,
'opType' | 'element' | 'model' | 'condition'
>;

export type InternalSubscriptionMessage<T extends PersistentModel> = {
opType: OpType;
element: T;
model: PersistentModelConstructor<T>;
Expand Down

0 comments on commit eb8a29d

Please sign in to comment.