@duet3d/objectmodel 3.4.0-b6 → 3.4.0-b9

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.
@@ -24,3 +24,14 @@ export declare class ModelCollection<T extends IModelObject> extends Array<T> im
24
24
  update(jsonElement: any): IModelObject | null;
25
25
  }
26
26
  export default ModelCollection;
27
+ /**
28
+ * Initialize a model collection from the given data
29
+ * @param itemType Item type to create
30
+ * @param data Data to assign
31
+ * @returns Initialized model collection
32
+ */
33
+ export declare function initCollection<T extends IModelObject>(itemType: {
34
+ new (): T;
35
+ }, data: Array<{
36
+ [Property in keyof T]?: T[Property];
37
+ }>): ModelCollection<T>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelCollection = void 0;
3
+ exports.initCollection = exports.ModelCollection = void 0;
4
4
  const ModelObject_1 = require("./ModelObject");
5
5
  const index_1 = require("./index");
6
6
  /**
@@ -79,3 +79,19 @@ class ModelCollection extends Array {
79
79
  }
80
80
  exports.ModelCollection = ModelCollection;
81
81
  exports.default = ModelCollection;
82
+ /**
83
+ * Initialize a model collection from the given data
84
+ * @param itemType Item type to create
85
+ * @param data Data to assign
86
+ * @returns Initialized model collection
87
+ */
88
+ function initCollection(itemType, data) {
89
+ const result = new ModelCollection(itemType);
90
+ for (let presetItem of data) {
91
+ const item = new itemType();
92
+ item.update(presetItem);
93
+ result.push(item);
94
+ }
95
+ return result;
96
+ }
97
+ exports.initCollection = initCollection;
@@ -27,3 +27,15 @@ export declare class ModelDictionary<T> extends Map<string, T | null> implements
27
27
  update(jsonElement: any): IModelObject | null;
28
28
  }
29
29
  export default ModelDictionary;
30
+ /**
31
+ * Initialize a model dictionary from the given data
32
+ * @param nullDeletesKeys Defines whether setting values to null deletes the corresponding key
33
+ * @param itemConstructor Item constructor
34
+ * @param data Data to assign
35
+ * @returns Initialized model dictionary
36
+ */
37
+ export declare function initDictionary<T>(nullDeletesKeys: boolean, itemConstructor: {
38
+ new (): T;
39
+ }, data: Record<string, {
40
+ [Property in keyof T]?: T[Property];
41
+ }>): ModelDictionary<T>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelDictionary = void 0;
3
+ exports.initDictionary = exports.ModelDictionary = void 0;
4
4
  const ModelObject_1 = require("./ModelObject");
5
5
  /**
6
6
  * Dictionary class to map object model data
@@ -67,3 +67,25 @@ class ModelDictionary extends Map {
67
67
  }
68
68
  exports.ModelDictionary = ModelDictionary;
69
69
  exports.default = ModelDictionary;
70
+ /**
71
+ * Initialize a model dictionary from the given data
72
+ * @param nullDeletesKeys Defines whether setting values to null deletes the corresponding key
73
+ * @param itemConstructor Item constructor
74
+ * @param data Data to assign
75
+ * @returns Initialized model dictionary
76
+ */
77
+ function initDictionary(nullDeletesKeys, itemConstructor, data) {
78
+ const result = new ModelDictionary(nullDeletesKeys, itemConstructor);
79
+ for (let key in data) {
80
+ const item = new itemConstructor();
81
+ if ((0, ModelObject_1.isModelObject)(item)) {
82
+ item.update(data[key]);
83
+ result.set(key, item);
84
+ }
85
+ else {
86
+ result.set(key, data[key]);
87
+ }
88
+ }
89
+ return result;
90
+ }
91
+ exports.initDictionary = initDictionary;
@@ -33,15 +33,15 @@ export declare abstract class ModelObject implements IModelObject {
33
33
  new (): T;
34
34
  }): void;
35
35
  }
36
+ export default ModelObject;
36
37
  /**
37
- * Initialize a class item from the given data
38
- * @param itemType Item type to create
38
+ * Initialize a model object from the given data
39
+ * @param itemType Model type to create
39
40
  * @param data Data to assign
40
- * @returns Initialized item instance
41
+ * @returns Initialized model instance
41
42
  */
