-
Notifications
You must be signed in to change notification settings - Fork 2
/
decorators-old.ts
131 lines (105 loc) · 2.9 KB
/
decorators-old.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// A reactive system that uses decorators instead of plain values.
import { createEffect, createMemo, createSignal } from "./solid.js"
export function signal<This, Value>(
value: ClassAccessorDecoratorTarget<This, Value>,
_context: ClassAccessorDecoratorContext<This, Value>,
): ClassAccessorDecoratorResult<This, Value> {
type Signal = [() => Value, (value: Value) => void]
function getSignalFor(self: This) {
let signal = value.get.call(self) as unknown as Signal | undefined
if (signal) {
return signal
}
signal = createSignal(value.get.call(self))
value.set.call(self, signal as any)
return signal
}
return {
get() {
return getSignalFor(this)[0]()
},
set(newValue) {
return getSignalFor(this)[1](newValue)
},
}
}
const UNSET = Symbol("unset")
export function cached<This, Value>(
value: (this: This) => Value,
_context: ClassGetterDecoratorContext<This, () => Value>,
) {
type Cache = [isOutdated: boolean, value: typeof UNSET | Value]
const caches = new WeakMap<any, Cache>()
function getCacheFor(self: This) {
{
const cache = caches.get(self)
if (cache) {
return cache
}
}
const cache: Cache = [false, UNSET]
caches.set(self, cache)
let isFirstTime = true
createEffect(() => {
cache[0] = !isFirstTime
cache[1] = isFirstTime ? value.call(self) : UNSET
isFirstTime = false
})
return cache
}
return function (this: This): Value {
const cache = getCacheFor(this)
if (cache[0] || cache[1] == UNSET) {
cache[0] = false
return (cache[1] = value.call(this))
}
return cache[1]
}
}
export function computed<This, Value>(
value: (this: This) => Value,
_context: ClassGetterDecoratorContext<This, Value>,
) {
const memos = new WeakMap<any, () => Value>()
function getMemoFor(self: This) {
{
const memo = memos.get(self)
if (memo) {
return memo
}
}
const memo = createMemo(value.bind(self) as () => Value)
memos.set(self, memo)
return memo
}
return function (this: This) {
return getMemoFor(this)()
}
}
export function effect<Class extends abstract new (...args: any) => any>(
fn: (this: InstanceType<Class>) => void,
) {
return (value: Class, _context: ClassDecoratorContext<Class>) => {
abstract class InnerClass extends value {
constructor(...args: any[]) {
super(...args)
createEffect(fn.bind(this as InstanceType<Class>) as () => void)
}
}
return InnerClass
}
}
export function untrack<
This,
Value extends (this: This, ...args: unknown[]) => unknown,
>(
value: Value,
context:
| ClassGetterDecoratorContext<This, Value>
| ClassSetterDecoratorContext<This, Value>
| ClassMethodDecoratorContext<This, Value>,
) {
return function (this: This) {
return untrack(() => value.apply(this, arguments as any), context)
}
}