@nymphjs/client 1.0.0-beta.13 → 1.0.0-beta.131

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +552 -0
  2. package/README.md +12 -8
  3. package/asyncitertest.js +53 -0
  4. package/dist/Entity.d.ts +158 -0
  5. package/dist/Entity.js +643 -0
  6. package/dist/Entity.js.map +1 -0
  7. package/dist/Entity.types.d.ts +216 -0
  8. package/dist/Entity.types.js +2 -0
  9. package/{lib → dist}/EntityWeakCache.d.ts +1 -1
  10. package/{lib → dist}/EntityWeakCache.js +5 -9
  11. package/dist/EntityWeakCache.js.map +1 -0
  12. package/{lib → dist}/HttpRequester.d.ts +18 -4
  13. package/dist/HttpRequester.js +365 -0
  14. package/dist/HttpRequester.js.map +1 -0
  15. package/{lib → dist}/Nymph.d.ts +48 -11
  16. package/{lib → dist}/Nymph.js +172 -56
  17. package/dist/Nymph.js.map +1 -0
  18. package/dist/Nymph.types.d.ts +227 -0
  19. package/dist/Nymph.types.js +45 -0
  20. package/dist/Nymph.types.js.map +1 -0
  21. package/{lib → dist}/PubSub.d.ts +16 -10
  22. package/{lib → dist}/PubSub.js +207 -135
  23. package/dist/PubSub.js.map +1 -0
  24. package/{lib → dist}/PubSub.types.d.ts +19 -6
  25. package/dist/PubSub.types.js +2 -0
  26. package/{lib → dist}/entityRefresh.d.ts +1 -1
  27. package/{lib → dist}/entityRefresh.js +21 -13
  28. package/dist/entityRefresh.js.map +1 -0
  29. package/dist/index.d.ts +13 -0
  30. package/dist/index.js +13 -2
  31. package/dist/index.js.map +1 -1
  32. package/{lib → dist}/utils.d.ts +2 -1
  33. package/dist/utils.js +145 -0
  34. package/dist/utils.js.map +1 -0
  35. package/jest.config.js +31 -5
  36. package/package.json +36 -32
  37. package/src/Entity.ts +361 -247
  38. package/src/Entity.types.ts +37 -57
  39. package/src/EntityWeakCache.ts +8 -6
  40. package/src/HttpRequester.ts +268 -31
  41. package/src/Nymph.ts +203 -96
  42. package/src/Nymph.types.ts +98 -2
  43. package/src/PubSub.ts +264 -181
  44. package/src/PubSub.types.ts +33 -19
  45. package/src/entityRefresh.ts +6 -6
  46. package/src/index.ts +10 -10
  47. package/src/utils.ts +67 -14
  48. package/tsconfig.json +7 -4
  49. package/typedoc.json +4 -0
  50. package/dist/index.js.LICENSE.txt +0 -8
  51. package/lib/Entity.d.ts +0 -51
  52. package/lib/Entity.js +0 -500
  53. package/lib/Entity.js.map +0 -1
  54. package/lib/Entity.types.d.ts +0 -65
  55. package/lib/Entity.types.js +0 -3
  56. package/lib/EntityWeakCache.js.map +0 -1
  57. package/lib/HttpRequester.js +0 -190
  58. package/lib/HttpRequester.js.map +0 -1
  59. package/lib/Nymph.js.map +0 -1
  60. package/lib/Nymph.types.d.ts +0 -98
  61. package/lib/Nymph.types.js +0 -3
  62. package/lib/Nymph.types.js.map +0 -1
  63. package/lib/PubSub.js.map +0 -1
  64. package/lib/PubSub.types.js +0 -3
  65. package/lib/entityRefresh.js.map +0 -1
  66. package/lib/index.d.ts +0 -13
  67. package/lib/index.js +0 -34
  68. package/lib/index.js.map +0 -1
  69. package/lib/utils.js +0 -86
  70. package/lib/utils.js.map +0 -1
  71. package/webpack.config.js +0 -28
  72. /package/{lib → dist}/Entity.types.js.map +0 -0
  73. /package/{lib → dist}/PubSub.types.js.map +0 -0
@@ -1,5 +1,5 @@
1
- import { EntityJson } from './Entity.types';
2
- import { PubSubSubscription } from './PubSub';
1
+ import { EntityJson } from './Entity.types.js';
2
+ import { PubSubSubscription } from './PubSub.js';
3
3
 
4
4
  export type PubSubResolveCallback<T> = (arg: T, event?: string) => void;
5
5
  export type PubSubRejectCallback = (err: any) => void;
