Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "regor",
"version": "1.3.1",
"version": "1.3.2",
"description": "A modern UI framework for web and desktop applications, inspired by Vue's concepts and powered by simplicity and flexibility.",
"author": "Ahmed Yasin Koculu",
"license": "MIT",
Expand Down
31 changes: 15 additions & 16 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,29 @@ type RawTypes =
| Promise<unknown>
| Error

declare const RefSymbol: unique symbol

declare const RawSymbol: unique symbol

declare const ScopeSymbol: unique symbol

export type AnyRef = (
newValue?: any,
eventSource?: any,
) => any & { [RefSymbol]: true }
type BivariantAnyRefCall = {
bivarianceHack(newValue?: unknown, eventSource?: unknown): unknown
}['bivarianceHack']

export type AnyRef = BivariantAnyRefCall

export type Ref<TValueType> = ((
newValue?:
| RefContent<TValueType>
| Ref<RefParam<TValueType>>
| SRef<RefContent<TValueType>>,
eventSource?: any,
eventSource?: unknown,
) => RefContent<TValueType>) & {
value: RefContent<TValueType>
}

export type SRef<TValueType> = ((
newValue?: TValueType | SRef<TValueType>,
eventSource?: any,
eventSource?: unknown,
) => SRefContent<TValueType>) & {
value: SRefContent<TValueType>
}
Expand Down Expand Up @@ -155,7 +154,7 @@ export type Flat<TValueType> =

export type ObserveCallback<TValueType> = (
newValue: TValueType,
eventSource?: any,
eventSource?: unknown,
) => void

