Skip to content

Commit 34a405a

Browse files
committed
Squashed commit of the following:
commit eaf755d Author: exuanbo <[email protected]> Date: Sun Sep 29 18:25:16 2024 +0100 chore: add vitest to test script commit 630ff2a Author: exuanbo <[email protected]> Date: Sun Sep 29 18:20:48 2024 +0100 build(vite): separate test config commit a184040 Author: exuanbo <[email protected]> Date: Sun Sep 29 17:27:25 2024 +0100 docs: add readme file to new core commit 237fc38 Author: exuanbo <[email protected]> Date: Sun Sep 29 17:25:47 2024 +0100 chore: update lock file commit 132042f Merge: 526697d 9dc98ad Author: exuanbo <[email protected]> Date: Sun Sep 29 17:18:52 2024 +0100 Merge branch 'main' into refactor-assembler commit 526697d Author: exuanbo <[email protected]> Date: Sun Sep 29 00:46:14 2024 +0100 refactor(assembler): renaming commit 8c9b482 Author: exuanbo <[email protected]> Date: Sun Sep 29 00:42:03 2024 +0100 chore: rename test files commit cac2a6b Author: exuanbo <[email protected]> Date: Sun Sep 29 00:14:11 2024 +0100 chore: move new assembler to src/core commit 880910c Author: exuanbo <[email protected]> Date: Sat Sep 28 21:41:40 2024 +0100 refactor(parser/stream): add parans around comma operator commit 92865c2 Author: exuanbo <[email protected]> Date: Sat Sep 28 21:41:33 2024 +0100 refactor(assembler): rename `distance` -> `offset` commit 90e86ad Author: exuanbo <[email protected]> Date: Sat Sep 28 21:33:00 2024 +0100 refactor(assembler): extract constants commit 1228173 Merge: 3b983d2 0995ebf Author: exuanbo <[email protected]> Date: Sat Sep 28 21:27:44 2024 +0100 Merge branch 'main' into refactor-assembler commit 3b983d2 Author: exuanbo <[email protected]> Date: Sat Sep 28 21:23:53 2024 +0100 refactor(assembler): rename `address` -> `offset` commit 0471379 Author: exuanbo <[email protected]> Date: Sat Sep 28 19:34:05 2024 +0100 refactor(assembler): use typed array to hold data commit e38d56e Author: exuanbo <[email protected]> Date: Sat Sep 28 17:04:42 2024 +0100 refactor(assembler/ast): use namespace for mnemonic types commit d921a28 Author: exuanbo <[email protected]> Date: Sat Sep 28 17:00:18 2024 +0100 refactor(assembler/parser): extract `parseHexNumber` commit 787d38c Author: exuanbo <[email protected]> Date: Sat Sep 28 16:13:55 2024 +0100 refactor(assembler): improve types commit 70e9ae5 Author: exuanbo <[email protected]> Date: Sat Sep 28 14:06:26 2024 +0100 refactor(assembler): use branded types commit 0b87463 Author: exuanbo <[email protected]> Date: Sat Sep 28 09:19:36 2024 +0100 refactor(assembler): improve handling pending chunks commit 97ce011 Author: exuanbo <[email protected]> Date: Fri Sep 27 22:40:11 2024 +0100 refactor(assembler): avoid creating new array from array-like commit 1512eb1 Author: exuanbo <[email protected]> Date: Mon Aug 12 11:17:40 2024 +0800 refactor: new assembler
1 parent 9dc98ad commit 34a405a

28 files changed

