grainjs 1.0.1 → 1.1.0
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.
- package/README.md +23 -26
- package/dist/cjs/index.js +28 -17
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lib/PriorityQueue.d.ts +2 -2
- package/dist/cjs/lib/PriorityQueue.js +1 -0
- package/dist/cjs/lib/PriorityQueue.js.map +1 -1
- package/dist/cjs/lib/_computed_queue.js +4 -3
- package/dist/cjs/lib/_computed_queue.js.map +1 -1
- package/dist/cjs/lib/binding.d.ts +11 -4
- package/dist/cjs/lib/binding.js +6 -5
- package/dist/cjs/lib/binding.js.map +1 -1
- package/dist/cjs/lib/browserGlobals.d.ts +4 -1
- package/dist/cjs/lib/browserGlobals.js +2 -0
- package/dist/cjs/lib/browserGlobals.js.map +1 -1
- package/dist/cjs/lib/computed.d.ts +49 -28
- package/dist/cjs/lib/computed.js +38 -51
- package/dist/cjs/lib/computed.js.map +1 -1
- package/dist/cjs/lib/dispose.d.ts +109 -96
- package/dist/cjs/lib/dispose.js +106 -79
- package/dist/cjs/lib/dispose.js.map +1 -1
- package/dist/cjs/lib/dom.d.ts +40 -18
- package/dist/cjs/lib/dom.js +63 -29
- package/dist/cjs/lib/dom.js.map +1 -1
- package/dist/cjs/lib/domComponent.d.ts +56 -51
- package/dist/cjs/lib/domComponent.js +46 -44
- package/dist/cjs/lib/domComponent.js.map +1 -1
- package/dist/cjs/lib/domComputed.d.ts +50 -20
- package/dist/cjs/lib/domComputed.js +37 -7
- package/dist/cjs/lib/domComputed.js.map +1 -1
- package/dist/cjs/lib/domDispose.d.ts +27 -12
- package/dist/cjs/lib/domDispose.js +27 -11
- package/dist/cjs/lib/domDispose.js.map +1 -1
- package/dist/cjs/lib/domForEach.d.ts +5 -4
- package/dist/cjs/lib/domForEach.js +41 -41
- package/dist/cjs/lib/domForEach.js.map +1 -1
- package/dist/cjs/lib/domImpl.d.ts +33 -10
- package/dist/cjs/lib/domImpl.js +29 -9
- package/dist/cjs/lib/domImpl.js.map +1 -1
- package/dist/cjs/lib/domMethods.d.ts +93 -47
- package/dist/cjs/lib/domMethods.js +91 -47
- package/dist/cjs/lib/domMethods.js.map +1 -1
- package/dist/cjs/lib/domevent.d.ts +87 -62
- package/dist/cjs/lib/domevent.js +85 -59
- package/dist/cjs/lib/domevent.js.map +1 -1
- package/dist/cjs/lib/emit.d.ts +62 -32
- package/dist/cjs/lib/emit.js +68 -53
- package/dist/cjs/lib/emit.js.map +1 -1
- package/dist/cjs/lib/kowrap.d.ts +6 -3
- package/dist/cjs/lib/kowrap.js +7 -3
- package/dist/cjs/lib/kowrap.js.map +1 -1
- package/dist/cjs/lib/obsArray.d.ts +91 -53
- package/dist/cjs/lib/obsArray.js +87 -54
- package/dist/cjs/lib/obsArray.js.map +1 -1
- package/dist/cjs/lib/observable.d.ts +25 -15
- package/dist/cjs/lib/observable.js +31 -19
- package/dist/cjs/lib/observable.js.map +1 -1
- package/dist/cjs/lib/pureComputed.d.ts +12 -15
- package/dist/cjs/lib/pureComputed.js +16 -18
- package/dist/cjs/lib/pureComputed.js.map +1 -1
- package/dist/cjs/lib/styled.d.ts +78 -61
- package/dist/cjs/lib/styled.js +27 -79
- package/dist/cjs/lib/styled.js.map +1 -1
- package/dist/cjs/lib/subscribe.d.ts +41 -37
- package/dist/cjs/lib/subscribe.js +31 -39
- package/dist/cjs/lib/subscribe.js.map +1 -1
- package/dist/cjs/lib/util.js +2 -0
- package/dist/cjs/lib/util.js.map +1 -1
- package/dist/cjs/lib/widgets/input.d.ts +3 -1
- package/dist/cjs/lib/widgets/input.js +7 -4
- package/dist/cjs/lib/widgets/input.js.map +1 -1
- package/dist/cjs/lib/widgets/select.d.ts +4 -2
- package/dist/cjs/lib/widgets/select.js +8 -5
- package/dist/cjs/lib/widgets/select.js.map +1 -1
- package/dist/esm/lib/_computed_queue.js +3 -3
- package/dist/esm/lib/_computed_queue.js.map +1 -1
- package/dist/esm/lib/binding.js +2 -2
- package/dist/esm/lib/binding.js.map +1 -1
- package/dist/esm/lib/browserGlobals.js +1 -0
- package/dist/esm/lib/browserGlobals.js.map +1 -1
- package/dist/esm/lib/computed.js +36 -50
- package/dist/esm/lib/computed.js.map +1 -1
- package/dist/esm/lib/dispose.js +104 -78
- package/dist/esm/lib/dispose.js.map +1 -1
- package/dist/esm/lib/dom.js +40 -18
- package/dist/esm/lib/dom.js.map +1 -1
- package/dist/esm/lib/domComponent.js +45 -44
- package/dist/esm/lib/domComponent.js.map +1 -1
- package/dist/esm/lib/domComputed.js +32 -5
- package/dist/esm/lib/domComputed.js.map +1 -1
- package/dist/esm/lib/domDispose.js +26 -11
- package/dist/esm/lib/domDispose.js.map +1 -1
- package/dist/esm/lib/domForEach.js +40 -41
- package/dist/esm/lib/domForEach.js.map +1 -1
- package/dist/esm/lib/domImpl.js +26 -7
- package/dist/esm/lib/domImpl.js.map +1 -1
- package/dist/esm/lib/domMethods.js +77 -35
- package/dist/esm/lib/domMethods.js.map +1 -1
- package/dist/esm/lib/domevent.js +84 -59
- package/dist/esm/lib/domevent.js.map +1 -1
- package/dist/esm/lib/emit.js +67 -53
- package/dist/esm/lib/emit.js.map +1 -1
- package/dist/esm/lib/kowrap.js +5 -2
- package/dist/esm/lib/kowrap.js.map +1 -1
- package/dist/esm/lib/obsArray.js +82 -50
- package/dist/esm/lib/obsArray.js.map +1 -1
- package/dist/esm/lib/observable.js +26 -15
- package/dist/esm/lib/observable.js.map +1 -1
- package/dist/esm/lib/pureComputed.js +15 -18
- package/dist/esm/lib/pureComputed.js.map +1 -1
- package/dist/esm/lib/styled.js +24 -77
- package/dist/esm/lib/styled.js.map +1 -1
- package/dist/esm/lib/subscribe.js +27 -36
- package/dist/esm/lib/subscribe.js.map +1 -1
- package/dist/esm/lib/util.js +1 -0
- package/dist/esm/lib/util.js.map +1 -1
- package/dist/esm/lib/widgets/input.js +3 -1
- package/dist/esm/lib/widgets/input.js.map +1 -1
- package/dist/esm/lib/widgets/select.js +3 -1
- package/dist/esm/lib/widgets/select.js.map +1 -1
- package/dist/grain-full.debug.js +2138 -3052
- package/dist/grain-full.debug.js.map +7 -0
- package/dist/grain-full.min.js +6 -2
- package/dist/grain-full.min.js.map +7 -1
- package/lib/binding.ts +9 -2
- package/lib/browserGlobals.ts +3 -1
- package/lib/computed.ts +56 -56
- package/lib/dispose.ts +110 -85
- package/lib/dom.ts +41 -20
- package/lib/domComponent.ts +68 -70
- package/lib/domComputed.ts +66 -21
- package/lib/domDispose.ts +28 -11
- package/lib/domForEach.ts +13 -12
- package/lib/domImpl.ts +30 -7
- package/lib/domMethods.ts +101 -46
- package/lib/domevent.ts +86 -61
- package/lib/emit.ts +64 -50
- package/lib/kowrap.ts +5 -2
- package/lib/obsArray.ts +89 -54
- package/lib/observable.ts +26 -15
- package/lib/pureComputed.ts +16 -22
- package/lib/styled.ts +85 -71
- package/lib/subscribe.ts +41 -45
- package/lib/util.ts +1 -0
- package/lib/widgets/input.ts +3 -1
- package/lib/widgets/select.ts +3 -1
- package/package.json +48 -38
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* pureComputed.js implements a variant of computed() suitable for use with a pure read function
|
|
4
|
-
* (free of side-effects). A pureComputed is only subscribed to its dependencies when something is
|
|
5
|
-
* subscribed to it. At other times, it is not subscribed to anything, and calls to `get()` will
|
|
6
|
-
* recompute its value each time by calling its read() function.
|
|
7
|
-
*
|
|
8
|
-
* Its syntax and usage are otherwise exactly as for a computed.
|
|
9
|
-
*
|
|
10
|
-
* In addition to being cheaper when unused, a pureComputed() also avoids leaking memory when
|
|
11
|
-
* unused (since it's not registered with dependencies), so it is not necessary to dispose it.
|
|
12
|
-
*/
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pureComputed = exports.PureComputed = void 0;
|
|
14
4
|
const observable_1 = require("./observable");
|
|
15
5
|
const subscribe_1 = require("./subscribe");
|
|
16
6
|
function _noWrite() {
|
|
@@ -21,10 +11,19 @@ function _useFunc(obs) {
|
|
|
21
11
|
}
|
|
22
12
|
// Constant empty array, which we use to avoid allocating new read-only empty arrays.
|
|
23
13
|
const emptyArray = [];
|
|
14
|
+
/**
|
|
15
|
+
* `PureComputed` is a variant of `Computed` suitable for use with a pure read function
|
|
16
|
+
* (free of side-effects). A `PureComputed` is only subscribed to its dependencies when something is
|
|
17
|
+
* subscribed to it. At other times, it is not subscribed to anything, and calls to `get()` will
|
|
18
|
+
* recompute its value each time by calling its `read()` function.
|
|
19
|
+
*
|
|
20
|
+
* Its syntax and usage are otherwise exactly as for a `Computed`.
|
|
21
|
+
*
|
|
22
|
+
* In addition to being cheaper when unused, a `PureComputed` also avoids leaking memory when
|
|
23
|
+
* unused (since it's not registered with dependencies), so it is not necessary to dispose it.
|
|
24
|
+
*/
|
|
24
25
|
class PureComputed extends observable_1.Observable {
|
|
25
|
-
|
|
26
|
-
* Internal constructor for a PureComputed. You should use pureComputed() function instead.
|
|
27
|
-
*/
|
|
26
|
+
// Internal constructor for a PureComputed. You should use pureComputed() function instead.
|
|
28
27
|
constructor(callback, dependencies) {
|
|
29
28
|
// At initialization we force an undefined value even though it's not of type T: it's not
|
|
30
29
|
// actually used as get() is overridden.
|
|
@@ -36,10 +35,12 @@ class PureComputed extends observable_1.Observable {
|
|
|
36
35
|
this._inCall = false;
|
|
37
36
|
this.setListenerChangeCB(this._onListenerChange, this);
|
|
38
37
|
}
|
|
38
|
+
/** @internal */
|
|
39
39
|
_getDepItem() {
|
|
40
40
|
this._activate();
|
|
41
41
|
return this._sub._getDepItem();
|
|
42
42
|
}
|
|
43
|
+
/** @override */
|
|
43
44
|
get() {
|
|
44
45
|
if (!this._sub && !this._inCall) {
|
|
45
46
|
// _inCall member prevents infinite recursion.
|
|
@@ -61,7 +62,7 @@ class PureComputed extends observable_1.Observable {
|
|
|
61
62
|
/**
|
|
62
63
|
* "Sets" the value of the pure computed by calling the write() callback if one was provided in
|
|
63
64
|
* the constructor. Throws an error if there was no such callback (not a "writable" computed).
|
|
64
|
-
* @param
|
|
65
|
+
* @param value - The value to pass to the write() callback.
|
|
65
66
|
*/
|
|
66
67
|
set(value) { this._write(value); }
|
|
67
68
|
/**
|
|
@@ -103,9 +104,6 @@ class PureComputed extends observable_1.Observable {
|
|
|
103
104
|
}
|
|
104
105
|
}
|
|
105
106
|
exports.PureComputed = PureComputed;
|
|
106
|
-
/**
|
|
107
|
-
* Creates and returns a new PureComputed. The interface is identical to that of a Computed.
|
|
108
|
-
*/
|
|
109
107
|
function pureComputed(...args) {
|
|
110
108
|
const readCb = args.pop();
|
|
111
109
|
// The cast helps ensure that Observable is compatible with ISubscribable abstraction that we use.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pureComputed.js","sourceRoot":"","sources":["../../../lib/pureComputed.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pureComputed.js","sourceRoot":"","sources":["../../../lib/pureComputed.ts"],"names":[],"mappings":";;;AAEA,6CAAwD;AACxD,2CAAiF;AAEjF,SAAS,QAAQ;IACf,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;AAC9D,CAAC;AAED,SAAS,QAAQ,CAAI,GAAiD;IACpE,OAAO,CAAC,KAAK,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;AACjD,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,GAAuB,EAAE,CAAC;AAE1C;;;;;;;;;;GAUG;AACH,MAAa,YAAgB,SAAQ,uBAAa;IAOhD,2FAA2F;IAC3F,YAAY,QAA2C,EAAE,YAA0C;QACjG,yFAAyF;QACzF,wCAAwC;QACxC,KAAK,CAAC,SAAgB,CAAC,CAAC;QACxB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC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agElem, TagName } from './domImpl';
|
|
2
|
+
import { cls } from './domMethods';
|
|
3
|
+
export interface IClsName {
|
|
4
|
+
className: string;
|
|
5
|
+
cls: typeof cls;
|
|
6
|
+
}
|
|
7
|
+
export type DomCreateFunc<R, Args extends IDomArgs<R> = IDomArgs<R>> = (...args: Args) => R;
|
|
1
8
|
/**
|
|
2
9
|
* In-code styling for DOM components, inspired by Reacts Styled Components.
|
|
3
10
|
*
|
|
4
11
|
* Usage:
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const cssTitle = styled('h1', `
|
|
14
|
+
* font-size: 1.5em;
|
|
15
|
+
* text-align: center;
|
|
16
|
+
* color: palevioletred;
|
|
17
|
+
* `);
|
|
18
|
+
*
|
|
19
|
+
* const cssWrapper = styled('section', `
|
|
20
|
+
* padding: 4em;
|
|
21
|
+
* background: papayawhip;
|
|
22
|
+
* `);
|
|
23
|
+
*
|
|
24
|
+
* cssWrapper(cssTitle('Hello world'))
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* This generates class names for `cssTitle` and `cssWrapper`, adds the styles to the document on
|
|
28
|
+
* first use, and the result is equivalent to:
|
|
29
|
+
* ```ts
|
|
30
|
+
* dom(`section.${cssWrapper.className}`, dom(`h1.${cssTitle.className}`, 'Hello world'));
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* What `styled(tag)` returns is a function that takes the same arguments `...args` as
|
|
34
|
+
* `dom(tag, ...args)`. In particular, you may call it with all the arguments that
|
|
35
|
+
* [`dom()`](#dom) takes: content, DOM methods, event handlers, etc.
|
|
36
|
+
*
|
|
37
|
+
* Calls to `styled()` should happen at the top level, at import time, in order to register all
|
|
24
38
|
* styles upfront. Actual work happens the first time a style is needed to create an element.
|
|
25
|
-
* Calling styled() elsewhere than at top level is wasteful and bad for performance.
|
|
26
|
-
*
|
|
27
|
-
* You may create a style that modifies an existing styled() or other component, e.g.
|
|
39
|
+
* Calling `styled()` elsewhere than at top level is wasteful and bad for performance.
|
|
28
40
|
*
|
|
29
|
-
*
|
|
41
|
+
* You may create a style that modifies an existing `styled()` or other component, e.g.
|
|
42
|
+
* ```ts
|
|
43
|
+
* const cssTitle2 = styled(cssTitle, `font-size: 1rem; color: red;`);
|
|
44
|
+
* ```
|
|
30
45
|
*
|
|
31
|
-
*
|
|
46
|
+
* Now calling `cssTitle2('Foo')` becomes equivalent to
|
|
47
|
+
* `dom('h1', {className: cssTitle.className + ' ' + cssTitle2.className})`.
|
|
32
48
|
*
|
|
33
49
|
* Styles may incorporate other related styles by nesting them under the main one as follows:
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* const myButton = styled('button', `
|
|
52
|
+
* border-radius: 0.5rem;
|
|
53
|
+
* border: 1px solid grey;
|
|
54
|
+
* font-size: 1rem;
|
|
55
|
+
*
|
|
56
|
+
* &:active {
|
|
57
|
+
* background: lightblue;
|
|
58
|
+
* }
|
|
59
|
+
* &-small {
|
|
60
|
+
* font-size: 0.6rem;
|
|
61
|
+
* }
|
|
62
|
+
* `);
|
|
63
|
+
* ```
|
|
47
64
|
*
|
|
48
65
|
* In nested styles, ampersand (&) gets replaced with the generated .className of the main element.
|
|
49
66
|
*
|
|
50
|
-
* The resulting styled component provides a
|
|
51
|
-
* behaves as dom.cls()
|
|
67
|
+
* The resulting styled component provides a `.cls()` helper to simplify using prefixed classes. It
|
|
68
|
+
* behaves as `dom.cls()`, but prefixes the class names with the generated className of the main
|
|
52
69
|
* element. E.g. for the example above,
|
|
70
|
+
* ```ts
|
|
71
|
+
* myButton(myButton.cls('-small'), 'Test')
|
|
72
|
+
* ```
|
|
53
73
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* creates a button with both the myButton style above, and the style specified under "&-small".
|
|
57
|
-
*
|
|
58
|
-
* Animations with @keyframes may be created with a unique name by using the keyframes() helper:
|
|
59
|
-
*
|
|
60
|
-
* const rotate360 = keyframes(`
|
|
61
|
-
* from { transform: rotate(0deg); }
|
|
62
|
-
* to { transform: rotate(360deg); }
|
|
63
|
-
* `);
|
|
64
|
-
*
|
|
65
|
-
* const Rotate = styled('div', `
|
|
66
|
-
* display: inline-block;
|
|
67
|
-
* animation: ${rotate360} 2s linear infinite;
|
|
68
|
-
* `);
|
|
74
|
+
* creates a button with both the `myButton` style above, and the style specified under "&-small".
|
|
69
75
|
*/
|
|
70
|
-
import { IDomArgs, TagElem, TagName } from './domImpl';
|
|
71
|
-
import { cls } from './domMethods';
|
|
72
|
-
export interface IClsName {
|
|
73
|
-
className: string;
|
|
74
|
-
cls: typeof cls;
|
|
75
|
-
}
|
|
76
|
-
export declare type DomCreateFunc<R, Args extends IDomArgs<R> = IDomArgs<R>> = (...args: Args) => R;
|
|
77
76
|
export declare function styled<Tag extends TagName>(tag: Tag, styles: string): DomCreateFunc<TagElem<Tag>> & IClsName;
|
|
78
77
|
export declare function styled<Args extends any[], R extends Element>(creator: (...args: Args) => R, styles: string): typeof creator & IClsName;
|
|
78
|
+
/**
|
|
79
|
+
* Animations with `@keyframes` may be created with a unique name by using the keyframes() helper:
|
|
80
|
+
* ```ts
|
|
81
|
+
* const rotate360 = keyframes(`
|
|
82
|
+
* from { transform: rotate(0deg); }
|
|
83
|
+
* to { transform: rotate(360deg); }
|
|
84
|
+
* `);
|
|
85
|
+
*
|
|
86
|
+
* const Rotate = styled('div', `
|
|
87
|
+
* display: inline-block;
|
|
88
|
+
* animation: ${rotate360} 2s linear infinite;
|
|
89
|
+
* `);
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* This function returns simply a string with the generated name. Note that keyframes do not
|
|
93
|
+
* support nesting or ampersand (&) handling, like `styled()` does, since these would be difficult
|
|
94
|
+
* and are entirely unneeded.
|
|
95
|
+
*/
|
|
79
96
|
export declare function keyframes(styles: string): string;
|
package/dist/cjs/lib/styled.js
CHANGED
|
@@ -1,74 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* In-code styling for DOM components, inspired by Reacts Styled Components.
|
|
4
|
-
*
|
|
5
|
-
* Usage:
|
|
6
|
-
* const title = styled('h1', `
|
|
7
|
-
* font-size: 1.5em;
|
|
8
|
-
* text-align: center;
|
|
9
|
-
* color: palevioletred;
|
|
10
|
-
* `);
|
|
11
|
-
*
|
|
12
|
-
* const wrapper = styled('section', `
|
|
13
|
-
* padding: 4em;
|
|
14
|
-
* background: papayawhip;
|
|
15
|
-
* `);
|
|
16
|
-
*
|
|
17
|
-
* wrapper(title('Hello world'))
|
|
18
|
-
*
|
|
19
|
-
* This generates class names for title and wrapper, adds the styles to the document on first use,
|
|
20
|
-
* and the result is equivalent to:
|
|
21
|
-
*
|
|
22
|
-
* dom(`section.${wrapper.className}`, dom(`h1.${title.className}`, 'Hello world'));
|
|
23
|
-
*
|
|
24
|
-
* Calls to styled() should happen at the top level, at import time, in order to register all
|
|
25
|
-
* styles upfront. Actual work happens the first time a style is needed to create an element.
|
|
26
|
-
* Calling styled() elsewhere than at top level is wasteful and bad for performance.
|
|
27
|
-
*
|
|
28
|
-
* You may create a style that modifies an existing styled() or other component, e.g.
|
|
29
|
-
*
|
|
30
|
-
* const title2 = styled(title, `font-size: 1rem; color: red;`);
|
|
31
|
-
*
|
|
32
|
-
* Calling title2('Foo') becomes equivalent to dom(`h1.${title.className}.${title2.className}`).
|
|
33
|
-
*
|
|
34
|
-
* Styles may incorporate other related styles by nesting them under the main one as follows:
|
|
35
|
-
*
|
|
36
|
-
* const myButton = styled('button', `
|
|
37
|
-
* border-radius: 0.5rem;
|
|
38
|
-
* border: 1px solid grey;
|
|
39
|
-
* font-size: 1rem;
|
|
40
|
-
*
|
|
41
|
-
* &:active {
|
|
42
|
-
* background: lightblue;
|
|
43
|
-
* }
|
|
44
|
-
* &-small {
|
|
45
|
-
* font-size: 0.6rem;
|
|
46
|
-
* }
|
|
47
|
-
* `);
|
|
48
|
-
*
|
|
49
|
-
* In nested styles, ampersand (&) gets replaced with the generated .className of the main element.
|
|
50
|
-
*
|
|
51
|
-
* The resulting styled component provides a .cls() helper to simplify using prefixed classes. It
|
|
52
|
-
* behaves as dom.cls(), but prefixes the class names with the generated className of the main
|
|
53
|
-
* element. E.g. for the example above,
|
|
54
|
-
*
|
|
55
|
-
* myButton(myButton.cls('-small'), 'Test')
|
|
56
|
-
*
|
|
57
|
-
* creates a button with both the myButton style above, and the style specified under "&-small".
|
|
58
|
-
*
|
|
59
|
-
* Animations with @keyframes may be created with a unique name by using the keyframes() helper:
|
|
60
|
-
*
|
|
61
|
-
* const rotate360 = keyframes(`
|
|
62
|
-
* from { transform: rotate(0deg); }
|
|
63
|
-
* to { transform: rotate(360deg); }
|
|
64
|
-
* `);
|
|
65
|
-
*
|
|
66
|
-
* const Rotate = styled('div', `
|
|
67
|
-
* display: inline-block;
|
|
68
|
-
* animation: ${rotate360} 2s linear infinite;
|
|
69
|
-
* `);
|
|
70
|
-
*/
|
|
71
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.keyframes = exports.styled = void 0;
|
|
72
4
|
// Use the browser globals in a way that allows replacing them with mocks in tests.
|
|
73
5
|
const browserGlobals_1 = require("./browserGlobals");
|
|
74
6
|
const domImpl_1 = require("./domImpl");
|
|
@@ -80,7 +12,7 @@ function styled(creator, styles) {
|
|
|
80
12
|
// Creator function reflects the input, with only the addition of style.use() at the end. Note
|
|
81
13
|
// that it needs to be at the end because creator() might take special initial arguments.
|
|
82
14
|
const newCreator = (typeof creator === 'string') ?
|
|
83
|
-
(...args) => style.addToElem(domImpl_1.dom(creator, ...args)) :
|
|
15
|
+
(...args) => style.addToElem((0, domImpl_1.dom)(creator, ...args)) :
|
|
84
16
|
(...args) => style.addToElem(creator(...args));
|
|
85
17
|
return Object.assign(newCreator, {
|
|
86
18
|
className: style.className,
|
|
@@ -88,8 +20,24 @@ function styled(creator, styles) {
|
|
|
88
20
|
});
|
|
89
21
|
}
|
|
90
22
|
exports.styled = styled;
|
|
91
|
-
|
|
92
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Animations with `@keyframes` may be created with a unique name by using the keyframes() helper:
|
|
25
|
+
* ```ts
|
|
26
|
+
* const rotate360 = keyframes(`
|
|
27
|
+
* from { transform: rotate(0deg); }
|
|
28
|
+
* to { transform: rotate(360deg); }
|
|
29
|
+
* `);
|
|
30
|
+
*
|
|
31
|
+
* const Rotate = styled('div', `
|
|
32
|
+
* display: inline-block;
|
|
33
|
+
* animation: ${rotate360} 2s linear infinite;
|
|
34
|
+
* `);
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* This function returns simply a string with the generated name. Note that keyframes do not
|
|
38
|
+
* support nesting or ampersand (&) handling, like `styled()` does, since these would be difficult
|
|
39
|
+
* and are entirely unneeded.
|
|
40
|
+
*/
|
|
93
41
|
function keyframes(styles) {
|
|
94
42
|
return (new KeyframePiece(styles)).className;
|
|
95
43
|
}
|
|
@@ -113,23 +61,23 @@ function getNextStyleNum() {
|
|
|
113
61
|
return g._grainNextStyleNum = (g._grainNextStyleNum || 0) + 1;
|
|
114
62
|
}
|
|
115
63
|
class StylePiece {
|
|
116
|
-
constructor(_styles) {
|
|
117
|
-
this._styles = _styles;
|
|
118
|
-
this._mounted = false;
|
|
119
|
-
this.className = StylePiece._nextClassName();
|
|
120
|
-
StylePiece._unmounted.add(this);
|
|
121
|
-
}
|
|
122
64
|
// Generate a new css class name. The suffix ensures that names like "&2" can't cause a conflict.
|
|
123
65
|
static _nextClassName() { return `_grain${getNextStyleNum()}_`; }
|
|
124
66
|
// Mount all unmounted StylePieces, and clear the _unmounted map.
|
|
125
67
|
static _mountAll() {
|
|
126
68
|
const sheet = Array.from(this._unmounted, (p) => p._createRules()).join("\n\n");
|
|
127
|
-
browserGlobals_1.G.document.head.appendChild(domImpl_1.dom('style', sheet));
|
|
69
|
+
browserGlobals_1.G.document.head.appendChild((0, domImpl_1.dom)('style', sheet));
|
|
128
70
|
for (const piece of this._unmounted) {
|
|
129
71
|
piece._mounted = true;
|
|
130
72
|
}
|
|
131
73
|
this._unmounted.clear();
|
|
132
74
|
}
|
|
75
|
+
constructor(_styles) {
|
|
76
|
+
this._styles = _styles;
|
|
77
|
+
this._mounted = false;
|
|
78
|
+
this.className = StylePiece._nextClassName();
|
|
79
|
+
StylePiece._unmounted.add(this);
|
|
80
|
+
}
|
|
133
81
|
addToElem(elem) {
|
|
134
82
|
if (!this._mounted) {
|
|
135
83
|
StylePiece._mountAll();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"styled.js","sourceRoot":"","sources":["../../../lib/styled.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"styled.js","sourceRoot":"","sources":["../../../lib/styled.ts"],"names":[],"mappings":";;;AAAA,mFAAmF;AACnF,qDAAmC;AACnC,uCAA0D;AAC1D,6CAA4C;AAmF5C,SAAgB,MAAM,CAAC,OAAY,EAAE,MAAc;IACjD,+FAA+F;IAC/F,kFAAkF;IAClF,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAErC,8FAA8F;IAC9F,yFAAyF;IACzF,MAAM,UAAU,GAAG,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC;QAChD,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,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IAAI,CAAC,OAAO,CAAC,CAAC;IAC5D,CAAC;;AAjCD,sDAAsD;AACvC,qBAAU,GAAG,IAAI,GAAG,EAAc,CAAC;AAmCpD,MAAM,aAAc,SAAQ,UAAU;IAC1B,YAAY;QACpB,OAAO,cAAc,IAAI,CAAC,SAAS,KAAK,IAAI,CAAC,OAAO,GAAG,CAAC;IAC1D,CAAC;CACF"}
|
|
@@ -1,40 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* subscribe.js implements subscriptions to several observables at once.
|
|
3
|
-
*
|
|
4
|
-
* E.g. if we have some existing observables (which may be instances of `computed`),
|
|
5
|
-
* we can subscribe to them explicitly:
|
|
6
|
-
* let obs1 = observable(5), obs2 = observable(12);
|
|
7
|
-
* subscribe(obs1, obs2, (use, v1, v2) => console.log(v1, v2));
|
|
8
|
-
*
|
|
9
|
-
* or implicitly by using `use(obs)` function, which allows dynamic subscriptions:
|
|
10
|
-
* subscribe(use => console.log(use(obs1), use(obs2)));
|
|
11
|
-
*
|
|
12
|
-
* In either case, if obs1 or obs2 is changed, the callbacks will get called automatically.
|
|
13
|
-
*
|
|
14
|
-
* Creating a subscription allows any number of dependencies to be specified explicitly, and their
|
|
15
|
-
* values will be passed to the callback(). These may be combined with automatic dependencies
|
|
16
|
-
* detected using use(). Note that constructor dependencies have less overhead.
|
|
17
|
-
*
|
|
18
|
-
* subscribe(...deps, ((use, ...depValues) => READ_CALLBACK));
|
|
19
|
-
*/
|
|
20
1
|
import { DepItem } from './_computed_queue';
|
|
21
2
|
import { IDisposableOwner } from './dispose';
|
|
22
3
|
import { Listener } from './emit';
|
|
23
4
|
import { IKnockoutReadObservable } from './kowrap';
|
|
24
5
|
import { BaseObservable as Obs } from './observable';
|
|
25
6
|
export interface ISubscribableObs {
|
|
7
|
+
/** @internal */
|
|
26
8
|
_getDepItem(): DepItem | null;
|
|
27
9
|
addListener(callback: (val: any, prev: any) => void, optContext?: object): Listener;
|
|
28
10
|
get(): any;
|
|
29
11
|
}
|
|
30
|
-
export
|
|
31
|
-
export
|
|
12
|
+
export type ISubscribable = ISubscribableObs | IKnockoutReadObservable<any>;
|
|
13
|
+
export type InferUseType<TObs extends Obs<any> | IKnockoutReadObservable<any>> = TObs extends Obs<infer T> ? T : TObs extends {
|
|
32
14
|
peek(): infer U;
|
|
33
15
|
} ? U : never;
|
|
34
|
-
export
|
|
16
|
+
export type UseCB = <TObs extends Obs<any> | IKnockoutReadObservable<any>>(obs: TObs) => InferUseType<TObs>;
|
|
35
17
|
export interface UseCBOwner extends UseCB {
|
|
36
18
|
owner: IDisposableOwner;
|
|
37
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* `Subscription` allows subscribing to several observables at once. It's the foundation for a
|
|
22
|
+
* `Computed`, but may also be used directly.
|
|
23
|
+
*
|
|
24
|
+
* E.g. if we have some existing observables (which may be instances of `Computed`),
|
|
25
|
+
* we can subscribe to them explicitly:
|
|
26
|
+
* ```ts
|
|
27
|
+
* const obs1 = observable(5), obs2 = observable(12);
|
|
28
|
+
* subscribe(obs1, obs2, (use, v1, v2) => console.log(v1, v2));
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* or implicitly by using `use(obs)` function, which allows dynamic subscriptions:
|
|
32
|
+
* ```ts
|
|
33
|
+
* subscribe(use => console.log(use(obs1), use(obs2)));
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* In either case, if `obs1` or `obs2` is changed, the callbacks will get called automatically.
|
|
37
|
+
*
|
|
38
|
+
* Creating a subscription allows any number of dependencies to be specified explicitly, and their
|
|
39
|
+
* values will be passed to the `callback`. These may be combined with automatic dependencies
|
|
40
|
+
* detected using `use()`. Note that constructor dependencies have less overhead.
|
|
41
|
+
* ```ts
|
|
42
|
+
* subscribe(...deps, ((use, ...depValues) => READ_CALLBACK));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
38
45
|
export declare class Subscription {
|
|
39
46
|
private readonly _depItem;
|
|
40
47
|
private readonly _dependencies;
|
|
@@ -42,11 +49,6 @@ export declare class Subscription {
|
|
|
42
49
|
private _dynDeps;
|
|
43
50
|
private _callback;
|
|
44
51
|
private _useFunc;
|
|
45
|
-
/**
|
|
46
|
-
* Internal constructor for a Subscription. You should use subscribe() function instead.
|
|
47
|
-
* The last owner argument is used by computed() to make itself available as the .owner property
|
|
48
|
-
* of the 'use' function that gets passed to the callback.
|
|
49
|
-
*/
|
|
50
52
|
constructor(callback: (use: UseCB, ...args: any[]) => void, dependencies: ReadonlyArray<ISubscribable>, owner?: any);
|
|
51
53
|
/**
|
|
52
54
|
* Disposes the computed, unsubscribing it from all observables it depends on.
|
|
@@ -54,38 +56,40 @@ export declare class Subscription {
|
|
|
54
56
|
dispose(): void;
|
|
55
57
|
/**
|
|
56
58
|
* For use by computed(): returns this subscription's hook into the _computed_queue.
|
|
59
|
+
* @internal
|
|
57
60
|
*/
|
|
58
61
|
_getDepItem(): DepItem;
|
|
59
62
|
/**
|
|
60
|
-
* @private
|
|
61
63
|
* Gets called when the callback calls `use(obs)` for an observable. It creates a
|
|
62
64
|
* subscription to `obs` if one doesn't yet exist.
|
|
63
|
-
* @param
|
|
65
|
+
* @param obs - The observable being used as a dependency.
|
|
64
66
|
*/
|
|
65
67
|
private _useDependency;
|
|
66
68
|
/**
|
|
67
|
-
* @private
|
|
68
69
|
* Calls the callback() with appropriate args, and updates subscriptions when it is done.
|
|
69
70
|
* I.e. adds dynamic subscriptions created via `use(obs)`, and disposes those no longer used.
|
|
70
71
|
*/
|
|
71
72
|
private _evaluate;
|
|
72
73
|
/**
|
|
73
|
-
* @private
|
|
74
74
|
* Subscribes this computed to another observable that it depends on.
|
|
75
|
-
* @param
|
|
76
|
-
* @returns
|
|
75
|
+
* @param obs - The observable to subscribe to.
|
|
76
|
+
* @returns Listener object.
|
|
77
77
|
*/
|
|
78
78
|
private _subscribeTo;
|
|
79
79
|
/**
|
|
80
|
-
* @private
|
|
81
80
|
* Adds this item to the recompute queue.
|
|
82
81
|
*/
|
|
83
82
|
private _enqueue;
|
|
84
83
|
}
|
|
85
84
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
85
|
+
* Creates a new Subscription.
|
|
86
|
+
* @param observables - The initial params, of which there may be zero or more, are
|
|
87
|
+
* observables on which this computed depends. When any of them change, the `callback`
|
|
88
|
+
* will be called with the values of these observables as arguments.
|
|
89
|
+
* @param callback - will be called with arguments `(use, ...values)`, i.e. the
|
|
90
|
+
* `use` function and values for all of the `...observables` that precede this argument.
|
|
91
|
+
* This callback is called immediately, and whenever any dependency changes.
|
|
92
|
+
* @returns The new `Subscription` which may be disposed to unsubscribe.
|
|
89
93
|
*/
|
|
90
94
|
export declare function subscribe(cb: (use: UseCB) => void): Subscription;
|
|
91
95
|
export declare function subscribe<A>(a: Obs<A>, cb: (use: UseCB, a: A) => void): Subscription;
|
|
@@ -1,34 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.subscribe = exports.Subscription = void 0;
|
|
4
|
+
const _computed_queue_1 = require("./_computed_queue");
|
|
5
|
+
const kowrap_1 = require("./kowrap");
|
|
6
|
+
// Constant empty array, which we use to avoid allocating new read-only empty arrays.
|
|
7
|
+
const emptyArray = [];
|
|
2
8
|
/**
|
|
3
|
-
*
|
|
9
|
+
* `Subscription` allows subscribing to several observables at once. It's the foundation for a
|
|
10
|
+
* `Computed`, but may also be used directly.
|
|
4
11
|
*
|
|
5
|
-
* E.g. if we have some existing observables (which may be instances of `
|
|
12
|
+
* E.g. if we have some existing observables (which may be instances of `Computed`),
|
|
6
13
|
* we can subscribe to them explicitly:
|
|
7
|
-
*
|
|
8
|
-
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const obs1 = observable(5), obs2 = observable(12);
|
|
16
|
+
* subscribe(obs1, obs2, (use, v1, v2) => console.log(v1, v2));
|
|
17
|
+
* ```
|
|
9
18
|
*
|
|
10
19
|
* or implicitly by using `use(obs)` function, which allows dynamic subscriptions:
|
|
11
|
-
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* subscribe(use => console.log(use(obs1), use(obs2)));
|
|
22
|
+
* ```
|
|
12
23
|
*
|
|
13
|
-
* In either case, if obs1 or obs2 is changed, the callbacks will get called automatically.
|
|
24
|
+
* In either case, if `obs1` or `obs2` is changed, the callbacks will get called automatically.
|
|
14
25
|
*
|
|
15
26
|
* Creating a subscription allows any number of dependencies to be specified explicitly, and their
|
|
16
|
-
* values will be passed to the callback
|
|
17
|
-
* detected using use()
|
|
18
|
-
*
|
|
19
|
-
*
|
|
27
|
+
* values will be passed to the `callback`. These may be combined with automatic dependencies
|
|
28
|
+
* detected using `use()`. Note that constructor dependencies have less overhead.
|
|
29
|
+
* ```ts
|
|
30
|
+
* subscribe(...deps, ((use, ...depValues) => READ_CALLBACK));
|
|
31
|
+
* ```
|
|
20
32
|
*/
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
const _computed_queue_1 = require("./_computed_queue");
|
|
23
|
-
const kowrap_1 = require("./kowrap");
|
|
24
|
-
// Constant empty array, which we use to avoid allocating new read-only empty arrays.
|
|
25
|
-
const emptyArray = [];
|
|
26
33
|
class Subscription {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
* of the 'use' function that gets passed to the callback.
|
|
31
|
-
*/
|
|
34
|
+
// Internal constructor for a Subscription. You should use subscribe() function instead.
|
|
35
|
+
// The last owner argument is used by computed() to make itself available as the .owner property
|
|
36
|
+
// of the 'use' function that gets passed to the callback.
|
|
32
37
|
constructor(callback, dependencies, owner) {
|
|
33
38
|
this._depItem = new _computed_queue_1.DepItem(this._evaluate, this);
|
|
34
39
|
this._dependencies = dependencies.length > 0 ? dependencies : emptyArray;
|
|
@@ -55,16 +60,16 @@ class Subscription {
|
|
|
55
60
|
}
|
|
56
61
|
/**
|
|
57
62
|
* For use by computed(): returns this subscription's hook into the _computed_queue.
|
|
63
|
+
* @internal
|
|
58
64
|
*/
|
|
59
65
|
_getDepItem() { return this._depItem; }
|
|
60
66
|
/**
|
|
61
|
-
* @private
|
|
62
67
|
* Gets called when the callback calls `use(obs)` for an observable. It creates a
|
|
63
68
|
* subscription to `obs` if one doesn't yet exist.
|
|
64
|
-
* @param
|
|
69
|
+
* @param obs - The observable being used as a dependency.
|
|
65
70
|
*/
|
|
66
71
|
_useDependency(_obs) {
|
|
67
|
-
const obs = ('_getDepItem' in _obs) ? _obs : kowrap_1.fromKo(_obs);
|
|
72
|
+
const obs = ('_getDepItem' in _obs) ? _obs : (0, kowrap_1.fromKo)(_obs);
|
|
68
73
|
let listener = this._dynDeps.get(obs);
|
|
69
74
|
if (!listener) {
|
|
70
75
|
listener = this._subscribeTo(obs);
|
|
@@ -75,7 +80,6 @@ class Subscription {
|
|
|
75
80
|
return obs.get();
|
|
76
81
|
}
|
|
77
82
|
/**
|
|
78
|
-
* @private
|
|
79
83
|
* Calls the callback() with appropriate args, and updates subscriptions when it is done.
|
|
80
84
|
* I.e. adds dynamic subscriptions created via `use(obs)`, and disposes those no longer used.
|
|
81
85
|
*/
|
|
@@ -105,17 +109,15 @@ class Subscription {
|
|
|
105
109
|
}
|
|
106
110
|
}
|
|
107
111
|
/**
|
|
108
|
-
* @private
|
|
109
112
|
* Subscribes this computed to another observable that it depends on.
|
|
110
|
-
* @param
|
|
111
|
-
* @returns
|
|
113
|
+
* @param obs - The observable to subscribe to.
|
|
114
|
+
* @returns Listener object.
|
|
112
115
|
*/
|
|
113
116
|
_subscribeTo(_obs) {
|
|
114
|
-
const obs = ('_getDepItem' in _obs) ? _obs : kowrap_1.fromKo(_obs);
|
|
117
|
+
const obs = ('_getDepItem' in _obs) ? _obs : (0, kowrap_1.fromKo)(_obs);
|
|
115
118
|
return obs.addListener(this._enqueue, this);
|
|
116
119
|
}
|
|
117
120
|
/**
|
|
118
|
-
* @private
|
|
119
121
|
* Adds this item to the recompute queue.
|
|
120
122
|
*/
|
|
121
123
|
_enqueue() {
|
|
@@ -123,16 +125,6 @@ class Subscription {
|
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
exports.Subscription = Subscription;
|
|
126
|
-
/**
|
|
127
|
-
* Creates a new Subscription.
|
|
128
|
-
* @param {Observable} ...observables: The initial params, of which there may be zero or more, are
|
|
129
|
-
* observables on which this computed depends. When any of them change, the callback()
|
|
130
|
-
* will be called with the values of these observables as arguments.
|
|
131
|
-
* @param {Function} callback: will be called with arguments (use, ...values), i.e. the
|
|
132
|
-
* `use` function and values for all of the ...observables that precede this argument.
|
|
133
|
-
* This callback is called immediately, and whenever any dependency changes.
|
|
134
|
-
* @returns {Subscription} The new subscription which may be disposed to unsubscribe.
|
|
135
|
-
*/
|
|
136
128
|
function subscribe(...args) {
|
|
137
129
|
const cb = args.pop();
|
|
138
130
|
// The cast helps ensure that Observable is compatible with ISubscribable abstraction that we use.
|