Skip to content

Commit 1284f83

Browse files
committed
feat: add reset function for observer
1 parent 3805120 commit 1284f83

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

packages/core/package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
"@babel/code-frame": "7.14.5",
2727
"change-case": "4.1.2",
2828
"css-color-function": "1.3.3",
29+
"deepcopy": "^2.1.0",
2930
"deepmerge": "4.2.2",
3031
"yaml": "1.10.2"
3132
},
3233
"devDependencies": {
3334
"@types/babel__code-frame": "7.0.3"
3435
},
35-
"files": ["lib"]
36+
"files": [
37+
"lib"
38+
]
3639
}

packages/core/src/internal/__tests__/observer.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ describe('ThemekitObserver', () => {
3737
],
3838
})
3939
})
40+
41+
test('should reset tokens on reset', async () => {
42+
const onWatch = jest.fn()
43+
const compile = createCompiler(simple.context)
44+
const observer = new ThemekitObserver(simple.options, compile)
45+
observer.watch(onWatch)
46+
observer.update('token1', 'value-1-updated')
47+
observer.reset()
48+
await wait(100)
49+
expect(onWatch).toBeCalledTimes(3)
50+
expect(onWatch).toHaveBeenLastCalledWith({
51+
css: [
52+
{
53+
content: 'token1:value-1,token2:value-2',
54+
destination: 'tokens.css',
55+
},
56+
],
57+
})
58+
})
4059
})
4160

4261
function wait(delay: number) {

packages/core/src/internal/observer.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { compile } from '../index'
22
import { CompileResult, CompileOptions } from '../compiler'
3-
import { TokenValue } from '../types'
3+
import { RawToken, TokenValue } from '../types'
4+
import deepcopy from 'deepcopy'
45

56
export type Watcher = (payload: CompileResult) => void
67
type Compile = typeof compile
78

89
export class ThemekitObserver {
10+
private originalTokens: RawToken[]
911
private options: CompileOptions
1012
private compile: Compile
1113
private watchers: Set<Watcher> = new Set()
1214

1315
constructor(options: CompileOptions, _compile: Compile = compile) {
1416
this.compile = _compile
15-
this.options = options
17+
this.options = deepcopy(options)
18+
this.originalTokens = deepcopy(options.tokens)
1619
this.run(options)
1720
}
1821

@@ -29,6 +32,11 @@ export class ThemekitObserver {
2932
this.run(this.options)
3033
}
3134

35+
reset() {
36+
this.options.tokens = deepcopy(this.originalTokens)
37+
this.run(this.options)
38+
}
39+
3240
private run(options: CompileOptions) {
3341
setImmediate(() => {
3442
this.emit(this.compile(options))

0 commit comments

Comments
 (0)