1
+ /* eslint-disable no-invalid-this */
1
2
'use strict' ;
2
3
3
4
var atoms = require ( './atoms' ) . atoms ;
@@ -7,27 +8,47 @@ var createSnake = function (renderer, rules) {
7
8
8
9
var defaultRules = renderer . assign ( { } , atoms , {
9
10
bgWhite : function ( ) {
10
- defaultRules . bg . call ( this , '#fff' ) ;
11
+ this . backgroundColor = '#fff' ;
11
12
} ,
12
13
13
14
bgBlack : function ( ) {
14
- defaultRules . bg . call ( this , '#000' ) ;
15
+ this . backgroundColor = '#000' ;
15
16
} ,
16
17
} ) ;
17
18
18
19
rules = renderer . assign ( { } , defaultRules , rules ) ;
19
20
20
- var snake = {
21
- start : function ( ) {
22
- var instance = Object . create ( snake ) ;
21
+ var snake = { } ;
23
22
24
- instance . obj = { } ;
25
- instance . toString = function ( ) {
26
- return renderer . cache ( instance . obj ) ;
27
- } ;
23
+ var start = function ( ) {
24
+ var instance = Object . create ( snake ) ;
28
25
29
- return instance ;
30
- }
26
+ instance . obj = { } ;
27
+ instance . toString = function ( ) {
28
+ if ( process . env . NODE_ENV !== 'production' ) {
29
+ require ( './__dev__/warnOnMissingDependencies' ) ( 'snake' , renderer , [ 'cache' ] ) ;
30
+ }
31
+
32
+ return renderer . cache ( instance . obj ) ;
33
+ } ;
34
+
35
+ return instance ;
36
+ } ;
37
+
38
+ var checkStart = function ( name , fn ) {
39
+ return function ( ) {
40
+ if ( ! this . obj ) {
41
+ var instance = start ( ) ;
42
+
43
+ if ( typeof instance [ name ] === 'function' ) {
44
+ return instance [ name ] . apply ( instance , arguments ) ;
45
+ }
46
+
47
+ return instance [ name ] ;
48
+ }
49
+
50
+ return fn . apply ( this , arguments ) ;
51
+ } ;
31
52
} ;
32
53
33
54
var onRule = function ( name ) {
@@ -36,22 +57,22 @@ var createSnake = function (renderer, rules) {
36
57
if ( typeof rule === 'function' ) {
37
58
if ( ! rule . length ) {
38
59
Object . defineProperty ( snake , name , {
39
- get : function ( ) {
60
+ get : checkStart ( name , function ( ) {
40
61
rule . call ( this . obj ) ;
41
62
return this ;
42
- }
63
+ } )
43
64
} ) ;
44
65
} else {
45
- snake [ name ] = function ( ) {
66
+ snake [ name ] = checkStart ( name , function ( ) {
46
67
rule . apply ( this . obj , arguments ) ;
47
68
return this ;
48
- } ;
69
+ } ) ;
49
70
}
50
71
} else {
51
- snake [ name ] = function ( value ) {
72
+ snake [ name ] = checkStart ( name , function ( value ) {
52
73
this . obj [ '' + rule ] = value ;
53
74
return this ;
54
- } ;
75
+ } ) ;
55
76
}
56
77
} ;
57
78
@@ -61,15 +82,7 @@ var createSnake = function (renderer, rules) {
61
82
} ;
62
83
63
84
exports . addon = function ( renderer ) {
64
- if ( process . env . NODE_ENV !== 'production' ) {
65
- require ( './__dev__/warnOnMissingDependencies' ) ( 'sheet' , renderer , [ 'cache' ] ) ;
66
- }
67
-
68
85
var snake = createSnake ( renderer ) ;
69
86
70
- Object . defineProperty ( renderer , 's' , {
71
- get : function ( ) {
72
- return snake . start ( ) ;
73
- }
74
- } ) ;
87
+ renderer . s = snake ;
75
88
} ;
0 commit comments