+10011
-50
lines changed

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"preview": "vite preview",
1111
"lint": "eslint . --cache --fix",
1212
"lint:ci": "eslint . --cache --cache-location ./node_modules/.cache/eslint/.eslintcache --max-warnings 0",
13-
"test": "run build && jest",
13+
"test": "run build && jest && vitest run",
1414
"all": "run lint && run test"
1515
},
1616
"dependencies": {
@@ -34,6 +34,9 @@
3434
"react": "18.3.1",
3535
"react-dom": "18.3.1",
3636
"rxjs": "8.0.0-alpha.14",
37+
"ts-brand": "0.2.0",
38+
"ts-enum-utilx": "0.3.0",
39+
"ts-expect": "1.3.0",
3740
"use-external-store": "0.2.2"
3841
},
3942
"devDependencies": {
@@ -51,6 +54,8 @@
5154
"@unocss/transformer-directives": "0.63.1",
5255
"@unocss/transformer-variant-group": "0.63.1",
5356
"@vitejs/plugin-react": "4.3.1",
57+
"@vitest/coverage-v8": "2.1.1",
58+
"@vitest/ui": "2.1.1",
5459
"eslint": "9.11.1",
5560
"eslint-plugin-react": "7.37.0",
5661
"eslint-plugin-react-hooks": "5.1.0-rc-3edc000d-20240926",
@@ -65,7 +70,8 @@
6570
"typescript-eslint": "8.7.0",
6671
"unocss": "0.63.1",
6772
"vite": "5.4.8",
68-
"vite-plugin-pwa": "0.20.5"
73+
"vite-plugin-pwa": "0.20.5",
74+
"vitest": "2.1.1"
6975
},
7076
"resolutions": {
7177
"immer": "npm:mutative-compat@^0.1.0"

src/common/utils/context.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Modifed from "在 JavaScript 中实现和使用 Context | Sukka's Blog"
2+
// https://blog.skk.moe/post/context-in-javascript/
3+
// CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
4+
5+
export type ApplyProvider = <R>(callback: () => R) => R
6+
7+
export interface ContextProvider<T> {
8+
<R>(props: { value: T, callback: () => R }): R
9+
(props: { value: T }): ApplyProvider
10+
}
11+
12+
export interface ContextConsumer<T> {
13+
<R>(callback: (value: T) => R): R
14+
(): T
15+
}
16+
17+
export interface Context<T> {
18+
Provider: ContextProvider<T>
19+
Consumer: ContextConsumer<T>
20+
}
21+
22+
const NO_VALUE_DEFAULT = Symbol('NO_VALUE_DEFAULT')
23+
type ContextValue<T> = T | typeof NO_VALUE_DEFAULT
24+
25+
export function createContext<T>(defaultValue: ContextValue<T> = NO_VALUE_DEFAULT): Context<T> {
26+
let contextValue = defaultValue
27+
28+
const Provider = <R>({ value, callback }: { value: T, callback?: () => R }) => {
29+
if (!callback) {
30+
return (fn: () => R) => Provider({ value, callback: fn })
31+
}
32+
const currentValue = contextValue
33+
contextValue = value
34+
try {
35+
return callback()
36+
}
37+
finally {
38+
contextValue = currentValue
39+
}
40+
}
41+
42+
const Consumer = <R>(callback?: (value: T) => R) => {
43+
if (contextValue === NO_VALUE_DEFAULT) {
44+
throw new TypeError(
45+
'You should only use useContext inside a Provider, or provide a default value!',
46+
)
47+
}
48+
if (!callback) {
49+
return contextValue
50+
}
51+
return callback(contextValue)
52+
}
53+
54+
return {
55+
Provider,
56+
Consumer,
57+
}
58+
}
59+
60+
export function useContext<T>(contextRef: Context<T>): T {
61+
return contextRef.Consumer()
62+
}
63+
64+
export interface ContextComposeProviderProps<R> {
65+
contexts: ApplyProvider[]
66+
callback: () => R
67+
}
68+
69+
export function ComposeProvider<R>({ contexts, callback }: ContextComposeProviderProps<R>): R {
70+
const applyProviders = contexts.reduceRight(
71+
(composed, current) => (fn) => current(() => composed(fn)),
72+
(fn) => fn(),
73+
)
74+
return applyProviders(callback)
75+
}

src/common/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TODO: remove this file
2+
13
export * from './classNames'
24
export * from './common'
35
export * from './invariant'

src/core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# WIP

0 commit comments

Comments
 (0)