export type StopObserving = () => void
Expand All @@ -182,10 +181,10 @@ export interface Directive {
/** Called on every value change. */
onChange?: (
el: HTMLElement,
values: any[],
previousValues?: any[],
option?: any,
previousOption?: any,
values: unknown[],
previousValues?: unknown[],
option?: unknown,
previousOption?: unknown,
flags?: string[],
) => void

Expand Down Expand Up @@ -339,7 +338,7 @@ export interface CreateComponentOptions<
* The props defined in the props list can be used with :foo or r-bind:foo syntax.
* `<MyComponent :prop-kebab-1="1" r-bind:prop-kebab-2="x ? 1 : 0" :props="{ propFoo3: true, propFoo4: x ? 'a' : 'b' }></MyComponent>`
* It is required to define prop-kebab-1 and prop-kebab-2 in the props list camelized.
* It is not required to define propFoo3 and propFoo4 in the props list because it uses :props binding. :props binding enables binding to any property of component regardless it is explicitly defined in props list.
* It is not required to define propFoo3 and propFoo4 in the props list because it uses :props binding. :props binding enables binding to arbitrary component properties regardless they are explicitly defined in props list.
*/
props?: string[]

Expand Down Expand Up @@ -369,10 +368,10 @@ export interface ComposableScope {
*/
export type SRefSignature<TValueType> = (
newValue?: TValueType,
eventSource?: any,
eventSource?: unknown,
operation?: RefOperation,
observer?: ObserveCallback<TValueType>,
) => any
) => unknown

/**
* @internal
Expand Down
9 changes: 6 additions & 3 deletions src/cleanup/getBindData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { type BindData } from '../api/types'
import { bindDataSymbol } from './bindDataSymbol'

export const getBindData = (node: any): BindData => {
const bindData = node[bindDataSymbol]
type BindableNode = { [bindDataSymbol]?: BindData }

export const getBindData = (node: object): BindData => {
const bindableNode = node as BindableNode
const bindData = bindableNode[bindDataSymbol]
if (bindData) return bindData as BindData
const newBindData: BindData = {
unbinders: [],
data: {},
}
node[bindDataSymbol] = newBindData
bindableNode[bindDataSymbol] = newBindData
return newBindData
}
28 changes: 19 additions & 9 deletions src/computed/computeMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ import { sref } from '../reactivity/sref'
import { trigger } from '../reactivity/trigger'
import { type ComputedOnce } from './computed'

export const computeMany = <TReturnType>(
sources: AnyRef[],
compute: (...values: any[]) => TReturnType,
type ComputeManySourceValues<TSources extends readonly AnyRef[]> = {
[TIndex in keyof TSources]: UnwrapRef<TSources[TIndex]>
}

export const computeMany = <
const TSources extends readonly AnyRef[],
TReturnType,
>(
sources: TSources,
compute: (...values: ComputeManySourceValues<TSources>) => TReturnType,
): ComputedRef<UnwrapRef<TReturnType>> => {
const status: ComputedOnce<TReturnType> = {}
let computer: any
const result = ((...args: unknown[]) => {
if (args.length <= 2 && 0 in args)
throw getError(ErrorType.ComputedIsReadOnly)
if (computer && !status.isStopped) return computer(...args)
computer = computeManyOnce(sources, compute, status)
computer = computeManyOnce<TSources, TReturnType>(sources, compute, status)
return computer(...args)
}) as ComputedRef<UnwrapRef<TReturnType>>

Expand All @@ -34,9 +41,9 @@ export const computeMany = <TReturnType>(
return result
}

const computeManyOnce = <TReturnType>(
sources: AnyRef[],
compute: (...values: any[]) => TReturnType,
const computeManyOnce = <TSources extends readonly AnyRef[], TReturnType>(
sources: TSources,
compute: (...values: ComputeManySourceValues<TSources>) => TReturnType,
status: ComputedOnce<TReturnType>,
): ComputedRef<UnwrapRef<TReturnType>> => {
const result =
Expand All @@ -47,14 +54,17 @@ const computeManyOnce = <TReturnType>(
status.ref = result
status.isStopped = false
let i = 0
const callback = (_: any): void => {
const callback = (_: unknown): void => {
if (i > 0) {
result.stop()
status.isStopped = true
trigger(result)
return
}
result(compute(...sources.map((x) => x())) as UnwrapRef<TReturnType>)
const values = sources.map((source) =>
source(),
) as ComputeManySourceValues<TSources>
result(compute(...values) as UnwrapRef<TReturnType>)
++i
}
const stopObservingList: StopObserving[] = []
Expand Down
4 changes: 3 additions & 1 deletion src/log/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ export const warning = (type: WarningType, ...args: unknown[]): void => {
else handler(item, ...item.args)
}

export const warningHandler = { warning: console.warn }
export const warningHandler: { warning: (...data: unknown[]) => void } = {
warning: console.warn,
}
4 changes: 2 additions & 2 deletions src/misc/isRaw.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { rawSymbol } from '../reactivity/refSymbols'

export const isRaw = (value: any): boolean => {
return !!value && value[rawSymbol] === 1
export const isRaw = (value: unknown): boolean => {
return !!value && (value as Record<symbol, unknown>)[rawSymbol] === 1
}
2 changes: 1 addition & 1 deletion src/misc/tagged-html.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const html = (
templates: TemplateStringsArray,
...args: any[]
...args: unknown[]
): string => {
let result = ''
const tpl = templates
Expand Down
2 changes: 1 addition & 1 deletion src/observer/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const observe = <TValueType extends AnyRef>(
): StopObserving => {
if (!isRef(source))
throw getError(ErrorType.RequiresRefSourceArgument, 'observe')
if (init) observer(source())
if (init) observer(source() as UnwrapRef<TValueType>)
const srefImpl = source as unknown as SRefSignature<UnwrapRef<TValueType>>
const stop = srefImpl(
undefined,
Expand Down
20 changes: 11 additions & 9 deletions src/observer/observeMany.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import {
type AnyRef,
type ObserveCallback,
type StopObserving,
} from '../api/types'
import { type AnyRef, type StopObserving, type UnwrapRef } from '../api/types'
import { onUnmounted } from '../composition/onUnmounted'
import { observe } from './observe'

export const observeMany = (
sources: AnyRef[],
observer: ObserveCallback<any[]>,
type ObserveManySourceValues<TSources extends readonly AnyRef[]> = {
[TIndex in keyof TSources]: UnwrapRef<TSources[TIndex]>
}

export const observeMany = <const TSources extends readonly AnyRef[]>(
sources: TSources,
observer: (values: ObserveManySourceValues<TSources>) => void,
init?: boolean,
): StopObserving => {
const stopObservingList: StopObserving[] = []
const callObserver = (): void => {
observer(sources.map((y) => y()))
observer(
sources.map((source) => source()) as ObserveManySourceValues<TSources>,
)
}
for (const source of sources) {
stopObservingList.push(observe(source, callObserver))
Expand Down
32 changes: 16 additions & 16 deletions src/parser/regorEval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const evalPrefixUpdate = {
'++': (context: Context, key: any) => {
const v = context[key]
if (isRef(v)) {
let r = v()
let r = v() as any
v(++r)
return r
}
Expand All @@ -117,7 +117,7 @@ const evalPrefixUpdate = {
'--': (context: Context, key: any) => {
const v = context[key]
if (isRef(v)) {
let r = v()
let r = v() as any
v(--r)
return r
}
Expand All @@ -129,7 +129,7 @@ const evalPostUpdate = {
'++': (context: Context, key: any) => {
const v = context[key]
if (isRef(v)) {
const r = v()
const r = v() as any

v(r + 1)
return r
Expand All @@ -139,7 +139,7 @@ const evalPostUpdate = {
'--': (context: Context, key: any) => {
const v = context[key]
if (isRef(v)) {
const r = v()
const r = v() as any
v(r - 1)
return r
}
Expand All @@ -157,85 +157,85 @@ const applyAssigment = {
'+=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() + value)
return v((v() as any) + value)
}

return (context[key] += value)
},
'-=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() - value)
return v((v() as any) - value)
}
return (context[key] -= value)
},
'*=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() * value)
return v((v() as any) * value)
}
return (context[key] *= value)
},
'/=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() / value)
return v((v() as any) / value)
}
return (context[key] /= value)
},
'%=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() % value)
return v((v() as any) % value)
}
return (context[key] %= value)
},
'**=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() ** value)
return v((v() as any) ** value)
}
return (context[key] **= value)
},
'<<=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() << value)
return v((v() as any) << value)
}
return (context[key] <<= value)
},
'>>=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() >> value)
return v((v() as any) >> value)
}
return (context[key] >>= value)
},
'>>>=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() >>> value)
return v((v() as any) >>> value)
}
return (context[key] >>>= value)
},
'|=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() | value)
return v((v() as any) | value)
}
return (context[key] |= value)
},
'&=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() & value)
return v((v() as any) & value)
}
return (context[key] &= value)
},
'^=': (context: Context, key: any, value: any) => {
const v = context[key]
if (isRef(v)) {
return v(v() ^ value)
return v((v() as any) ^ value)
}
return (context[key] ^= value)
},
Expand Down
2 changes: 1 addition & 1 deletion src/reactivity/sref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const sref = <TValueType>(
if (0 in args) {
if (refObj._value === newValue) return newValue
if (isRef(newValue)) {
newValue = newValue()
newValue = newValue() as TValueType
if (refObj._value === newValue) return newValue
}

Expand Down
2 changes: 1 addition & 1 deletion src/reactivity/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isRef } from './isRef'

export const trigger = <TValueType extends AnyRef>(
source: TValueType,
eventSource?: any,
eventSource?: unknown,
isRecursive?: boolean,
): void => {
if (!isRef(source)) return
Expand Down
Loading