2023-10-17 22:14:08 +07:00
|
|
|
/**
|
2023-08-25 00:15:49 +07:00
|
|
|
* Copyright 2023 Fluence Labs Limited
|
2023-02-13 17:41:35 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-11-14 04:55:50 +07:00
|
|
|
import type { CallAquaFunctionArgs } from "./compilerSupport/callFunction.js";
|
|
|
|
import { ServiceImpl } from "./compilerSupport/types.js";
|
2023-10-17 22:14:08 +07:00
|
|
|
import { FluencePeer } from "./jsPeer/FluencePeer.js";
|
|
|
|
|
|
|
|
import { callAquaFunction, Fluence, registerService } from "./index.js";
|
2023-09-05 21:38:59 +07:00
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
export const isFluencePeer = (
|
|
|
|
fluencePeerCandidate: unknown,
|
|
|
|
): fluencePeerCandidate is FluencePeer => {
|
|
|
|
return fluencePeerCandidate instanceof FluencePeer;
|
2023-09-05 21:38:59 +07:00
|
|
|
};
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-11-14 04:55:50 +07:00
|
|
|
type CallAquaFunctionArgsTuned = Pick<CallAquaFunctionArgs, "args" | "script"> &
|
|
|
|
Partial<Pick<CallAquaFunctionArgs, "config" | "peer">>;
|
2023-10-17 22:14:08 +07:00
|
|
|
|
2023-11-14 04:55:50 +07:00
|
|
|
type RegisterServiceArgs = {
|
|
|
|
peer?: FluencePeer;
|
|
|
|
service: ServiceImpl;
|
|
|
|
serviceId: string;
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-11-14 04:55:50 +07:00
|
|
|
* Convenience function to support Aqua `func` generation backend
|
|
|
|
* The compiler only need to generate a call the function and provide the air script
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-11-14 04:55:50 +07:00
|
|
|
export const v5_callFunction = async ({
|
|
|
|
config = {},
|
|
|
|
peer,
|
|
|
|
args,
|
|
|
|
script,
|
|
|
|
}: CallAquaFunctionArgsTuned): Promise<unknown> => {
|
|
|
|
if (peer == null) {
|
2023-10-17 22:14:08 +07:00
|
|
|
if (Fluence.defaultClient == null) {
|
|
|
|
throw new Error(
|
|
|
|
"Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?",
|
|
|
|
);
|
2023-02-13 17:41:35 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
peer = Fluence.defaultClient;
|
|
|
|
}
|
2023-02-13 17:41:35 +03:00
|
|
|
|
2023-11-14 04:55:50 +07:00
|
|
|
return callAquaFunction({
|
|
|
|
args,
|
|
|
|
script,
|
|
|
|
config,
|
|
|
|
peer,
|
|
|
|
});
|
|
|
|
};
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
2023-11-14 04:55:50 +07:00
|
|
|
* Convenience function to support Aqua `service` generation backend
|
|
|
|
* The compiler only need to generate a call the function and provide the air script
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-11-14 04:55:50 +07:00
|
|
|
export const v5_registerService = ({
|
|
|
|
serviceId,
|
|
|
|
service,
|
|
|
|
peer,
|
|
|
|
}: RegisterServiceArgs): void => {
|
|
|
|
if (peer == null) {
|
2023-10-17 22:14:08 +07:00
|
|
|
if (Fluence.defaultClient == null) {
|
|
|
|
throw new Error(
|
|
|
|
"Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?",
|
|
|
|
);
|
2023-02-13 17:41:35 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 22:14:08 +07:00
|
|
|
peer = Fluence.defaultClient;
|
|
|
|
}
|
|
|
|
|
2023-11-14 04:55:50 +07:00
|
|
|
registerService({
|
|
|
|
service,
|
2023-10-17 22:14:08 +07:00
|
|
|
serviceId,
|
2023-11-14 04:55:50 +07:00
|
|
|
peer,
|
|
|
|
});
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|