@octostar/platform-api 0.5.12 → 0.5.14

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,24 +1,29 @@
1
1
  import { Context, ContextAPISetParams, ContextAPIStaticData, ContextListener } from '@octostar/platform-types';
2
- type ContextAPIContructorParams<ContextData extends Record<string, any>> = ContextAPIStaticData<ContextData> & {
3
- id?: string;
2
+ type ContextAPIConstructorParams<ContextData extends Record<string, any>> = ContextAPIStaticData<ContextData> & {
3
+ targetWindow?: Window;
4
+ channelId?: string;
4
5
  };
5
6
  export declare class ContextAPI<ContextData extends Record<string, any>> implements Context<ContextData> {
6
- readonly static_data: ContextAPIStaticData<ContextData>;
7
- private channel;
7
+ private contextData;
8
+ targetWindow: Window | undefined;
9
+ readonly id: string;
8
10
  private listeners;
9
- private id;
10
- constructor({ id, ...static_data }: ContextAPIContructorParams<ContextData>);
11
- get context(): ContextData;
12
- set context(context: ContextData);
11
+ private postRobotListener;
12
+ private internalChannelId;
13
+ private postRobotEventId;
14
+ constructor({ targetWindow, contextData, channelId }: ContextAPIConstructorParams<ContextData>);
13
15
  get channelId(): string | undefined;
14
16
  set channelId(channelId: string | undefined);
17
+ get context(): ContextData;
18
+ set context(context: ContextData);
15
19
  getContext(): ContextData;
16
20
  setContext(context: ContextAPISetParams<ContextData>): void;
17
21
  notifyListeners(): void;
18
- syncContextToChannel(): void;
22
+ syncContextToChannel(): Promise<void>;
19
23
  subscribeToChanges(contextListener: ContextListener<ContextData>): string;
20
24
  subscribeToChanges(id: string, contextListener: ContextListener<ContextData>): string;
21
25
  unsubscribeFromChanges(id: string): void;
26
+ destroy(): void;
22
27
  }
23
28
  export {};
24
29
  //# sourceMappingURL=ContextAPI.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContextAPI.d.ts","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAq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from './classInstanceToObject';
2
+ import postRobot from 'post-robot';
3
3
  export class ContextAPI {
4
- constructor({ id, ...static_data }) {
5
- this.static_data = static_data;
4
+ constructor({ targetWindow, contextData, channelId }) {
5
+ this.contextData = contextData;
6
6
  this.listeners = {};
7
- this.id = id ?? uuidv4();
8
- this.channelId = static_data.channelId;
7
+ this.id = uuidv4();
8
+ this.targetWindow = targetWindow;
9
+ this.channelId = channelId;
10
+ this.postRobotListener = postRobot.on(this.postRobotEventId, (event) => {
11
+ if (event.data.from !== this.id) {
12
+ this.contextData = { ...this.contextData, ...event.data.contextData };
13
+ }
14
+ });
15
+ }
16
+ get channelId() {
17
+ return this.internalChannelId;
18
+ }
19
+ set channelId(channelId) {
20
+ this.internalChannelId = channelId;
21
+ if (channelId) {
22
+ this.postRobotEventId = `context-api-update::${channelId}`;
23
+ }
9
24
  }
10
25
  get context() {
11
- return this.static_data.context;
26
+ return this.contextData;
12
27
  }
13
28
  set context(context) {
14
- this.static_data.context = context;
29
+ this.contextData = context;
15
30
  this.notifyListeners();
16
31
  this.syncContextToChannel();
17
32
  }
18
- get channelId() {
19
- return this.static_data.channelId;
20
- }
21
- set channelId(channelId) {
22
- this.static_data.channelId = channelId;
23
- const contextChannelId = this.static_data.channelId && `ContextAPI-${this.static_data.channelId}`;
24
- if (!contextChannelId) {
25
- return;
26
- }
27
- this.channel = new BroadcastChannel(contextChannelId);
28
- this.channel.onmessage = (event) => {
29
- if (event.data.from !== this.id) {
30
- this.static_data.context = event.data.context;
31
- }
32
- };
33
- }
34
33
  getContext() {
35
34
  return this.context;
36
35
  }
@@ -41,14 +40,15 @@ export class ContextAPI {
41
40
  notifyListeners() {
42
41
  Object.values(this.listeners).forEach(listener => listener(this.context));
43
42
  }
44
- syncContextToChannel() {
45
- if (!this.channel) {
43
+ async syncContextToChannel() {
44
+ if (!this.targetWindow) {
46
45
  return;
47
46
  }
48
- const context = classInstanceToObject(this.context);
49
- this.channel.postMessage({
47
+ /** Try to exclude functions in the context */
48
+ // const context = classInstanceToObject(this.context);
49
+ await postRobot.send(this.targetWindow, this.postRobotEventId, {
50
50
  from: this.id,
51
- context,
51
+ context: this.context,
52
52
  });
53
53
  }
54
54
  subscribeToChanges(idOrContextListener, contextListenerOrNothing) {
@@ -65,5 +65,8 @@ export class ContextAPI {
65
65
  unsubscribeFromChanges(id) {
66
66
  delete this.listeners[id];
67
67
  }
68
+ destroy() {
69
+ this.postRobotListener.cancel();
70
+ }
68
71
  }
69
72
  //# sourceMappingURL=ContextAPI.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContextAPI.js","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAMhE,MAAM,OAAO,UAAU;IASrB,YAAY,EAAE,EAAE,EAAE,GAAG,WAAW,EAA2C;QACzE,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,MAAM,EAAE,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC,SAAS,CAAC;IACzC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC;IAClC,CAAC;IAED,IAAI,OAAO,CAAC,OAAoB;QAC9B,IAAI,CAAC,WAAW,CAAC,OAAO,GAAG,OAAO,CAAC;QAEnC,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC9B,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC;IACpC,CAAC;IAEC,IAAI,SAAS,CAAC,SAA6B;QACzC,IAAI,CAAC,WAAW,CAAC,SAAS,GAAG,SAAS,CAAC;QACvC,MA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O,EAAE,IAAI,CAAC,OAAO;SACxB,CAAC,CAAC;IACL,CAAC;IASD,kBAAkB,CAChB,mBAA0D,EAC1D,wBAAuD;QAEvD,MAAM,EAAE,GACN,OAAO,mBAAmB,KAAK,QAAQ,CAAC,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QAC3E,MAAM,eAAe,GACnB,OAAO,mBAAmB,KAAK,UAAU;YACvC,CAAC,CAAC,mBAAmB;YACrB,CAAC,CAAC,wBAAwB,CAAC;QAE/B,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QAClD,CAAC;QAED,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,eAAe,CAAC;QACrC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,sBAAsB,CAAC,EAAU;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED,OAAO;QACL,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,CAAC;IAClC,CAAC;CACF"}
@@ -8,5 +8,5 @@ export declare const classInstanceToObject: <T extends {
8
8
  }) => boolean;
9
9
  };
10
10
  methodWrappers?: { [K in keyof T]?: (fn: T[K], ...args: T[K] extends (...args: infer P) => any ? P : never) => T[K] extends (...args: any) => infer R ? R : never; };
11
- }) => T;
11
+ }) => Record<string, any>;
12
12
  //# sourceMappingURL=classInstanceToObject.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"classInstanceToObject.d.ts","sourceRoot":"","sources":["../../src/lib/classInstanceToObject.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,GAAI,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,EACpE,UAAU,CAAC,EACX,UAAU;IACR,OAAO,CAAC,EAAE;QACR,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE;YAAE,MAAM,EA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G,CAsCpB,CAAC"}
@@ -1,44 +1,47 @@
1
+ // Check if an object contains functions deeply
2
+ const containsDeepFunction = (obj) => {
3
+ if (obj === null || obj === undefined) {
4
+ return false;
5
+ }
6
+ if (typeof obj === 'function') {
7
+ return true;
8
+ }
9
+ if (typeof obj !== 'object') {
10
+ return false;
11
+ }
12
+ if (Array.isArray(obj)) {
13
+ return obj.some(item => containsDeepFunction(item));
14
+ }
15
+ return Object.values(obj).some(value => containsDeepFunction(value));
16
+ };
1
17
  export const classInstanceToObject = (instance, options) => {
2
- const obj = {};
3
- // const prototype = Object.getPrototypeOf(instance);
4
- const properties = [
5
- // ...Object.getOwnPropertyNames(instance),
6
- // ...Object.getOwnPropertyNames(prototype),
7
- ];
8
- let currentClass = instance;
9
- while (currentClass && currentClass !== Object) {
10
- // console.log('current class', currentClass); // TODO: test if it omits the Object methods
11
- properties.push(...Object.getOwnPropertyNames(currentClass));
12
- currentClass = Object.getPrototypeOf(currentClass);
18
+ // Handle null or undefined
19
+ if (instance === null || instance === undefined) {
20
+ return instance;
13
21
  }
14
- properties.forEach(property => {
15
- if (['constructor'].includes(property)) {
22
+ // Handle primitive values
23
+ if (typeof instance !== 'object' || instance instanceof Date) {
24
+ return instance;
25
+ }
26
+ // Handle arrays
27
+ if (Array.isArray(instance)) {
28
+ return instance.map(item => classInstanceToObject(item, options));
29
+ }
30
+ // Handle objects
31
+ const obj = {};
32
+ // Get all own properties (data properties, not methods)
33
+ Object.keys(instance).forEach(key => {
34
+ const value = instance[key];
35
+ // Skip functions directly
36
+ if (typeof value === 'function') {
16
37
  return;
17
38
  }
18
- let method = instance[property];
19
- method =
20
- typeof method === 'function' && typeof method.bind === 'function'
21
- ? method.bind(instance)
22
- : method;
23
- if (options?.globals?.canCallAction) {
24
- const originalMethod = method;
25
- method = (...args) => {
26
- const canCall = options.globals.canCallAction({
27
- method: method.name,
28
- class: instance.constructor.name,
29
- });
30
- if (!canCall) {
31
- throw new Error(`No permissions for call: ${method.name}`); // TODO: consider usage of a specific permissions error
32
- }
33
- return originalMethod(...args);
34
- };
35
- }
36
- if (typeof options?.methodWrappers?.[property] === 'function') {
37
- obj[property] = (...args) => options.methodWrappers[property](method, ...args);
38
- }
39
- else {
40
- obj[property] = (...args) => method(...args);
39
+ // Skip properties that contain functions deeply
40
+ if (typeof value === 'object' && value !== null && containsDeepFunction(value)) {
41
+ return;
41
42
  }
43
+ // Recursively convert nested objects
44
+ obj[key] = classInstanceToObject(value, options);
42
45
  });
43
46
  return obj;
44
47
  };
@@ -1 +1 @@
1
- {"version":3,"file":"classInstanceToObject.js","sourceRoot":"","sources":["../../src/lib/classInstanceToObject.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAW,EACX,OAUC,EACE,EAAE;IACL,MAAM,GAAG,GAA4B,EAAE,CAAC;IACxC,qDAAqD;IACrD,MAAM,UAAU,GAAa;IAC3B,2CAA2C;IAC3C,4CAA4C;KAC7C,CAAC;IACF,IAAI,YAAY,GAAG,QAAQ,CAAC;IAC5B,OAAO,YAAY,IAAK,YAAoB,KAAK,MAAM,EAAE,CAAC;QACxD,2FAA2F;QAE3F,UAAU,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,mBAAmB,CAAC,YAAY,CAAC,CAAC,CAAC;QAC7D,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;IACrD,CAAC;IAED,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QAC5B,IAAI,CAAC,aAAa,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;YACvC,OAAO;QACT,CAAC;QAED,IAAI,MAAM,GAAI,QAAgB,CAAC,QAAQ,CAAa,CAAC;QAErD,MAAM;YACJ,OAAO,MAAM,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU;gBAC/D,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;gBACvB,CA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QAE5B,0BAA0B;QAC1B,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;YAChC,OAAO;QACT,CAAC;QAED,gDAAgD;QAChD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,oBAAoB,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/E,OAAO;QACT,CAAC;QAED,qCAAqC;QACrC,GAAG,CAAC,GAAG,CAAC,GAAG,qBAAqB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACb,CAAC,CAAC"}
@@ -7078,11 +7078,15 @@ Object.defineProperty(exports, "__esModule", {
7078
7078
  });
7079
7079
  exports.ContextAPI = void 0;
7080
7080
  var _uuid = require("uuid");
7081
- var _classInstanceToObject = require("./classInstanceToObject");
7082
- var _excluded = ["id"];
7081
+ var _postRobot = _interopRequireDefault(require("post-robot"));
7082
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
7083
7083
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7084
- function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
7085
- function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
7084
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { return Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function c(t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == _typeof(f) && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i["return"] && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(_typeof(r) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = c(GeneratorFunctionPrototype, u, "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function reset(r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function abrupt(t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function complete(t, r) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f; }, finish: function finish(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, "catch": function _catch(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; }
7085
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
7086
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
7087
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
7088
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7089
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
7086
7090
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
7087
7091
  function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
7088
7092
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
@@ -7090,42 +7094,42 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
7090
7094
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7091
7095
  var ContextAPI = exports.ContextAPI = /*#__PURE__*/function () {
7092
7096
  function ContextAPI(_ref) {
7093
- var id = _ref.id,
7094
- static_data = _objectWithoutProperties(_ref, _excluded);
7097
+ var _this = this;
7098
+ var targetWindow = _ref.targetWindow,
7099
+ contextData = _ref.contextData,
7100
+ channelId = _ref.channelId;
7095
7101
  _classCallCheck(this, ContextAPI);
7096
- this.static_data = static_data;
7102
+ this.contextData = contextData;
7097
7103
  this.listeners = {};
7098
- this.id = id !== null && id !== void 0 ? id : (0, _uuid.v4)();
7099
- this.channelId = static_data.channelId;
7104
+ this.id = (0, _uuid.v4)();
7105
+ this.targetWindow = targetWindow;
7106
+ this.channelId = channelId;
7107
+ this.postRobotListener = _postRobot["default"].on(this.postRobotEventId, function (event) {
7108
+ if (event.data.from !== _this.id) {
7109
+ _this.contextData = _objectSpread(_objectSpread({}, _this.contextData), event.data.contextData);
7110
+ }
7111
+ });
7100
7112
  }
7101
7113
  return _createClass(ContextAPI, [{
7102
- key: "context",
7114
+ key: "channelId",
7103
7115
  get: function get() {
7104
- return this.static_data.context;
7116
+ return this.internalChannelId;
7105
7117
  },
7106
- set: function set(context) {
7107
- this.static_data.context = context;
7108
- this.notifyListeners();
7109
- this.syncContextToChannel();
7118
+ set: function set(channelId) {
7119
+ this.internalChannelId = channelId;
7120
+ if (channelId) {
7121
+ this.postRobotEventId = "context-api-update::".concat(channelId);
7122
+ }
7110
7123
  }
7111
7124
  }, {
7112
- key: "channelId",
7125
+ key: "context",
7113
7126
  get: function get() {
7114
- return this.static_data.channelId;
7127
+ return this.contextData;
7115
7128
  },
7116
- set: function set(channelId) {
7117
- var _this = this;
7118
- this.static_data.channelId = channelId;
7119
- var contextChannelId = this.static_data.channelId && "ContextAPI-".concat(this.static_data.channelId);
7120
- if (!contextChannelId) {
7121
- return;
7122
- }
7123
- this.channel = new BroadcastChannel(contextChannelId);
7124
- this.channel.onmessage = function (event) {
7125
- if (event.data.from !== _this.id) {
7126
- _this.static_data.context = event.data.context;
7127
- }
7128
- };
7129
+ set: function set(context) {
7130
+ this.contextData = context;
7131
+ this.notifyListeners();
7132
+ this.syncContextToChannel();
7129
7133
  }
7130
7134
  }, {
7131
7135
  key: "getContext",
@@ -7147,16 +7151,33 @@ var ContextAPI = exports.ContextAPI = /*#__PURE__*/function () {
7147
7151
  }
7148
7152
  }, {
7149
7153
  key: "syncContextToChannel",
7150
- value: function syncContextToChannel() {
7151
- if (!this.channel) {
7152
- return;
7154
+ value: function () {
7155
+ var _syncContextToChannel = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
7156
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
7157
+ while (1) switch (_context.prev = _context.next) {
7158
+ case 0:
7159
+ if (this.targetWindow) {
7160
+ _context.next = 2;
7161
+ break;
7162
+ }
7163
+ return _context.abrupt("return");
7164
+ case 2:
7165
+ _context.next = 4;
7166
+ return _postRobot["default"].send(this.targetWindow, this.postRobotEventId, {
7167
+ from: this.id,
7168
+ context: this.context
7169
+ });
7170
+ case 4:
7171
+ case "end":
7172
+ return _context.stop();
7173
+ }
7174
+ }, _callee, this);
7175
+ }));
7176
+ function syncContextToChannel() {
7177
+ return _syncContextToChannel.apply(this, arguments);
7153
7178
  }
7154
- var context = (0, _classInstanceToObject.classInstanceToObject)(this.context);
7155
- this.channel.postMessage({
7156
- from: this.id,
7157
- context: context
7158
- });
7159
- }
7179
+ return syncContextToChannel;
7180
+ }()
7160
7181
  }, {
7161
7182
  key: "subscribeToChanges",
7162
7183
  value: function subscribeToChanges(idOrContextListener, contextListenerOrNothing) {
@@ -7173,10 +7194,15 @@ var ContextAPI = exports.ContextAPI = /*#__PURE__*/function () {
7173
7194
  value: function unsubscribeFromChanges(id) {
7174
7195
  delete this.listeners[id];
7175
7196
  }
7197
+ }, {
7198
+ key: "destroy",
7199
+ value: function destroy() {
7200
+ this.postRobotListener.cancel();
7201
+ }
7176
7202
  }]);
7177
7203
  }();
7178
7204
 
7179
- },{"./classInstanceToObject":41,"uuid":6}],39:[function(require,module,exports){
7205
+ },{"post-robot":5,"uuid":6}],39:[function(require,module,exports){
7180
7206
  "use strict";
7181
7207
 
7182
7208
  Object.defineProperty(exports, "__esModule", {
@@ -7304,58 +7330,57 @@ Object.defineProperty(exports, "__esModule", {
7304
7330
  value: true
7305
7331
  });
7306
7332
  exports.classInstanceToObject = void 0;
7307
- function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
7308
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7309
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
7310
- function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
7311
- function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
7312
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
7333
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7334
+ // Check if an object contains functions deeply
7335
+ var containsDeepFunction = function containsDeepFunction(obj) {
7336
+ if (obj === null || obj === undefined) {
7337
+ return false;
7338
+ }
7339
+ if (typeof obj === 'function') {
7340
+ return true;
7341
+ }
7342
+ if (_typeof(obj) !== 'object') {
7343
+ return false;
7344
+ }
7345
+ if (Array.isArray(obj)) {
7346
+ return obj.some(function (item) {
7347
+ return containsDeepFunction(item);
7348
+ });
7349
+ }
7350
+ return Object.values(obj).some(function (value) {
7351
+ return containsDeepFunction(value);
7352
+ });
7353
+ };
7313
7354
  var classInstanceToObject = exports.classInstanceToObject = function classInstanceToObject(instance, options) {
7355
+ // Handle null or undefined
7356
+ if (instance === null || instance === undefined) {
7357
+ return instance;
7358
+ }
7359
+ // Handle primitive values
7360
+ if (_typeof(instance) !== 'object' || instance instanceof Date) {
7361
+ return instance;
7362
+ }
7363
+ // Handle arrays
7364
+ if (Array.isArray(instance)) {
7365
+ return instance.map(function (item) {
7366
+ return classInstanceToObject(item, options);
7367
+ });
7368
+ }
7369
+ // Handle objects
7314
7370
  var obj = {};
7315
- // const prototype = Object.getPrototypeOf(instance);
7316
- var properties = [
7317
- // ...Object.getOwnPropertyNames(instance),
7318
- // ...Object.getOwnPropertyNames(prototype),
7319
- ];
7320
- var currentClass = instance;
7321
- while (currentClass && currentClass !== Object) {
7322
- // console.log('current class', currentClass); // TODO: test if it omits the Object methods
7323
- properties.push.apply(properties, _toConsumableArray(Object.getOwnPropertyNames(currentClass)));
7324
- currentClass = Object.getPrototypeOf(currentClass);
7325
- }
7326
- properties.forEach(function (property) {
7327
- var _options$globals, _options$methodWrappe;
7328
- if (['constructor'].includes(property)) {
7371
+ // Get all own properties (data properties, not methods)
7372
+ Object.keys(instance).forEach(function (key) {
7373
+ var value = instance[key];
7374
+ // Skip functions directly
7375
+ if (typeof value === 'function') {
7329
7376
  return;
7330
7377
  }
7331
- var _method = instance[property];
7332
- _method = typeof _method === 'function' && typeof _method.bind === 'function' ? _method.bind(instance) : _method;
7333
- if (options !== null && options !== void 0 && (_options$globals = options.globals) !== null && _options$globals !== void 0 && _options$globals.canCallAction) {
7334
- var originalMethod = _method;
7335
- _method = function method() {
7336
- var canCall = options.globals.canCallAction({
7337
- method: _method.name,
7338
- "class": instance.constructor.name
7339
- });
7340
- if (!canCall) {
7341
- throw new Error("No permissions for call: ".concat(_method.name)); // TODO: consider usage of a specific permissions error
7342
- }
7343
- return originalMethod.apply(void 0, arguments);
7344
- };
7345
- }
7346
- if (typeof (options === null || options === void 0 || (_options$methodWrappe = options.methodWrappers) === null || _options$methodWrappe === void 0 ? void 0 : _options$methodWrappe[property]) === 'function') {
7347
- obj[property] = function () {
7348
- var _options$methodWrappe2;
7349
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
7350
- args[_key] = arguments[_key];
7351
- }
7352
- return (_options$methodWrappe2 = options.methodWrappers)[property].apply(_options$methodWrappe2, [_method].concat(args));
7353
- };
7354
- } else {
7355
- obj[property] = function () {
7356
- return _method.apply(void 0, arguments);
7357
- };
7378
+ // Skip properties that contain functions deeply
7379
+ if (_typeof(value) === 'object' && value !== null && containsDeepFunction(value)) {
7380
+ return;
7358
7381
  }
7382
+ // Recursively convert nested objects
7383
+ obj[key] = classInstanceToObject(value, options);
7359
7384
  });
7360
7385
  return obj;
7361
7386
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octostar/platform-api",
3
- "version": "0.5.12",
3
+ "version": "0.5.14",
4
4
  "description": "Provides access to the API exposed by Octostar platform to an application run in an iframe",
5
5
  "keywords": [
6
6
  "octostar",
@@ -38,10 +38,10 @@
38
38
  "rimraf": "^5.0.5",
39
39
  "tsup": "^8.4.0",
40
40
  "typescript": "^5.8.2",
41
- "@octostar/platform-types": "0.5.15"
41
+ "@octostar/platform-types": "0.5.17"
42
42
  },
43
43
  "peerDependencies": {
44
- "@octostar/platform-types": "^0.5.15"
44
+ "@octostar/platform-types": "^0.5.17"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"