@evoke-platform/context 1.1.0-testing.2 → 1.1.0-testing.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,6 +35,8 @@ const applications = useObject('application');
35
35
  - [getInstanceHistory](#getinstancehistoryinstanceid)
36
36
  - [newInstance](#newinstanceinput)
37
37
  - [instanceAction](#instanceactioninput)
38
+ - [invalidateCache](#invalidatecache)
39
+ - [invalidateAllCache](#invalidateallcache-static)
38
40
 
39
41
  #### `useObject(objectId)`
40
42
 
@@ -66,15 +68,21 @@ applications.findInstances(callback);
66
68
  const results = await applications.findInstances();
67
69
  ```
68
70
 
71
+ ObjectStore includes built-in caching for object definitions with a 30-second time-to-live (TTL). This improves performance by reducing API calls for frequently accessed objects.
72
+
69
73
  ##### `get(options)`
70
74
 
71
75
  Get the object definition for this store's object. The returned object includes inherited properties and actions if
72
- this object is derived from another object.
76
+ this object is derived from another object. Results are cached for improved performance.
73
77
 
74
78
  - `options` _[object]_ - _optional_
75
79
  - `sanitized` _[boolean]_
76
80
  - If `true`, returns a sanitized version of the object reflecting only the properties and actions available
77
81
  to the current user.
82
+ - `bypassCache` _[boolean]_
83
+ - If `true`, bypasses the cache and forces a new API call. The cache is still updated with the results of the new API call.
84
+ - `skipAlphabetize` _[boolean]_
85
+ - If `true`, preserves the original order of properties instead of alphabetizing them (properties are alphabetized by default).
78
86
 
79
87
  ##### `findInstances(filter)`
80
88
 
@@ -123,6 +131,14 @@ Performs an action on an existing instance.
123
131
  - Action to be executed. The action must not be a create action.
124
132
  - Returns updated instance.
125
133
 
134
+ ##### `invalidateCache()`
135
+
136
+ Invalidates cached data for this specific object ID and all its option variants. Use this when you know the object definition has changed on the server.
137
+
138
+ ##### `invalidateAllCache()` (static)
139
+
140
+ Static method that invalidates the entire object cache across all ObjectStore instances. Use this when you need to force fresh data for all objects.
141
+
126
142
  ### Page Context
127
143
 
128
144
  - [usePageParam](#usepageparamparam)
@@ -312,26 +312,89 @@ export type Reference = {
312
312
  name?: string;
313
313
  };
314
314
  export type ObjectOptions = {
315
+ /**
316
+ * When true, returns a sanitized version of the object reflecting
317
+ * only the properties and actions available to the current user.
318
+ */
315
319
  sanitized?: boolean;
320
+ /**
321
+ * When true, bypasses the cache and forces a new API call.
322
+ */
323
+ bypassCache?: boolean;
324
+ /**
325
+ * When true, preserves the original order of properties instead of
326
+ * alphabetizing them (properties are alphabetized by default).
327
+ */
328
+ skipAlphabetize?: boolean;
316
329
  };
330
+ /**
331
+ * Provides methods for working with objects and their instances in Evoke.
332
+ * Supports retrieving object definitions, finding/retrieving instances,
333
+ * creating new instances, and performing actions on existing instances.
334
+ */
317
335
  export declare class ObjectStore {
318
336
  private services;
319
337
  private objectId;
338
+ private static cache;
320
339
  constructor(services: ApiServices, objectId: string);
340
+ private getCacheKey;
341
+ private processObject;
342
+ /**
343
+ * Invalidates cached data for this specific object ID and all its option variants.
344
+ * Use this when you know the object definition has changed on the server.
345
+ */
346
+ invalidateCache(): void;
347
+ /**
348
+ * Invalidates the entire object cache across all ObjectStore instances.
349
+ * Use this when you need to force fresh data for all objects.
350
+ */
351
+ static invalidateAllCache(): void;
352
+ /**
353
+ * Retrieves the object definition with inherited properties and actions.
354
+ * Results are cached with a 30-second TTL to reduce API calls for frequently accessed objects.
355
+ *
356
+ * By default, properties are alphabetized by name. Use options to customize behavior.
357
+ *
358
+ * @param options - Configuration options for object retrieval and processing
359
+ * @returns A promise resolving to the object with root
360
+ */
321
361
  get(options?: ObjectOptions): Promise<ObjWithRoot>;
322
362
  get(cb?: Callback<ObjWithRoot>): void;
323
363
  get(options: ObjectOptions, cb?: Callback<ObjWithRoot>): void;
364
+ /**
365
+ * Finds instances of the object that match the specified filter criteria.
366
+ */
324
367
  findInstances<T extends ObjectInstance = ObjectInstance>(filter?: Filter): Promise<T[]>;
325
368
  findInstances<T extends ObjectInstance = ObjectInstance>(cb: Callback<T[]>): void;
326
369
  findInstances<T extends ObjectInstance = ObjectInstance>(filter: Filter, cb: Callback<T[]>): void;
370
+ /**
371
+ * Retrieves a specific instance of the object by ID.
372
+ */
327
373
  getInstance<T extends ObjectInstance = ObjectInstance>(id: string): Promise<T>;
328
374
  getInstance<T extends ObjectInstance = ObjectInstance>(id: string, cb: Callback<T>): void;
375
+ /**
376
+ * Retrieves the history of an instance of the object.
377
+ */
329
378
  getInstanceHistory(id: string): Promise<History[]>;
330
379
  getInstanceHistory(id: string, cb: Callback<History[]>): void;
380
+ /**
381
+ * Creates a new instance of the object.
382
+ */
331
383
  newInstance<T extends ObjectInstance = ObjectInstance>(input: ActionRequest): Promise<T>;
332
384
  newInstance<T extends ObjectInstance = ObjectInstance>(input: ActionRequest, cb: Callback<T>): void;
385
+ /**
386
+ * Performs an action on an existing instance of the object.
387
+ */
333
388
  instanceAction<T extends ObjectInstance = ObjectInstance>(id: string, input: ActionRequest): Promise<T>;
334
389
  instanceAction<T extends ObjectInstance = ObjectInstance>(id: string, input: ActionRequest, cb: Callback<T>): void;
335
390
  }
391
+ /**
392
+ * Creates an ObjectStore instance for the specified object.
393
+ * Provides access to object definitions and instance operations.
394
+ * Object definitions are cached for performance.
395
+ *
396
+ * @param objectId - ID of the object to access
397
+ * @returns ObjectStore instance
398
+ */
336
399
  export declare function useObject(objectId: string): ObjectStore;
337
400
  export {};
@@ -1,12 +1,50 @@
1
1
  // Copyright (c) 2023 System Automation Corporation.
2
2
  // This file is licensed under the MIT License.
3
+ import TTLCache from '@isaacs/ttlcache';
3
4
  import { useMemo } from 'react';
4
5
  import { useApiServices } from '../api/index.js';
6
+ /**
7
+ * Provides methods for working with objects and their instances in Evoke.
8
+ * Supports retrieving object definitions, finding/retrieving instances,
9
+ * creating new instances, and performing actions on existing instances.
10
+ */
5
11
  export class ObjectStore {
6
12
  constructor(services, objectId) {
7
13
  this.services = services;
8
14
  this.objectId = objectId;
9
15
  }
16
+ getCacheKey(options) {
17
+ return `${this.objectId}:${(options === null || options === void 0 ? void 0 : options.sanitized) ? 'sanitized' : 'default'}:${(options === null || options === void 0 ? void 0 : options.skipAlphabetize) ? 'unsorted' : 'sorted'}`;
18
+ }
19
+ processObject(object, options) {
20
+ const result = Object.assign({}, object);
21
+ if (result.properties) {
22
+ // alphabetize properties by default unless disabled
23
+ if (!(options === null || options === void 0 ? void 0 : options.skipAlphabetize)) {
24
+ result.properties = [...result.properties].sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }));
25
+ }
26
+ }
27
+ return result;
28
+ }
29
+ /**
30
+ * Invalidates cached data for this specific object ID and all its option variants.
31
+ * Use this when you know the object definition has changed on the server.
32
+ */
33
+ invalidateCache() {
34
+ const prefix = `${this.objectId}:`;
35
+ for (const key of ObjectStore.cache.keys()) {
36
+ if (typeof key === 'string' && key.startsWith(prefix)) {
37
+ ObjectStore.cache.delete(key);
38
+ }
39
+ }
40
+ }
41
+ /**
42
+ * Invalidates the entire object cache across all ObjectStore instances.
43
+ * Use this when you need to force fresh data for all objects.
44
+ */
45
+ static invalidateAllCache() {
46
+ ObjectStore.cache.clear();
47
+ }
10
48
  get(optionsOrCallback, cb) {
11
49
  let options;
12
50
  if (cb) {
@@ -18,15 +56,33 @@ export class ObjectStore {
18
56
  else {
19
57
  options = optionsOrCallback;
20
58
  }
59
+ const cacheKey = this.getCacheKey(options);
60
+ if (!(options === null || options === void 0 ? void 0 : options.bypassCache)) {
61
+ const cachedPromise = ObjectStore.cache.get(cacheKey);
62
+ if (cachedPromise) {
63
+ if (cb) {
64
+ const callback = cb;
65
+ cachedPromise.then((data) => callback(null, data)).catch((err) => callback(err));
66
+ return;
67
+ }
68
+ return cachedPromise;
69
+ }
70
+ }
21
71
  const config = {
22
72
  params: {
23
73
  sanitizedVersion: options === null || options === void 0 ? void 0 : options.sanitized,
24
74
  },
25
75
  };
26
- if (!cb) {
27
- return this.services.get(`data/objects/${this.objectId}/effective`, config);
76
+ const promise = this.services
77
+ .get(`data/objects/${this.objectId}/effective`, config)
78
+ .then((result) => this.processObject(result, options));
79
+ ObjectStore.cache.set(cacheKey, promise);
80
+ if (cb) {
81
+ const callback = cb;
82
+ promise.then((data) => callback(null, data)).catch((err) => callback(err));
83
+ return;
28
84
  }
29
- this.services.get(`data/objects/${this.objectId}/effective`, config, cb);
85
+ return promise;
30
86
  }
31
87
  findInstances(filterOrCallback, cb) {
32
88
  let filter;
@@ -74,6 +130,19 @@ export class ObjectStore {
74
130
  this.services.post(`data/objects/${this.objectId}/instances/${id}/actions`, input, cb);
75
131
  }
76
132
  }
133
+ // Cache that stores in-flight promises
134
+ // 30 second TTL for cached promises
135
+ ObjectStore.cache = new TTLCache({
136
+ ttl: 30 * 1000,
137
+ });
138
+ /**
139
+ * Creates an ObjectStore instance for the specified object.
140
+ * Provides access to object definitions and instance operations.
141
+ * Object definitions are cached for performance.
142
+ *
143
+ * @param objectId - ID of the object to access
144
+ * @returns ObjectStore instance
145
+ */
77
146
  export function useObject(objectId) {
78
147
  const services = useApiServices();
79
148
  return useMemo(() => new ObjectStore(services, objectId), [services, objectId]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evoke-platform/context",
3
- "version": "1.1.0-testing.2",
3
+ "version": "1.1.0-testing.3",
4
4
  "description": "Utilities that provide context to Evoke platform widgets",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -67,6 +67,7 @@
67
67
  "react-router-dom": ">=6"
68
68
  },
69
69
  "dependencies": {
70
+ "@isaacs/ttlcache": "^1.4.1",
70
71
  "@microsoft/signalr": "^7.0.12",
71
72
  "axios": "^1.7.9",
72
73
  "uuid": "^9.0.1"