mutts 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/chunks/{decorator-BXsign4Z.js → decorator-8qjFb7dw.js} +2 -2
  2. package/dist/chunks/decorator-8qjFb7dw.js.map +1 -0
  3. package/dist/chunks/{decorator-CPbZNnsX.esm.js → decorator-AbRkXM5O.esm.js} +2 -2
  4. package/dist/chunks/decorator-AbRkXM5O.esm.js.map +1 -0
  5. package/dist/decorator.d.ts +1 -1
  6. package/dist/decorator.esm.js +1 -1
  7. package/dist/decorator.js +1 -1
  8. package/dist/destroyable.esm.js +1 -1
  9. package/dist/destroyable.js +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.esm.js +2 -2
  12. package/dist/index.js +2 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/mutts.umd.js +1 -1
  15. package/dist/mutts.umd.js.map +1 -1
  16. package/dist/mutts.umd.min.js +1 -1
  17. package/dist/mutts.umd.min.js.map +1 -1
  18. package/dist/reactive.d.ts +4 -3
  19. package/dist/reactive.esm.js +61 -57
  20. package/dist/reactive.esm.js.map +1 -1
  21. package/dist/reactive.js +61 -56
  22. package/dist/reactive.js.map +1 -1
  23. package/dist/std-decorators.esm.js +1 -1
  24. package/dist/std-decorators.js +1 -1
  25. package/docs/reactive.md +616 -0
  26. package/package.json +1 -2
  27. package/dist/chunks/decorator-BXsign4Z.js.map +0 -1
  28. package/dist/chunks/decorator-CPbZNnsX.esm.js.map +0 -1
  29. package/src/decorator.test.ts +0 -495
  30. package/src/decorator.ts +0 -205
  31. package/src/destroyable.test.ts +0 -155
  32. package/src/destroyable.ts +0 -158
  33. package/src/eventful.test.ts +0 -380
  34. package/src/eventful.ts +0 -69
  35. package/src/index.ts +0 -7
  36. package/src/indexable.test.ts +0 -388
  37. package/src/indexable.ts +0 -124
  38. package/src/promiseChain.test.ts +0 -201
  39. package/src/promiseChain.ts +0 -99
  40. package/src/reactive/array.test.ts +0 -923
  41. package/src/reactive/array.ts +0 -352
  42. package/src/reactive/core.test.ts +0 -1663
  43. package/src/reactive/core.ts +0 -866
  44. package/src/reactive/index.ts +0 -28
  45. package/src/reactive/interface.test.ts +0 -1477
  46. package/src/reactive/interface.ts +0 -231
  47. package/src/reactive/map.test.ts +0 -866
  48. package/src/reactive/map.ts +0 -162
  49. package/src/reactive/set.test.ts +0 -289
  50. package/src/reactive/set.ts +0 -142
  51. package/src/std-decorators.test.ts +0 -679
  52. package/src/std-decorators.ts +0 -182
  53. package/src/utils.ts +0 -52
@@ -1,99 +0,0 @@
1
- type Resolved<T> = T extends Promise<infer U>
2
- ? Resolved<U>
3
- : T extends (...args: infer Args) => infer R
4
- ? (...args: Args) => Resolved<R>
5
- : T extends object
6
- ? {
7
- [k in keyof T]: k extends 'then' | 'catch' | 'finally' ? T[k] : Resolved<T[k]>
8
- }
9
- : T
10
- type PromiseAnd<T> = Resolved<T> & Promise<Resolved<T>>
11
- export type PromiseChain<T> = T extends (...args: infer Args) => infer R
12
- ? PromiseAnd<(...args: Args) => PromiseChain<Resolved<R>>>
13
- : T extends object
14
- ? PromiseAnd<{
15
- [k in keyof T]: k extends 'then' | 'catch' | 'finally' ? T[k] : PromiseChain<Resolved<T[k]>>
16
- }>
17
- : Promise<Resolved<T>>
18
-
19
- const forward =
20
- (name: string, target: any) =>
21
- (...args: any[]) => {
22
- return target[name](...args)
23
- }
24
-
25
- const alreadyChained = new WeakMap<any, PromiseChain<any>>()
26
- const originals = new WeakMap<Promise<any>, any>()
27
-
28
- function cache(target: any, rv: PromiseChain<any>) {
29
- originals.set(rv, target)
30
- alreadyChained.set(target, rv)
31
- }
32
-
33
- type ChainedFunction<T> = ((...args: any[]) => PromiseChain<T>) & {
34
- then: Promise<T>['then']
35
- catch: Promise<T>['catch']
36
- finally: Promise<T>['finally']
37
- }
38
-
39
- const promiseProxyHandler: ProxyHandler<ChainedFunction<any>> = {
40
- //@ts-expect-error
41
- [Symbol.toStringTag]: 'MutTs PromiseChain function',
42
- get(target, prop) {
43
- if (prop === Symbol.toStringTag) return 'PromiseProxy'
44
- if (typeof prop === 'string' && ['then', 'catch', 'finally'].includes(prop))
45
- return target[prop as keyof typeof target]
46
- return chainPromise(target.then((r) => r[prop as keyof typeof r]))
47
- },
48
- }
49
- const promiseForward = (target: any) => ({
50
- // biome-ignore lint/suspicious/noThenProperty: This one is the whole point
51
- then: forward('then', target),
52
- catch: forward('catch', target),
53
- finally: forward('finally', target),
54
- })
55
- const objectProxyHandler: ProxyHandler<any> = {
56
- //@ts-expect-error
57
- [Symbol.toStringTag]: 'MutTs PromiseChain object',
58
- get(target, prop, receiver) {
59
- const getter = Object.getOwnPropertyDescriptor(target, prop)?.get
60
- const rv = getter ? getter.call(receiver) : target[prop]
61
- // Allows fct.call or fct.apply to bypass the chain system
62
- if (typeof target === 'function') return rv
63
- return chainPromise(rv)
64
- },
65
- apply(target, thisArg, args) {
66
- return chainPromise(target.apply(thisArg, args))
67
- },
68
- }
69
- function chainObject<T extends object | Function>(given: T): PromiseChain<T> {
70
- const rv = new Proxy(given, objectProxyHandler) as PromiseChain<T>
71
- cache(given, rv)
72
- return rv
73
- }
74
-
75
- function chainable(x: any): x is object | Function {
76
- return x && ['function', 'object'].includes(typeof x)
77
- }
78
- export function chainPromise<T>(given: Promise<T> | T): PromiseChain<T> {
79
- if (!chainable(given)) return given as PromiseChain<T>
80
- if (alreadyChained.has(given)) return alreadyChained.get(given) as PromiseChain<T>
81
- if (!(given instanceof Promise)) return chainObject(given)
82
- // @ts-expect-error It's ok as we check if it's an object above
83
- given = given.then((r) => (chainable(r) ? chainObject(r) : r))
84
- const target = Object.assign(function (this: any, ...args: any[]) {
85
- return chainPromise(
86
- given.then((r) => {
87
- return this?.then
88
- ? this.then((t: any) => (r as any).apply(t, args))
89
- : (r as any).apply(this, args)
90
- })
91
- )
92
- }, promiseForward(given)) as ChainedFunction<T>
93
- const chained = new Proxy(
94
- target,
95
- promiseProxyHandler as ProxyHandler<ChainedFunction<T>>
96
- ) as PromiseChain<T>
97
- cache(given, chained as PromiseChain<any>)
98
- return chained
99
- }