Skip to content

Commit

Permalink
Experiment with no proxy for objects (#1031)
Browse files Browse the repository at this point in the history
* Experiment with no proxy for objects

reorder for diff

reorder for diff

reorder for diff

reorder for diff

update snapshot

* Remove proxy for interface

* Cleanup getDolloarLink

* Changeset
  • Loading branch information
ericanderson authored Dec 9, 2024
1 parent 5a4ebd7 commit 720218d
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 531 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-tomatoes-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/client": minor
---

OSDK Client no longer uses javascript proxies for its objects. This results in a 13% increase in construction time but at 1kb per object reduction in memory
7 changes: 7 additions & 0 deletions packages/client/src/fetchMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ describe("FetchMetadata", () => {
"primaryKeyApiName": "employeeId",
"primaryKeyType": "integer",
"properties": {
"class": {
"description": "",
"displayName": undefined,
"multiplicity": false,
"nullable": true,
"type": "string",
},
"employeeId": {
"description": undefined,
"displayName": undefined,
Expand Down
123 changes: 1 addition & 122 deletions packages/client/src/object/convertWireToOsdkObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe("convertWireToOsdkObjects", () => {

expect(Object.keys(employee).sort()).toEqual([
"employeeId",
"$title",
"fullName",
"office",
"class",
Expand Down Expand Up @@ -227,128 +228,6 @@ describe("convertWireToOsdkObjects", () => {
expect(prototypeBefore).not.toBe(prototypeAfter);
});

it("updates interface when underlying changes - old", async () => {
const clientCtx = createMinimalClient(
{ ontologyRid: $ontologyRid },
"https://stack.palantir.com",
async () => "myAccessToken",
);

let objectFromWire = {
__apiName: "Employee" as const,
__primaryKey: 0,
__title: "Steve",
fullName: "Steve",
employeeId: "5",
} satisfies OntologyObjectV2;

const [obj] = (await convertWireToOsdkObjects(
clientCtx,
[objectFromWire],
undefined,
)) as unknown as Osdk<Employee>[];

expect(obj.fullName).toEqual("Steve");
expect(Object.keys(obj).sort()).toEqual([
"$apiName",
"$objectType",
"$primaryKey",
"$title",
"employeeId",
"fullName",
].sort());

const objAsFoo = obj.$as(FooInterface);
expect(objAsFoo).toMatchObject({
fooSpt: obj.fullName,
$apiName: FooInterface.apiName,
$primaryKey: obj.$primaryKey,
$objectType: obj.$objectType,
$title: obj.$title,
});

console.log(obj);
console.log(objAsFoo);

(obj as any).$updateInternalValues({
fullName: "Bob",
});
expect(obj.fullName).toEqual("Bob");
expect(objAsFoo.fooSpt).toEqual(obj.fullName);

expect(Object.keys(objAsFoo).sort()).toEqual([
"$apiName",
"$objectType",
"$primaryKey",
"$title",
"fooSpt",
].sort());

expect(obj).toBe(objAsFoo.$as(Employee));
expect(objAsFoo).toBe(obj.$as(FooInterface));
});

it("updates interface when underlying changes - new", async () => {
const clientCtx = createMinimalClient(
{ ontologyRid: $ontologyRid },
"https://stack.palantir.com",
async () => "myAccessToken",
);

let objectFromWire = {
__apiName: "Employee" as const,
__primaryKey: 0,
__title: "Steve",
fullName: "Steve",
employeeId: "5",
} satisfies OntologyObjectV2;

const [obj] = (await convertWireToOsdkObjects2(
clientCtx,
[objectFromWire],
undefined,
)) as unknown as Osdk<Employee>[];

expect(obj.fullName).toEqual("Steve");
expect(Object.keys(obj).sort()).toEqual([
"$apiName",
"$objectType",
"$primaryKey",
"$title",
"employeeId",
"fullName",
].sort());

const objAsFoo = obj.$as(FooInterface);
expect(objAsFoo).toMatchObject({
fooSpt: obj.fullName,
$apiName: FooInterface.apiName,
$primaryKey: obj.$primaryKey,
$objectType: obj.$objectType,
$title: obj.$title,
});

console.log(obj);
console.log(objAsFoo);

(obj as any).$updateInternalValues({
fullName: "Bob",
});
expect(obj.fullName).toEqual("Bob");
expect(objAsFoo.fooSpt).toEqual(obj.fullName);

expect(Object.keys(objAsFoo).sort()).toEqual([
"$apiName",
"$objectType",
"$primaryKey",
"$title",
"fooSpt",
].sort());

expect(obj).toBe(objAsFoo.$as(Employee));
expect(objAsFoo).toBe(obj.$as(FooInterface));
});

it("reconstitutes interfaces properly without rid - old", async () => {
const clientCtx = createMinimalClient(
{ ontologyRid: $ontologyRid },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ import type {
import type { ObjectHolder } from "./ObjectHolder.js";

/** @internal */
export interface InterfaceHolderOwnProps<
export interface InterfaceHolder<
Q extends FetchedObjectTypeDefinition,
> {
[UnderlyingOsdkObject]: Osdk<Q> & ObjectHolder<Q>;
[InterfaceDefRef]: InterfaceMetadata;
}

/** @internal */
export interface InterfaceHolder<
Q extends FetchedObjectTypeDefinition,
> extends InterfaceHolderOwnProps<Q> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,11 @@ export const InterfaceDefRef = Symbol(
process.env.MODE !== "production" ? "InterfaceDefinition" : undefined,
);

/** A symbol for getting the raw data object off of the holder an osdk object proxy points to */
/** @internal */
export const RawObject = Symbol(
process.env.MODE !== "production" ? "RawObject" : undefined,
);

/** @internal */
export const ClientRef = Symbol(
process.env.MODE !== "production" ? "ClientRef" : undefined,
);

/** @internal */
export const CachedPropertiesRef = Symbol(
process.env.MODE !== "production" ? "CachedProperties" : undefined,
);

export interface HolderBase<T extends ObjectOrInterfaceDefinition> {
[UnderlyingOsdkObject]: OsdkBase<any>;
[ObjectDefRef]?: T;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,21 @@
*/

import type { Osdk } from "@osdk/api";
import type { OntologyObjectV2 } from "@osdk/internal.foundry.core";
import type { MinimalClient } from "../../MinimalClientContext.js";
import type { FetchedObjectTypeDefinition } from "../../ontology/OntologyProvider.js";
import type { DollarAsFn } from "./getDollarAs.js";
import type { get$link } from "./getDollarLink.js";
import type {
CachedPropertiesRef,
ClientRef,
ObjectDefRef,
RawObject,
UnderlyingOsdkObject,
} from "./InternalSymbols.js";

/** @internal */
export interface ObjectHolderPrototypeOwnProps {
export interface ObjectHolder<Q extends FetchedObjectTypeDefinition> {
readonly [UnderlyingOsdkObject]: Osdk<Q>;
readonly [ObjectDefRef]: FetchedObjectTypeDefinition;
readonly [ClientRef]: MinimalClient;
readonly "$as": DollarAsFn;
readonly "$link": ReturnType<typeof get$link>;
readonly "$updateInternalValues": (newValues: Record<string, any>) => void;
}
/** @internal */
export interface ObjectHolderOwnProperties {
[RawObject]: OntologyObjectV2;
[CachedPropertiesRef]: Map<string | symbol, any>;
}

/** @internal */
export interface ObjectHolder<Q extends FetchedObjectTypeDefinition>
extends ObjectHolderPrototypeOwnProps, ObjectHolderOwnProperties
{
[UnderlyingOsdkObject]: Osdk<Q>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ describe(createOsdkInterface, () => {
status: "ACTIVE",
} satisfies FetchedObjectTypeDefinition,
};
// underlying: Osdk<Q> & ObjectHolder<Q>,
// interfaceDef: InterfaceMetadata,

const iface = createOsdkInterface(underlying as any, {
"apiName": "IFoo",
displayName: "",
Expand Down Expand Up @@ -114,8 +113,7 @@ describe(createOsdkInterface, () => {
status: "ACTIVE",
} satisfies FetchedObjectTypeDefinition,
};
// underlying: Osdk<Q> & ObjectHolder<Q>,
// interfaceDef: InterfaceMetadata,

const iface = createOsdkInterface(underlying as any, {
"apiName": "a.IFoo",
displayName: "",
Expand Down Expand Up @@ -176,8 +174,7 @@ describe(createOsdkInterface, () => {
status: "ACTIVE",
} satisfies FetchedObjectTypeDefinition,
};
// underlying: Osdk<Q> & ObjectHolder<Q>,
// interfaceDef: InterfaceMetadata,

const iface = createOsdkInterface(underlying as any, {
"apiName": "a.IFoo",
displayName: "",
Expand Down
Loading

0 comments on commit 720218d

Please sign in to comment.