From 992eca5fc167fab3989ef14f9961b9487cdcff15 Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 08:42:49 -0600 Subject: [PATCH 01/31] before axe ws tests --- packages/use-arcade/package.json | 7 ++- .../hooks/__tests__/useArcadeRelay.test.ts | 45 +++++++++++++++++++ .../use-arcade/src/hooks/useArcadeRelay.ts | 32 +++++++++---- .../src/hooks/websocketTestUtils.ts | 12 +++++ yarn.lock | 45 +++++++++++++++++++ 5 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts create mode 100644 packages/use-arcade/src/hooks/websocketTestUtils.ts diff --git a/packages/use-arcade/package.json b/packages/use-arcade/package.json index 90a8152..59ee934 100644 --- a/packages/use-arcade/package.json +++ b/packages/use-arcade/package.json @@ -14,7 +14,7 @@ "lint": "eslint ./src --max-warnings 0 --cache", "lint:fix": "yarn lint --fix", "tsc": "tsc", - "test": "jest", + "test": "jest --detectOpenHandles --runInBand", "test:watch": "yarn test --watch", "build": "microbundle --no-compress" }, @@ -23,6 +23,7 @@ "@testing-library/react-hooks": "8.0.1", "@types/jest": "26.0.15", "@types/react": "18.0.15", + "@types/ws": "8.5.3", "@typescript-eslint/eslint-plugin": "4.6.0", "@typescript-eslint/parser": "4.6.0", "babel-eslint": "10.1.0", @@ -37,6 +38,7 @@ "eslint-plugin-react": "7.21.5", "eslint-plugin-react-hooks": "4.2.0", "jest": "26.6.3", + "jest-websocket-mock": "2.3.0", "microbundle": "0.15.0", "prettier": "2.1.2", "react": "18.0.0", @@ -48,5 +50,8 @@ }, "peerDependencies": { "react": ">=18.0.0" + }, + "dependencies": { + "ws": "8.8.1" } } diff --git a/packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts b/packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts new file mode 100644 index 0000000..e35eef6 --- /dev/null +++ b/packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts @@ -0,0 +1,45 @@ +import WS from 'jest-websocket-mock' +// import WebSocket from 'ws' +import { act, renderHook } from '@testing-library/react-hooks' +import { useArcadeRelay } from '../useArcadeRelay' + +// import { waitForSocketState } from '../websocketTestUtils' + +let ws: WS + +beforeEach(async () => { + ws = new WS('ws://localhost:8088') + // const { result } = renderHook(() => useArcadeRelay()) + // ws = result.current[0] + // await waitForSocketState(ws, ws.OPEN) + // console.log(ws) +}) + +afterEach(() => { + WS.clean() +}) + +describe('useArcadeRelay', () => { + // afterAll(async () => { + // ws.close() + // await waitForSocketState(ws, ws.CLOSED) + // console.log('Relay closed!') + // }) + + it('should set true', () => { + const { result } = renderHook(() => useArcadeRelay()) + const [, , actions] = result.current + + act(() => actions.doSomething()) + console.log(ws) + // console.log(result.current) + + // expect(result.current[0]).toBe(false) + + // act(setPause(true)) + + // act(() => actions.setTrue()) + + expect(true).toBe(true) + }) +}) diff --git a/packages/use-arcade/src/hooks/useArcadeRelay.ts b/packages/use-arcade/src/hooks/useArcadeRelay.ts index 8870995..2c2acbb 100644 --- a/packages/use-arcade/src/hooks/useArcadeRelay.ts +++ b/packages/use-arcade/src/hooks/useArcadeRelay.ts @@ -1,15 +1,26 @@ -import { useEffect, useRef, useState } from 'react' +import 'websocket-polyfill' +import { SetStateAction, useEffect, useRef, useState } from 'react' +import WebSocket from 'ws' -export const useArcadeRelay = () => { - const [isPaused, setPause] = useState(false) +type VoidFunction = () => void + +export type UseArcadeRelayActions = { + setPause: React.Dispatch> + doSomething: VoidFunction +} + +type UseArcadeRelayFunction = () => [any, boolean, UseArcadeRelayActions] + +export const useArcadeRelay: UseArcadeRelayFunction = () => { + const [isPaused, setPause] = useState(false) const ws = useRef(null) useEffect(() => { ws.current = new WebSocket('wss://relay.arcade.city') - ws.current.onopen = () => { - console.log('ws opened. attempting subscribe') - ws.current?.send(JSON.stringify(['REQ', Math.random().toString().slice(2), { kind: 60 }])) - } + // ws.current.onopen = () => { + // console.log('ws opened. attempting subscribe') + // ws.current?.send(JSON.stringify(['REQ', Math.random().toString().slice(2), { kind: 60 }])) + // } ws.current.onclose = () => console.log('ws closed') const wsCurrent = ws.current @@ -22,12 +33,15 @@ export const useArcadeRelay = () => { useEffect(() => { if (!ws.current) return - ws.current.onmessage = (e) => { + ws.current.onmessage = (e: any) => { if (isPaused) return const message = JSON.parse(e.data) console.log(message) } }, [isPaused]) - return [isPaused, setPause] + const doSomething = () => {} + const actions: UseArcadeRelayActions = { setPause, doSomething } + + return [ws, isPaused, actions] } diff --git a/packages/use-arcade/src/hooks/websocketTestUtils.ts b/packages/use-arcade/src/hooks/websocketTestUtils.ts new file mode 100644 index 0000000..307c562 --- /dev/null +++ b/packages/use-arcade/src/hooks/websocketTestUtils.ts @@ -0,0 +1,12 @@ +// @ts-nocheck +export function waitForSocketState(socket, state) { + return new Promise(function (resolve) { + setTimeout(function () { + if (socket.readyState === state) { + resolve() + } else { + waitForSocketState(socket, state).then(resolve) + } + }, 5) + }) +} diff --git a/yarn.lock b/yarn.lock index 195a58a..47f9eaf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3174,6 +3174,13 @@ resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.17.0.tgz#f99ce359f1bfd87da90cc4a57cab0a18f34a48d0" integrity sha512-eHSaNYEyxRA5IAG0Ym/yCyf86niZUIF/TpWKofQI/CVfh5HsMEUyfE2kwFxha4ow0s5g0LfISQxpDKjbRDrizw== +"@types/ws@8.5.3": + version "8.5.3" + resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d" + integrity sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w== + dependencies: + "@types/node" "*" + "@types/yargs-parser@*": version "21.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b" @@ -5292,6 +5299,11 @@ diff-sequences@^26.6.2: resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1" integrity sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q== +diff-sequences@^27.5.1: + version "27.5.1" + resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-27.5.1.tgz#eaecc0d327fd68c8d9672a1e64ab8dccb2ef5327" + integrity sha512-k1gCAXAsNgLwEL+Y8Wvl+M6oEFj5bgazfZULpS5CneoPPXRaCCW7dm+q21Ky2VEE5X+VeRDBVg1Pcvvsr4TtNQ== + diffie-hellman@^5.0.0: version "5.0.3" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875" @@ -7824,6 +7836,16 @@ jest-diff@^26.0.0, jest-diff@^26.6.2: jest-get-type "^26.3.0" pretty-format "^26.6.2" +jest-diff@^27.0.2: + version "27.5.1" + resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-27.5.1.tgz#a07f5011ac9e6643cf8a95a462b7b1ecf6680def" + integrity sha512-m0NvkX55LDt9T4mctTEgnZk3fmEg3NRYutvMPWM/0iPnkFj2wIeF45O1718cMSOFO1vINkqmxqD8vE37uTEbqw== + dependencies: + chalk "^4.0.0" + diff-sequences "^27.5.1" + jest-get-type "^27.5.1" + pretty-format "^27.5.1" + jest-docblock@^26.0.0: version "26.0.0" resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-26.0.0.tgz#3e2fa20899fc928cb13bd0ff68bd3711a36889b5" @@ -7887,6 +7909,11 @@ jest-get-type@^26.3.0: resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-26.3.0.tgz#e97dc3c3f53c2b406ca7afaed4493b1d099199e0" integrity sha512-TpfaviN1R2pQWkIihlfEanwOXK0zcxrKEE4MlU6Tn7keoXdN6/3gK/xl0yEh8DOunn5pOVGKf8hB4R9gVh04ig== +jest-get-type@^27.5.1: + version "27.5.1" + resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-27.5.1.tgz#3cd613c507b0f7ace013df407a1c1cd578bcb4f1" + integrity sha512-2KY95ksYSaK7DMBWQn6dQz3kqAf3BB64y2udeG+hv4KfSOb9qwcYQstTJc1KCbsix+wLZWZYN8t7nwX3GOBLRw== + jest-haste-map@^26.6.2: version "26.6.2" resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-26.6.2.tgz#dd7e60fe7dc0e9f911a23d79c5ff7fb5c2cafeaa" @@ -8227,6 +8254,14 @@ jest-watcher@^27.0.0: jest-util "^27.5.1" string-length "^4.0.1" +jest-websocket-mock@2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/jest-websocket-mock/-/jest-websocket-mock-2.3.0.tgz#317e7d7f8ba54ba632a7300777b02b7ebb606845" + integrity sha512-kXhRRApRdT4hLG/4rhsfcR0Ke0OzqIsDj0P5t0dl5aiAftShSgoRqp/0pyjS5bh+b9GrIzmfkrV2cn9LxxvSvA== + dependencies: + jest-diff "^27.0.2" + mock-socket "^9.1.0" + jest-worker@^26.2.1, jest-worker@^26.6.2: version "26.6.2" resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-26.6.2.tgz#7f72cbc4d643c365e27b9fd775f9d0eaa9c7a8ed" @@ -9304,6 +9339,11 @@ mobx@6.6.1: resolved "https://registry.yarnpkg.com/mobx/-/mobx-6.6.1.tgz#70ee6aa82f25aeb7e7d522bd621207434e509318" integrity sha512-7su3UZv5JF+ohLr2opabjbUAERfXstMY+wiBtey8yNAPoB8H187RaQXuhFjNkH8aE4iHbDWnhDFZw0+5ic4nGQ== +mock-socket@^9.1.0: + version "9.1.5" + resolved "https://registry.yarnpkg.com/mock-socket/-/mock-socket-9.1.5.tgz#2c4e44922ad556843b6dfe09d14ed8041fa2cdeb" + integrity sha512-3DeNIcsQixWHHKk6NdoBhWI4t1VMj5/HzfnI1rE/pLl5qKx7+gd4DNA07ehTaZ6MoUU053si6Hd+YtiM/tQZfg== + moment@2.29.4: version "2.29.4" resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108" @@ -13266,6 +13306,11 @@ write@1.0.3: dependencies: mkdirp "^0.5.1" +ws@8.8.1: + version "8.8.1" + resolved "https://registry.yarnpkg.com/ws/-/ws-8.8.1.tgz#5dbad0feb7ade8ecc99b830c1d77c913d4955ff0" + integrity sha512-bGy2JzvzkPowEJV++hF07hAD6niYSr0JzBNo/J29WsB57A2r7Wlc1UFcTR9IzrPvuNVO4B8LGqF8qcpsVOhJCA== + ws@^6.1.4: version "6.2.2" resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.2.tgz#dd5cdbd57a9979916097652d78f1cc5faea0c32e" From 61b29509e56e5caf5b5feb4bbf91be7542c0da86 Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 08:43:11 -0600 Subject: [PATCH 02/31] axe that for now --- .../hooks/__tests__/useArcadeRelay.test.ts | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts diff --git a/packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts b/packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts deleted file mode 100644 index e35eef6..0000000 --- a/packages/use-arcade/src/hooks/__tests__/useArcadeRelay.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import WS from 'jest-websocket-mock' -// import WebSocket from 'ws' -import { act, renderHook } from '@testing-library/react-hooks' -import { useArcadeRelay } from '../useArcadeRelay' - -// import { waitForSocketState } from '../websocketTestUtils' - -let ws: WS - -beforeEach(async () => { - ws = new WS('ws://localhost:8088') - // const { result } = renderHook(() => useArcadeRelay()) - // ws = result.current[0] - // await waitForSocketState(ws, ws.OPEN) - // console.log(ws) -}) - -afterEach(() => { - WS.clean() -}) - -describe('useArcadeRelay', () => { - // afterAll(async () => { - // ws.close() - // await waitForSocketState(ws, ws.CLOSED) - // console.log('Relay closed!') - // }) - - it('should set true', () => { - const { result } = renderHook(() => useArcadeRelay()) - const [, , actions] = result.current - - act(() => actions.doSomething()) - console.log(ws) - // console.log(result.current) - - // expect(result.current[0]).toBe(false) - - // act(setPause(true)) - - // act(() => actions.setTrue()) - - expect(true).toBe(true) - }) -}) From 0d544404fc15496e79cb86408c3e1715571ede9a Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 09:12:41 -0600 Subject: [PATCH 03/31] Axe server ws --- packages/use-arcade/package.json | 10 ++-- .../use-arcade/src/hooks/useArcadeRelay.ts | 21 ++++---- yarn.lock | 51 +++---------------- 3 files changed, 20 insertions(+), 62 deletions(-) diff --git a/packages/use-arcade/package.json b/packages/use-arcade/package.json index 59ee934..43e6f4e 100644 --- a/packages/use-arcade/package.json +++ b/packages/use-arcade/package.json @@ -14,16 +14,16 @@ "lint": "eslint ./src --max-warnings 0 --cache", "lint:fix": "yarn lint --fix", "tsc": "tsc", - "test": "jest --detectOpenHandles --runInBand", + "test": "jest", "test:watch": "yarn test --watch", - "build": "microbundle --no-compress" + "build": "microbundle --no-compress", + "dev": "microbundle watch --no-compress" }, "devDependencies": { "@testing-library/react": "13.3.0", "@testing-library/react-hooks": "8.0.1", "@types/jest": "26.0.15", "@types/react": "18.0.15", - "@types/ws": "8.5.3", "@typescript-eslint/eslint-plugin": "4.6.0", "@typescript-eslint/parser": "4.6.0", "babel-eslint": "10.1.0", @@ -51,7 +51,5 @@ "peerDependencies": { "react": ">=18.0.0" }, - "dependencies": { - "ws": "8.8.1" - } + "dependencies": {} } diff --git a/packages/use-arcade/src/hooks/useArcadeRelay.ts b/packages/use-arcade/src/hooks/useArcadeRelay.ts index 2c2acbb..62b8505 100644 --- a/packages/use-arcade/src/hooks/useArcadeRelay.ts +++ b/packages/use-arcade/src/hooks/useArcadeRelay.ts @@ -1,15 +1,15 @@ import 'websocket-polyfill' import { SetStateAction, useEffect, useRef, useState } from 'react' -import WebSocket from 'ws' -type VoidFunction = () => void +export type UseArcadeRelayState = { + isPaused: boolean +} export type UseArcadeRelayActions = { setPause: React.Dispatch> - doSomething: VoidFunction } -type UseArcadeRelayFunction = () => [any, boolean, UseArcadeRelayActions] +type UseArcadeRelayFunction = () => [UseArcadeRelayState, UseArcadeRelayActions] export const useArcadeRelay: UseArcadeRelayFunction = () => { const [isPaused, setPause] = useState(false) @@ -17,10 +17,7 @@ export const useArcadeRelay: UseArcadeRelayFunction = () => { useEffect(() => { ws.current = new WebSocket('wss://relay.arcade.city') - // ws.current.onopen = () => { - // console.log('ws opened. attempting subscribe') - // ws.current?.send(JSON.stringify(['REQ', Math.random().toString().slice(2), { kind: 60 }])) - // } + ws.current.onopen = () => console.log('ws opened') ws.current.onclose = () => console.log('ws closed') const wsCurrent = ws.current @@ -33,15 +30,15 @@ export const useArcadeRelay: UseArcadeRelayFunction = () => { useEffect(() => { if (!ws.current) return - ws.current.onmessage = (e: any) => { + ws.current.onmessage = (e) => { if (isPaused) return const message = JSON.parse(e.data) console.log(message) } }, [isPaused]) - const doSomething = () => {} - const actions: UseArcadeRelayActions = { setPause, doSomething } + const state: UseArcadeRelayState = { isPaused } + const actions: UseArcadeRelayActions = { setPause } - return [ws, isPaused, actions] + return [state, actions] } diff --git a/yarn.lock b/yarn.lock index 47f9eaf..10ca15a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1540,28 +1540,7 @@ node-forge "^1.2.1" nullthrows "^1.1.1" -"@expo/config-plugins@4.1.5", "@expo/config-plugins@~4.1.5": - version "4.1.5" - resolved "https://registry.yarnpkg.com/@expo/config-plugins/-/config-plugins-4.1.5.tgz#9d357d2cda9c095e511b51583ede8a3b76174068" - integrity sha512-RVvU40RtZt12HavuDAe+LDIq9lHj7sheOfMEHdmpJ/uTA8pgvkbc56XF6JHQD+yRr6+uhhb+JnAasGq49dsQbw== - dependencies: - "@expo/config-types" "^45.0.0" - "@expo/json-file" "8.2.36" - "@expo/plist" "0.0.18" - "@expo/sdk-runtime-versions" "^1.0.0" - "@react-native/normalize-color" "^2.0.0" - chalk "^4.1.2" - debug "^4.3.1" - find-up "~5.0.0" - getenv "^1.0.0" - glob "7.1.6" - resolve-from "^5.0.0" - semver "^7.3.5" - slash "^3.0.0" - xcode "^3.0.1" - xml2js "0.4.23" - -"@expo/config-plugins@~5.0.0": +"@expo/config-plugins@4.1.5", "@expo/config-plugins@^5.0.0", "@expo/config-plugins@~4.1.5", "@expo/config-plugins@~5.0.0": version "5.0.0" resolved "https://registry.yarnpkg.com/@expo/config-plugins/-/config-plugins-5.0.0.tgz#19f699aafa5809756b9be055189a14842f8da7ae" integrity sha512-Bgjgv64f/XqpXXKPAoGhc5dbmuJB8eOBkhV6FMI/RMP06HfL7EQvXgcBBoJThLAZVyd29XikFgaCvABt/NavxQ== @@ -2140,6 +2119,11 @@ "@jridgewell/resolve-uri" "^3.0.3" "@jridgewell/sourcemap-codec" "^1.4.10" +"@noble/hashes@1.1.2": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.2.tgz#e9e035b9b166ca0af657a7848eb2718f0f22f183" + integrity sha512-KYRCASVTv6aeUi1tsF8/vpyR7zpfs3FUzy2Jqm+MU+LmUKhQ0y2FpfwqkCcxSg2ua4GALJd8k2R76WxwZGbQpA== + "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" @@ -3174,13 +3158,6 @@ resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.17.0.tgz#f99ce359f1bfd87da90cc4a57cab0a18f34a48d0" integrity sha512-eHSaNYEyxRA5IAG0Ym/yCyf86niZUIF/TpWKofQI/CVfh5HsMEUyfE2kwFxha4ow0s5g0LfISQxpDKjbRDrizw== -"@types/ws@8.5.3": - version "8.5.3" - resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d" - integrity sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w== - dependencies: - "@types/node" "*" - "@types/yargs-parser@*": version "21.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b" @@ -11006,22 +10983,13 @@ react-testing-library@8.0.1: resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-8.0.1.tgz#b3dd43bce3fa88423cf0a23292fb819023c227cc" integrity sha512-Gq4JC9r3prA4hYwo7afcbHHMFckO29+5Nrh2KblAEPuK/DWaU0bJE1vtpAgLhzhY9bBirmcgjjIHljHEwGAXKw== -react@18.0.0: +react@18.0.0, react@^16.8.3: version "18.0.0" resolved "https://registry.yarnpkg.com/react/-/react-18.0.0.tgz#b468736d1f4a5891f38585ba8e8fb29f91c3cb96" integrity sha512-x+VL6wbT4JRVPm7EGxXhZ8w8LTROaxPXOqhlGyVSrv0sB1jkyFGgXxJ8LVoPRLvPR6/CIZGFmfzqUa2NYeMr2A== dependencies: loose-envify "^1.1.0" -react@^16.8.3: - version "16.14.0" - resolved "https://registry.yarnpkg.com/react/-/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d" - integrity sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g== - dependencies: - loose-envify "^1.1.0" - object-assign "^4.1.1" - prop-types "^15.6.2" - reactotron-core-client@2.8.10: version "2.8.10" resolved "https://registry.yarnpkg.com/reactotron-core-client/-/reactotron-core-client-2.8.10.tgz#798f2a7aa9fd7e18e7a510531a613e8ae3008eb0" @@ -13306,11 +13274,6 @@ write@1.0.3: dependencies: mkdirp "^0.5.1" -ws@8.8.1: - version "8.8.1" - resolved "https://registry.yarnpkg.com/ws/-/ws-8.8.1.tgz#5dbad0feb7ade8ecc99b830c1d77c913d4955ff0" - integrity sha512-bGy2JzvzkPowEJV++hF07hAD6niYSr0JzBNo/J29WsB57A2r7Wlc1UFcTR9IzrPvuNVO4B8LGqF8qcpsVOhJCA== - ws@^6.1.4: version "6.2.2" resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.2.tgz#dd5cdbd57a9979916097652d78f1cc5faea0c32e" From f374271cb0d5800d00bcf21d75aa0f3bb0ab3f10 Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 09:34:53 -0600 Subject: [PATCH 04/31] Add use-arcade store --- packages/use-arcade/.gitignore | 2 +- packages/use-arcade/package.json | 4 +- packages/use-arcade/src/store/index.ts | 20 ++++++++++ packages/use-arcade/src/types/index.ts | 1 + packages/use-arcade/src/types/nostr.ts | 31 +++++++++++++++ yarn.lock | 52 ++++++++++++++++++++++---- 6 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 packages/use-arcade/src/store/index.ts create mode 100644 packages/use-arcade/src/types/index.ts create mode 100644 packages/use-arcade/src/types/nostr.ts diff --git a/packages/use-arcade/.gitignore b/packages/use-arcade/.gitignore index 3e232ab..5d781bf 100644 --- a/packages/use-arcade/.gitignore +++ b/packages/use-arcade/.gitignore @@ -10,7 +10,7 @@ node_modules # builds build dist -types +/types .rpt2_cache # misc diff --git a/packages/use-arcade/package.json b/packages/use-arcade/package.json index 43e6f4e..8d4c021 100644 --- a/packages/use-arcade/package.json +++ b/packages/use-arcade/package.json @@ -51,5 +51,7 @@ "peerDependencies": { "react": ">=18.0.0" }, - "dependencies": {} + "dependencies": { + "valtio": "1.6.3" + } } diff --git a/packages/use-arcade/src/store/index.ts b/packages/use-arcade/src/store/index.ts new file mode 100644 index 0000000..6fa4094 --- /dev/null +++ b/packages/use-arcade/src/store/index.ts @@ -0,0 +1,20 @@ +import { NostrEvent, NostrKind } from 'types' +import { proxy, useSnapshot } from 'valtio' +import { proxyMap } from 'valtio/utils' + +interface Store { + events: Map +} + +export const store = proxy({ + events: proxyMap(), +}) + +export const addEvent = (event: NostrEvent) => { + store.events.set(event.id, event) +} + +export const useRideRequests = () => { + const snapshot = useSnapshot(store) + return Array.from(snapshot.events.values()).filter((event) => event.kind === NostrKind.riderequest) +} diff --git a/packages/use-arcade/src/types/index.ts b/packages/use-arcade/src/types/index.ts new file mode 100644 index 0000000..e69da0f --- /dev/null +++ b/packages/use-arcade/src/types/index.ts @@ -0,0 +1 @@ +export * from './nostr' diff --git a/packages/use-arcade/src/types/nostr.ts b/packages/use-arcade/src/types/nostr.ts new file mode 100644 index 0000000..045a600 --- /dev/null +++ b/packages/use-arcade/src/types/nostr.ts @@ -0,0 +1,31 @@ +export interface NostrEvent { + // <32-bytes sha256 of the the serialized event data> + id: string + // <32-bytes hex-encoded public key of the event creator> + pubkey: string + // + created_at: number + // (representing the kind of event) + kind: NostrKind + /** + * ["e", <32-bytes hex of the id of another event>, ], + * ["p", <32-bytes hex of the key>, ], + * ... // other kinds of tags may be included later + */ + tags: string[] + // + content: string + // <64-bytes signature of the sha256 hash of the serialized event data, which is the same as the "id" field> + sig: string +} + +export enum NostrKind { + metadata = 0, + text = 1, + contacts = 3, + dm = 4, + delete = 5, + boost = 6, + like = 7, + riderequest = 60, +} diff --git a/yarn.lock b/yarn.lock index 10ca15a..115c320 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1540,7 +1540,28 @@ node-forge "^1.2.1" nullthrows "^1.1.1" -"@expo/config-plugins@4.1.5", "@expo/config-plugins@^5.0.0", "@expo/config-plugins@~4.1.5", "@expo/config-plugins@~5.0.0": +"@expo/config-plugins@4.1.5", "@expo/config-plugins@~4.1.5": + version "4.1.5" + resolved "https://registry.yarnpkg.com/@expo/config-plugins/-/config-plugins-4.1.5.tgz#9d357d2cda9c095e511b51583ede8a3b76174068" + integrity sha512-RVvU40RtZt12HavuDAe+LDIq9lHj7sheOfMEHdmpJ/uTA8pgvkbc56XF6JHQD+yRr6+uhhb+JnAasGq49dsQbw== + dependencies: + "@expo/config-types" "^45.0.0" + "@expo/json-file" "8.2.36" + "@expo/plist" "0.0.18" + "@expo/sdk-runtime-versions" "^1.0.0" + "@react-native/normalize-color" "^2.0.0" + chalk "^4.1.2" + debug "^4.3.1" + find-up "~5.0.0" + getenv "^1.0.0" + glob "7.1.6" + resolve-from "^5.0.0" + semver "^7.3.5" + slash "^3.0.0" + xcode "^3.0.1" + xml2js "0.4.23" + +"@expo/config-plugins@~5.0.0": version "5.0.0" resolved "https://registry.yarnpkg.com/@expo/config-plugins/-/config-plugins-5.0.0.tgz#19f699aafa5809756b9be055189a14842f8da7ae" integrity sha512-Bgjgv64f/XqpXXKPAoGhc5dbmuJB8eOBkhV6FMI/RMP06HfL7EQvXgcBBoJThLAZVyd29XikFgaCvABt/NavxQ== @@ -2119,11 +2140,6 @@ "@jridgewell/resolve-uri" "^3.0.3" "@jridgewell/sourcemap-codec" "^1.4.10" -"@noble/hashes@1.1.2": - version "1.1.2" - resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.2.tgz#e9e035b9b166ca0af657a7848eb2718f0f22f183" - integrity sha512-KYRCASVTv6aeUi1tsF8/vpyR7zpfs3FUzy2Jqm+MU+LmUKhQ0y2FpfwqkCcxSg2ua4GALJd8k2R76WxwZGbQpA== - "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" @@ -10551,6 +10567,11 @@ prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2: object-assign "^4.1.1" react-is "^16.13.1" +proxy-compare@2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/proxy-compare/-/proxy-compare-2.2.0.tgz#2357330407241b091501644d08a446a4a28e0e15" + integrity sha512-hEtMJevUmOByZCTw1NRUVaWWHCGJLO0ogizpey8yX6zMPolDe8YVa+PHgMOTiZuyUkFj+hMKs/1UaM0+ZkuvgA== + proxy-from-env@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" @@ -10983,13 +11004,22 @@ react-testing-library@8.0.1: resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-8.0.1.tgz#b3dd43bce3fa88423cf0a23292fb819023c227cc" integrity sha512-Gq4JC9r3prA4hYwo7afcbHHMFckO29+5Nrh2KblAEPuK/DWaU0bJE1vtpAgLhzhY9bBirmcgjjIHljHEwGAXKw== -react@18.0.0, react@^16.8.3: +react@18.0.0: version "18.0.0" resolved "https://registry.yarnpkg.com/react/-/react-18.0.0.tgz#b468736d1f4a5891f38585ba8e8fb29f91c3cb96" integrity sha512-x+VL6wbT4JRVPm7EGxXhZ8w8LTROaxPXOqhlGyVSrv0sB1jkyFGgXxJ8LVoPRLvPR6/CIZGFmfzqUa2NYeMr2A== dependencies: loose-envify "^1.1.0" +react@^16.8.3: + version "16.14.0" + resolved "https://registry.yarnpkg.com/react/-/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d" + integrity sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g== + dependencies: + loose-envify "^1.1.0" + object-assign "^4.1.1" + prop-types "^15.6.2" + reactotron-core-client@2.8.10: version "2.8.10" resolved "https://registry.yarnpkg.com/reactotron-core-client/-/reactotron-core-client-2.8.10.tgz#798f2a7aa9fd7e18e7a510531a613e8ae3008eb0" @@ -13047,6 +13077,14 @@ validate-npm-package-name@^3.0.0: dependencies: builtins "^1.0.3" +valtio@1.6.3: + version "1.6.3" + resolved "https://registry.yarnpkg.com/valtio/-/valtio-1.6.3.tgz#7ea4aa72cd3deb14f18283ecb1ef2e4f802899f6" + integrity sha512-ySg2q5nFFSY0zSm/a/sxEGHshWGGvelo4m4y/oAlO92fuXZ9WHSi0J214P3ZDjDiveGp08wck0qJZcA2ubEj+Q== + dependencies: + proxy-compare "2.2.0" + use-sync-external-store "1.2.0" + vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" From cdc27a24b7e7432ae198337cc49730699ded1d1c Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 09:42:41 -0600 Subject: [PATCH 05/31] Initial subscribe --- apps/nostr-client/App.tsx | 9 ++++++- .../use-arcade/src/hooks/useArcadeRelay.ts | 27 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/apps/nostr-client/App.tsx b/apps/nostr-client/App.tsx index 1b9dbc4..ac786a0 100644 --- a/apps/nostr-client/App.tsx +++ b/apps/nostr-client/App.tsx @@ -1,4 +1,5 @@ import { StatusBar } from 'expo-status-bar' +import { useEffect } from 'react' import { SafeAreaProvider } from 'react-native-safe-area-context' import { useArcadeRelay } from '@arcadecity/use-arcade' import useCachedResources from './hooks/useCachedResources' @@ -8,7 +9,13 @@ import Navigation from './navigation' export const App = () => { const isLoadingComplete = useCachedResources() const colorScheme = useColorScheme() - useArcadeRelay() + const [state, actions] = useArcadeRelay() + + useEffect(() => { + if (state.ready) { + actions.initialSubscribe() + } + }, [state.ready]) if (!isLoadingComplete) { return null diff --git a/packages/use-arcade/src/hooks/useArcadeRelay.ts b/packages/use-arcade/src/hooks/useArcadeRelay.ts index 62b8505..b7e2764 100644 --- a/packages/use-arcade/src/hooks/useArcadeRelay.ts +++ b/packages/use-arcade/src/hooks/useArcadeRelay.ts @@ -3,9 +3,11 @@ import { SetStateAction, useEffect, useRef, useState } from 'react' export type UseArcadeRelayState = { isPaused: boolean + ready: boolean } export type UseArcadeRelayActions = { + initialSubscribe: () => void setPause: React.Dispatch> } @@ -13,12 +15,19 @@ type UseArcadeRelayFunction = () => [UseArcadeRelayState, UseArcadeRelayActions] export const useArcadeRelay: UseArcadeRelayFunction = () => { const [isPaused, setPause] = useState(false) + const [ready, setReady] = useState(false) const ws = useRef(null) useEffect(() => { ws.current = new WebSocket('wss://relay.arcade.city') - ws.current.onopen = () => console.log('ws opened') - ws.current.onclose = () => console.log('ws closed') + ws.current.onopen = () => { + console.log('ws opened') + setReady(true) + } + ws.current.onclose = () => { + console.log('ws closed') + setReady(false) + } const wsCurrent = ws.current @@ -37,8 +46,18 @@ export const useArcadeRelay: UseArcadeRelayFunction = () => { } }, [isPaused]) - const state: UseArcadeRelayState = { isPaused } - const actions: UseArcadeRelayActions = { setPause } + const initialSubscribe = () => { + if (!ws.current) return + if (ws.current.readyState !== WebSocket.OPEN) { + console.log('Not ready.') + setReady(false) + return + } + ws.current.send(JSON.stringify(['REQ', Math.random().toString().slice(2), { kind: 60 }])) + } + + const state: UseArcadeRelayState = { isPaused, ready } + const actions: UseArcadeRelayActions = { initialSubscribe, setPause } return [state, actions] } From 54781787a75ba13df70b52633ea4109c6dc2d487 Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 10:00:37 -0600 Subject: [PATCH 06/31] Fix type of useRideRequests --- apps/nostr-client/App.tsx | 5 ++--- packages/use-arcade/src/hooks/useArcadeRelay.ts | 16 +++++++++++++++- packages/use-arcade/src/store/index.ts | 4 +++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/apps/nostr-client/App.tsx b/apps/nostr-client/App.tsx index ac786a0..80a8a7f 100644 --- a/apps/nostr-client/App.tsx +++ b/apps/nostr-client/App.tsx @@ -12,9 +12,8 @@ export const App = () => { const [state, actions] = useArcadeRelay() useEffect(() => { - if (state.ready) { - actions.initialSubscribe() - } + if (!state.ready) return + actions.initialSubscribe() }, [state.ready]) if (!isLoadingComplete) { diff --git a/packages/use-arcade/src/hooks/useArcadeRelay.ts b/packages/use-arcade/src/hooks/useArcadeRelay.ts index b7e2764..7249fff 100644 --- a/packages/use-arcade/src/hooks/useArcadeRelay.ts +++ b/packages/use-arcade/src/hooks/useArcadeRelay.ts @@ -1,5 +1,7 @@ import 'websocket-polyfill' import { SetStateAction, useEffect, useRef, useState } from 'react' +import { addEvent } from '../store' +import { NostrEvent } from '../types' export type UseArcadeRelayState = { isPaused: boolean @@ -42,7 +44,8 @@ export const useArcadeRelay: UseArcadeRelayFunction = () => { ws.current.onmessage = (e) => { if (isPaused) return const message = JSON.parse(e.data) - console.log(message) + const event = message[2] + addEvent(event) } }, [isPaused]) @@ -61,3 +64,14 @@ export const useArcadeRelay: UseArcadeRelayFunction = () => { return [state, actions] } + +export const normalizeEvent = (event: any) => + ({ + id: event.nid, + pubkey: event.pubkey, + created_at: event.created_at, + kind: event.kind, + tags: event.tags ?? [], + content: event.content, + sig: event.sig, + } as NostrEvent) diff --git a/packages/use-arcade/src/store/index.ts b/packages/use-arcade/src/store/index.ts index 6fa4094..5cc9d1a 100644 --- a/packages/use-arcade/src/store/index.ts +++ b/packages/use-arcade/src/store/index.ts @@ -12,9 +12,11 @@ export const store = proxy({ export const addEvent = (event: NostrEvent) => { store.events.set(event.id, event) + console.log(`Stored event ${event.id}`) } export const useRideRequests = () => { const snapshot = useSnapshot(store) - return Array.from(snapshot.events.values()).filter((event) => event.kind === NostrKind.riderequest) + const eventArray = Array.from(snapshot.events.values()) as NostrEvent[] + return eventArray.filter((event: NostrEvent) => event.kind === NostrKind.riderequest) } From e7f8cb0764aadf01e97d281c280d8e17e2a28e55 Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 10:06:22 -0600 Subject: [PATCH 07/31] useRideRequests() working --- apps/nostr-client/screens/TabOneScreen.tsx | 20 +++++++++++--------- packages/use-arcade/src/index.ts | 2 ++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/apps/nostr-client/screens/TabOneScreen.tsx b/apps/nostr-client/screens/TabOneScreen.tsx index cae37f5..e4e7ede 100644 --- a/apps/nostr-client/screens/TabOneScreen.tsx +++ b/apps/nostr-client/screens/TabOneScreen.tsx @@ -1,17 +1,19 @@ -import { StyleSheet } from 'react-native'; - -import EditScreenInfo from '../components/EditScreenInfo'; -import { Text, View } from '../components/Themed'; -import { RootTabScreenProps } from '../types'; +import { StyleSheet } from 'react-native' +import { useRideRequests } from '@arcadecity/use-arcade' +import EditScreenInfo from '../components/EditScreenInfo' +import { Text, View } from '../components/Themed' +import { RootTabScreenProps } from '../types' export default function TabOneScreen({ navigation }: RootTabScreenProps<'TabOne'>) { + const rideRequests = useRideRequests() + console.log(rideRequests.length) return ( Tab One - - + + - ); + ) } const styles = StyleSheet.create({ @@ -29,4 +31,4 @@ const styles = StyleSheet.create({ height: 1, width: '80%', }, -}); +}) diff --git a/packages/use-arcade/src/index.ts b/packages/use-arcade/src/index.ts index c36813b..5de59cd 100644 --- a/packages/use-arcade/src/index.ts +++ b/packages/use-arcade/src/index.ts @@ -1,4 +1,6 @@ export { useArcadeRelay } from './hooks/useArcadeRelay' export { useBoolean } from './hooks/useBoolean' +export { store, useRideRequests } from './store' + export type { UseBoolean, UseBooleanActions } from './hooks/useBoolean' From bca8dd64e689c315c412be74855377fb0337bcbb Mon Sep 17 00:00:00 2001 From: Christopher David Date: Tue, 2 Aug 2022 10:19:58 -0600 Subject: [PATCH 08/31] Try pull in UI, eh --- apps/nostr-client/package.json | 1 + apps/nostr-client/screens/TabOneScreen.tsx | 15 +++++---- yarn.lock | 39 ++++------------------ 3 files changed, 16 insertions(+), 39 deletions(-) diff --git a/apps/nostr-client/package.json b/apps/nostr-client/package.json index 9e62ad0..21a4919 100644 --- a/apps/nostr-client/package.json +++ b/apps/nostr-client/package.json @@ -13,6 +13,7 @@ "preset": "jest-expo" }, "dependencies": { + "@arcadecity/ui": "*", "@arcadecity/use-arcade": "*", "@expo/vector-icons": "^13.0.0", "@react-navigation/bottom-tabs": "^6.0.5", diff --git a/apps/nostr-client/screens/TabOneScreen.tsx b/apps/nostr-client/screens/TabOneScreen.tsx index e4e7ede..d826452 100644 --- a/apps/nostr-client/screens/TabOneScreen.tsx +++ b/apps/nostr-client/screens/TabOneScreen.tsx @@ -1,17 +1,18 @@ -import { StyleSheet } from 'react-native' -import { useRideRequests } from '@arcadecity/use-arcade' -import EditScreenInfo from '../components/EditScreenInfo' +import { Button, StyleSheet } from 'react-native' +import { useArcadeRelay, useRideRequests } from '@arcadecity/use-arcade' import { Text, View } from '../components/Themed' import { RootTabScreenProps } from '../types' export default function TabOneScreen({ navigation }: RootTabScreenProps<'TabOne'>) { const rideRequests = useRideRequests() - console.log(rideRequests.length) + // useArcadeRelay() + // const createChannel = () => { + // console.log('create channel') + // } return ( - Tab One - - + {rideRequests.length} requests + {/*