@forge/bridge
Advanced tools
Comparing version 0.1.0-next.1 to 0.1.0-next.2
# @forge/bridge | ||
## 0.1.0-next.2 | ||
### Patch Changes | ||
- 0c63b43: Retry failed invoke calls due to bridge not being estabilished yet | ||
## 0.1.0-next.1 | ||
@@ -4,0 +10,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var __1 = require(".."); | ||
@@ -7,3 +8,3 @@ describe('invoke', function () { | ||
beforeEach(function () { | ||
jest.clearAllMocks(); | ||
jest.resetAllMocks(); | ||
global.__bridge = { | ||
@@ -13,10 +14,75 @@ callBridge: callBridgeMock | ||
}); | ||
it('calls the global bridge with the correct arguments', function () { | ||
__1.invoke('key', { value: 'value' }); | ||
expect(callBridgeMock).toHaveBeenCalledWith('invoke', { functionKey: 'key', payload: { value: 'value' } }); | ||
}); | ||
it('calls the global bridge with the correct arguments when no payload is supplied', function () { | ||
__1.invoke('key'); | ||
expect(callBridgeMock).toHaveBeenCalledWith('invoke', { functionKey: 'key' }); | ||
}); | ||
it('calls the global bridge with the correct arguments', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, __1.invoke('key', { value: 'value' })]; | ||
case 1: | ||
_a.sent(); | ||
expect(callBridgeMock).toHaveBeenCalledTimes(1); | ||
expect(callBridgeMock).toHaveBeenCalledWith('invoke', { functionKey: 'key', payload: { value: 'value' } }); | ||
return [2]; | ||
} | ||
}); | ||
}); }); | ||
it('calls the global bridge with the correct arguments when no payload is supplied', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, __1.invoke('key')]; | ||
case 1: | ||
_a.sent(); | ||
expect(callBridgeMock).toHaveBeenCalledTimes(1); | ||
expect(callBridgeMock).toHaveBeenCalledWith('invoke', { functionKey: 'key' }); | ||
return [2]; | ||
} | ||
}); | ||
}); }); | ||
it('retries if callBridgeMock returns an error due to a handler not being found', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
callBridgeMock.mockRejectedValueOnce(new Error('No handler found for post message: invoke ')); | ||
return [4, __1.invoke('key')]; | ||
case 1: | ||
_a.sent(); | ||
expect(callBridgeMock).toHaveBeenCalledTimes(2); | ||
return [2]; | ||
} | ||
}); | ||
}); }); | ||
it('retries should respect the max number of retries', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { | ||
var err_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
callBridgeMock.mockRejectedValue(new Error('No handler found for post message: invoke ')); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4, __1.invoke('key')]; | ||
case 2: | ||
_a.sent(); | ||
return [3, 4]; | ||
case 3: | ||
err_1 = _a.sent(); | ||
expect(callBridgeMock).toHaveBeenCalledTimes(10); | ||
return [3, 4]; | ||
case 4: return [2]; | ||
} | ||
}); | ||
}); }); | ||
it('does not retry if callBridgeMock returns an unexpected error not being found', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { | ||
var unknownError; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
unknownError = new Error('Something went wrong'); | ||
callBridgeMock.mockRejectedValue(unknownError); | ||
return [4, expect(__1.invoke('key')).rejects.toEqual(unknownError)]; | ||
case 1: | ||
_a.sent(); | ||
expect(callBridgeMock).toHaveBeenCalledTimes(1); | ||
return [2]; | ||
} | ||
}); | ||
}); }); | ||
}); |
@@ -1,12 +0,10 @@ | ||
export declare const invoke: (functionKey: string, payload?: { | ||
[x: string]: any; | ||
[x: number]: any; | ||
} | undefined) => any; | ||
declare type InvokePayload = { | ||
[key in number | string]: any; | ||
}; | ||
declare type InvokeResponse = Record<string, any> | void; | ||
export declare const invoke: (functionKey: string, payload?: InvokePayload | undefined) => Promise<InvokeResponse>; | ||
declare const _default: { | ||
invoke: (functionKey: string, payload?: { | ||
[x: string]: any; | ||
[x: number]: any; | ||
} | undefined) => any; | ||
invoke: (functionKey: string, payload?: InvokePayload | undefined) => Promise<InvokeResponse>; | ||
}; | ||
export default _default; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.invoke = void 0; | ||
var tslib_1 = require("tslib"); | ||
var delay = function (time) { return Promise.resolve(function (res) { return setTimeout(res, time); }); }; | ||
var validatePayload = function (payload) { | ||
if (!payload) | ||
return; | ||
if (Object.values(payload).some(function (val) { return typeof val === 'function'; })) { | ||
throw new Error('Passing functions as part of the payload is not supported!'); | ||
} | ||
}; | ||
var MAX_INVOKE_RETRIES = 10; | ||
exports.invoke = function (functionKey, payload) { | ||
return window.__bridge.callBridge('invoke', { functionKey: functionKey, payload: payload }); | ||
if (typeof functionKey !== 'string') { | ||
throw new Error('functionKey must be a string!'); | ||
} | ||
validatePayload(payload); | ||
var callBridge = window.__bridge.callBridge; | ||
var retriableInvoke = function (retryCount, error) { | ||
if (retryCount === void 0) { retryCount = MAX_INVOKE_RETRIES; } | ||
return tslib_1.__awaiter(void 0, void 0, void 0, function () { | ||
var error_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (retryCount <= 0) { | ||
throw error; | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 7]); | ||
return [4, callBridge('invoke', { functionKey: functionKey, payload: payload })]; | ||
case 2: return [2, _a.sent()]; | ||
case 3: | ||
error_1 = _a.sent(); | ||
if (!(error_1 === null || error_1 === void 0 ? void 0 : error_1.message.startsWith('No handler found for post message: invoke'))) return [3, 5]; | ||
return [4, delay((MAX_INVOKE_RETRIES - retryCount + 1) * 50)]; | ||
case 4: | ||
_a.sent(); | ||
return [2, retriableInvoke(--retryCount, error_1)]; | ||
case 5: throw error_1; | ||
case 6: return [3, 7]; | ||
case 7: return [2]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return retriableInvoke(); | ||
}; | ||
@@ -7,0 +51,0 @@ exports.default = { |
{ | ||
"name": "@forge/bridge", | ||
"version": "0.1.0-next.1", | ||
"version": "0.1.0-next.2", | ||
"description": "Forge bridge API for custom UI apps", | ||
@@ -5,0 +5,0 @@ "author": "Atlassian", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7846
149