@searchspring/snap-toolbox 0.46.0 → 0.48.0

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.
@@ -1 +1 @@
1
- {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,aAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAEF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAsEzG"}
1
+ {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,aAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAEF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAwEzG"}
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
14
  exports.getContext = void 0;
4
15
  function getContext(evaluate, script) {
@@ -27,24 +38,21 @@ function getContext(evaluate, script) {
27
38
  throw new Error('getContext: first parameter must be an array of strings');
28
39
  }
29
40
  var siteIdString = 'siteId';
30
- var variables = {};
31
- // evaluate text and put into variables
32
- evaluate === null || evaluate === void 0 ? void 0 : evaluate.forEach(function (name) {
33
- var fn = new Function("\n\t\t\tvar ".concat(evaluate.join(', '), ";\n\t\t\t").concat(scriptElem.innerHTML, "\n\t\t\treturn ").concat(name, ";\n\t\t"));
34
- variables[name] = fn();
35
- });
41
+ var attributeVariables = {};
36
42
  // grab element attributes and put into variables
37
43
  Object.values(scriptElem.attributes).map(function (attr) {
38
44
  var name = attr.nodeName;
39
45
  if (evaluate.includes(name)) {
40
- variables[name] = scriptElem.getAttribute(name);
46
+ attributeVariables[name] = scriptElem.getAttribute(name);
41
47
  }
42
48
  });
43
- // remove undefined entries
44
- Object.keys(variables).forEach(function (key) {
45
- if (typeof variables[key] === 'undefined')
46
- delete variables[key];
49
+ var scriptVariables = {};
50
+ // evaluate text and put into variables
51
+ evaluate === null || evaluate === void 0 ? void 0 : evaluate.forEach(function (name) {
52
+ var fn = new Function("\n\t\t\tvar ".concat(evaluate.join(', '), ";\n\t\t\t").concat(scriptElem.innerHTML, "\n\t\t\treturn ").concat(name, ";\n\t\t"));
53
+ scriptVariables[name] = fn();
47
54
  });
55
+ var variables = __assign(__assign({}, removeUndefined(attributeVariables)), removeUndefined(scriptVariables));
48
56
  if (evaluate.includes(siteIdString)) {
49
57
  // if we didnt find a siteId in the context, lets grab the id from the src url.
50
58
  if (!variables[siteIdString]) {
@@ -57,3 +65,10 @@ function getContext(evaluate, script) {
57
65
  return variables;
58
66
  }
59
67
  exports.getContext = getContext;
68
+ function removeUndefined(variables) {
69
+ Object.keys(variables).forEach(function (key) {
70
+ if (typeof variables[key] === 'undefined')
71
+ delete variables[key];
72
+ });
73
+ return variables;
74
+ }
@@ -0,0 +1,10 @@
1
+ export declare type UntilOptions = {
2
+ checkMax: number;
3
+ checkCount: number;
4
+ checkTime: number;
5
+ exponential: number;
6
+ defer: boolean;
7
+ executeFunction: boolean;
8
+ };
9
+ export declare const until: (thing: unknown, customOptions?: Partial<UntilOptions> | undefined) => Promise<unknown>;
10
+ //# sourceMappingURL=until.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"until.d.ts","sourceRoot":"","sources":["../../../src/until/until.ts"],"names":[],"mappings":"AAAA,oBAAY,YAAY,GAAG;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;IACf,eAAe,EAAE,OAAO,CAAC;CACzB,CAAC;AAWF,eAAO,MAAM,KAAK,UAAiB,OAAO,wDAA0C,QAAQ,OAAO,CA8ClG,CAAC"}
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.until = void 0;
51
+ var defaultUntilOptions = {
52
+ checkMax: 25,
53
+ checkCount: 0,
54
+ checkTime: 60,
55
+ exponential: 1.1,
56
+ defer: false,
57
+ executeFunction: true,
58
+ };
59
+ var until = function (thing, customOptions) { return __awaiter(void 0, void 0, void 0, function () {
60
+ var options;
61
+ return __generator(this, function (_a) {
62
+ options = __assign(__assign({}, defaultUntilOptions), (customOptions || {}));
63
+ return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
64
+ var checkForThing, thingCheck, waiting_1;
65
+ return __generator(this, function (_a) {
66
+ switch (_a.label) {
67
+ case 0:
68
+ checkForThing = function (thing) { return __awaiter(void 0, void 0, void 0, function () {
69
+ return __generator(this, function (_a) {
70
+ switch (typeof thing) {
71
+ case 'function': {
72
+ if (options.executeFunction) {
73
+ return [2 /*return*/, thing()];
74
+ }
75
+ return [2 /*return*/, thing];
76
+ }
77
+ default: {
78
+ if (thing) {
79
+ return [2 /*return*/, thing];
80
+ }
81
+ }
82
+ }
83
+ return [2 /*return*/];
84
+ });
85
+ }); };
86
+ return [4 /*yield*/, checkForThing(thing)];
87
+ case 1:
88
+ thingCheck = _a.sent();
89
+ if (thingCheck && !options.defer) {
90
+ resolve(thingCheck);
91
+ }
92
+ else {
93
+ waiting_1 = function () {
94
+ window === null || window === void 0 ? void 0 : window.setTimeout(function () { return __awaiter(void 0, void 0, void 0, function () {
95
+ var thingCheck;
96
+ return __generator(this, function (_a) {
97
+ switch (_a.label) {
98
+ case 0: return [4 /*yield*/, checkForThing(thing)];
99
+ case 1:
100
+ thingCheck = _a.sent();
101
+ if (thingCheck) {
102
+ return [2 /*return*/, resolve(thingCheck)];
103
+ }
104
+ options.checkCount++;
105
+ options.checkTime *= options.exponential;
106
+ if (options.checkCount < options.checkMax) {
107
+ return [2 /*return*/, waiting_1()];
108
+ }
109
+ // timeout reached
110
+ return [2 /*return*/, reject()];
111
+ }
112
+ });
113
+ }); }, options.checkTime);
114
+ };
115
+ waiting_1();
116
+ }
117
+ return [2 /*return*/];
118
+ }
119
+ });
120
+ }); })];
121
+ });
122
+ }); };
123
+ exports.until = until;
@@ -1 +1 @@
1
- {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,aAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAEF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAsEzG"}
1
+ {"version":3,"file":"getContext.d.ts","sourceRoot":"","sources":["../../../src/getContext/getContext.ts"],"names":[],"mappings":"AAAA,aAAK,gBAAgB,GAAG;IACvB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;CACxB,CAAC;AAEF,wBAAgB,UAAU,CAAC,QAAQ,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,CAwEzG"}
@@ -22,7 +22,15 @@ export function getContext(evaluate = [], script) {
22
22
  throw new Error('getContext: first parameter must be an array of strings');
23
23
  }
24
24
  const siteIdString = 'siteId';
25
- const variables = {};
25
+ const attributeVariables = {};
26
+ // grab element attributes and put into variables
27
+ Object.values(scriptElem.attributes).map((attr) => {
28
+ const name = attr.nodeName;
29
+ if (evaluate.includes(name)) {
30
+ attributeVariables[name] = scriptElem.getAttribute(name);
31
+ }
32
+ });
33
+ const scriptVariables = {};
26
34
  // evaluate text and put into variables
27
35
  evaluate?.forEach((name) => {
28
36
  const fn = new Function(`
@@ -30,20 +38,12 @@ export function getContext(evaluate = [], script) {
30
38
  ${scriptElem.innerHTML}
31
39
  return ${name};
32
40
  `);
33
- variables[name] = fn();
34
- });
35
- // grab element attributes and put into variables
36
- Object.values(scriptElem.attributes).map((attr) => {
37
- const name = attr.nodeName;
38
- if (evaluate.includes(name)) {
39
- variables[name] = scriptElem.getAttribute(name);
40
- }
41
- });
42
- // remove undefined entries
43
- Object.keys(variables).forEach((key) => {
44
- if (typeof variables[key] === 'undefined')
45
- delete variables[key];
41
+ scriptVariables[name] = fn();
46
42
  });
43
+ const variables = {
44
+ ...removeUndefined(attributeVariables),
45
+ ...removeUndefined(scriptVariables),
46
+ };
47
47
  if (evaluate.includes(siteIdString)) {
48
48
  // if we didnt find a siteId in the context, lets grab the id from the src url.
49
49
  if (!variables[siteIdString]) {
@@ -55,3 +55,10 @@ export function getContext(evaluate = [], script) {
55
55
  }
56
56
  return variables;
57
57
  }
58
+ function removeUndefined(variables) {
59
+ Object.keys(variables).forEach((key) => {
60
+ if (typeof variables[key] === 'undefined')
61
+ delete variables[key];
62
+ });
63
+ return variables;
64
+ }
@@ -0,0 +1,10 @@
1
+ export declare type UntilOptions = {
2
+ checkMax: number;
3
+ checkCount: number;
4
+ checkTime: number;
5
+ exponential: number;
6
+ defer: boolean;
7
+ executeFunction: boolean;
8
+ };
9
+ export declare const until: (thing: unknown, customOptions?: Partial<UntilOptions> | undefined) => Promise<unknown>;
10
+ //# sourceMappingURL=until.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"until.d.ts","sourceRoot":"","sources":["../../../src/until/until.ts"],"names":[],"mappings":"AAAA,oBAAY,YAAY,GAAG;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;IACf,eAAe,EAAE,OAAO,CAAC;CACzB,CAAC;AAWF,eAAO,MAAM,KAAK,UAAiB,OAAO,wDAA0C,QAAQ,OAAO,CA8ClG,CAAC"}
@@ -0,0 +1,53 @@
1
+ const defaultUntilOptions = {
2
+ checkMax: 25,
3
+ checkCount: 0,
4
+ checkTime: 60,
5
+ exponential: 1.1,
6
+ defer: false,
7
+ executeFunction: true,
8
+ };
9
+ export const until = async (thing, customOptions) => {
10
+ const options = {
11
+ ...defaultUntilOptions,
12
+ ...(customOptions || {}),
13
+ };
14
+ return new Promise(async (resolve, reject) => {
15
+ const checkForThing = async (thing) => {
16
+ switch (typeof thing) {
17
+ case 'function': {
18
+ if (options.executeFunction) {
19
+ return thing();
20
+ }
21
+ return thing;
22
+ }
23
+ default: {
24
+ if (thing) {
25
+ return thing;
26
+ }
27
+ }
28
+ }
29
+ };
30
+ const thingCheck = await checkForThing(thing);
31
+ if (thingCheck && !options.defer) {
32
+ resolve(thingCheck);
33
+ }
34
+ else {
35
+ const waiting = () => {
36
+ window?.setTimeout(async () => {
37
+ const thingCheck = await checkForThing(thing);
38
+ if (thingCheck) {
39
+ return resolve(thingCheck);
40
+ }
41
+ options.checkCount++;
42
+ options.checkTime *= options.exponential;
43
+ if (options.checkCount < options.checkMax) {
44
+ return waiting();
45
+ }
46
+ // timeout reached
47
+ return reject();
48
+ }, options.checkTime);
49
+ };
50
+ waiting();
51
+ }
52
+ });
53
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@searchspring/snap-toolbox",
3
- "version": "0.46.0",
3
+ "version": "0.48.0",
4
4
  "description": "Snap Toolbox",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -23,5 +23,5 @@
23
23
  "files": [
24
24
  "dist/**/*"
25
25
  ],
26
- "gitHead": "b92fc5fe0dd848d4ccdd6242dce7a70be53cb99c"
26
+ "gitHead": "d6b208d3109772af36cefc76ad584b8f90a817fd"
27
27
  }