mirror of
https://github.com/fluencelabs/fluent-pad
synced 2025-04-25 00:42:14 +00:00
Update JS SDK api and compiler to the latest versions (#6)
This commit is contained in:
parent
0d105e1caa
commit
18cdf5f99f
@ -2,11 +2,11 @@
|
||||
"services": {
|
||||
"history": {
|
||||
"dependencies": ["history_inmemory"],
|
||||
"node": "12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3"
|
||||
"node": "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e"
|
||||
},
|
||||
"user_list": {
|
||||
"dependencies": ["user_list_inmemory"],
|
||||
"node": "12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3"
|
||||
"node": "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e"
|
||||
}
|
||||
},
|
||||
"modules": {
|
||||
@ -28,8 +28,7 @@
|
||||
"variables": {
|
||||
"function": "is_authenticated",
|
||||
"json_path": "$.is_authenticated"
|
||||
},
|
||||
"node": "12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3"
|
||||
}
|
||||
}
|
||||
},
|
||||
"script_storage": {}
|
||||
|
@ -1,7 +1,12 @@
|
||||
import "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
import "fluent-pad.aqua"
|
||||
import "history.aqua"
|
||||
module App
|
||||
|
||||
import PeerId, Op, Peer from "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
import AppConfig, UserStatus, TextState from "fluent-pad.aqua"
|
||||
import User, UserList, AuthResult from "user-list.aqua"
|
||||
import EmptyServiceResult from "common.aqua"
|
||||
import History, GetEntriesServiceResult, AddServiceResult from "history.aqua"
|
||||
|
||||
export join, getUserList, initAfterJoin, updateOnlineStatuses, leave, auth, getHistory, addEntry, UserStatus, TextState, AppConfig
|
||||
|
||||
func join(user: User) -> EmptyServiceResult:
|
||||
app <- AppConfig.getApp()
|
||||
@ -24,10 +29,10 @@ func initAfterJoin(me: User) -> []User:
|
||||
isOnline <- Peer.is_connected(user.peer_id)
|
||||
if isOnline:
|
||||
on user.peer_id via user.relay_id:
|
||||
FluentPad.notifyUserAdded(me, true)
|
||||
else:
|
||||
Op.identity()
|
||||
par FluentPad.notifyUserAdded(user, isOnline)
|
||||
UserStatus.notifyUserAdded(me, true)
|
||||
-- else:
|
||||
--` Op.identity("dontcare")
|
||||
par UserStatus.notifyUserAdded(user, isOnline)
|
||||
<- allUsers
|
||||
|
||||
|
||||
@ -36,17 +41,17 @@ func updateOnlineStatuses():
|
||||
for user <- allUsers par:
|
||||
on user.peer_id via user.relay_id:
|
||||
isOnline <- Peer.is_connected(user.peer_id)
|
||||
FluentPad.notifyOnline(user.peer_id, isOnline)
|
||||
UserStatus.notifyOnline(user.peer_id, isOnline)
|
||||
|
||||
func leave():
|
||||
app <- AppConfig.getApp()
|
||||
on app.user_list.peer_id:
|
||||
UserList app.user_list.service_id
|
||||
res <- UserList.leave(%init_peer_id%)
|
||||
res <- UserList.leave(INIT_PEER_ID)
|
||||
allUsers <- getUserList()
|
||||
for user <- allUsers par:
|
||||
on user.peer_id via user.relay_id:
|
||||
FluentPad.notifyUserRemoved(%init_peer_id%)
|
||||
UserStatus.notifyUserRemoved(INIT_PEER_ID)
|
||||
|
||||
func auth() -> AuthResult:
|
||||
app <- AppConfig.getApp()
|
||||
@ -71,7 +76,7 @@ func addEntry(entry: string) -> AddServiceResult:
|
||||
res <- History.add(entry, authRes.is_authenticated)
|
||||
allUsers <- getUserList()
|
||||
for user <- allUsers par:
|
||||
if user.peer_id != %init_peer_id%:
|
||||
if user.peer_id != INIT_PEER_ID:
|
||||
on user.peer_id via user.relay_id:
|
||||
FluentPad.notifyTextUpdate(entry, authRes.is_authenticated)
|
||||
TextState.notifyTextUpdate(entry, authRes.is_authenticated)
|
||||
<- res
|
||||
|
@ -1,3 +1,5 @@
|
||||
module Common declares EmptyServiceResult
|
||||
|
||||
data EmptyServiceResult:
|
||||
ret_code: i32
|
||||
err_msg: string
|
||||
|
@ -1,5 +1,7 @@
|
||||
import "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
import "user-list.aqua"
|
||||
module FluentPad declares AppConfig, UserStatus, TextState
|
||||
|
||||
import PeerId from "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
import User from "user-list.aqua"
|
||||
|
||||
data ServiceInstance:
|
||||
peer_id: PeerId
|
||||
@ -9,10 +11,12 @@ data App:
|
||||
history: ServiceInstance
|
||||
user_list: ServiceInstance
|
||||
|
||||
service FluentPad("fluence/fluent-pad"):
|
||||
service UserStatus("fluence/fluent-pad/status"):
|
||||
notifyOnline(userPeerId: string, isOnline: bool)
|
||||
notifyUserAdded(currentUser: User, isOnline: bool)
|
||||
notifyUserRemoved(userPeerId: PeerId)
|
||||
|
||||
service TextState("fluence/fluent-pad/text-state"):
|
||||
notifyTextUpdate(changes: string, isAuthorized: bool)
|
||||
|
||||
service AppConfig("fluence/get-config"):
|
||||
|
@ -1,4 +1,6 @@
|
||||
import "common.aqua"
|
||||
module History declares History, GetEntriesServiceResult, AddServiceResult
|
||||
|
||||
import EmptyServiceResult from "common.aqua"
|
||||
|
||||
data AddServiceResult:
|
||||
ret_code: i32
|
||||
@ -15,7 +17,7 @@ data GetEntriesServiceResult:
|
||||
entries: []HistoryEntry
|
||||
|
||||
service History:
|
||||
get_all: bool -> GetEntriesServiceResult
|
||||
get_last: u64, bool -> GetEntriesServiceResult
|
||||
add: string, bool -> AddServiceResult
|
||||
set_tetraplet: string, string, string, string -> EmptyServiceResult
|
||||
get_all(arg1: bool) -> GetEntriesServiceResult
|
||||
get_last(arg1: u64, arg2: bool) -> GetEntriesServiceResult
|
||||
add(arg1: string, arg2: bool) -> AddServiceResult
|
||||
set_tetraplet(arg1: string, arg2: string, arg3: string, arg4: string) -> EmptyServiceResult
|
||||
|
@ -1,5 +1,7 @@
|
||||
import "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
import "common.aqua"
|
||||
module UserList declares User, UserList, AuthResult
|
||||
|
||||
import PeerId from "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
import EmptyServiceResult from "common.aqua"
|
||||
|
||||
data User:
|
||||
peer_id: PeerId
|
||||
@ -17,8 +19,8 @@ data AuthResult:
|
||||
is_authenticated: bool
|
||||
|
||||
service UserList:
|
||||
is_authenticated: -> AuthResult
|
||||
get_users: -> GetUsersServiceResult
|
||||
join: User -> EmptyServiceResult
|
||||
leave: string -> EmptyServiceResult -- user peerId
|
||||
is_exists: string -> () -- user peerId
|
||||
is_authenticated() -> AuthResult
|
||||
get_users() -> GetUsersServiceResult
|
||||
join(user: User) -> EmptyServiceResult
|
||||
leave(userPeerId: PeerId) -> EmptyServiceResult
|
||||
is_exists(userPeerId: PeerId)
|
||||
|
2053
client/package-lock.json
generated
2053
client/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -3,8 +3,8 @@
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@fluencelabs/fluence": "0.9.46",
|
||||
"@fluencelabs/fluence-network-environment": "1.0.8",
|
||||
"@fluencelabs/fluence": "0.11.0",
|
||||
"@fluencelabs/fluence-network-environment": "^1.0.10",
|
||||
"@testing-library/jest-dom": "^5.11.9",
|
||||
"@testing-library/react": "^11.2.5",
|
||||
"@testing-library/user-event": "^12.6.3",
|
||||
@ -28,7 +28,8 @@
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject",
|
||||
"compile-aqua": "aqua-cli -i ./aqua/ -o ./src/aqua/"
|
||||
"compile-aqua": "aqua -i ./aqua -o ./src/_aqua",
|
||||
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\""
|
||||
},
|
||||
"eslintConfig": {
|
||||
"parser": "@typescript-eslint/parser",
|
||||
@ -53,8 +54,9 @@
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"@fluencelabs/aqua-cli": "^0.1.1-105",
|
||||
"@fluencelabs/aqua-lib": "0.1.1",
|
||||
"@types/lodash": "^4.14.168"
|
||||
"@fluencelabs/aqua": "^0.3.0-224",
|
||||
"@fluencelabs/aqua-lib": "^0.1.14",
|
||||
"@types/lodash": "^4.14.168",
|
||||
"chokidar-cli": "^3.0.0"
|
||||
}
|
||||
}
|
||||
|
1156
client/src/_aqua/app.ts
Normal file
1156
client/src/_aqua/app.ts
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,8 +0,0 @@
|
||||
import { FluenceClient } from '@fluencelabs/fluence';
|
||||
import { createContext, useContext } from 'react';
|
||||
|
||||
export const FluenceClientContext = createContext<FluenceClient | null>(null);
|
||||
|
||||
export const useFluenceClient = () => {
|
||||
return useContext(FluenceClientContext);
|
||||
};
|
@ -1,12 +1,5 @@
|
||||
import config from 'src/app.json';
|
||||
import { testNet } from '@fluencelabs/fluence-network-environment';
|
||||
|
||||
export const fluentPadServiceId = 'fluence/fluent-pad';
|
||||
|
||||
export const notifyOnlineFnName = 'notifyOnline';
|
||||
export const notifyUserAddedFnName = 'notifyUserAdded';
|
||||
export const notifyUserRemovedFnName = 'notifyUserRemoved';
|
||||
export const notifyTextUpdateFnName = 'notifyTextUpdate';
|
||||
import { krasnodar } from '@fluencelabs/fluence-network-environment';
|
||||
|
||||
export const userList = {
|
||||
peer_id: config.services.user_list.node,
|
||||
@ -23,8 +16,4 @@ export const fluentPadApp = {
|
||||
history: history,
|
||||
};
|
||||
|
||||
// export const relayNode = testNet[0];
|
||||
export const relayNode = {
|
||||
multiaddr: '/ip4/127.0.0.1/tcp/4310/ws/p2p/12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3',
|
||||
peerId: '12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3',
|
||||
};
|
||||
export const relayNode = krasnodar[0];
|
||||
|
@ -1,791 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: 0.1.1-105
|
||||
*
|
||||
*/
|
||||
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
|
||||
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
|
||||
|
||||
export async function join(
|
||||
client: FluenceClient,
|
||||
user: { name: string; peer_id: string; relay_id: string },
|
||||
): Promise<{ err_msg: string; ret_code: number }> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{ err_msg: string; ret_code: number }>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "user") [] user)
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.user_list.peer_id! (app.$.user_list.service_id! "join") [user] res)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'user', () => {
|
||||
return user;
|
||||
});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for join');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
export async function getUserList(
|
||||
client: FluenceClient,
|
||||
): Promise<{ name: string; peer_id: string; relay_id: string }[]> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{ name: string; peer_id: string; relay_id: string }[]>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.user_list.peer_id! (app.$.user_list.service_id! "get_users") [] allUsers)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [allUsers.$.users!])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for getUserList');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
export async function initAfterJoin(
|
||||
client: FluenceClient,
|
||||
me: { name: string; peer_id: string; relay_id: string },
|
||||
): Promise<{ name: string; peer_id: string; relay_id: string }[]> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{ name: string; peer_id: string; relay_id: string }[]>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "me") [] me)
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.user_list.peer_id! (app.$.user_list.service_id! "get_users") [] allUsers)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(fold allUsers.$.users! user
|
||||
(par
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(xor
|
||||
(call user.$.relay_id! ("peer" "is_connected") [user.$.peer_id!] isOnline)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(par
|
||||
(xor
|
||||
(match isOnline true
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call user.$.peer_id! ("fluence/fluent-pad" "notifyUserAdded") [me true])
|
||||
(seq
|
||||
(seq
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(seq
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("fluence/fluent-pad" "notifyUserAdded") [user isOnline])
|
||||
)
|
||||
)
|
||||
(next user)
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [allUsers])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
|
||||
)
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'me', () => {
|
||||
return me;
|
||||
});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for initAfterJoin');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
export async function updateOnlineStatuses(client: FluenceClient): Promise<void> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.user_list.peer_id! (app.$.user_list.service_id! "get_users") [] allUsers)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(fold allUsers.$.users! user
|
||||
(par
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call user.$.peer_id! ("peer" "is_connected") [user.$.peer_id!] isOnline)
|
||||
(seq
|
||||
(seq
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("fluence/fluent-pad" "notifyOnline") [user.$.peer_id! isOnline])
|
||||
)
|
||||
(next user)
|
||||
)
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for updateOnlineStatuses');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return Promise.race([promise, Promise.resolve()]);
|
||||
}
|
||||
|
||||
export async function leave(client: FluenceClient): Promise<void> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.user_list.peer_id! (app.$.user_list.service_id! "leave") [%init_peer_id%] res)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app0)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app0.$.user_list.peer_id! (app0.$.user_list.service_id! "get_users") [] allUsers)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(fold allUsers.$.users! user
|
||||
(par
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call user.$.peer_id! ("fluence/fluent-pad" "notifyUserRemoved") [%init_peer_id%])
|
||||
(seq
|
||||
(seq
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("op" "identity") [])
|
||||
)
|
||||
(next user)
|
||||
)
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for leave');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return Promise.race([promise, Promise.resolve()]);
|
||||
}
|
||||
|
||||
export async function auth(
|
||||
client: FluenceClient,
|
||||
): Promise<{ err_msg: string; is_authenticated: boolean; ret_code: number }> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{ err_msg: string; is_authenticated: boolean; ret_code: number }>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.user_list.peer_id! (app.$.user_list.service_id! "is_authenticated") [] res)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for auth');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
export async function getHistory(
|
||||
client: FluenceClient,
|
||||
): Promise<{ entries: { body: string; id: number }[]; err_msg: string; ret_code: number }> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{ entries: { body: string; id: number }[]; err_msg: string; ret_code: number }>(
|
||||
(resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app0)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app0.$.user_list.peer_id! (app0.$.user_list.service_id! "is_authenticated") [] res0)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.history.peer_id! (app.$.history.service_id! "get_all") [res0.$.is_authenticated!] res)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||
)
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for getHistory');
|
||||
})
|
||||
.build();
|
||||
},
|
||||
);
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
export async function addEntry(
|
||||
client: FluenceClient,
|
||||
entry: string,
|
||||
): Promise<{ entry_id: number; err_msg: string; ret_code: number }> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{ entry_id: number; err_msg: string; ret_code: number }>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "entry") [] entry)
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app)
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app0)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app0.$.user_list.peer_id! (app0.$.user_list.service_id! "is_authenticated") [] res0)
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app.$.history.peer_id! (app.$.history.service_id! "add") [entry res0.$.is_authenticated!] res)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("fluence/get-config" "getApp") [] app1)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call app1.$.user_list.peer_id! (app1.$.user_list.service_id! "get_users") [] allUsers)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(fold allUsers.$.users! user
|
||||
(par
|
||||
(xor
|
||||
(mismatch user.$.peer_id! %init_peer_id%
|
||||
(seq
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
)
|
||||
(xor
|
||||
(call user.$.peer_id! ("fluence/fluent-pad" "notifyTextUpdate") [entry res0.$.is_authenticated!])
|
||||
(seq
|
||||
(seq
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(null)
|
||||
)
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call user.$.relay_id! ("op" "identity") [])
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(next user)
|
||||
)
|
||||
(call -relay- ("op" "identity") [])
|
||||
)
|
||||
(call %init_peer_id% ("op" "identity") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
|
||||
)
|
||||
)
|
||||
(seq
|
||||
(call -relay- ("op" "identity") [])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 6])
|
||||
)
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'entry', () => {
|
||||
return entry;
|
||||
});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for addEntry');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
@ -1,49 +1,50 @@
|
||||
import { createClient, FluenceClient } from '@fluencelabs/fluence';
|
||||
import { FluencePeer } from '@fluencelabs/fluence';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
import './App.scss';
|
||||
|
||||
import { FluenceClientContext } from '../app/FluenceClientContext';
|
||||
import { UserList } from './UserList';
|
||||
import { CollaborativeEditor } from './CollaborativeEditor';
|
||||
import { fluentPadApp, relayNode } from 'src/app/constants';
|
||||
import { CheckResponse, withErrorHandlingAsync } from './util';
|
||||
import { join, leave } from 'src/aqua/app';
|
||||
import { join, leave, registerAppConfig } from 'src/_aqua/app';
|
||||
|
||||
const createClientEx = async (relay) => {
|
||||
const client = await createClient(relay);
|
||||
client.aquaCallHandler.on('fluence/get-config', 'getApp', () => {
|
||||
return fluentPadApp;
|
||||
});
|
||||
client.aquaCallHandler.on('fluence/get-config', 'get_init_peer_id', () => {
|
||||
return client.selfPeerId;
|
||||
});
|
||||
client.aquaCallHandler.on('fluence/get-config', 'get_init_relay', () => {
|
||||
return client.relayPeerId!;
|
||||
});
|
||||
return client;
|
||||
};
|
||||
|
||||
const App = () => {
|
||||
const [client, setClient] = useState<FluenceClient | null>(null);
|
||||
const [isConnected, setIsConnected] = useState<boolean>(false);
|
||||
const [isInRoom, setIsInRoom] = useState<boolean>(false);
|
||||
const [nickName, setNickName] = useState('');
|
||||
|
||||
const connect = async () => {
|
||||
try {
|
||||
await FluencePeer.default.init({ connectTo: relayNode });
|
||||
|
||||
setIsConnected(true);
|
||||
|
||||
registerAppConfig({
|
||||
getApp: () => {
|
||||
return fluentPadApp
|
||||
},
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
console.log('Peer initialization failed', err)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
createClientEx(relayNode)
|
||||
.then((client) => setClient(client))
|
||||
.catch((err) => console.log('Client initialization failed', err));
|
||||
connect()
|
||||
}, []);
|
||||
|
||||
const joinRoom = async () => {
|
||||
if (!client) {
|
||||
if (!isConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
await withErrorHandlingAsync(async () => {
|
||||
const res = await join(client, {
|
||||
peer_id: client.selfPeerId,
|
||||
relay_id: client.relayPeerId!,
|
||||
const res = await join( {
|
||||
peer_id: FluencePeer.default.connectionInfo.selfPeerId,
|
||||
relay_id: FluencePeer.default.connectionInfo.connectedRelay!,
|
||||
name: nickName,
|
||||
});
|
||||
if (CheckResponse(res)) {
|
||||
@ -53,18 +54,18 @@ const App = () => {
|
||||
};
|
||||
|
||||
const leaveRoom = async () => {
|
||||
if (!client) {
|
||||
if (!isConnected) {
|
||||
return;
|
||||
}
|
||||
|
||||
await withErrorHandlingAsync(async () => {
|
||||
await leave(client);
|
||||
await leave();
|
||||
setIsInRoom(false);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<FluenceClientContext.Provider value={client}>
|
||||
<>
|
||||
<div className="header-wrapper">
|
||||
<div className="header">
|
||||
<div className="header-item">
|
||||
@ -76,7 +77,7 @@ const App = () => {
|
||||
</div>
|
||||
|
||||
<div className="header-item">
|
||||
Connection status: {client ? <span className="accent">connected</span> : 'disconnected'}
|
||||
Connection status: {isConnected ? <span className="accent">connected</span> : 'disconnected'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -106,7 +107,7 @@ const App = () => {
|
||||
<input
|
||||
type="submit"
|
||||
className="join-button"
|
||||
disabled={isInRoom || !client || !nickName}
|
||||
disabled={isInRoom || !isConnected || !nickName}
|
||||
value="Join"
|
||||
/>
|
||||
</form>
|
||||
@ -121,7 +122,7 @@ const App = () => {
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</FluenceClientContext.Provider>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -1,12 +1,9 @@
|
||||
import _ from 'lodash';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { PeerIdB58, subscribeToEvent } from '@fluencelabs/fluence';
|
||||
|
||||
import { fluentPadServiceId, notifyTextUpdateFnName } from 'src/app/constants';
|
||||
import { useFluenceClient } from '../app/FluenceClientContext';
|
||||
import { getUpdatedDocFromText, initDoc, SyncClient } from '../app/sync';
|
||||
import { withErrorHandlingAsync } from './util';
|
||||
import { addEntry, getHistory } from 'src/aqua/app';
|
||||
import { addEntry, getHistory, registerTextState } from 'src/_aqua/app';
|
||||
|
||||
const broadcastUpdates = _.debounce((text: string, syncClient: SyncClient) => {
|
||||
let doc = syncClient.getDoc();
|
||||
@ -17,7 +14,6 @@ const broadcastUpdates = _.debounce((text: string, syncClient: SyncClient) => {
|
||||
}, 100);
|
||||
|
||||
export const CollaborativeEditor = () => {
|
||||
const client = useFluenceClient()!;
|
||||
const [text, setText] = useState<string | null>(null);
|
||||
const [syncClient, setSyncClient] = useState(new SyncClient());
|
||||
|
||||
@ -28,7 +24,7 @@ export const CollaborativeEditor = () => {
|
||||
|
||||
syncClient.handleSendChanges = (changes: string) => {
|
||||
withErrorHandlingAsync(async () => {
|
||||
const res = await addEntry(client, changes);
|
||||
const res = await addEntry(changes);
|
||||
if (res.ret_code !== 0) {
|
||||
throw new Error(
|
||||
`Failed to add message to history service, code=${res.ret_code}, message=${res.err_msg}`,
|
||||
@ -37,19 +33,19 @@ export const CollaborativeEditor = () => {
|
||||
});
|
||||
};
|
||||
|
||||
const unsub = subscribeToEvent(client, fluentPadServiceId, notifyTextUpdateFnName, (args, tetraplets) => {
|
||||
const [changes, isAuthorized] = args as [string, boolean];
|
||||
|
||||
registerTextState({
|
||||
notifyTextUpdate: (changes, isAuthorized) => {
|
||||
if (changes) {
|
||||
syncClient.receiveChanges(changes);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
syncClient.start();
|
||||
|
||||
// don't block
|
||||
withErrorHandlingAsync(async () => {
|
||||
const res = await getHistory(client);
|
||||
const res = await getHistory();
|
||||
for (let e of res.entries) {
|
||||
syncClient.receiveChanges(e.body);
|
||||
}
|
||||
@ -60,7 +56,6 @@ export const CollaborativeEditor = () => {
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsub();
|
||||
syncClient.stop();
|
||||
};
|
||||
}, []);
|
||||
|
@ -1,14 +1,10 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import {
|
||||
fluentPadServiceId,
|
||||
notifyOnlineFnName,
|
||||
notifyUserAddedFnName,
|
||||
notifyUserRemovedFnName,
|
||||
} from 'src/app/constants';
|
||||
import { useFluenceClient } from '../app/FluenceClientContext';
|
||||
import { PeerIdB58, subscribeToEvent } from '@fluencelabs/fluence';
|
||||
|
||||
|
||||
import { withErrorHandlingAsync } from './util';
|
||||
import { initAfterJoin, updateOnlineStatuses } from 'src/aqua/app';
|
||||
import { initAfterJoin, updateOnlineStatuses } from 'src/_aqua/app';
|
||||
import { registerUserStatus } from 'src/_aqua/app';
|
||||
import { FluencePeer, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
|
||||
interface User {
|
||||
id: PeerIdB58;
|
||||
@ -16,16 +12,9 @@ interface User {
|
||||
isOnline: boolean;
|
||||
}
|
||||
|
||||
interface ApiUser {
|
||||
name: string;
|
||||
peer_id: string;
|
||||
relay_id: string;
|
||||
}
|
||||
|
||||
const refreshOnlineStatusTimeoutMs = 10000;
|
||||
|
||||
export const UserList = (props: { selfName: string }) => {
|
||||
const client = useFluenceClient()!;
|
||||
const [users, setUsers] = useState<Map<PeerIdB58, User>>(new Map());
|
||||
|
||||
const updateOnlineStatus = (user, onlineStatus) => {
|
||||
@ -42,12 +31,15 @@ export const UserList = (props: { selfName: string }) => {
|
||||
useEffect(() => {
|
||||
const listRefreshTimer = setInterval(() => {
|
||||
withErrorHandlingAsync(async () => {
|
||||
// await updateOnlineStatuses(client);
|
||||
await updateOnlineStatuses();
|
||||
});
|
||||
}, refreshOnlineStatusTimeoutMs);
|
||||
|
||||
const unsub1 = subscribeToEvent(client, fluentPadServiceId, notifyUserAddedFnName, (args, _) => {
|
||||
const [user, isOnline] = args as [ApiUser, boolean];
|
||||
registerUserStatus({
|
||||
notifyOnline: (user, onlineStatus) => {
|
||||
updateOnlineStatus(user, onlineStatus);
|
||||
},
|
||||
notifyUserAdded: (user, isOnline) => {
|
||||
setUsers((prev) => {
|
||||
const u = user;
|
||||
const result = new Map(prev);
|
||||
@ -63,36 +55,29 @@ export const UserList = (props: { selfName: string }) => {
|
||||
|
||||
return result;
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
const unsub2 = subscribeToEvent(client, fluentPadServiceId, notifyUserRemovedFnName, (args, _) => {
|
||||
const [userLeft] = args as [PeerIdB58];
|
||||
notifyUserRemoved: (userLeft) => {
|
||||
setUsers((prev) => {
|
||||
const result = new Map(prev);
|
||||
result.delete(userLeft);
|
||||
return result;
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
const unsub3 = subscribeToEvent(client, fluentPadServiceId, notifyOnlineFnName, (args, _) => {
|
||||
const [user, onlineStatus] = args as [PeerIdB58, boolean];
|
||||
updateOnlineStatus(user, onlineStatus);
|
||||
});
|
||||
|
||||
// don't block
|
||||
withErrorHandlingAsync(async () => {
|
||||
await initAfterJoin(client, {
|
||||
await initAfterJoin({
|
||||
name: props.selfName,
|
||||
peer_id: client.selfPeerId,
|
||||
relay_id: client.relayPeerId!,
|
||||
peer_id: FluencePeer.default.connectionInfo.selfPeerId,
|
||||
relay_id: FluencePeer.default.connectionInfo.connectedRelay!,
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
clearTimeout(listRefreshTimer);
|
||||
unsub1();
|
||||
unsub2();
|
||||
unsub3();
|
||||
};
|
||||
}, []);
|
||||
|
||||
@ -105,7 +90,7 @@ export const UserList = (props: { selfName: string }) => {
|
||||
<ul>
|
||||
{usersArray.map((x) => (
|
||||
<li key={x.id}>
|
||||
<span className={x.id === client.selfPeerId ? 'bold' : ''}>{x.name}</span>
|
||||
<span className={x.id === FluencePeer.default.connectionInfo.selfPeerId ? 'bold' : ''}>{x.name}</span>
|
||||
<span className={x.isOnline ? 'green' : 'red'}> ({x.isOnline ? 'online' : 'offline'})</span>
|
||||
</li>
|
||||
))}
|
||||
|
3
deploy.sh
Executable file
3
deploy.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
fldist deploy_app --env krasnodar -s Fs6nQaGEsM5EgnprUbUtoLYWhUC8o6QK1gseP9pfhzUm -i app/app.config.json -o client/src/app.json
|
@ -1,5 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
docker kill fluence_node
|
||||
docker run -d --rm --name fluence_node -e RUST_LOG="info" -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj
|
||||
fldist deploy_app --env local -s Fs6nQaGEsM5EgnprUbUtoLYWhUC8o6QK1gseP9pfhzUm -i app/app.config.json -o client/src/app.json
|
@ -1,5 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
docker kill fluence_node
|
||||
docker run -d --rm --name fluence_node -e RUST_LOG="info" -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj
|
||||
fldist deploy_app --env local -s Fs6nQaGEsM5EgnprUbUtoLYWhUC8o6QK1gseP9pfhzUm -i app/app.config.json -o client/src/app.json
|
Loading…
x
Reference in New Issue
Block a user