chore(runtime): Add some JSDocs (#81)

This commit is contained in:
Eric Tuvesson
2024-11-18 17:01:49 +01:00
committed by GitHub
parent e25155556f
commit 14786b2144
4 changed files with 105 additions and 72 deletions

View File

@@ -1,6 +1,6 @@
"use strict"; 'use strict';
var Model = require("./model"); var Model = require('./model');
// Get and set proxy // Get and set proxy
/*const proxies = {} /*const proxies = {}
@@ -221,48 +221,48 @@ Collection.prototype.toJSON = function() {
}*/ }*/
// ---- // ----
Object.defineProperty(Array.prototype, "items", { Object.defineProperty(Array.prototype, 'items', {
enumerable: false, enumerable: false,
get() { get() {
return this; return this;
}, },
set(data) { set(data) {
this.set(data); this.set(data);
}, }
}); });
Object.defineProperty(Array.prototype, "each", { Object.defineProperty(Array.prototype, 'each', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: Array.prototype.forEach, value: Array.prototype.forEach
}); });
Object.defineProperty(Array.prototype, "size", { Object.defineProperty(Array.prototype, 'size', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function () { value: function () {
return this.length; return this.length;
}, }
}); });
Object.defineProperty(Array.prototype, "get", { Object.defineProperty(Array.prototype, 'get', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (index) { value: function (index) {
return this[index]; return this[index];
}, }
}); });
Object.defineProperty(Array.prototype, "getId", { Object.defineProperty(Array.prototype, 'getId', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function () { value: function () {
return this._id; return this._id;
}, }
}); });
Object.defineProperty(Array.prototype, "id", { Object.defineProperty(Array.prototype, 'id', {
enumerable: false, enumerable: false,
get() { get() {
return this.getId(); return this.getId();
}, }
}); });
Object.defineProperty(Array.prototype, "set", { Object.defineProperty(Array.prototype, 'set', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (src) { value: function (src) {
@@ -323,10 +323,10 @@ Object.defineProperty(Array.prototype, "set", {
for (i = aItems.length; i < bItems.length; i++) { for (i = aItems.length; i < bItems.length; i++) {
this.add(bItems[i]); this.add(bItems[i]);
} }
}, }
}); });
Object.defineProperty(Array.prototype, "notify", { Object.defineProperty(Array.prototype, 'notify', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: async function (event, args) { value: async function (event, args) {
@@ -337,80 +337,80 @@ Object.defineProperty(Array.prototype, "notify", {
for (var i = 0; i < l.length; i++) { for (var i = 0; i < l.length; i++) {
await l[i](args); await l[i](args);
} }
}, }
}); });
Object.defineProperty(Array.prototype, "contains", { Object.defineProperty(Array.prototype, 'contains', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (item) { value: function (item) {
return this.indexOf(item) !== -1; return this.indexOf(item) !== -1;
}, }
}); });
Object.defineProperty(Array.prototype, "add", { Object.defineProperty(Array.prototype, 'add', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: async function (item) { value: async function (item) {
if (this.contains(item)) return; // Already contains item if (this.contains(item)) return; // Already contains item
this.items.push(item); this.items.push(item);
await this.notify("add", { item: item, index: this.items.length - 1 }); await this.notify('add', { item: item, index: this.items.length - 1 });
await this.notify("change"); await this.notify('change');
await item.notify("add", { collection: this }); await item.notify('add', { collection: this });
}, }
}); });
Object.defineProperty(Array.prototype, "remove", { Object.defineProperty(Array.prototype, 'remove', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (item) { value: function (item) {
var idx = this.items.indexOf(item); var idx = this.items.indexOf(item);
if (idx !== -1) this.removeAtIndex(idx); if (idx !== -1) this.removeAtIndex(idx);
}, }
}); });
Object.defineProperty(Array.prototype, "addAtIndex", { Object.defineProperty(Array.prototype, 'addAtIndex', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: async function (item, index) { value: async function (item, index) {
if (this.contains(item)) return; // Already contains item if (this.contains(item)) return; // Already contains item
this.items.splice(index, 0, item); this.items.splice(index, 0, item);
await this.notify("add", { item: item, index: index }); await this.notify('add', { item: item, index: index });
await this.notify("change"); await this.notify('change');
await item.notify("add", { collection: this, index: index }); await item.notify('add', { collection: this, index: index });
}, }
}); });
Object.defineProperty(Array.prototype, "removeAtIndex", { Object.defineProperty(Array.prototype, 'removeAtIndex', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: async function (idx) { value: async function (idx) {
var item = this.items[idx]; var item = this.items[idx];
this.items.splice(idx, 1); this.items.splice(idx, 1);
await this.notify("remove", { item: item, index: idx }); await this.notify('remove', { item: item, index: idx });
await this.notify("change"); await this.notify('change');
await item.notify("remove", { collection: this }); await item.notify('remove', { collection: this });
}, }
}); });
Object.defineProperty(Array.prototype, "on", { Object.defineProperty(Array.prototype, 'on', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (event, listener) { value: function (event, listener) {
if (!this._listeners) if (!this._listeners)
Object.defineProperty(this, "_listeners", { Object.defineProperty(this, '_listeners', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: {}, value: {}
}); });
if (!this._listeners[event]) this._listeners[event] = []; if (!this._listeners[event]) this._listeners[event] = [];
this._listeners[event].push(listener); this._listeners[event].push(listener);
}, }
}); });
Object.defineProperty(Array.prototype, "off", { Object.defineProperty(Array.prototype, 'off', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: function (event, listener) { value: function (event, listener) {
@@ -418,20 +418,20 @@ Object.defineProperty(Array.prototype, "off", {
if (!this._listeners[event]) return; if (!this._listeners[event]) return;
var idx = this._listeners[event].indexOf(listener); var idx = this._listeners[event].indexOf(listener);
if (idx !== -1) this._listeners[event].splice(idx, 1); if (idx !== -1) this._listeners[event].splice(idx, 1);
}, }
}); });
class Collection extends Array {} class Collection extends Array {}
var collections = (Collection._collections = {}); const collections = (Collection._collections = {});
Collection.create = function (items) { Collection.create = function (items) {
const name = Model.guid(); const name = Model.guid();
collections[name] = new Collection(); collections[name] = new Collection();
Object.defineProperty(collections[name], "_id", { Object.defineProperty(collections[name], '_id', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: name, value: name
}); });
if (items) { if (items) {
collections[name].set(items); collections[name].set(items);
@@ -439,14 +439,18 @@ Collection.create = function (items) {
return collections[name]; return collections[name];
}; };
/**
* @param {string} name
* @returns {Collection}
*/
Collection.get = function (name) { Collection.get = function (name) {
if (name === undefined) name = Model.guid(); if (name === undefined) name = Model.guid();
if (!collections[name]) { if (!collections[name]) {
collections[name] = new Collection(); collections[name] = new Collection();
Object.defineProperty(collections[name], "_id", { Object.defineProperty(collections[name], '_id', {
enumerable: false, enumerable: false,
writable: false, writable: false,
value: name, value: name
}); });
} }

View File

@@ -35,6 +35,11 @@ const _modelProxyHandler = {
} }
}; };
/**
*
* @param {*} id
* @returns {Model}
*/
Model.get = function (id) { Model.get = function (id) {
if (id === undefined) id = Model.guid(); if (id === undefined) id = Model.guid();
if (!models[id]) { if (!models[id]) {
@@ -122,13 +127,22 @@ Model.prototype.fill = function (value = null) {
} }
}; };
/**
* @param {string} name
* @param {unknown} value
* @param {{
* resolve?: boolean;
* forceChange?: boolean;
* silent?: boolean;
* }} args
*/
Model.prototype.set = function (name, value, args) { Model.prototype.set = function (name, value, args) {
if (args && args.resolve && name.indexOf('.') !== -1) { if (args && args.resolve && name.indexOf('.') !== -1) {
// We should resolve path references // We should resolve path references
var path = name.split('.'); const path = name.split('.');
var model = this; let model = this;
for (var i = 0; i < path.length - 1; i++) { for (let i = 0; i < path.length - 1; i++) {
var v = model.get(path[i]); const v = model.get(path[i]);
if (Model.instanceOf(v)) model = v; if (Model.instanceOf(v)) model = v;
else return; // Path resolve failed else return; // Path resolve failed
} }
@@ -138,24 +152,35 @@ Model.prototype.set = function (name, value, args) {
const forceChange = args && args.forceChange; const forceChange = args && args.forceChange;
var oldValue = this.data[name]; const oldValue = this.data[name];
this.data[name] = value; this.data[name] = value;
(forceChange || oldValue !== value) &&
(!args || !args.silent) && if ((forceChange || oldValue !== value) && (!args || !args.silent)) {
this.notify('change', { name: name, value: value, old: oldValue }); this.notify('change', { name: name, value: value, old: oldValue });
}
}; };
/**
* @returns {string}
*/
Model.prototype.getId = function () { Model.prototype.getId = function () {
return this.id; return this.id;
}; };
/**
* @param {string} name
* @param {{
* resolve?: boolean;
* }} args
* @returns {unknown}
*/
Model.prototype.get = function (name, args) { Model.prototype.get = function (name, args) {
if (args && args.resolve && name.indexOf('.') !== -1) { if (args && args.resolve && name.indexOf('.') !== -1) {
// We should resolve path references // We should resolve path references
var path = name.split('.'); const path = name.split('.');
var model = this; let model = this;
for (var i = 0; i < path.length - 1; i++) { for (let i = 0; i < path.length - 1; i++) {
var v = model.get(path[i]); const v = model.get(path[i]);
if (Model.instanceOf(v)) model = v; if (Model.instanceOf(v)) model = v;
else return; // Path resolve failed else return; // Path resolve failed
} }

View File

@@ -40,8 +40,8 @@ const ClosePopupNode = {
this._internal.closeCallback = cb; this._internal.closeCallback = cb;
}, },
scheduleClose: function () { scheduleClose: function () {
var _this = this; const _this = this;
var internal = this._internal; const internal = this._internal;
if (!internal.hasScheduledClose) { if (!internal.hasScheduledClose) {
internal.hasScheduledClose = true; internal.hasScheduledClose = true;
this.scheduleAfterInputsHaveUpdated(function () { this.scheduleAfterInputsHaveUpdated(function () {
@@ -113,9 +113,8 @@ module.exports = {
var closeActions = node.parameters['closeActions']; var closeActions = node.parameters['closeActions'];
if (closeActions) { if (closeActions) {
closeActions = closeActions ? closeActions.split(',') : undefined; closeActions = closeActions ? closeActions.split(',') : undefined;
for (var i in closeActions) { for (const i in closeActions) {
var p = closeActions[i]; const p = closeActions[i];
ports.push({ ports.push({
type: 'signal', type: 'signal',
plug: 'input', plug: 'input',

View File

@@ -10,8 +10,7 @@ const SetVariableNodeDefinition = {
usePortAsLabel: 'name', usePortAsLabel: 'name',
color: 'data', color: 'data',
initialize: function () { initialize: function () {
var internal = this._internal; const internal = this._internal;
internal.variablesModel = Model.get('--ndl--global-variables'); internal.variablesModel = Model.get('--ndl--global-variables');
}, },
getInspectInfo() { getInspectInfo() {
@@ -79,17 +78,22 @@ const SetVariableNodeDefinition = {
if (this.hasScheduledStore) return; if (this.hasScheduledStore) return;
this.hasScheduledStore = true; this.hasScheduledStore = true;
var internal = this._internal; const internal = this._internal;
this.scheduleAfterInputsHaveUpdated(function () { this.scheduleAfterInputsHaveUpdated(function () {
this.hasScheduledStore = false; this.hasScheduledStore = false;
var value = internal.setWith === 'emptyString' ? '' : internal.value; let value = internal.setWith === 'emptyString' ? '' : internal.value;
// Can set arrays with "id" or array
if (internal.setWith === 'object' && typeof value === 'string') value = Model.get(value);
// Can set arrays with "id" or array
if (internal.setWith === 'array' && typeof value === 'string') value = Collection.get(value);
if (internal.setWith === 'object' && typeof value === 'string') value = Model.get(value); // Can set arrays with "id" or array
if (internal.setWith === 'array' && typeof value === 'string') value = Collection.get(value); // Can set arrays with "id" or array
if (internal.setWith === 'boolean') value = !!value; if (internal.setWith === 'boolean') value = !!value;
//use forceChange to always trigger Variable nodes to send the value on their output, even if it's the same value twice // use forceChange to always trigger Variable nodes to send the value on
// their output, even if it's the same value twice
internal.variablesModel.set(internal.name, value, { internal.variablesModel.set(internal.name, value, {
forceChange: true forceChange: true
}); });
@@ -101,12 +105,13 @@ const SetVariableNodeDefinition = {
return; return;
} }
if (name === 'value') if (name === 'value') {
this.registerInput(name, { this.registerInput(name, {
set: this.setValue.bind(this) set: this.setValue.bind(this)
}); });
} }
} }
}
}; };
module.exports = { module.exports = {