feat!: Expose updated JS Client API via js-client.api package (#246)

This commit is contained in:
Pavel
2023-02-15 03:00:42 +03:00
committed by GitHub
parent 9667c4fec6
commit d4bb8fb429
25 changed files with 858 additions and 564 deletions

View File

@ -14,36 +14,12 @@
* limitations under the License.
*/
import type { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/interfaces/compilerSupport';
import type { IFluenceClient } from '@fluencelabs/interfaces/fluenceClient';
import { getArgumentTypes } from '@fluencelabs/interfaces/compilerSupport';
import { isFluencePeer } from '@fluencelabs/interfaces/fluenceClient';
import type { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/interfaces';
import type { IFluenceClient } from '@fluencelabs/interfaces';
import { getArgumentTypes } from '@fluencelabs/interfaces';
import { isFluencePeer } from '@fluencelabs/interfaces';
import { getDefaultPeer } from '../index.js';
export type { IFluenceClient, CallParams } from '@fluencelabs/interfaces/fluenceClient';
export {
ArrayType,
ArrowType,
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FnConfig,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
NilType,
NonArrowType,
OptionType,
ProductType,
ScalarNames,
ScalarType,
ServiceDef,
StructType,
TopType,
UnlabeledProductType,
} from '@fluencelabs/interfaces/compilerSupport';
import { getDefaultPeer } from '../util.js';
/**
* Convenience function to support Aqua `func` generation backend
@ -66,7 +42,6 @@ export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef,
/**
* Convenience function to support Aqua `service` generation backend
* The compiler only need to generate a call the function and provide the corresponding definitions and the air script
*
* @param args - raw arguments passed by user to the generated function
* @param def - service definition generated by the Aqua compiler
*/

View File

@ -1,41 +0,0 @@
/*
* Copyright 2022 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { IFluenceClient } from './implementation.js';
export { CallParams as CallParams$$ } from './implementation.js';
export {
ArrayType as ArrayType$$,
ArrowType as ArrowType$$,
ArrowWithCallbacks as ArrowWithCallbacks$$,
ArrowWithoutCallbacks as ArrowWithoutCallbacks$$,
BottomType as BottomType$$,
FnConfig as FnConfig$$,
FunctionCallConstants as FunctionCallConstants$$,
FunctionCallDef as FunctionCallDef$$,
LabeledProductType as LabeledProductType$$,
NilType as NilType$$,
NonArrowType as NonArrowType$$,
OptionType as OptionType$$,
ProductType as ProductType$$,
ScalarNames as ScalarNames$$,
ScalarType as ScalarType$$,
ServiceDef as ServiceDef$$,
StructType as StructType$$,
TopType as TopType$$,
UnlabeledProductType as UnlabeledProductType$$,
callFunction as callFunction$$,
registerService as registerService$$,
} from './implementation.js';

View File

@ -1,40 +1,33 @@
import type { IFluenceClient, ClientOptions } from '@fluencelabs/interfaces/fluenceClient';
import { getDefaultPeer } from './util.js';
import type { IFluenceClient, ClientOptions } from '@fluencelabs/interfaces';
export type { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces';
export { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces/fluenceClient';
export {
ArrayType,
ArrowType,
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FnConfig,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
NilType,
NonArrowType,
OptionType,
ProductType,
ScalarNames,
ScalarType,
ServiceDef,
StructType,
TopType,
UnlabeledProductType,
} from '@fluencelabs/interfaces';
// TODO: hack needed to kinda have backward compat with compiler api
export type FluencePeer = IFluenceClient;
const getPeerFromGlobalThis = (): IFluenceClient | undefined => {
// @ts-ignore
return globalThis.defaultPeer;
};
// TODO: DXJ-271
const REJECT_MESSAGE = 'You probably forgot to add script tag. Read about it here: ';
/**
* Wait until the js client script it loaded and return the default peer from globalThis
*/
export const getDefaultPeer = (): Promise<IFluenceClient> => {
return new Promise((resolve, reject) => {
let interval: NodeJS.Timer | undefined;
let hits = 50;
interval = setInterval(() => {
if (hits === 0) {
clearInterval(interval);
reject(REJECT_MESSAGE);
}
let res = getPeerFromGlobalThis();
if (res) {
clearInterval(interval);
resolve(res);
}
hits--;
}, 100);
});
};
export {
callFunction as v5_callFunction,
registerService as v5_registerService,
} from './compilerSupport/implementation.js';
/**
* Public interface to Fluence JS

View File

@ -0,0 +1,41 @@
import type { IFluenceClient } from '@fluencelabs/interfaces';
const getPeerFromGlobalThis = (): IFluenceClient | undefined => {
// @ts-ignore
return globalThis.defaultPeer;
};
// TODO: DXJ-271
const REJECT_MESSAGE = 'You probably forgot to add script tag. Read about it here: ';
// Let's assume that if the library has not been loaded in 5 seconds, then the user has forgotten to add the script tag
const POLL_PEER_TIMEOUT = 5000;
// The script might be cached so need to try loading it ASAP, thus short interval
const POLL_PEER_INTERVAL = 100;
/**
* Wait until the js client script it loaded and return the default peer from globalThis
*/
export const getDefaultPeer = (): Promise<IFluenceClient> => {
return new Promise((resolve, reject) => {
// This function is internal
// Make it sure that would be zero way for unnecessary types
// to break out into the public API
let interval: any;
let hits = POLL_PEER_TIMEOUT / POLL_PEER_INTERVAL;
interval = setInterval(() => {
if (hits === 0) {
clearInterval(interval);
reject(REJECT_MESSAGE);
}
let res = getPeerFromGlobalThis();
if (res) {
clearInterval(interval);
resolve(res);
}
hits--;
}, POLL_PEER_INTERVAL);
});
};