fng-ui-select 0.12.0-beta.191 → 0.12.0-beta.192a

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fng-ui-select",
3
3
  "author": "Mark Chapman <support@forms-angular.org>",
4
- "version": "0.12.0-beta.191",
4
+ "version": "0.12.0-beta.192a",
5
5
  "description": "angular-ui-select plugin for forms-angular",
6
6
  "repository": {
7
7
  "type": "git",
@@ -5,7 +5,39 @@
5
5
 
6
6
  uiSelectModule.factory('uiSelectHelper', ['$rootScope', '$q', 'SubmissionsService', function ($rootScope, $q, SubmissionsService) {
7
7
  var lastW, lastH;
8
- var localLookups = {};
8
+ const localLookups = {};
9
+ // this ultra-simple cache stores { [id: string]: stringValue } as a means of avoiding unnecessary network round-trips, and the UI refresh
10
+ // lagging caused by the promises used to make them.
11
+ // there is no cache life-span because all we're storing here is the concatenation of records' list fields, and the chances of those
12
+ // changing while present in this cache is miniscule. even if they did change, displaying a stale record description in a picklist
13
+ // would surely not cause any great concern.
14
+ // there is no cache size limit because we're storing tiny amounts of data.
15
+ // NB: this cache assumes that record ids are unique across all resources (as is the case with a standard MongoDB back-end).
16
+ // TODO: provide an alternative implementation that includes the resourceName in the cache ids to avoid this limitation.
17
+ let valueCache = {};
18
+ // we're going to populate the select with this value whenever a SubmissionsService.getListAttributes(...) call is rejected. because we're
19
+ // only asking for list fields, permissions should not be the cause of failures, making "record not found" (where the foreign key
20
+ // reference has been broken) by far the most likely explanation.
21
+ // TODO: interpret the actual error condition and translate this into a user-friendly message
22
+ const RECORD_NOT_FOUND = "record not found";
23
+
24
+ function useCacheOrLookItUp(resourceName, id, cb) {
25
+ if (valueCache[id]) {
26
+ cb(valueCache[id]);
27
+ } else
28
+ var text;
29
+ SubmissionsService.getListAttributes(resourceName, id)
30
+ .then((response) => {
31
+ text = response.data.list;
32
+ })
33
+ .catch(() => {
34
+ text = RECORD_NOT_FOUND; // cache this as well as there's no point in repeatedly performing a failing lookup
35
+ })
36
+ .finally(() => {
37
+ cb(text);
38
+ valueCache[id] = text;
39
+ })
40
+ }
9
41
  return {
10
42
  windowChanged: function (w, h) {
11
43
  var result = false;
@@ -19,41 +51,45 @@
19
51
  addClientLookup: function (lkpName, lkpData) {
20
52
  localLookups[lkpName] = lkpData;
21
53
  },
54
+ clearCache: function () {
55
+ valueCache = {};
56
+ },
22
57
  lookupFunc: function (value, formSchema, cb) {
23
58
  if (formSchema.array) {
24
- // TODO extend back end to do multiple lookups in one hit
25
-
26
- var promises = [];
27
- var results = [];
28
- angular.forEach(value, function (obj) {
29
- promises.push(SubmissionsService.getListAttributes(formSchema.ref, obj.x));
59
+ const promises = value.map((value) => {
60
+ const id = value.x?.id || value.x; // if it's already been converted, throw away the result of the previous conversion
61
+ if (!id) {
62
+ return $q.resolve({ data: { list: ""} }); // nothing to convert
63
+ } else if (valueCache[id]) {
64
+ return $q.resolve({ data: { list: valueCache[id] }}); // already cached
65
+ } else {
66
+ return SubmissionsService.getListAttributes(formSchema.ref, id); // need to look it up
67
+ }
30
68
  });
31
- $q.all(promises).then(function (responses) {
32
- angular.forEach(responses, function (response) {
33
- results.push({x: {id: value.shift().x, text: response.data.list}});
69
+ // TODO: no error handling here
70
+ // TODO: perform lookups of cache misses using a single round-trip
71
+ $q.all(promises).then((responses) => {
72
+ const results = responses.map((response) => {
73
+ let id = value.shift().x;
74
+ id = id.id || id; // in case it's already been converted
75
+ const text = response.data.list
76
+ valueCache[id] = text; // cache it for next time
77
+ return { id, text }
34
78
  });
35
79
  cb(formSchema, results);
36
80
  setTimeout(function () {
37
81
  $rootScope.$digest();
38
82
  });
39
83
  });
84
+ } else if (typeof value !== "string") {
85
+ cb(formSchema, value); // already converted
40
86
  } else if (formSchema.fngUiSelect.deriveOptions) {
41
- var retVal;
42
- if (typeof value === 'string') {
43
- var obj = localLookups[formSchema.fngUiSelect.deriveOptions].find(function (test) {
44
- return test.id == value
45
- });
46
- retVal = {id: value, text: obj ? obj.text : ''}
47
- } else {
48
- retVal = value;
49
- }
50
- cb(formSchema, retVal);
87
+ const obj = localLookups[formSchema.fngUiSelect.deriveOptions].find((test) => test.id === value);
88
+ cb(formSchema, { id: value, text: obj?.text || "" });
51
89
  } else {
52
- SubmissionsService.getListAttributes(formSchema.ref, value).then(function (response) {
53
- cb(formSchema, {id: value, text: response.data.list});
54
- });
90
+ useCacheOrLookItUp(formSchema.ref, value, (text) => { cb(formSchema, { id: value, text }) });
55
91
  }
56
- }
92
+ },
57
93
  };
58
94
  }]);
59
95