42
- export declare function initItem<T>(itemType: {
43
+ export declare function initObject<T>(itemType: {
43
44
  new (): T;
44
45
  }, data: {
45
46
  [Property in keyof T]?: T[Property];
46
47
  }): T;
47
- export default ModelObject;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initItem = exports.ModelObject = exports.isModelObject = void 0;
3
+ exports.initObject = exports.ModelObject = exports.isModelObject = void 0;
4
4
  const index_1 = require("./index");
5
5
  /**
6
6
  * Check whether a given value provides model update functionality
@@ -71,6 +71,25 @@ class ModelObject {
71
71
  console.warn(`Model array ${key} could not be changed because the target type ${typeof value} is invalid`);
72
72
  }
73
73
  }
74
+ else if (prop instanceof Set) {
75
+ if (value instanceof Array || value instanceof Set) {
76
+ // Remove deleted items
77
+ for (let item of new Set(prop)) {
78
+ if (!prop.has(item)) {
79
+ prop.delete(item);
80
+ }
81
+ }
82
+ // Add new items
83
+ for (let item of value) {
84
+ if (!prop.has(item)) {
85
+ prop.add(item);
86
+ }
87
+ }
88
+ }
89
+ else if (process.env.NODE_ENV !== "production") {
90
+ console.warn(`Model set ${key} could not be changed because the target type ${typeof value} is invalid`);
91
+ }
92
+ }
74
93
  else if (prop === null || value === null) {
75
94
  // Unfortunately we cannot do type checks during runtime without excessive extra work and possibly
76
95
  // third-party libraries, so skip them for null values until there is a better solution.
@@ -153,18 +172,24 @@ class ModelObject {
153
172
  }
154
173
  }
155
174
  exports.ModelObject = ModelObject;
175
+ exports.default = ModelObject;
156
176
  /**
157
- * Initialize a class item from the given data
158
- * @param itemType Item type to create
177
+ * Initialize a model object from the given data
178
+ * @param itemType Model type to create
159
179
  * @param data Data to assign
160
- * @returns Initialized item instance
180
+ * @returns Initialized model instance
161
181
  */
162
- function initItem(itemType, data) {
182
+ function initObject(itemType, data) {
163
183
  const result = new itemType();
164
184
  for (let key in data) {
165
- result[key] = data[key];
185
+ const resultKey = result[key];
186
+ if (isModelObject(resultKey)) {
187
+ resultKey.update(data[key]);
188
+ }
189
+ else {
190
+ result[key] = data[key];
191
+ }
166
192
  }
167
193
  return result;
168
194
  }
169
- exports.initItem = initItem;
170
- exports.default = ModelObject;
195
+ exports.initObject = initObject;
@@ -12,6 +12,7 @@ import Job from "./job";
12
12
  import Limits from "./limits";
13
13
  import Message from "./messages";
14
14
  import Move from "./move";
15
+ import Network from "./network";
15
16
  import Plugin from "./plugins";
16
17
  import Scanner from "./scanner";
17
18
  import Sensors from "./sensors";
@@ -33,6 +34,7 @@ export declare class ObjectModel extends ModelObject {
33
34
  readonly limits: Limits;
34
35
  readonly messages: ModelCollection<Message>;
35
36
  readonly move: Move;
37
+ readonly network: Network;
36
38
  readonly plugins: ModelDictionary<Plugin>;
37
39
  readonly scanner: Scanner;
38
40
  readonly sensors: Sensors;
@@ -15,6 +15,7 @@ const job_1 = require("./job");
15
15
  const limits_1 = require("./limits");
16
16
  const messages_1 = require("./messages");
17
17
  const move_1 = require("./move");
18
+ const network_1 = require("./network");
18
19
  const plugins_1 = require("./plugins");
19
20
  const scanner_1 = require("./scanner");
20
21
  const sensors_1 = require("./sensors");
@@ -38,6 +39,7 @@ class ObjectModel extends ModelObject_1.default {
38
39
  this.limits = new limits_1.default();
39
40
  this.messages = new ModelCollection_1.default(messages_1.default); // must be manually cleared after updates
40
41
  this.move = new move_1.default();
42
+ this.network = new network_1.default();
41
43
  this.plugins = new ModelDictionary_1.default(true, plugins_1.default);
42
44
  this.scanner = new scanner_1.default();
43
45
  this.sensors = new sensors_1.default();
@@ -1,7 +1,9 @@
1
1
  import ModelObject, { IModelObject } from "../ModelObject";
2
+ export declare function isDriverId(value: any): value is DriverId;
2
3
  export declare class DriverId extends ModelObject {
3
4
  board: number;
4
5
  driver: number;
6
+ equals(value?: DriverId | null): boolean;
5
7
  update(jsonElement: any): IModelObject | null;
6
8
  toString(): string;
7
9
  }
@@ -1,14 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DriverId = void 0;
3
+ exports.DriverId = exports.isDriverId = void 0;
4
4
  const ModelObject_1 = require("../ModelObject");
5
+ function isDriverId(value) {
6
+ return (value instanceof Object) && (typeof value.board === "number") && (typeof value.driver === "number");
7
+ }
8
+ exports.isDriverId = isDriverId;
5
9
  class DriverId extends ModelObject_1.default {
6
10
  constructor() {
7
11
  super(...arguments);
8
12
  this.board = 0;
9
13
  this.driver = 0;
10
14
  }
15
+ equals(value) {
16
+ return isDriverId(value) && value.board === this.board && value.driver === this.driver;
17
+ }
11
18
  update(jsonElement) {
19
+ if (isDriverId(jsonElement)) {
20
+ this.board = jsonElement.board;
21
+ this.driver = jsonElement.driver;
22
+ }
12
23
  if (typeof jsonElement === "string") {
13
24
  const matches = /(\d+)\.(\d+)/.exec(jsonElement);
14
25
  if (matches !== null) {
@@ -22,7 +22,7 @@ class DeltaKinematics extends KinematicsBase_1.default {
22
22
  this.deltaRadius = 0;
23
23
  this.homedHeight = 0;
24
24
  this.printRadius = 0;
25
- this.towers = new ModelCollection_1.default(DeltaTower);
25
+ this.towers = (0, ModelCollection_1.initCollection)(DeltaTower, [{}, {}, {}]);
26
26
  this.xTilt = 0;
27
27
  this.yTilt = 0;
28
28
  }
package/package.json CHANGED
@@ -1,30 +1,30 @@
1
- {
2
- "name": "@duet3d/objectmodel",
3
- "version": "3.4.0-b6",
4
- "description": "TypeScript implementation of the Duet3D Object Model",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "test": "jest --config jestconfig.json",
9
- "build": "tsc",
10
- "prepare" : "npm run build",
11
- "prepublishOnly" : "npm test"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://github.com/Duet3D/ObjectModel.git"
16
- },
17
- "author": "Christian Hammacher",
18
- "license": "LGPL-2.1",
19
- "bugs": {
20
- "url": "https://github.com/Duet3D/ObjectModel/issues"
21
- },
22
- "homepage": "https://github.com/Duet3D/ObjectModel#readme",
23
- "devDependencies": {
24
- "@types/jest": "^27.0.3",
25
- "jest": "^27.4.5",
26
- "ts-jest": "^27.1.2",
27
- "typescript": "^4.5.2"
28
- },
29
- "files": ["/dist"]
30
- }
1
+ {
2
+ "name": "@duet3d/objectmodel",
3
+ "version": "3.4.0-b9",
4
+ "description": "TypeScript implementation of the Duet3D Object Model",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "test": "jest --config jestconfig.json",
9
+ "build": "tsc",
10
+ "prepare" : "npm run build",
11
+ "prepublishOnly" : "npm test"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/Duet3D/ObjectModel.git"
16
+ },
17
+ "author": "Christian Hammacher",
18
+ "license": "LGPL-2.1",
19
+ "bugs": {
20
+ "url": "https://github.com/Duet3D/ObjectModel/issues"
21
+ },
22
+ "homepage": "https://github.com/Duet3D/ObjectModel#readme",
23
+ "devDependencies": {
24
+ "@types/jest": "^27.0.3",
25
+ "jest": "^27.4.5",
26
+ "ts-jest": "^27.1.2",
27
+ "typescript": "^4.5.2"
28
+ },
29
+ "files": ["/dist"]
30
+ }