Skip to content

Commit

Permalink
Implement subscriptions per the v1 api behavior (#214)
Browse files Browse the repository at this point in the history
* Implement subscriptions per the v1 api behavior

* PR Feedback
  • Loading branch information
ericanderson authored Apr 24, 2024
1 parent 123b630 commit 97f627e
Show file tree
Hide file tree
Showing 13 changed files with 628 additions and 204 deletions.
11 changes: 11 additions & 0 deletions .changeset/big-llamas-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@osdk/client": patch
---

Supports more than one concurrent subscription

Also:

- Introduced an optional pino logger to the client
- Fixes issues with where clauses for equality in subscriptions
- Fixes issues with inconsistent idname and apiName's in the mapping data
1 change: 1 addition & 0 deletions examples-extra/basic/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@osdk/client": "workspace:*",
"@osdk/examples.basic.sdk": "workspace:*",
"@osdk/omniapi": "workspace:*",
"pino": "^8.20.0",
"tiny-invariant": "^1.3.1"
},
"devDependencies": {
Expand Down
42 changes: 39 additions & 3 deletions examples-extra/basic/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { Osdk, PageResult } from "@osdk/client";
import type { ObjectSetListener, Osdk, PageResult } from "@osdk/client";
import { createClient } from "@osdk/client";
import {
assignEmployee1,
Expand All @@ -26,6 +26,7 @@ import {
WeatherStation,
} from "@osdk/examples.basic.sdk";
import * as LanguageModel from "@osdk/omniapi/Models_LanguageModel";
import { pino } from "pino";
import invariant from "tiny-invariant";
import type { TypeOf } from "ts-expect";
import { expectType } from "ts-expect";
Expand All @@ -40,14 +41,19 @@ import { typeChecks } from "./typeChecks.js";
invariant(process.env.FOUNDRY_STACK !== undefined);
invariant(process.env.FOUNDRY_USER_TOKEN !== undefined);

const logger = pino({ level: "debug" });

export const client = createClient(
process.env.FOUNDRY_STACK,
"ri.ontology.main.ontology.00000000-0000-0000-0000-000000000000",
() => process.env.FOUNDRY_USER_TOKEN!,
{ logger },
);

const runOld = false;

const testSubscriptions = true;

async function runTests() {
try {
if (runOld) {
Expand All @@ -59,8 +65,10 @@ async function runTests() {
await fetchEmployeeLead(client, "bob");
}

const foo = await LanguageModel.listLanguageModels(client.ctx as any);
console.log(foo.data);
const models = await LanguageModel.listLanguageModels(client.ctx as any);
logger.info({
models: models.data.map(m => `'${m.apiName}' in ${m.source}`),
});

// const { data: boundaries } = await client(BoundariesUsState).fetchPage();
// let didThrow = false;
Expand All @@ -74,6 +82,34 @@ async function runTests() {
// if (!didThrow) {
// throw new Error("Should not be allowed to convert between mixed types");
// }
const makeObjectSetListener = (prefix: string): ObjectSetListener<any> => {
return {
onError(err) {
logger.error({ err }, "%s: Error in subscription", prefix);
},

onOutOfDate() {
logger.info("%s: out of date", prefix);
},

onChange(objects) {
logger.info("%s: Changed objects: %o", prefix, objects);
},
};
};

if (testSubscriptions) {
client(Employee).where({
jobProfile: "Echo",
}).subscribe(makeObjectSetListener("Sub(Echo)"));

client(Employee).where({
jobProfile: "Delta",
}).subscribe(makeObjectSetListener("Sub(Delta)"));

// we don't need the console flooded with additional things
return;
}

// this has the nice effect of faking a 'race' with the below code
(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"clean": "rm -rf lib dist types build tsconfig.tsbuildinfo",
"fix-lint": "eslint . --fix && dprint fmt --config $(find-up dprint.json)",
"lint": "eslint . && dprint check --config $(find-up dprint.json)",
"test": "vitest run",
"test": "vitest run --pool=forks",
"test:watch": "vitest",
"transpile": "tsup",
"transpileWatch": "tsup --watch",
Expand Down
4 changes: 4 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"jest-extended": "^4.0.2",
"p-defer": "^4.0.0",
"p-state": "^2.0.0",
"pino": "^8.20.0",
"ts-expect": "^1.3.0",
"type-fest": "^4.14.0",
"typescript": "^5.4.2"
Expand All @@ -70,6 +71,9 @@
"main": "./build/js/index.cjs",
"module": "./build/js/index.mjs",
"types": "./build/types/index.d.ts",
"peerDependencies": {
"pino": "^8.20.0"
},
"sls": {
"dependencies": {
"com.palantir.foundry.api:api-gateway": {
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/MinimalClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import type { ClientContext } from "@osdk/shared.net";
import type { Logger } from "pino";
import type { OntologyProvider } from "./ontology/OntologyProvider.js";

/*
Expand All @@ -26,6 +27,7 @@ export interface MinimalClient
{
ontologyRid: string;
ontologyProvider: OntologyProvider;
logger?: Logger;
}

export type MinimalClientParams = {
Expand Down
5 changes: 3 additions & 2 deletions packages/client/src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
ObjectOrInterfaceDefinition,
ObjectTypeDefinition,
} from "@osdk/api";
import type { Logger } from "pino";
import type { ActionSignatureFromDef } from "./actions/Actions.js";
import { createActionInvoker } from "./actions/createActionInvoker.js";
import type { Client } from "./Client.js";
Expand All @@ -32,7 +33,7 @@ export function createClient(
stack: string,
ontologyRid: string,
tokenProvider: () => Promise<string> | string,
HOLD: undefined = undefined,
options: { logger?: Logger } | undefined = undefined,
fetchFn: typeof globalThis.fetch = fetch,
): Client {
const clientCtx: MinimalClient = createMinimalClient(
Expand All @@ -42,7 +43,7 @@ export function createClient(
},
stack,
tokenProvider,
{},
options,
fetchFn,
);

Expand Down
6 changes: 4 additions & 2 deletions packages/client/src/createMinimalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { createClientContext } from "@osdk/shared.net";
import type { Logger } from "pino";
import type {
MinimalClient,
MinimalClientParams,
Expand All @@ -29,7 +30,7 @@ export function createMinimalClient(
metadata: MinimalClientParams["metadata"],
stack: string,
tokenProvider: () => Promise<string> | string,
ontologyCachingOptions: OntologyCachingOptions = {},
options: OntologyCachingOptions & { logger?: Logger } = {},
fetchFn: (
input: RequestInfo | URL,
init?: RequestInit | undefined,
Expand All @@ -55,9 +56,10 @@ export function createMinimalClient(
),
ontologyRid: metadata.ontologyRid,
ontologyProvider: undefined as any,
logger: options.logger,
};
clientCtx.ontologyProvider = createStandardOntologyProviderFactory(
ontologyCachingOptions,
options,
)(clientCtx);
return clientCtx;
}
Loading

0 comments on commit 97f627e

Please sign in to comment.