@@ -7,29 +7,43 @@ export type PubSubCountCallback = (count: number) => void;
7
7
  export type PubSubCallbacks<T> = [
8
8
  PubSubResolveCallback<T> | undefined,
9
9
  PubSubRejectCallback | undefined,
10
- PubSubCountCallback | undefined
10
+ PubSubCountCallback | undefined,
11
11
  ];
12
- export type PubSubEventType = 'connect' | 'disconnect';
12
+ export type PubSubEventType = 'connect' | 'disconnect' | 'error';
13
13
  export type PubSubConnectCallback = () => void;
14
14
  export type PubSubDisconnectCallback = () => void;
15
+ /**
16
+ * The error event is for unknown errors. Query errors fire their own reject
17
+ * callbacks.
18
+ */
19
+ export type PubSubErrorCallback = (err: any) => void;
20
+ export type PubSubUpdateRemoved = {
21
+ query: string;
22
+ removed: string;
23
+ };
24
+ export type PubSubUpdateAdded = {
25
+ query: string;
26
+ added: string;
27
+ data: EntityJson;
28
+ };
29
+ export type PubSubUpdateUpdated = {
30
+ query: string;
31
+ updated: string;
32
+ data: EntityJson;
33
+ };
34
+ export type PubSubUpdateMultiple = {
35
+ query: string;
36
+ multiple: true;
37
+ messages: (PubSubUpdateRemoved | PubSubUpdateAdded | PubSubUpdateUpdated)[];
38
+ };
15
39
  export type PubSubUpdate<T> =
16
40
  | T
17
- | {
18
- query: string;
19
- removed: string;
20
- }
21
- | {
22
- query: string;
23
- added: string;
24
- data: EntityJson;
25
- }
26
- | {
27
- query: string;
28
- updated: string;
29
- data: EntityJson;
30
- };
41
+ | PubSubUpdateRemoved
42
+ | PubSubUpdateAdded
43
+ | PubSubUpdateUpdated
44
+ | PubSubUpdateMultiple;
31
45
  export type PubSubSubscribable<T> = (
32
46
  resolve?: PubSubResolveCallback<T> | undefined,
33
47
  reject?: PubSubRejectCallback | undefined,
34
- count?: PubSubCountCallback | undefined
48
+ count?: PubSubCountCallback | undefined,
35
49
  ) => PubSubSubscription<T>;
@@ -1,4 +1,4 @@
1
- import Entity from './Entity';
1
+ import Entity from './Entity.js';
2
2
 
3
3
  // Save referenced entities in an entity's data.
