Skip to content

Commit 8616d19

Browse files
author
vitalets
committed
allow several build() calls for hot module replacement, fix #7
1 parent d5254c4 commit 8616d19

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/__tests__/api.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ describe('EStyleSheet API', function () {
7272
});
7373
});
7474

75-
it('should throw error on second `build` call', function () {
76-
api.build();
77-
expect(() => api.build()).toThrowError('No need to call `EStyleSheet.build()` more than once');
75+
it('should work correctly with several `build()` calls', function () {
76+
const res1 = api.create({$b: '$a'});
77+
api.build({a: 1});
78+
const res2 = api.create({$b: '$a'});
79+
expect(res1).toEqual({$b: 1});
80+
expect(res2).toEqual({$b: 1});
81+
api.build({a: 2});
82+
expect(res1).toEqual({$b: 2});
83+
expect(res2).toEqual({$b: 2});
7884
});
7985

8086
it('should calculate value', function () {

src/api.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ export default class {
3434
let sheet = new Sheet(obj);
3535
if (this.builded) {
3636
sheet.calc(this.globalVars);
37-
} else {
38-
this.sheets.push(sheet);
3937
}
38+
this.sheets.push(sheet);
4039
return sheet.getResult();
4140
}
4241

@@ -45,11 +44,7 @@ export default class {
4544
* @param {Object} [gVars]
4645
*/
4746
build(gVars) {
48-
if (this.builded) {
49-
throw new Error('No need to call `EStyleSheet.build()` more than once');
50-
} else {
51-
this.builded = true;
52-
}
47+
this.builded = true;
5348
this._calcVars(gVars);
5449
this._calcSheets();
5550
this._callListeners(BUILD_EVENT);
@@ -95,13 +90,11 @@ export default class {
9590

9691
_calcSheets() {
9792
this.sheets.forEach(sheet => sheet.calc(this.globalVars));
98-
this.sheets.length = 0;
9993
}
10094

10195
_callListeners(event) {
102-
if (this.listeners[event]) {
96+
if (Array.isArray(this.listeners[event])) {
10397
this.listeners[event].forEach(listener => listener());
104-
delete this.listeners[event];
10598
}
10699
}
107100
}

0 commit comments

Comments
 (0)