Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

fix: running in browser #913

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ await Promise.all([
name: "./patterns/signature/polkadot",
path: "./patterns/signature/polkadot.ts",
},
{
name: "./patterns/compat/pjs_sender",
path: "./patterns/compat/pjs_sender.ts",
},
{
name: "./patterns/consensus",
path: "./patterns/consensus/mod.ts",
Expand Down
5 changes: 3 additions & 2 deletions deps/shims/upgradeWebSocket.node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { WebSocketServer } from "./ws.ts"
import * as WS from "./ws.ts"

const wss = new WebSocketServer({ noServer: true })
let wss: WS.WebSocketServer | undefined

export function upgradeWebSocket(request: Request) {
wss ??= new WS.WebSocketServer({ noServer: true })
// Set by http.node.ts
const [req, socket, head] = (request as any)._upgrade
return {
Expand Down
1 change: 1 addition & 0 deletions deps/shims/ws.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This will be shimmed to the `ws` npm package by DNT

export declare const WebSocketServer: any
export declare type WebSocketServer = any
2 changes: 1 addition & 1 deletion patterns/compat/pjs_sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hex, ss58 } from "../../crypto/mod.ts"
import * as $ from "../../deps/scale.ts"
import { AddressPrefixChain, Chain, ChainRune } from "../../fluent/ChainRune.ts"
import { ExtrinsicSender } from "../../fluent/ExtrinsicRune.ts"
import { Rune, RunicArgs } from "../../rune/Rune.ts"
import { Rune, RunicArgs } from "../../rune/mod.ts"

export type PjsSigner = { signPayload(payload: any): Promise<{ signature: string }> }

Expand Down
2 changes: 1 addition & 1 deletion patterns/signature/polkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Polkadot } from "@capi/polkadot"
import { AddressPrefixChain, Chain, ChainRune } from "../../fluent/ChainRune.ts"
import { ExtrinsicSender, SignatureData } from "../../fluent/ExtrinsicRune.ts"
import { $, hex, ss58, ValueRune } from "../../mod.ts"
import { Rune, RunicArgs } from "../../rune/Rune.ts"
import { Rune, RunicArgs } from "../../rune/mod.ts"
import { Era } from "../../scale_info/overrides/Era.ts"

export interface SignatureProps<T extends Chain> {
Expand Down
2 changes: 2 additions & 0 deletions rune/ArrayRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export class ArrayRune<T, U> extends Rune<T[], U> {
.into(ArrayRune)
}
}

Rune.ArrayRune = ArrayRune
2 changes: 2 additions & 0 deletions rune/FnRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export class FnRune<F extends (...args: any[]) => any, U> extends Rune<F, U> {
return Rune.tuple([this.as(Rune), ...args]).map(([fn, ...args]) => fn(...args))
}
}

Rune.FnRune = FnRune
48 changes: 28 additions & 20 deletions rune/Rune.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { deferred } from "../deps/std/async.ts"
import { getOrInit } from "../util/state.ts"
import { ArrayRune, FnRune, ValueRune } from "./mod.ts"
import { EventSource, Receipt, Timeline } from "./Timeline.ts"

// Hack to work around circularity issues
// @deno-types="./mod.ts"
import * as _ from "./_empty.js"

export class Batch {
constructor(
readonly timeline = new Timeline(),
Expand Down Expand Up @@ -41,9 +44,12 @@ export class Batch {
declare const _T: unique symbol
declare const _U: unique symbol

export namespace Rune {
export declare namespace Rune {
export type T<R> = R extends { [_T]: infer T } ? T : R
export type U<R> = R extends { [_U]: infer U } ? U : never
export import ValueRune = _.ValueRune
export import ArrayRune = _.ArrayRune
export import FnRune = _.FnRune
}

export interface Rune<out T, out U = never> {
Expand Down Expand Up @@ -89,11 +95,11 @@ export class Rune<out T, out U = never> {
}

static constant<T>(value: T) {
return ValueRune.new(RunConstant, value)
return Rune.ValueRune.new(RunConstant, value)
}

static resolve<V>(value: V): ValueRune<Rune.T<V>, Rune.U<V>> {
return value instanceof Rune ? value.into(ValueRune) : Rune.constant(value)
static resolve<V>(value: V): Rune.ValueRune<Rune.T<V>, Rune.U<V>> {
return value instanceof Rune ? value.into(Rune.ValueRune) : Rune.constant(value)
}

static str<X>(strings: TemplateStringsArray, ..._values: RunicArgs<X, unknown[]>) {
Expand All @@ -111,24 +117,24 @@ export class Rune<out T, out U = never> {

static tuple<R extends unknown[]>(
runes: [...R],
): ValueRune<{ [K in keyof R]: Rune.T<R[K]> }, Rune.U<R[number]>>
): Rune.ValueRune<{ [K in keyof R]: Rune.T<R[K]> }, Rune.U<R[number]>>
static tuple<R extends unknown[]>(
runes: [...R],
): ValueRune<Rune.T<R[number]>[], Rune.U<R[number]>> {
return ValueRune.new(RunLs, runes.map(Rune.resolve))
): Rune.ValueRune<Rune.T<R[number]>[], Rune.U<R[number]>> {
return Rune.ValueRune.new(RunLs, runes.map(Rune.resolve))
}

static array<T, X>(runes: RunicArgs<X, T[]>) {
return ValueRune.new(RunLs, RunicArgs.resolve(runes)).into(ArrayRune)
return Rune.ValueRune.new(RunLs, RunicArgs.resolve(runes)).into(Rune.ArrayRune)
}

static fn<A extends any[], T, X>(...[fn]: RunicArgs<X, [(...args: A) => T]>) {
return Rune.resolve(fn).into(FnRune)
return Rune.resolve(fn).into(Rune.FnRune)
}

static object<R extends {}>(
runes: R,
): ValueRune<{ [K in keyof R]: Rune.T<R[K]> }, Rune.U<R[keyof R]>> {
): Rune.ValueRune<{ [K in keyof R]: Rune.T<R[K]> }, Rune.U<R[keyof R]>> {
const keys = Object.keys(runes)
const values = Object.values(runes)
return Rune.tuple(values).map((values) => {
Expand All @@ -139,25 +145,27 @@ export class Rune<out T, out U = never> {
static captureUnhandled<R extends unknown[], T2, U2>(
sources: [...R],
fn: (
...runes: { [K in keyof R]: ValueRune<Rune.T<R[K]>, never> }
...runes: { [K in keyof R]: Rune.ValueRune<Rune.T<R[K]>, never> }
) => Rune<T2, U2>,
): ValueRune<T2, Rune.U<R[number]> | U2>
): Rune.ValueRune<T2, Rune.U<R[number]> | U2>
static captureUnhandled<R, T2, U2>(
sources: any[],
fn: (...runes: ValueRune<Rune.T<R>, never>[]) => Rune<T2, U2>,
): ValueRune<T2, Rune.U<R> | U2> {
fn: (...runes: Rune.ValueRune<Rune.T<R>, never>[]) => Rune<T2, U2>,
): Rune.ValueRune<T2, Rune.U<R> | U2> {
const symbol = Symbol()
return ValueRune.new(
return Rune.ValueRune.new(
RunCaptureUnhandled<T2, U2, Rune.U<R>>,
fn(
...sources.map((source) => ValueRune.new(RunBubbleUnhandled, Rune.resolve(source), symbol)),
...sources.map((source) =>
Rune.ValueRune.new(RunBubbleUnhandled, Rune.resolve(source), symbol)
),
),
symbol,
)
}

static asyncIter<T>(fn: (signal: AbortSignal) => AsyncIterable<T>): ValueRune<T, never> {
return ValueRune.new(RunAsyncIter, fn)
static asyncIter<T>(fn: (signal: AbortSignal) => AsyncIterable<T>): Rune.ValueRune<T, never> {
return Rune.ValueRune.new(RunAsyncIter, fn)
}

static _placeholder() {
Expand Down Expand Up @@ -390,7 +398,7 @@ export namespace RunicArgs {
export type U<X> = X extends unknown[] ? Rune.U<X[number]> : Rune.U<X[keyof X]>
export function resolve<X, A>(
args: RunicArgs<X, A>,
): { [K in keyof A]: ValueRune<A[K], RunicArgs.U<X>> }
): { [K in keyof A]: Rune.ValueRune<A[K], RunicArgs.U<X>> }
export function resolve(args: any): any {
return args instanceof Array
? args.map(Rune.resolve)
Expand Down
2 changes: 2 additions & 0 deletions rune/ValueRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class RunMap<T1, U, T2> extends Run<T2, U> {
}
}

Rune.ValueRune = ValueRune

class RunHandle<T, T2 extends T, T3, U, U2> extends Run<Exclude<T, T2> | T3, U | U2> {
child
alt
Expand Down
2 changes: 2 additions & 0 deletions rune/_empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// does nothing at runtime
5 changes: 3 additions & 2 deletions rune/mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// moderate
export * from "./Rune.ts"

// moderate --exclude Rune.ts

export * from "./ArrayRune.ts"
export * from "./FnRune.ts"
export * from "./Id.ts"
export * from "./MetaRune.ts"
export * from "./Rune.ts"
export * from "./Timeline.ts"
export * from "./ValueRune.ts"