Skip to content

Commit

Permalink
Finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestKz committed Mar 4, 2024
1 parent 7d2ebdd commit 7c04afb
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 191 deletions.
13 changes: 7 additions & 6 deletions icepeak-ts-client/lib/icepeak-core.mts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function createIcepeakCore<FetchTokenError, FetchTokenExtraData> (
config: IcepeakCoreConfig<FetchTokenError, FetchTokenExtraData>
) : IcepeakCore<FetchTokenError, FetchTokenExtraData> {

const icepeakCorePrivate : IcepeakCorePrivate<FetchTokenError, FetchTokenExtraData> = {
const icepeakCorePrivate : IcepeakCorePrivate<FetchTokenError, FetchTokenExtraData> = {
config : config,
state : { pathSubscriptions: {}, wsConnState: { connState: "Uninitialised" } },

Expand Down Expand Up @@ -239,7 +239,7 @@ function connectWs(this: IcepeakCorePrivate<any, any>): Promise<void> {
this.config.logger("Debug", "Connecting to server...")
this.state.wsConnState = { connState: "Connecting" }
const wsConn = this.config.websocketConstructor(this.config.websocketUrl)
wsConn.onopen = _ => { this.connectWsOnOpen(wsConn); resolve()}
wsConn.onopen = _ => { this.connectWsOnOpen(wsConn); resolve() }
wsConn.onerror = e => this.connectWsOnError(e, resolve, reject)
})
}
Expand All @@ -248,9 +248,9 @@ function connectWsOnOpen(this: IcepeakCorePrivate<any, any>, openedWsConn: ws.We
this.config.logger("Debug", "Connected to server.")
const connectedWs: WsConnConnected = { connState: "Connected", wsConn: openedWsConn }
this.state.wsConnState = connectedWs
openedWsConn.onmessage = this.onWsMessageEvent
openedWsConn.onclose = this.onWsErrorOrClose
openedWsConn.onerror = this.onWsErrorOrClose
openedWsConn.onmessage = this.onWsMessageEvent.bind(this)
openedWsConn.onclose = this.onWsErrorOrClose.bind(this)
openedWsConn.onerror = this.onWsErrorOrClose.bind(this)
this.syncSubscribers(connectedWs)
}

Expand Down Expand Up @@ -282,7 +282,7 @@ function onWsMessageEvent(this: IcepeakCorePrivate<any, any>, event: ws.MessageE
return
}
const incomingPayload = mbIncomingPayload.value
this.config.logger("Debug", "Incoming payload.", incomingPayload)
this.config.logger("Debug", "Incoming payload.", [incomingPayload, this.state.pathSubscriptions])
switch (incomingPayload.type) {
case "update": return this.onUpdatePayload(incomingPayload)
case "subscribe": return this.onSubscribeResponse(incomingPayload)
Expand All @@ -298,6 +298,7 @@ function onUpdatePayload(
update : icepeak_payload.ValueUpdate
): void {
if (!(update.path in this.state.pathSubscriptions)) return
console.log(update.path)
const subs = this.state.pathSubscriptions[update.path].subscribers;
for (const sub of subs) sub.onUpdate(update.value);
}
Expand Down
47 changes: 47 additions & 0 deletions icepeak-ts-client/lib/useIcepeak.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export { createUseIcepeak, createCoreUseIcepeak }

import * as icepeak_core from './icepeak-core.mjs';
import * as icepeak from './icepeak.mjs';

import { useState, useEffect } from 'react';

function createUseIcepeak(icepeakObject : icepeak.Icepeak) {

function useIcepeak(path : string) {
const [ icepeakPathValue, setIcepeakPathValue ]
= useState<unknown>(null);

useEffect(() => {
const subscription = icepeakObject
.subscribe(path, setIcepeakPathValue)
return () => subscription.unsubscribe()
})
return icepeakPathValue
}

return useIcepeak
}

function createCoreUseIcepeak<TokenExtraData>(
icepeakCoreObject : icepeak_core.IcepeakCore<any, TokenExtraData>,
) {

function useIcepeak(path : string, tokenExtraData : TokenExtraData) {
const [ icepeakPathValue, setIcepeakPathValue ]
= useState<unknown>(null);

useEffect(() => {
const subscriptionRef
= icepeakCoreObject.createSubscriptionRef(path)

subscriptionRef.onUpdate(setIcepeakPathValue)
subscriptionRef.subscribe(tokenExtraData)

return () => subscriptionRef.unusubscribe()
})
return icepeakPathValue
}

return useIcepeak
}

60 changes: 60 additions & 0 deletions icepeak-ts-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions icepeak-ts-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
"scripts": {
"build": "npx tsc --build lib",
"build-test": "npx tsc --build test",
"test": "npm run build-test && node ./dist/test/icepeak.spec.mjs",
"test-icepeak": "npm run build-test && node ./dist/test/icepeak.spec.mjs",
"test-icepeak-core": "npm run build-test && node ./dist/test/icepeak-core.spec.mjs",
"watch": "npx tsc --watch ./lib/*.mts",
"format": "npx prettier --check ./lib/*.mts",
"format-fix": "npx prettier --write ./lib/*.mts"
"format": "npx prettier --check ./lib/*.mts",
"format-fix": "npx prettier --write ./lib/*.mts"
},
"author": "channable",
"license": "BSD-3-Clause",
"devDependencies": {
"@types/react": "^18.2.61",
"@types/ws": "^8.5.10",
"prettier": "^3.2.5",
"typescript": "^5.3.3",
"ws": "^8.16.0"
},
"dependencies": {
"react": "^18.2.0"
}
}
36 changes: 36 additions & 0 deletions icepeak-ts-client/test/icepeak-core.spec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as test_util from "./test-util.mjs"
import WebSocket from 'ws'

import * as icepeak from "../lib/icepeak.mjs"
import * as icepeak_core from "../lib/icepeak-core.mjs"

test_util.fill_node_event_queue()
test_util.handle_test_result(icepeakCoreTest())


async function icepeakCoreTest() {
console.log("Starting Test")
const config = {
websocketUrl: "ws://localhost:3000/?method=reusable",
websocketConstructor: (url: string) => new WebSocket(url),
fetchTokenFn: test_util.fetch_dummy_token,
calculateRetry: () => null,
logger: test_util.log_everything
};

await test_util.put_data({}, "")

const w1 = new test_util.Wait()
const w2 = new test_util.Wait()
const icepeakCore = icepeak_core.createIcepeakCore(config);
const s1 = icepeakCore.createSubscriptionRef("root/sub1")
const s2 = icepeakCore.createSubscriptionRef("root")
s2.onUpdate = u => {console.log("S2 updated:", u)}
s2.subscribe(null)
s1.onSuccess = _ => {console.log("S1 subscribed"); w1.done()}
s1.onUpdate = u => {console.log("S1 updated:", u); w2.done()}
s1.subscribe(null)
await w1.wait
test_util.put_data(3, "/root/sub1")
await w2.wait
}
21 changes: 18 additions & 3 deletions icepeak-ts-client/test/icepeak.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function icepeakTest() {
console.log("Starting Test")

// Clear Icepeak server data
await test_util.put_data(JSON.stringify({}), "")
await test_util.put_data({}, "")

const config : icepeak.IcepeakConfig = {
websocketUrl: "ws://localhost:3000/?method=reusable",
Expand All @@ -20,10 +20,25 @@ async function icepeakTest() {
}

const w1 = new test_util.Wait()
const w2 = new test_util.Wait()
const icepeakObj = icepeak.createIcepeak(config);
icepeakObj.subscribe("hi", val => console.log("Received value:", val))
await w1.wait
// const s1 = icepeakObj.subscribe("root", val => { console.log("s1 received:", val); w1.done() })

// await test_util.put_data(8, "/root")
// await test_util.put_data(11, "/root")
const s2 = icepeakObj.subscribe("/root/sub1", val => { console.log("s2 received:", val); w2.done() })
const s3 = icepeakObj.subscribe("/root/sub1", val => { console.log("s3 received:", val); w2.done() })

// await test_util.put_data(12, "/root/sub1/sub2")
await test_util.put_data({"sub1": { "sub2": 11 }}, "/root")
await test_util.put_data({"sub1": { "sub2": 11 }}, "/root")
await test_util.put_data({"sub1": { "sub2": 11 }}, "/root")
// s1.unsubscribe()
// s2.unsubscribe()
// s3.unsubscribe()

await w1.wait
await w2.wait

}

Expand Down

0 comments on commit 7c04afb

Please sign in to comment.