Skip to content

Commit

Permalink
Merge branch 'release/v0.20.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Apr 8, 2024
2 parents f8e849b + d979d84 commit c5f3294
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.20.0",
"version": "0.20.1",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
28 changes: 26 additions & 2 deletions src/common/bin/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Uint8ArrayToHexDump, toHex } from '..'
import { bitfield, createBinaryStreamDecoder, createBinaryStreamEncoder } from '.'
import { Uint8ArrayToHexDump, equalBinary, toHex } from '..'
import { bitfield, createBinaryStreamDecoder, createBinaryStreamEncoder, encodeJson } from '.'

describe('encoder', () => {
it('should write a stream', () => {
Expand Down Expand Up @@ -83,4 +83,28 @@ Uint8Array [
expect(enc(256)).toMatchInlineSnapshot('"8002"')
expect(enc(691529286)).toMatchInlineSnapshot('"c6ccdfc902"')
})

it('should be consistent', () => {
const bin1 = encodeJson({
a: 1,
b: 2,
})
const bin2 = encodeJson({
b: 2,
a: 1,
})
expect(equalBinary(bin1, bin2)).toBe(true)
})

// it('should be consistent JSON', () => {
// const bin1 = JSON.stringify({
// a: 1,
// b: 2,
// })
// const bin2 = JSON.stringify({
// b: 2,
// a: 1,
// })
// expect(bin1 !== bin2).toBe(true)
// })
})
1 change: 1 addition & 0 deletions src/common/bin/lib0/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ export function writeAny(encoder: Encoder, data: undefined | null | number | big
// TYPE 118: Object
write(encoder, 118)
const keys = Object.keys(data)
keys.sort() // guarantee comparability and consistency
writeVarUint(encoder, keys.length)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
Expand Down
4 changes: 3 additions & 1 deletion src/common/data/signal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { arrayRemoveElement } from './array'
import { objectPlain } from './object'

// I'm aware of this one, but like mine better ;)
// https://github.com/proposal-signals/proposal-signals

export type SignalWatcher<T> = (value: T, oldValue: T) => void

Expand Down
30 changes: 24 additions & 6 deletions src/common/msg/channel-resilient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,31 @@ export class ResillientChannel extends Channel {

dispose = useDisposeWithUtils()

private postMessageRaw(data: Uint8Array | string): boolean {
try {
if (this.channel?.isConnected) {
this.channel.postMessage(data)
return true
}
}
catch (err) {
// log.warn('send failed', err)
}
return false
}

/** Post all buffered messages */
flushBuffer() {
while (this.buffer.length)
this.channel?.postMessage(this.buffer.shift())
while (this.buffer.length) {
const data = this.buffer.shift()
if (data && !this.postMessageRaw(data)) {
this.buffer.unshift(data)
break
}
}
}

/** Reset buffer */
/** Reset buffer without force sending */
emptyBuffer() {
this.buffer = []
}
Expand All @@ -38,14 +56,14 @@ export class ResillientChannel extends Channel {
}
}

/** @deprecated use `setChannel(undefined)` */
deleteChannel() {
this.setChannel()
}

postMessage(data: Uint8Array | string): void {
if (this.channel?.isConnected)
this.channel.postMessage(data)
else
this.flushBuffer()
if (!this.postMessageRaw(data))
this.buffer.push(data)
}

Expand Down

0 comments on commit c5f3294

Please sign in to comment.