4
4
  export function saveEntities(entity: Entity) {
@@ -14,7 +14,7 @@ export function saveEntities(entity: Entity) {
14
14
 
15
15
  const addEntitiesToObject = (
16
16
  item: any,
17
- entitiesObject: { [k: string]: Entity }
17
+ entitiesObject: { [k: string]: Entity },
18
18
  ) => {
19
19
  // @ts-ignore: Accessing a private property.
20
20
  if (item instanceof Entity && !item.$isASleepingReference) {
@@ -37,7 +37,7 @@ const addEntitiesToObject = (
37
37
  // after the restore.
38
38
  export function restoreEntities(
39
39
  entity: Entity,
40
- savedEntities: { [k: string]: Entity }
40
+ savedEntities: { [k: string]: Entity },
41
41
  ) {
42
42
  let data: { containsSleepingReference: boolean } = {
43
43
  containsSleepingReference: false,
@@ -49,7 +49,7 @@ export function restoreEntities(
49
49
  entity.$dataStore[key] = retoreEntitiesFromObject(
50
50
  value,
51
51
  savedEntities,
52
- data
52
+ data,
53
53
  );
54
54
  }
55
55
 
@@ -59,7 +59,7 @@ export function restoreEntities(
59
59
  const retoreEntitiesFromObject = <T extends any>(
60
60
  item: T,
61
61
  entitiesObject: { [k: string]: Entity },
62
- data: { containsSleepingReference: boolean }
62
+ data: { containsSleepingReference: boolean },
63
63
  ): T => {
64
64
  if (item instanceof Entity) {
65
65
  // @ts-ignore: Accessing a private property.
@@ -78,7 +78,7 @@ const retoreEntitiesFromObject = <T extends any>(
78
78
  } else if (Array.isArray(item)) {
79
79
  // Recurse into lower arrays.
80
80
  return item.map((item) =>
81
- retoreEntitiesFromObject(item, entitiesObject, data)
81
+ retoreEntitiesFromObject(item, entitiesObject, data),
82
82
  ) as T;
83
83
  } else if (item instanceof Object) {
84
84
  for (let [key, value] of Object.entries(item)) {
package/src/index.ts CHANGED
@@ -1,19 +1,19 @@
1
- export * from './Entity';
2
- import Entity from './Entity';
1
+ export * from './Entity.js';
2
+ import Entity from './Entity.js';
3
3
  export { Entity };
4
4
 
5
- export * from './Entity.types';
5
+ export * from './Entity.types.js';
6
6
 
7
- export * from './HttpRequester';
7
+ export * from './HttpRequester.js';
8
8
 
9
- export * from './Nymph';
10
- import Nymph from './Nymph';
9
+ export * from './Nymph.js';
10
+ import Nymph from './Nymph.js';
11
11
  export { Nymph };
12
12
 
13
- export * from './Nymph.types';
13
+ export * from './Nymph.types.js';
14
14
 
15
- export * from './PubSub';
16
- import PubSub from './PubSub';
15
+ export * from './PubSub.js';
16
+ import PubSub from './PubSub.js';
17
17
  export { PubSub };
18
18
 
19
- export * from './PubSub.types';
19
+ export * from './PubSub.types.js';
package/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { EntityReference } from './Entity.types';
2
- import Nymph from './Nymph';
3
- import Entity from './Entity';
1
+ import { EntityReference } from './Entity.types.js';
2
+ import Nymph from './Nymph.js';
3
+ import Entity from './Entity.js';
4
4
 
5
5
  export function uniqueStrings(array: string[]) {
6
6
  const obj: { [k: string]: true } = {};
@@ -11,13 +11,14 @@ export function uniqueStrings(array: string[]) {
11
11
  }
12
12
 
13
13
  export function entityConstructorsToClassNames(item: any): any {
14
- if (
15
- typeof item === 'function' &&
16
- item.prototype instanceof Entity &&
17
- typeof item.class === 'string'
18
- ) {
19
- // Convert entity classes to strings.
20
- return item.class;
14
+ if (item == null) {
15
+ return item;
16
+ } else if (typeof item === 'function' && item.prototype instanceof Entity) {
17
+ // Convert entity classes to class references.
18
+ return ['nymph_class_reference', item.class];
19
+ } else if (item instanceof Entity) {
20
+ // Don't touch entities.
21
+ return item;
21
22
  } else if (Array.isArray(item)) {
22
23
  // Recurse into lower arrays.
23
24
  return item.map((entry) => entityConstructorsToClassNames(entry));
@@ -32,10 +33,52 @@ export function entityConstructorsToClassNames(item: any): any {
32
33
  return item;
33
34
  }
34
35
 
36
+ export function classNamesToEntityConstructors<T extends any>(
37
+ nymph: Nymph,
38
+ item: T,
39
+ ): T {
40
+ if (item == null) {
41
+ return item;
42
+ } else if (
43
+ Array.isArray(item) &&
44
+ item.length === 2 &&
45
+ item[0] === 'nymph_class_reference' &&
46
+ typeof item[1] === 'string'
47
+ ) {
48
+ // Convert class references to entity classes.
49
+ const EntityClass = nymph.getEntityClass(item[1]);
50
+ return EntityClass as T;
51
+ } else if (item instanceof Entity) {
52
+ // Don't touch entities.
53
+ return item;
54
+ } else if (typeof item === 'function' && item.prototype instanceof Entity) {
55
+ // Don't touch Entity classes.
56
+ return item;
57
+ } else if (Array.isArray(item)) {
58
+ // Recurse into lower arrays.
59
+ return item.map((entry) =>
60
+ classNamesToEntityConstructors(nymph, entry),
61
+ ) as T;
62
+ } else if (item instanceof Object) {
63
+ let newObj = Object.create(item);
64
+ for (let [key, value] of Object.entries(item)) {
65
+ newObj[key] = classNamesToEntityConstructors(nymph, value);
66
+ }
67
+ return newObj;
68
+ }
69
+ // Not an entity or array, just return it.
70
+ return item;
71
+ }
72
+
35
73
  export function entitiesToReferences(item: any): any {
36
- if (item instanceof Entity && typeof item.$toReference === 'function') {
74
+ if (item == null) {
75
+ return item;
76
+ } else if (item instanceof Entity) {
37
77
  // Convert entities to references.
38
78
  return item.$toReference();
79
+ } else if (typeof item === 'function' && item.prototype instanceof Entity) {
80
+ // Don't touch Entity classes.
81
+ return item;
39
82
  } else if (Array.isArray(item)) {
40
83
  // Recurse into lower arrays.
41
84
  return item.map((entry) => entitiesToReferences(entry));
@@ -51,7 +94,9 @@ export function entitiesToReferences(item: any): any {
51
94
  }
52
95
 
53
96
  export function referencesToEntities(item: any, nymph: Nymph): any {
54
- if (Array.isArray(item)) {
97
+ if (item == null) {
98
+ return item;
99
+ } else if (Array.isArray(item)) {
55
100
  // Check if it's a reference.
56
101
  if (item[0] === 'nymph_entity_reference') {
57
102
  try {
@@ -64,10 +109,18 @@ export function referencesToEntities(item: any, nymph: Nymph): any {
64
109
  // Recurse into lower arrays.
65
110
  return item.map((item) => referencesToEntities(item, nymph));
66
111
  }
67
- } else if (Entity && item instanceof Object && !(item instanceof Entity)) {
112
+ } else if (item instanceof Entity) {
113
+ // Don't touch entities.
114
+ return item;
115
+ } else if (typeof item === 'function' && item.prototype instanceof Entity) {
116
+ // Don't touch Entity classes.
117
+ return item;
118
+ } else if (item instanceof Object) {
119
+ let newObj = Object.create(item);
68
120
  for (let [key, value] of Object.entries(item)) {
69
- item[key] = referencesToEntities(value, nymph);
121
+ newObj[key] = referencesToEntities(value, nymph);
70
122
  }
123
+ return newObj;
71
124
  }
72
125
  // Not an array, just return it.
73
126
  return item;
package/tsconfig.json CHANGED
@@ -2,12 +2,15 @@
2
2
  "extends": "@tsconfig/recommended/tsconfig.json",
3
3
 
4
4
  "compilerOptions": {
5
- "lib": ["DOM", "ES2021"],
6
- "target": "ES2021",
5
+ "types": ["jest"],
6
+ "module": "nodenext",
7
+ "moduleResolution": "nodenext",
8
+ "lib": ["DOM", "es2024"],
9
+ "target": "es2024",
7
10
  "noImplicitAny": true,
8
- "removeComments": true,
11
+ "removeComments": false,
9
12
  "sourceMap": true,
10
- "outDir": "lib",
13
+ "outDir": "dist",
11
14
  "resolveJsonModule": true,
12
15
  "rootDir": "src/",
13
16
  "declaration": true
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../typedoc.base.json"],
3
+ "entryPoints": ["src/index.ts"]
4
+ }
@@ -1,8 +0,0 @@
1
- /**
2
- * @license
3
- * Lodash <https://lodash.com/>
4
- * Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
5
- * Released under MIT license <https://lodash.com/license>
6
- * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
7
- * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
8
- */
package/lib/Entity.d.ts DELETED
@@ -1,51 +0,0 @@
1
- import type Nymph from './Nymph';
2
- import { EntityConstructor, EntityData, EntityInterface, EntityJson, EntityPatch, EntityReference } from './Entity.types';
3
- export default class Entity<T extends EntityData = EntityData> implements EntityInterface {
4
- static nymph: Nymph;
5
- static class: string;
6
- $nymph: Nymph;
7
- guid: string | null;
8
- cdate: number | null;
9
- mdate: number | null;
10
- tags: string[];
11
- protected $originalTags: string[];
12
- protected $dirty: {
13
- [k: string]: boolean;
14
- };
15
- protected $dataHandler: Object;
16
- protected $dataStore: T;
17
- protected $data: T;
18
- protected $isASleepingReference: boolean;
19
- protected $sleepingReference: EntityReference | null;
20
- protected $readyPromise: Promise<Entity<T>> | null;
21
- constructor(guid?: string);
22
- static factory(guid?: string): Promise<Entity<EntityData>>;
23
- static factorySync(guid?: string): Entity<EntityData>;
24
- static factoryReference(reference: EntityReference): Entity<EntityData>;
25
- static serverCallStatic(method: string, params: Iterable<any>): Promise<any>;
26
- toJSON(): EntityJson<EntityConstructor>;
27
- $init(entityJson: EntityJson | null): Entity<T>;
28
- $addTag(...tags: string[]): void;
29
- $arraySearch(array: any[], strict?: boolean): number;
30
- $delete(): Promise<boolean>;
31
- $equals(object: any): boolean;
32
- $getPatch(): EntityPatch;
33
- $hasTag(...tags: string[]): boolean;
34
- $inArray(array: any[], strict?: boolean): boolean;
35
- $is(object: any): boolean;
36
- $patch(): Promise<boolean>;
37
- $ready(): Promise<Entity<T>>;
38
- $readyAll(level?: number): Promise<Entity<T>>;
39
- protected $referenceSleep(reference: EntityReference): void;
40
- $refresh(): Promise<boolean | 0>;
41
- $removeTag(...tags: string[]): void;
42
- $save(): Promise<boolean>;
43
- $serverCall(method: string, params: Iterable<any>, stateless?: boolean): Promise<any>;
44
- $toReference(): EntityReference | EntityInterface;
45
- }
46
- export declare class EntityIsSleepingReferenceError extends Error {
47
- constructor(message: string);
48
- }
49
- export declare class InvalidStateError extends Error {
50
- constructor(message: string);
51
- }