mirror of
https://github.com/urbit/shrub.git
synced 2024-11-28 05:22:27 +03:00
Merge remote-tracking branch 'origin/master' into release/next-sys
This commit is contained in:
commit
d15d832ef4
@ -26,6 +26,6 @@
|
||||
<div id="portal-root"></div>
|
||||
<script src="/~landscape/js/channel.js"></script>
|
||||
<script src="/~landscape/js/session.js"></script>
|
||||
<script src="/~btc/js/bundle/index.3e8bcc150ebd820dd3b2.js"></script>
|
||||
<script src="/~btc/js/bundle/index.02730169f3d73fd22950.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -5,8 +5,8 @@
|
||||
/- glob, *resource
|
||||
/+ default-agent, verb, dbug
|
||||
|%
|
||||
++ landscape-hash 0vrbiqe.v6al2.0b4jc.u9vp7.k1e0i
|
||||
++ btc-wallet-hash 0v7.v4dng.o33qi.kc497.5jc02.ke5es
|
||||
++ landscape-hash 0v4.56ejl.qq4js.h1i0m.t3e94.bg6qr
|
||||
++ btc-wallet-hash 0v2.ifoe4.fbv35.aigir.66su4.fbspu
|
||||
+$ state-0 [%0 hash=@uv glob=(unit (each glob:glob tid=@ta))]
|
||||
+$ state-1 [%1 =globs:glob]
|
||||
+$ all-states
|
||||
|
@ -24,6 +24,6 @@
|
||||
<div id="portal-root"></div>
|
||||
<script src="/~landscape/js/channel.js"></script>
|
||||
<script src="/~landscape/js/session.js"></script>
|
||||
<script src="/~landscape/js/bundle/index.8074ae0006fba19803f5.js"></script>
|
||||
<script src="/~landscape/js/bundle/index.306e3d6fb700897c1bc1.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -25,8 +25,6 @@
|
||||
^- (list @tas)
|
||||
:~ %group-store
|
||||
%metadata-store
|
||||
%contact-store
|
||||
%contact-hook
|
||||
%invite-store
|
||||
%graph-store
|
||||
==
|
||||
|
@ -5,4 +5,4 @@ dojo:
|
||||
|
||||
it should return with the following hash:
|
||||
|
||||
`0v7.v4dng.o33qi.kc497.5jc02.ke5es`
|
||||
`0v2.ifoe4.fbv35.aigir.66su4.fbspu`
|
||||
|
@ -103,6 +103,9 @@ const addMembers = (json: GroupUpdate, state: GroupState): GroupState => {
|
||||
if ('addMembers' in json) {
|
||||
const { resource, ships } = json.addMembers;
|
||||
const resourcePath = resourceAsPath(resource);
|
||||
if(!(resourcePath in state.groups)) {
|
||||
return;
|
||||
}
|
||||
for (const member of ships) {
|
||||
state.groups[resourcePath].members.add(member);
|
||||
if (
|
||||
|
@ -177,9 +177,6 @@ const ChatEditor = React.forwardRef<CodeMirrorShim, ChatEditorProps>(({ inCodeMo
|
||||
}, [inCodeMode, placeholder]);
|
||||
|
||||
function messageChange(editor, data, value) {
|
||||
if(value.endsWith('/')) {
|
||||
editor.showHint(['test', 'foo']);
|
||||
}
|
||||
if (message !== '' && value == '') {
|
||||
setMessage(value);
|
||||
}
|
||||
|
@ -138,7 +138,8 @@ class ChatWindow extends Component<
|
||||
}
|
||||
if(this.unreadSet &&
|
||||
this.dismissedInitialUnread() &&
|
||||
this.virtualList!.startOffset() < 5) {
|
||||
this.virtualList!.startOffset() < 5 &&
|
||||
document.hasFocus()) {
|
||||
this.props.dismissUnread();
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ function GroupRoutes(props: { group: string; url: string }) {
|
||||
return null;
|
||||
}
|
||||
if(!graphKeys.has(`${ship.slice(1)}/${name}`)) {
|
||||
if(graphKeys.size > 0) {
|
||||
if(graphKeys.size > 1) { // TODO: Better loading logic see https://github.com/urbit/landscape/issues/1063
|
||||
return <Redirect
|
||||
to={toQuery(
|
||||
{ auto: 'y', redir: location.pathname },
|
||||
|
@ -2,7 +2,7 @@ import { BaseAnchor, Box, Center, Col, Icon, Row, Text } from '@tlon/indigo-reac
|
||||
import { Association, GraphNode, resourceFromPath, GraphConfig } from '@urbit/api';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import _ from 'lodash';
|
||||
import { useHistory, useLocation } from 'react-router-dom';
|
||||
import { Link, useLocation } from 'react-router-dom';
|
||||
import {
|
||||
getPermalinkForGraph, GraphPermalink as IGraphPermalink, parsePermalink
|
||||
} from '~/logic/lib/permalinks';
|
||||
@ -76,7 +76,6 @@ function GraphPermalink(
|
||||
}
|
||||
) {
|
||||
const { full = false, showOurContact, pending, graph, group, index, transcluded } = props;
|
||||
const history = useHistory();
|
||||
const location = useLocation();
|
||||
const { ship, name } = resourceFromPath(graph);
|
||||
const node = useGraphState(
|
||||
@ -114,11 +113,6 @@ function GraphPermalink(
|
||||
const showTransclusion = Boolean(association && node && transcluded < 1);
|
||||
const permalink = getPermalinkForGraph(group, graph, index);
|
||||
|
||||
const navigate = (e) => {
|
||||
e.stopPropagation();
|
||||
history.push(`/perma${permalink.slice(16)}`);
|
||||
};
|
||||
|
||||
const [nodeGroupHost, nodeGroupName] = association?.group.split('/').slice(-2) ?? ['Unknown', 'Unknown'];
|
||||
const [nodeChannelHost, nodeChannelName] = association?.resource
|
||||
.split('/')
|
||||
@ -141,15 +135,16 @@ function GraphPermalink(
|
||||
|
||||
return (
|
||||
<Col
|
||||
as={Link}
|
||||
to={`/perma${permalink.slice(16)}`}
|
||||
width="100%"
|
||||
bg="white"
|
||||
maxWidth={full ? null : '500px'}
|
||||
border={full ? null : '1'}
|
||||
borderColor="lightGray"
|
||||
borderRadius={2}
|
||||
cursor="pointer"
|
||||
onClick={(e) => {
|
||||
navigate(e);
|
||||
e.stopPropagation();
|
||||
}}
|
||||
>
|
||||
{loading && association && !errored && Placeholder((association.metadata.config as GraphConfig).graph)}
|
||||
@ -167,7 +162,6 @@ function GraphPermalink(
|
||||
showTransclusion={showTransclusion}
|
||||
icon={getModuleIcon((association.metadata.config as GraphConfig).graph as GraphModule)}
|
||||
title={association.metadata.title}
|
||||
permalink={permalink}
|
||||
/>
|
||||
)}
|
||||
{association && isInSameResource && transcluded === 2 && !loading && (
|
||||
@ -176,7 +170,6 @@ function GraphPermalink(
|
||||
showTransclusion={showTransclusion}
|
||||
icon={getModuleIcon((association.metadata.config as GraphConfig).graph as GraphModule)}
|
||||
title={association.metadata.title}
|
||||
permalink={permalink}
|
||||
/>
|
||||
)}
|
||||
{isInSameResource && transcluded !== 2 && !loading && <Row height='2' />}
|
||||
@ -185,7 +178,6 @@ function GraphPermalink(
|
||||
icon="Groups"
|
||||
showDetails={false}
|
||||
title={graph.slice(5)}
|
||||
permalink={permalink}
|
||||
/>
|
||||
)}
|
||||
</Col>
|
||||
@ -195,7 +187,6 @@ function GraphPermalink(
|
||||
function PermalinkDetails(props: {
|
||||
title: string;
|
||||
icon: any;
|
||||
permalink: string;
|
||||
showTransclusion?: boolean;
|
||||
showDetails?: boolean;
|
||||
known?: boolean;
|
||||
|
@ -110,6 +110,7 @@ return false;
|
||||
group={group}
|
||||
isRelativeTime
|
||||
></Author>
|
||||
{!post.pending &&
|
||||
<Box opacity={hovering ? '100%' : '0%'}>
|
||||
<Dropdown
|
||||
alignX="right"
|
||||
@ -154,6 +155,7 @@ return false;
|
||||
<Icon icon="Ellipsis" />
|
||||
</Dropdown>
|
||||
</Box>
|
||||
}
|
||||
</Row>
|
||||
<GraphContent
|
||||
borderRadius={1}
|
||||
|
@ -1,24 +1,26 @@
|
||||
import { Box, Col, Icon, Row, Text } from '@tlon/indigo-react';
|
||||
import React, { ReactElement, useCallback } from 'react';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { useModal } from '~/logic/lib/useModal';
|
||||
import useMetadataState, { usePreview } from '~/logic/state/metadata';
|
||||
import { PropFunc } from '~/types';
|
||||
import { JoinGroup } from '../landscape/components/JoinGroup';
|
||||
import { MetadataIcon } from '../landscape/components/MetadataIcon';
|
||||
|
||||
export function GroupLink(
|
||||
props: {
|
||||
type GroupLinkProps = {
|
||||
resource: string;
|
||||
detailed?: boolean;
|
||||
} & PropFunc<typeof Row>
|
||||
): ReactElement {
|
||||
const { resource, ...rest } = props;
|
||||
} & PropFunc<typeof Row>
|
||||
|
||||
export function GroupLink({
|
||||
resource,
|
||||
borderColor,
|
||||
...rest
|
||||
}: GroupLinkProps): ReactElement {
|
||||
const name = resource.slice(6);
|
||||
const joined = useMetadataState(
|
||||
useCallback(s => resource in s.associations.groups, [resource])
|
||||
);
|
||||
const history = useHistory();
|
||||
|
||||
const { modal, showModal } = useModal({
|
||||
modal: <JoinGroup autojoin={name} />
|
||||
@ -27,26 +29,31 @@ export function GroupLink(
|
||||
const { preview } = usePreview(resource);
|
||||
|
||||
return (
|
||||
<Box
|
||||
maxWidth="500px"
|
||||
cursor='pointer'
|
||||
{...rest}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
}}
|
||||
backgroundColor='white'
|
||||
borderColor={props.borderColor}
|
||||
>
|
||||
<>
|
||||
{modal}
|
||||
<Row
|
||||
width="100%"
|
||||
{...rest}
|
||||
as={Link}
|
||||
to={joined ? `/~landscape/ship/${name}` : `/perma/group/${name}`}
|
||||
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
|
||||
e.stopPropagation();
|
||||
|
||||
if (e.metaKey || e.ctrlKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
showModal();
|
||||
}}
|
||||
flexShrink={1}
|
||||
alignItems="center"
|
||||
width="100%"
|
||||
maxWidth="500px"
|
||||
py={2}
|
||||
pr={2}
|
||||
onClick={
|
||||
joined ? () => history.push(`/~landscape/ship/${name}`) : showModal
|
||||
}
|
||||
cursor='pointer'
|
||||
backgroundColor='white'
|
||||
borderColor={borderColor}
|
||||
opacity={preview ? '1' : '0.6'}
|
||||
>
|
||||
<MetadataIcon height={6} width={6} metadata={preview ? preview.metadata : { color: '0x0' , picture: '' }} />
|
||||
@ -70,6 +77,6 @@ export function GroupLink(
|
||||
</Box>
|
||||
</Col>
|
||||
</Row>
|
||||
</Box>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import {
|
||||
Text
|
||||
} from '@tlon/indigo-react';
|
||||
import React, { useRef } from 'react';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { Link, useHistory } from 'react-router-dom';
|
||||
import { Sigil } from '~/logic/lib/sigil';
|
||||
import { uxToHex } from '~/logic/lib/util';
|
||||
import useContactState from '~/logic/state/contact';
|
||||
@ -75,11 +75,12 @@ const StatusBar = (props) => {
|
||||
>
|
||||
<Row>
|
||||
<Button
|
||||
as={Link}
|
||||
to="/"
|
||||
width='32px'
|
||||
borderColor='lightGray'
|
||||
mr={2}
|
||||
px={2}
|
||||
onClick={() => history.push('/')}
|
||||
{...props}
|
||||
>
|
||||
<Icon icon='Dashboard' color='black' />
|
||||
@ -126,9 +127,10 @@ const StatusBar = (props) => {
|
||||
<Icon icon="Bug" color="#000000" />
|
||||
</StatusBarItem>
|
||||
<StatusBarItem
|
||||
as={Link}
|
||||
to="/~landscape/messages"
|
||||
width='32px'
|
||||
mr={2}
|
||||
onClick={() => props.history.push('/~landscape/messages')}
|
||||
>
|
||||
<Icon icon='Messages' />
|
||||
</StatusBarItem>
|
||||
@ -150,24 +152,26 @@ const StatusBar = (props) => {
|
||||
boxShadow='0px 0px 0px 3px'
|
||||
>
|
||||
<Row
|
||||
as={Link}
|
||||
to={`/~profile/~${ship}`}
|
||||
color='black'
|
||||
cursor='pointer'
|
||||
fontSize={1}
|
||||
fontWeight='500'
|
||||
px={3}
|
||||
py={2}
|
||||
onClick={() => history.push(`/~profile/~${ship}`)}
|
||||
>
|
||||
View Profile
|
||||
</Row>
|
||||
<Row
|
||||
as={Link}
|
||||
to="/~settings"
|
||||
color='black'
|
||||
cursor='pointer'
|
||||
fontSize={1}
|
||||
fontWeight='500'
|
||||
px={3}
|
||||
py={2}
|
||||
onClick={() => history.push('/~settings')}
|
||||
>
|
||||
System Preferences
|
||||
</Row>
|
||||
|
@ -1,17 +1,20 @@
|
||||
import { Col, ColProps } from '@tlon/indigo-react';
|
||||
import { Post } from '@urbit/api';
|
||||
import React, { ReactElement } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import React, { ReactElement, useCallback, useState } from 'react';
|
||||
import styled, { css } from 'styled-components';
|
||||
import { GraphContent } from '~/views/landscape/components/Graph/GraphContent';
|
||||
|
||||
type TruncateProps = ColProps & {
|
||||
truncate?: number;
|
||||
truncate: boolean;
|
||||
}
|
||||
|
||||
const TruncatedBox = styled(Col)<TruncateProps>`
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: ${p => p.truncate ?? 'unset'};
|
||||
-webkit-box-orient: vertical;
|
||||
${p => p.truncate && css`
|
||||
max-height: 300px;
|
||||
mask-image: linear-gradient(to bottom, rgba(0,0,0,1) 0%, rgba(0,0,0,1) 60%, transparent 100%);
|
||||
`}
|
||||
`;
|
||||
|
||||
interface PostContentProps {
|
||||
@ -20,16 +23,24 @@ interface PostContentProps {
|
||||
isReply: boolean;
|
||||
}
|
||||
|
||||
const PostContent = (props: PostContentProps): ReactElement => {
|
||||
const { post, isParent } = props;
|
||||
const PostContent = ({ post, isParent }: PostContentProps): ReactElement => {
|
||||
const [height, setHeight] = useState(0);
|
||||
const showFade = !isParent && height >= 300;
|
||||
|
||||
const measuredRef = useCallback((node) => {
|
||||
if (node !== null) {
|
||||
setHeight(node.getBoundingClientRect().height);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<TruncatedBox
|
||||
ref={measuredRef}
|
||||
truncate={showFade}
|
||||
display="-webkit-box"
|
||||
width="90%"
|
||||
px={2}
|
||||
pb={2}
|
||||
truncate={isParent ? null : 8}
|
||||
textOverflow="ellipsis"
|
||||
overflow="hidden"
|
||||
>
|
||||
|
2
pkg/npm/api/package-lock.json
generated
2
pkg/npm/api/package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@urbit/api",
|
||||
"version": "1.2.0",
|
||||
"version": "1.3.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@urbit/api",
|
||||
"version": "1.2.0",
|
||||
"version": "1.3.0",
|
||||
"description": "",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -21,7 +21,7 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@types/lodash": "^4.14.168",
|
||||
"@urbit/eslint-config": "^1.0.1",
|
||||
"@urbit/eslint-config": "^1.0.2",
|
||||
"big-integer": "^1.6.48",
|
||||
"immer": "^9.0.1",
|
||||
"lodash": "^4.17.20",
|
||||
|
2
pkg/npm/eslint-config/package-lock.json
generated
2
pkg/npm/eslint-config/package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@urbit/eslint-config",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@urbit/eslint-config",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"description": "",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
2
pkg/npm/http-api/package-lock.json
generated
2
pkg/npm/http-api/package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@urbit/http-api",
|
||||
"version": "1.2.3",
|
||||
"version": "1.3.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@urbit/http-api",
|
||||
"version": "1.2.3",
|
||||
"version": "1.3.0",
|
||||
"license": "MIT",
|
||||
"description": "Library to interact with an Urbit ship over HTTP",
|
||||
"repository": {
|
||||
|
@ -164,14 +164,18 @@ export class Urbit {
|
||||
/**
|
||||
* Initializes the SSE pipe for the appropriate channel.
|
||||
*/
|
||||
eventSource(): Promise<void> {
|
||||
if(this.lastEventId === 0) {
|
||||
// Can't receive events until the channel is open
|
||||
this.skipDebounce = true;
|
||||
return this.poke({ app: 'hood', mark: 'helm-hi', json: 'Opening API channel' }).then(() => {});
|
||||
async eventSource(): Promise<void> {
|
||||
if(this.sseClientInitialized) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
if(this.lastEventId === 0) {
|
||||
// Can't receive events until the channel is open,
|
||||
// so poke and open then
|
||||
await this.poke({ app: 'hood', mark: 'helm-hi', json: 'Opening API channel' });
|
||||
return;
|
||||
}
|
||||
this.sseClientInitialized = true;
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.sseClientInitialized) {
|
||||
const sseOptions: SSEOptions = {
|
||||
headers: {}
|
||||
};
|
||||
@ -193,6 +197,7 @@ export class Urbit {
|
||||
resolve();
|
||||
return; // everything's good
|
||||
} else {
|
||||
this.onError && this.onError(new Error('bad response'));
|
||||
reject();
|
||||
}
|
||||
},
|
||||
@ -201,15 +206,14 @@ export class Urbit {
|
||||
console.log('Received SSE: ', event);
|
||||
}
|
||||
if (!event.id) return;
|
||||
this.ack(Number(event.id));
|
||||
if (event.data && JSON.parse(event.data)) {
|
||||
|
||||
const data: any = JSON.parse(event.data);
|
||||
|
||||
if (data.response === 'diff') {
|
||||
this.clearQueue();
|
||||
this.lastEventId = parseInt(event.id, 10);
|
||||
if((this.lastEventId - this.lastAcknowledgedEventId) > 20) {
|
||||
this.ack(this.lastEventId);
|
||||
}
|
||||
|
||||
if (event.data && JSON.parse(event.data)) {
|
||||
const data: any = JSON.parse(event.data);
|
||||
|
||||
if (data.response === 'poke' && this.outstandingPokes.has(data.id)) {
|
||||
const funcs = this.outstandingPokes.get(data.id);
|
||||
if (data.hasOwnProperty('ok')) {
|
||||
@ -221,8 +225,8 @@ export class Urbit {
|
||||
console.error('Invalid poke response', data);
|
||||
}
|
||||
this.outstandingPokes.delete(data.id);
|
||||
} else if (data.response === 'subscribe' ||
|
||||
(data.response === 'poke' && this.outstandingSubscriptions.has(data.id))) {
|
||||
} else if (data.response === 'subscribe'
|
||||
&& this.outstandingSubscriptions.has(data.id)) {
|
||||
const funcs = this.outstandingSubscriptions.get(data.id);
|
||||
if (data.hasOwnProperty('err')) {
|
||||
console.error(data.err);
|
||||
@ -231,7 +235,11 @@ export class Urbit {
|
||||
}
|
||||
} else if (data.response === 'diff' && this.outstandingSubscriptions.has(data.id)) {
|
||||
const funcs = this.outstandingSubscriptions.get(data.id);
|
||||
try {
|
||||
funcs.event(data.json);
|
||||
} catch (e) {
|
||||
console.error('Failed to call subscription event callback', e);
|
||||
}
|
||||
} else if (data.response === 'quit' && this.outstandingSubscriptions.has(data.id)) {
|
||||
const funcs = this.outstandingSubscriptions.get(data.id);
|
||||
funcs.quit(data);
|
||||
@ -257,10 +265,7 @@ export class Urbit {
|
||||
|
||||
},
|
||||
});
|
||||
this.sseClientInitialized = true;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@ -272,12 +277,6 @@ export class Urbit {
|
||||
this.abort.abort();
|
||||
this.abort = new AbortController();
|
||||
this.uid = `${Math.floor(Date.now() / 1000)}-${hexString(6)}`;
|
||||
if(this.debounceTimer) {
|
||||
clearTimeout(this.debounceTimer);
|
||||
this.debounceTimer = null;
|
||||
}
|
||||
this.calm = true;
|
||||
this.outstandingJSON = [];
|
||||
this.lastEventId = 0;
|
||||
this.lastAcknowledgedEventId = 0;
|
||||
this.outstandingSubscriptions = new Map();
|
||||
@ -299,6 +298,7 @@ export class Urbit {
|
||||
* @param eventId The event to acknowledge.
|
||||
*/
|
||||
private async ack(eventId: number): Promise<number | void> {
|
||||
this.lastAcknowledgedEventId = eventId;
|
||||
const message: Message = {
|
||||
action: 'ack',
|
||||
'event-id': eventId
|
||||
@ -307,91 +307,19 @@ export class Urbit {
|
||||
return eventId;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a wrapper method that can be used to send any action with data.
|
||||
*
|
||||
* Every message sent has some common parameters, like method, headers, and data
|
||||
* structure, so this method exists to prevent duplication.
|
||||
*
|
||||
* @param action The action to send
|
||||
* @param data The data to send with the action
|
||||
*
|
||||
* @returns void | number If successful, returns the number of the message that was sent
|
||||
*/
|
||||
// async sendMessage(action: Action, data?: object): Promise<number | void> {
|
||||
// const id = this.getEventId();
|
||||
// if (this.verbose) {
|
||||
// console.log(`Sending message ${id}:`, action, data,);
|
||||
// }
|
||||
// const message: Message = { id, action, ...data };
|
||||
// await this.sendJSONtoChannel(message);
|
||||
// return id;
|
||||
// }
|
||||
|
||||
private outstandingJSON: Message[] = [];
|
||||
|
||||
private debounceTimer: NodeJS.Timeout = null;
|
||||
private debounceInterval = 10;
|
||||
private skipDebounce = false;
|
||||
private calm = true;
|
||||
|
||||
private sendJSONtoChannel(json: Message): Promise<boolean | void> {
|
||||
this.outstandingJSON.push(json);
|
||||
return this.processQueue();
|
||||
}
|
||||
|
||||
private processQueue(): Promise<boolean | void> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const process = async () => {
|
||||
if (this.calm) {
|
||||
if (this.outstandingJSON.length === 0) resolve(true);
|
||||
this.calm = false; // We are now occupied
|
||||
const json = this.outstandingJSON;
|
||||
const body = JSON.stringify(json);
|
||||
this.outstandingJSON = [];
|
||||
if (body === '[]') {
|
||||
this.calm = true;
|
||||
return resolve(false);
|
||||
}
|
||||
try {
|
||||
private async sendJSONtoChannel(...json: Message[]): Promise<void> {
|
||||
const response = await fetch(this.channelUrl, {
|
||||
...this.fetchOptions,
|
||||
method: 'PUT',
|
||||
body
|
||||
body: JSON.stringify(json)
|
||||
});
|
||||
if(!response.ok) {
|
||||
throw new Error('failed to PUT');
|
||||
throw new Error('Failed to PUT channel');
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
json.forEach(failed => this.outstandingJSON.push(failed));
|
||||
if (this.onError) {
|
||||
this.onError(error);
|
||||
} else {
|
||||
throw error;
|
||||
if(!this.sseClientInitialized) {
|
||||
await this.eventSource();
|
||||
}
|
||||
}
|
||||
this.calm = true;
|
||||
if (!this.sseClientInitialized) {
|
||||
this.eventSource().then(resolve); // We can open the channel for subscriptions once we've sent data over it
|
||||
}
|
||||
resolve(true);
|
||||
} else {
|
||||
clearTimeout(this.debounceTimer);
|
||||
this.debounceTimer = setTimeout(process, this.debounceInterval);
|
||||
resolve(false);
|
||||
}
|
||||
}
|
||||
if(this.skipDebounce) {
|
||||
process();
|
||||
this.skipDebounce = false;
|
||||
}
|
||||
|
||||
this.debounceTimer = setTimeout(process, this.debounceInterval);
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a subscription, waits for a fact and then unsubscribes
|
||||
@ -433,24 +361,6 @@ export class Urbit {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
// resetDebounceTimer() {
|
||||
// if (this.debounceTimer) {
|
||||
// clearTimeout(this.debounceTimer);
|
||||
// this.debounceTimer = null;
|
||||
// }
|
||||
// this.calm = false;
|
||||
// this.debounceTimer = setTimeout(() => {
|
||||
// this.calm = true;
|
||||
// }, this.debounceInterval);
|
||||
// }
|
||||
|
||||
clearQueue() {
|
||||
clearTimeout(this.debounceTimer);
|
||||
this.debounceTimer = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pokes a ship with data.
|
||||
*
|
||||
@ -458,7 +368,7 @@ export class Urbit {
|
||||
* @param mark The mark of the data being sent
|
||||
* @param json The data to send
|
||||
*/
|
||||
poke<T>(params: PokeInterface<T>): Promise<number> {
|
||||
async poke<T>(params: PokeInterface<T>): Promise<number> {
|
||||
const {
|
||||
app,
|
||||
mark,
|
||||
@ -472,7 +382,6 @@ export class Urbit {
|
||||
ship: this.ship,
|
||||
...params
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
const message: Message = {
|
||||
id: this.getEventId(),
|
||||
action: 'poke',
|
||||
@ -481,6 +390,9 @@ export class Urbit {
|
||||
mark,
|
||||
json
|
||||
};
|
||||
const [send, result] = await Promise.all([
|
||||
this.sendJSONtoChannel(message),
|
||||
new Promise<number>((resolve, reject) => {
|
||||
this.outstandingPokes.set(message.id, {
|
||||
onSuccess: () => {
|
||||
onSuccess();
|
||||
@ -491,10 +403,9 @@ export class Urbit {
|
||||
reject(event.err);
|
||||
}
|
||||
});
|
||||
this.sendJSONtoChannel(message).then(() => {
|
||||
resolve(message.id);
|
||||
});
|
||||
});
|
||||
})
|
||||
]);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,7 @@
|
||||
|
||||
import Urbit from '../src';
|
||||
import { Readable } from 'streams';
|
||||
import 'jest';
|
||||
|
||||
function fakeSSE(messages = [], timeout = 0) {
|
||||
const ourMessages = [...messages];
|
||||
@ -60,7 +62,6 @@ describe('Initialisation', () => {
|
||||
let fetchSpy;
|
||||
beforeEach(() => {
|
||||
airlock = new Urbit('', '+code');
|
||||
airlock.debounceInterval = 10;
|
||||
});
|
||||
afterEach(() => {
|
||||
fetchSpy.mockReset();
|
||||
@ -73,7 +74,7 @@ describe('Initialisation', () => {
|
||||
Promise.resolve({ ok: true, body: fakeSSE() })
|
||||
)
|
||||
.mockImplementationOnce(() =>
|
||||
Promise.resolve({ ok: true, body: fakeSSE() })
|
||||
Promise.resolve({ ok: true, body: fakeSSE([ack(1)]) })
|
||||
);
|
||||
await airlock.eventSource();
|
||||
|
||||
@ -82,13 +83,16 @@ describe('Initialisation', () => {
|
||||
it('should handle failures', async () => {
|
||||
fetchSpy = jest.spyOn(window, 'fetch');
|
||||
fetchSpy
|
||||
.mockImplementation(() =>
|
||||
.mockImplementationOnce(() =>
|
||||
Promise.resolve({ ok: true, body: fakeSSE() })
|
||||
)
|
||||
.mockImplementationOnce(() =>
|
||||
Promise.resolve({ ok: false, body: fakeSSE() })
|
||||
)
|
||||
|
||||
airlock.onError = jest.fn();
|
||||
try {
|
||||
await airlock.eventSource();
|
||||
wait(100);
|
||||
} catch (e) {
|
||||
expect(airlock.onError).toHaveBeenCalled();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user