@evoke-platform/context 1.1.0-dev.0 → 1.1.0-dev.2

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
@@ -36,6 +36,8 @@ const applications = useObject('application');
36
36
  - [getInstanceHistory](#getinstancehistoryinstanceid)
37
37
  - [newInstance](#newinstanceinput)
38
38
  - [instanceAction](#instanceactioninput)
39
+ - [invalidateCache](#invalidatecache)
40
+ - [invalidateAllCache](#invalidateallcache-static)
39
41
 
40
42
  #### `useObject(objectId)`
41
43
 
@@ -67,15 +69,21 @@ applications.findInstances(callback);
67
69
  const results = await applications.findInstances();
68
70
  ```
69
71
 
72
+ 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.
73
+
70
74
  ##### `get(options)`
71
75
 
72
76
  Get the object definition for this store's object. The returned object includes inherited properties and actions if
73
- this object is derived from another object.
77
+ this object is derived from another object. Results are cached for improved performance.
74
78
 
75
79
  - `options` _[object]_ - _optional_
76
80
  - `sanitized` _[boolean]_
77
81
  - If `true`, returns a sanitized version of the object reflecting only the properties and actions available
78
82
  to the current user.
83
+ - `bypassCache` _[boolean]_
84
+ - If `true`, bypasses the cache and forces a new API call. The cache is still updated with the results of the new API call.
85
+ - `skipAlphabetize` _[boolean]_
86
+ - If `true`, preserves the original order of properties instead of alphabetizing them (properties are alphabetized by default).
79
87
 
80
88
  ##### `findInstances(filter)`
81
89
 
@@ -124,6 +132,14 @@ Performs an action on an existing instance.
124
132
  - Action to be executed. The action must not be a create action.
125
133
  - Returns updated instance.
126
134
 
135
+ ##### `invalidateCache()`
136
+
137
+ 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.
138
+
139
+ ##### `invalidateAllCache()` (static)
140
+
141
+ Static method that invalidates the entire object cache across all ObjectStore instances. Use this when you need to force fresh data for all objects.
142
+
127
143
  ### Page Context
128
144
 
129
145
  - [usePageParam](#usepageparamparam)
@@ -11,6 +11,7 @@ export type App = {
11
11
  pages?: Page[];
12
12
  navigation?: NavigationMenu;
13
13
  defaultPages?: Record<string, string>;
14
+ mfa?: 'required' | 'optional';
14
15
  };
15
16
  export type Page = {
16
17
  id: string;
@@ -139,6 +139,7 @@ export type VisibilityConfiguration = {
139
139
  property: string;
140
140
  operator: 'eq' | 'neq';
141
141
  value: string | number | boolean;
142
+ isInstanceProperty?: boolean;
142
143
  }[];
143
144
  };
144
145
  export type RelatedObjectDefaultValue = {
@@ -311,26 +312,89 @@ export type Reference = {
311
312
  name?: string;
312
313
  };
313
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
+ */
314
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;
315
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
+ */
316
335
  export declare class ObjectStore {
317
336
  private services;
318
337
  private objectId;
338
+ private static cache;
319
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
+ */
320
361
  get(options?: ObjectOptions): Promise<ObjWithRoot>;
321
362
  get(cb?: Callback<ObjWithRoot>): void;
322
363
  get(options: ObjectOptions, cb?: Callback<ObjWithRoot>): void;
364
+ /**
365
+ * Finds instances of the object that match the specified filter criteria.
366
+ */
323
367
  findInstances<T extends ObjectInstance = ObjectInstance>(filter?: Filter): Promise<T[]>;
324
368
  findInstances<T extends ObjectInstance = ObjectInstance>(cb: Callback<T[]>): void;
325
369
  findInstances<T extends ObjectInstance = ObjectInstance>(filter: Filter, cb: Callback<T[]>): void;
370
+ /**
371
+ * Retrieves a specific instance of the object by ID.
372
+ */
326
373
  getInstance<T extends ObjectInstance = ObjectInstance>(id: string): Promise<T>;
327
374
  getInstance<T extends ObjectInstance = ObjectInstance>(id: string, cb: Callback<T>): void;
375
+ /**
376
+ * Retrieves the history of an instance of the object.
377
+ */
328
378
  getInstanceHistory(id: string): Promise<History[]>;
329
379
  getInstanceHistory(id: string, cb: Callback<History[]>): void;
380
+ /**
381
+ * Creates a new instance of the object.
382
+ */
330
383
  newInstance<T extends ObjectInstance = ObjectInstance>(input: ActionRequest): Promise<T>;
331
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
+ */
332
388
  instanceAction<T extends ObjectInstance = ObjectInstance>(id: string, input: ActionRequest): Promise<T>;
333
389
  instanceAction<T extends ObjectInstance = ObjectInstance>(id: string, input: ActionRequest, cb: Callback<T>): void;
334
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
+ */
335
399
  export declare function useObject(objectId: string): ObjectStore;
336
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-dev.0",
3
+ "version": "1.1.0-dev.2",
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"