@octostar/platform-api 0.5.16 → 0.5.18
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/dist/lib/ContextAPI.d.ts
CHANGED
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
import { Context, ContextAPISetParams,
|
|
2
|
-
export type ContextAPIConstructorParams<ContextData extends Record<string, any>> = ContextAPIStaticData<ContextData> & {
|
|
3
|
-
targetWindow?: Window;
|
|
4
|
-
channelId?: string;
|
|
5
|
-
};
|
|
1
|
+
import { Context, ContextAPISetParams, ContextListener } from '@octostar/platform-types';
|
|
6
2
|
export declare class ContextAPI<ContextData extends Record<string, any>> implements Context<ContextData> {
|
|
7
3
|
private contextData;
|
|
8
|
-
targetWindow: Window | undefined;
|
|
9
|
-
readonly id: string;
|
|
10
4
|
private listeners;
|
|
11
5
|
private postRobotListener;
|
|
12
|
-
private
|
|
13
|
-
private
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
private type;
|
|
7
|
+
private channelId;
|
|
8
|
+
private iframeChannelAttr;
|
|
9
|
+
constructor(contextData: ContextData);
|
|
10
|
+
private getTargetWindow;
|
|
11
|
+
private setContextAndNotify;
|
|
12
|
+
private getPostRobotEventName;
|
|
13
|
+
private connect;
|
|
14
|
+
private sendContextUpdate;
|
|
20
15
|
getContext(): ContextData;
|
|
21
16
|
setContext(context: ContextAPISetParams<ContextData>): void;
|
|
22
|
-
notifyListeners(): void;
|
|
23
|
-
syncContextToChannel(): Promise<void>;
|
|
24
17
|
subscribeToChanges(contextListener: ContextListener<ContextData>): string;
|
|
25
18
|
subscribeToChanges(id: string, contextListener: ContextListener<ContextData>): string;
|
|
26
19
|
unsubscribeFromChanges(id: string): void;
|
|
20
|
+
connectToIframe(): {
|
|
21
|
+
channelId: string;
|
|
22
|
+
onIframeLoad: React.ReactEventHandler<HTMLIFrameElement>;
|
|
23
|
+
};
|
|
24
|
+
connectToParent(): void;
|
|
27
25
|
destroy(): void;
|
|
28
26
|
}
|
|
29
27
|
//# sourceMappingURL=ContextAPI.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContextAPI.d.ts","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,
|
|
1
|
+
{"version":3,"file":"ContextAPI.d.ts","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,mBAAmB,EACnB,eAAe,EAChB,MAAM,0BAA0B,CAAC;AAMlC,qBAAa,UAAU,CAAC,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAE,YAAW,OAAO,CAAC,WAAW,CAAC;IAE9F,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,SAAS,CAAoD;IACrE,OAAO,CAAC,iBAAiB,CAAgD;IACzE,OAAO,CAAC,IAAI,CAAsB;IAClC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,iBAAiB,CAAkC;gBAE/C,WAAW,EAAE,WAAW;IAOpC,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,mBAAmB;IAS3B,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,OAAO;IAkBf,OAAO,CAAC,iBAAiB;IAYzB,UAAU;IAIV,UAAU,CAAC,OAAO,EAAE,mBAAmB,CAAC,WAAW,CAAC;IAKpD,kBAAkB,CAAC,eAAe,EAAE,eAAe,CAAC,WAAW,CAAC,GAAG,MAAM;IAEzE,kBAAkB,CAChB,EAAE,EAAE,MAAM,EACV,eAAe,EAAE,eAAe,CAAC,WAAW,CAAC,GAC5C,MAAM;IAsBT,sBAAsB,CAAC,EAAE,EAAE,MAAM;IAIjC,eAAe,IAAI;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,KAAK,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,CAAA;KAAE;IAalG,eAAe;IAQf,OAAO;CAGR"}
|
package/dist/lib/ContextAPI.js
CHANGED
|
@@ -1,65 +1,69 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
2
|
import * as postRobot from 'post-robot';
|
|
3
|
+
const CONTEXT_UPDATE_EVENT = 'context-update';
|
|
3
4
|
export class ContextAPI {
|
|
4
|
-
constructor(
|
|
5
|
-
this.contextData = contextData;
|
|
5
|
+
constructor(contextData) {
|
|
6
6
|
this.listeners = {};
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return this.internalChannelId;
|
|
7
|
+
this.iframeChannelAttr = 'data-octostar-iframe-channel';
|
|
8
|
+
this.contextData = contextData;
|
|
9
|
+
const iframeChannelId = window.frameElement?.getAttribute(this.iframeChannelAttr);
|
|
10
|
+
this.channelId = iframeChannelId || uuidv4();
|
|
11
|
+
this.type = iframeChannelId ? 'iframe' : 'parent';
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
this.
|
|
16
|
-
|
|
17
|
-
this.postRobotEventId = `context-api-update::${channelId}`;
|
|
18
|
-
this.listenToPostRobot();
|
|
13
|
+
getTargetWindow() {
|
|
14
|
+
if (this.type === 'iframe') {
|
|
15
|
+
return window.top;
|
|
19
16
|
}
|
|
17
|
+
return document.querySelector(`iframe[${this.iframeChannelAttr}='${this.channelId}']`)?.contentWindow || undefined;
|
|
20
18
|
}
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
|
|
19
|
+
setContextAndNotify(context) {
|
|
20
|
+
if (typeof context === 'function') {
|
|
21
|
+
this.contextData = context(this.contextData);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.contextData = context;
|
|
24
25
|
}
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
Object.values(this.listeners).forEach(listener => listener(this.contextData));
|
|
27
|
+
}
|
|
28
|
+
getPostRobotEventName(channelId) {
|
|
29
|
+
return `${channelId}-${CONTEXT_UPDATE_EVENT}`;
|
|
30
|
+
}
|
|
31
|
+
connect(channelId) {
|
|
32
|
+
const targetWindow = this.getTargetWindow();
|
|
33
|
+
if (!targetWindow) {
|
|
34
|
+
const error = new Error('Target window not found');
|
|
35
|
+
throw error;
|
|
27
36
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
37
|
+
const postRobotEventName = this.getPostRobotEventName(channelId);
|
|
38
|
+
this.postRobotListener = (postRobot.on(postRobotEventName, { window: targetWindow }, (event) => {
|
|
39
|
+
if (event.data.type === 'init') {
|
|
40
|
+
this.setContext((context) => ({ ...context, ...event.data.context }));
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.setContextAndNotify((context) => ({ ...context, ...event.data.context }));
|
|
31
44
|
}
|
|
32
45
|
return Promise.resolve();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
get context() {
|
|
36
|
-
return this.contextData;
|
|
46
|
+
}));
|
|
47
|
+
this.sendContextUpdate(true);
|
|
37
48
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
49
|
+
sendContextUpdate(isInit = false) {
|
|
50
|
+
const targetWindow = this.getTargetWindow();
|
|
51
|
+
if (!targetWindow) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
postRobot.send(targetWindow, this.getPostRobotEventName(this.channelId), { context: this.contextData, ...(isInit && { type: 'init' }) });
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
// do nothing
|
|
59
|
+
}
|
|
42
60
|
}
|
|
43
61
|
getContext() {
|
|
44
|
-
return this.
|
|
62
|
+
return this.contextData;
|
|
45
63
|
}
|
|
46
64
|
setContext(context) {
|
|
47
|
-
this.context
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
notifyListeners() {
|
|
51
|
-
Object.values(this.listeners).forEach(listener => listener(this.context));
|
|
52
|
-
}
|
|
53
|
-
async syncContextToChannel() {
|
|
54
|
-
if (!this.targetWindow || !this.postRobotEventId) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
/** Try to exclude functions in the context */
|
|
58
|
-
// const context = classInstanceToObject(this.context);
|
|
59
|
-
await postRobot.send(this.targetWindow, this.postRobotEventId, {
|
|
60
|
-
from: this.id,
|
|
61
|
-
context: this.context,
|
|
62
|
-
});
|
|
65
|
+
this.setContextAndNotify(context);
|
|
66
|
+
this.sendContextUpdate();
|
|
63
67
|
}
|
|
64
68
|
subscribeToChanges(idOrContextListener, contextListenerOrNothing) {
|
|
65
69
|
const id = typeof idOrContextListener === 'string' ? idOrContextListener : uuidv4();
|
|
@@ -67,7 +71,8 @@ export class ContextAPI {
|
|
|
67
71
|
? idOrContextListener
|
|
68
72
|
: contextListenerOrNothing;
|
|
69
73
|
if (!contextListener) {
|
|
70
|
-
|
|
74
|
+
const error = new Error('Context listener is required');
|
|
75
|
+
throw error;
|
|
71
76
|
}
|
|
72
77
|
this.listeners[id] = contextListener;
|
|
73
78
|
return id;
|
|
@@ -75,6 +80,23 @@ export class ContextAPI {
|
|
|
75
80
|
unsubscribeFromChanges(id) {
|
|
76
81
|
delete this.listeners[id];
|
|
77
82
|
}
|
|
83
|
+
connectToIframe() {
|
|
84
|
+
if (this.type !== 'parent') {
|
|
85
|
+
const error = new Error('ContextAPI is not a parent');
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
const onIframeLoad = () => {
|
|
89
|
+
this.connect(this.channelId);
|
|
90
|
+
};
|
|
91
|
+
return { channelId: this.channelId, onIframeLoad };
|
|
92
|
+
}
|
|
93
|
+
connectToParent() {
|
|
94
|
+
if (this.type !== 'iframe') {
|
|
95
|
+
const error = new Error('ContextAPI is not an iframe');
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
this.connect(this.channelId);
|
|
99
|
+
}
|
|
78
100
|
destroy() {
|
|
79
101
|
this.postRobotListener?.cancel();
|
|
80
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContextAPI.js","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ContextAPI.js","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,SAAS,MAAM,YAAY,CAAC;AAExC,MAAM,oBAAoB,GAAG,gBAAgB,CAAC;AAE9C,MAAM,OAAO,UAAU;IASrB,YAAY,WAAwB;QAN5B,cAAS,GAAiD,EAAE,CAAC;QAI7D,sBAAiB,GAAG,8BAA8B,CAAC;QAGzD,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,MAAM,eAAe,GAAG,MAAM,CAAC,YAAY,EAAE,YAAY,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;QAClF,IAAI,CAAC,SAAS,GAAG,eAAe,IAAI,MAAM,EAAE,CAAC;QAC7C,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;IACpD,CAAC;IAEO,eAAe;QACrB,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,OAAO,MAAM,CAAC,GAAG,CAAC;QACpB,CAAC;QACD,OAAO,QAAQ,CAAC,aAAa,CAAoB,UAAU,IAAI,CAAC,iBAAiB,KAAK,IAAI,CAAC,SAAS,IAAI,CAAC,EAAE,aAAa,IAAI,SAAS,CAAC;IACxI,CAAC;IAEO,mBAAmB,CAAC,OAAyC;QACnE,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;YAClC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC/C,CAAC;aAAM,CAAC;YACN,I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,SAAS,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED,eAAe;QACb,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;YACtD,MAAM,KAAK,CAAC;QACd,CAAC;QAED,MAAM,YAAY,GAA+C,GAAG,EAAE;YACpE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC/B,CAAC,CAAA;QAED,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,YAAY,EAAE,CAAA;IACpD,CAAC;IAED,eAAe;QACb,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;YACvD,MAAM,KAAK,CAAC;QACd,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO;QACL,IAAI,CAAC,iBAAiB,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -7081,9 +7081,6 @@ var _uuid = require("uuid");
|
|
|
7081
7081
|
var postRobot = _interopRequireWildcard(require("post-robot"));
|
|
7082
7082
|
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
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 _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
7084
|
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
7085
|
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
7086
|
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; }
|
|
@@ -7092,111 +7089,108 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o =
|
|
|
7092
7089
|
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
7093
7090
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
7094
7091
|
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); }
|
|
7092
|
+
var CONTEXT_UPDATE_EVENT = 'context-update';
|
|
7095
7093
|
var ContextAPI = exports.ContextAPI = /*#__PURE__*/function () {
|
|
7096
|
-
function ContextAPI(
|
|
7097
|
-
var
|
|
7098
|
-
contextData = _ref.contextData,
|
|
7099
|
-
channelId = _ref.channelId;
|
|
7094
|
+
function ContextAPI(contextData) {
|
|
7095
|
+
var _window$frameElement;
|
|
7100
7096
|
_classCallCheck(this, ContextAPI);
|
|
7101
|
-
this.contextData = contextData;
|
|
7102
7097
|
this.listeners = {};
|
|
7103
|
-
this.
|
|
7104
|
-
this.
|
|
7105
|
-
|
|
7098
|
+
this.iframeChannelAttr = 'data-octostar-iframe-channel';
|
|
7099
|
+
this.contextData = contextData;
|
|
7100
|
+
var iframeChannelId = (_window$frameElement = window.frameElement) === null || _window$frameElement === void 0 ? void 0 : _window$frameElement.getAttribute(this.iframeChannelAttr);
|
|
7101
|
+
this.channelId = iframeChannelId || (0, _uuid.v4)();
|
|
7102
|
+
this.type = iframeChannelId ? 'iframe' : 'parent';
|
|
7106
7103
|
}
|
|
7107
7104
|
return _createClass(ContextAPI, [{
|
|
7108
|
-
key: "
|
|
7109
|
-
|
|
7110
|
-
|
|
7111
|
-
|
|
7112
|
-
|
|
7113
|
-
this.internalChannelId = channelId;
|
|
7114
|
-
if (channelId) {
|
|
7115
|
-
this.postRobotEventId = "context-api-update::".concat(channelId);
|
|
7116
|
-
this.listenToPostRobot();
|
|
7105
|
+
key: "getTargetWindow",
|
|
7106
|
+
value: function getTargetWindow() {
|
|
7107
|
+
var _document$querySelect;
|
|
7108
|
+
if (this.type === 'iframe') {
|
|
7109
|
+
return window.top;
|
|
7117
7110
|
}
|
|
7111
|
+
return ((_document$querySelect = document.querySelector("iframe[".concat(this.iframeChannelAttr, "='").concat(this.channelId, "']"))) === null || _document$querySelect === void 0 ? void 0 : _document$querySelect.contentWindow) || undefined;
|
|
7118
7112
|
}
|
|
7119
7113
|
}, {
|
|
7120
|
-
key: "
|
|
7121
|
-
value: function
|
|
7114
|
+
key: "setContextAndNotify",
|
|
7115
|
+
value: function setContextAndNotify(context) {
|
|
7122
7116
|
var _this = this;
|
|
7123
|
-
if (
|
|
7124
|
-
|
|
7117
|
+
if (typeof context === 'function') {
|
|
7118
|
+
this.contextData = context(this.contextData);
|
|
7119
|
+
} else {
|
|
7120
|
+
this.contextData = context;
|
|
7125
7121
|
}
|
|
7126
|
-
|
|
7127
|
-
|
|
7122
|
+
Object.values(this.listeners).forEach(function (listener) {
|
|
7123
|
+
return listener(_this.contextData);
|
|
7124
|
+
});
|
|
7125
|
+
}
|
|
7126
|
+
}, {
|
|
7127
|
+
key: "getPostRobotEventName",
|
|
7128
|
+
value: function getPostRobotEventName(channelId) {
|
|
7129
|
+
return "".concat(channelId, "-").concat(CONTEXT_UPDATE_EVENT);
|
|
7130
|
+
}
|
|
7131
|
+
}, {
|
|
7132
|
+
key: "connect",
|
|
7133
|
+
value: function connect(channelId) {
|
|
7134
|
+
var _this2 = this;
|
|
7135
|
+
var targetWindow = this.getTargetWindow();
|
|
7136
|
+
if (!targetWindow) {
|
|
7137
|
+
var error = new Error('Target window not found');
|
|
7138
|
+
throw error;
|
|
7128
7139
|
}
|
|
7129
|
-
|
|
7130
|
-
|
|
7131
|
-
|
|
7140
|
+
var postRobotEventName = this.getPostRobotEventName(channelId);
|
|
7141
|
+
this.postRobotListener = postRobot.on(postRobotEventName, {
|
|
7142
|
+
window: targetWindow
|
|
7143
|
+
}, function (event) {
|
|
7144
|
+
if (event.data.type === 'init') {
|
|
7145
|
+
_this2.setContext(function (context) {
|
|
7146
|
+
return _objectSpread(_objectSpread({}, context), event.data.context);
|
|
7147
|
+
});
|
|
7148
|
+
} else {
|
|
7149
|
+
_this2.setContextAndNotify(function (context) {
|
|
7150
|
+
return _objectSpread(_objectSpread({}, context), event.data.context);
|
|
7151
|
+
});
|
|
7132
7152
|
}
|
|
7133
7153
|
return Promise.resolve();
|
|
7134
7154
|
});
|
|
7155
|
+
this.sendContextUpdate(true);
|
|
7135
7156
|
}
|
|
7136
7157
|
}, {
|
|
7137
|
-
key: "
|
|
7138
|
-
|
|
7139
|
-
|
|
7140
|
-
|
|
7141
|
-
|
|
7142
|
-
|
|
7143
|
-
|
|
7144
|
-
|
|
7158
|
+
key: "sendContextUpdate",
|
|
7159
|
+
value: function sendContextUpdate() {
|
|
7160
|
+
var isInit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
7161
|
+
var targetWindow = this.getTargetWindow();
|
|
7162
|
+
if (!targetWindow) {
|
|
7163
|
+
return;
|
|
7164
|
+
}
|
|
7165
|
+
try {
|
|
7166
|
+
postRobot.send(targetWindow, this.getPostRobotEventName(this.channelId), _objectSpread({
|
|
7167
|
+
context: this.contextData
|
|
7168
|
+
}, isInit && {
|
|
7169
|
+
type: 'init'
|
|
7170
|
+
}));
|
|
7171
|
+
} catch (error) {
|
|
7172
|
+
// do nothing
|
|
7173
|
+
}
|
|
7145
7174
|
}
|
|
7146
7175
|
}, {
|
|
7147
7176
|
key: "getContext",
|
|
7148
7177
|
value: function getContext() {
|
|
7149
|
-
return this.
|
|
7178
|
+
return this.contextData;
|
|
7150
7179
|
}
|
|
7151
7180
|
}, {
|
|
7152
7181
|
key: "setContext",
|
|
7153
7182
|
value: function setContext(context) {
|
|
7154
|
-
this.
|
|
7155
|
-
|
|
7156
|
-
}, {
|
|
7157
|
-
key: "notifyListeners",
|
|
7158
|
-
value: function notifyListeners() {
|
|
7159
|
-
var _this2 = this;
|
|
7160
|
-
Object.values(this.listeners).forEach(function (listener) {
|
|
7161
|
-
return listener(_this2.context);
|
|
7162
|
-
});
|
|
7183
|
+
this.setContextAndNotify(context);
|
|
7184
|
+
this.sendContextUpdate();
|
|
7163
7185
|
}
|
|
7164
|
-
}, {
|
|
7165
|
-
key: "syncContextToChannel",
|
|
7166
|
-
value: function () {
|
|
7167
|
-
var _syncContextToChannel = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
7168
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
7169
|
-
while (1) switch (_context.prev = _context.next) {
|
|
7170
|
-
case 0:
|
|
7171
|
-
if (!(!this.targetWindow || !this.postRobotEventId)) {
|
|
7172
|
-
_context.next = 2;
|
|
7173
|
-
break;
|
|
7174
|
-
}
|
|
7175
|
-
return _context.abrupt("return");
|
|
7176
|
-
case 2:
|
|
7177
|
-
_context.next = 4;
|
|
7178
|
-
return postRobot.send(this.targetWindow, this.postRobotEventId, {
|
|
7179
|
-
from: this.id,
|
|
7180
|
-
context: this.context
|
|
7181
|
-
});
|
|
7182
|
-
case 4:
|
|
7183
|
-
case "end":
|
|
7184
|
-
return _context.stop();
|
|
7185
|
-
}
|
|
7186
|
-
}, _callee, this);
|
|
7187
|
-
}));
|
|
7188
|
-
function syncContextToChannel() {
|
|
7189
|
-
return _syncContextToChannel.apply(this, arguments);
|
|
7190
|
-
}
|
|
7191
|
-
return syncContextToChannel;
|
|
7192
|
-
}()
|
|
7193
7186
|
}, {
|
|
7194
7187
|
key: "subscribeToChanges",
|
|
7195
7188
|
value: function subscribeToChanges(idOrContextListener, contextListenerOrNothing) {
|
|
7196
7189
|
var id = typeof idOrContextListener === 'string' ? idOrContextListener : (0, _uuid.v4)();
|
|
7197
7190
|
var contextListener = typeof idOrContextListener === 'function' ? idOrContextListener : contextListenerOrNothing;
|
|
7198
7191
|
if (!contextListener) {
|
|
7199
|
-
|
|
7192
|
+
var error = new Error('Context listener is required');
|
|
7193
|
+
throw error;
|
|
7200
7194
|
}
|
|
7201
7195
|
this.listeners[id] = contextListener;
|
|
7202
7196
|
return id;
|
|
@@ -7206,6 +7200,31 @@ var ContextAPI = exports.ContextAPI = /*#__PURE__*/function () {
|
|
|
7206
7200
|
value: function unsubscribeFromChanges(id) {
|
|
7207
7201
|
delete this.listeners[id];
|
|
7208
7202
|
}
|
|
7203
|
+
}, {
|
|
7204
|
+
key: "connectToIframe",
|
|
7205
|
+
value: function connectToIframe() {
|
|
7206
|
+
var _this3 = this;
|
|
7207
|
+
if (this.type !== 'parent') {
|
|
7208
|
+
var error = new Error('ContextAPI is not a parent');
|
|
7209
|
+
throw error;
|
|
7210
|
+
}
|
|
7211
|
+
var onIframeLoad = function onIframeLoad() {
|
|
7212
|
+
_this3.connect(_this3.channelId);
|
|
7213
|
+
};
|
|
7214
|
+
return {
|
|
7215
|
+
channelId: this.channelId,
|
|
7216
|
+
onIframeLoad: onIframeLoad
|
|
7217
|
+
};
|
|
7218
|
+
}
|
|
7219
|
+
}, {
|
|
7220
|
+
key: "connectToParent",
|
|
7221
|
+
value: function connectToParent() {
|
|
7222
|
+
if (this.type !== 'iframe') {
|
|
7223
|
+
var error = new Error('ContextAPI is not an iframe');
|
|
7224
|
+
throw error;
|
|
7225
|
+
}
|
|
7226
|
+
this.connect(this.channelId);
|
|
7227
|
+
}
|
|
7209
7228
|
}, {
|
|
7210
7229
|
key: "destroy",
|
|
7211
7230
|
value: function destroy() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octostar/platform-api",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.18",
|
|
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",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"rimraf": "^5.0.5",
|
|
40
40
|
"tsup": "^8.4.0",
|
|
41
41
|
"typescript": "^5.8.2",
|
|
42
|
-
"@octostar/platform-types": "0.5.
|
|
42
|
+
"@octostar/platform-types": "0.5.22"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@octostar/platform-types": "^0.5.
|
|
45
|
+
"@octostar/platform-types": "^0.5.22"
|
|
46
46
|
},
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|