Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an example for subscriptions #990

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions etc/api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ export namespace Osdk {
linksType?: any;
} ? Q["linksType"] : Q extends ObjectTypeDefinition ? OsdkObjectLinksObject<Q> : never;
readonly $as: <NEW_Q extends ValidToFrom<Q>>(type: NEW_Q | string) => Osdk.Instance<NEW_Q, OPTIONS, ConvertProps<Q, NEW_Q, P>>;
readonly $cloneAndUpdate: <O extends PropertyKeys<Q>, T extends Osdk.Instance<Q, any, O>>(newObject: T) => Osdk.Instance<Q, OPTIONS, P>;
} & (IsNever<OPTIONS> extends true ? {} : IsAny<OPTIONS> extends true ? {} : "$rid" extends OPTIONS ? {
readonly $rid: string;
} : {});
Expand Down
4 changes: 4 additions & 0 deletions etc/client.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { InterfaceMetadata } from '@osdk/api';
import { isOk } from '@osdk/api';
import type { MinimalObjectSet } from '@osdk/api/unstable';
import { ObjectMetadata } from '@osdk/api';
import type { ObjectOrInterfaceDefinition } from '@osdk/api';
import type { ObjectQueryDataType } from '@osdk/api';
import { ObjectSet } from '@osdk/api';
import type { ObjectSetQueryDataType } from '@osdk/api';
Expand Down Expand Up @@ -94,6 +95,9 @@ export interface Client extends SharedClient, SharedClient_2 {
fetchMetadata<Q extends (ObjectTypeDefinition | InterfaceDefinition | ActionDefinition<any> | QueryDefinition<any>)>(o: Q): Promise<Q extends ObjectTypeDefinition ? ObjectMetadata : Q extends InterfaceDefinition ? InterfaceMetadata : Q extends ActionDefinition<any> ? ActionMetadata : Q extends QueryDefinition<any> ? QueryMetadata : never>;
}

// @public (undocumented)
export function consolidateOsdkObject<T extends Osdk.Instance<V, any, any>, U extends Osdk.Instance<V, any, any>, V extends ObjectOrInterfaceDefinition>(oldObject: T, upToDateObject: U): T;

// @public (undocumented)
export function createAttachmentUpload(data: Blob, name: string): AttachmentUpload;

Expand Down
51 changes: 50 additions & 1 deletion examples-extra/docs_example/src/osdkExample.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient } from "@osdk/client";
import { consolidateOsdkObject, createClient, Osdk } from "@osdk/client";
import {
$ontologyRid,
Employee,
Expand Down Expand Up @@ -182,4 +182,53 @@ export async function osdkObjectSetExample() {
unionObjectSet,
subtractObjectSet,
);

/**
* SUBSCRIPTIONS
*/

const objects: Record<string, Osdk.Instance<Employee, never, "employeeId">> =
{};

const subscription = client(Employee).subscribe({
onChange: (update) => {
if (update.state === "ADDED_OR_UPDATED") {
console.log(
`Employee with primary key ${update.object.$primaryKey} was added or updated`,
);

objects[update.object.$primaryKey] = consolidateOsdkObject(
objects[update.object.$primaryKey],
update.object,
);
} else if (update.state === "REMOVED") {
console.log(
`Employee with primary key ${update.object.$primaryKey} was deleted`,
);

delete objects[update.object.$primaryKey];
}
},
onOutOfDate: async () => {
console.log("Object Set Out of Date. Reloading Object Set");

for await (const obj of client(Employee).asyncIter()) {
objects[obj.$primaryKey] = obj;
}
},
onSuccessfulSubscription() {
console.log("Subscription successful");
setTimeout(() => {
subscription.unsubscribe();
}, 10000);
},
onError(err) {
console.error("Error in subscription and subscription closed", err);
},
// The properties that will be returned in updates can optionally be specified below. Updates may still be sent for properties not specified here,
// and not all properties may be returned.
}, { properties: ["employeeId"] });

// An empty subscription will request all properties
client(Employee).subscribe({ onChange: () => {} });
}
10 changes: 10 additions & 0 deletions packages/api/src/OsdkObjectFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ export namespace Osdk {
OPTIONS,
ConvertProps<Q, NEW_Q, P>
>;
readonly $cloneAndUpdate: <
T extends Osdk.Instance<Q, any, L>,
L extends PropertyKeys<Q>,
Copy link
Contributor Author

@nihalbhatnagar nihalbhatnagar Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I'm still figuring out, not sure why but I can't pass in an object that is not Osdk.Instance, eg Instance<Employee, never, "id"> doesn't work with capturing the generic or any

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to check it out to play with it.

You probably also want to just support an object that is the raw values?

>(
newObject: T,
) => Osdk.Instance<
Q,
OPTIONS,
P
>;
}
// We are hiding the $rid field if it wasn't requested as we want to discourage its use
& (IsNever<OPTIONS> extends true ? {}
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ export {
extractDateInLocalTime,
extractDateInUTC,
} from "./util/datetimeConverters.js";

export { consolidateOsdkObject } from "./util/consolidateOsdkObject.js";
Loading
Loading