skandha 0.12.0 → 0.12.1
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 +24 -25
- package/internal/logging.js +1 -1
- package/lib/registerCtr.d.ts +7 -2
- package/lib/registerCtr.js +5 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,22 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
SkandhaJS is a library that provides data containers with reusable behaviors, or "facets," that are implemented as classes. Examples of facets are selection, highlighting, filtering, drag-and-drop, etc. These facets provide interfaces for specific behaviors and rely on callback functions provided by the programmer to specify the details of those behaviors. The goal of SkandhaJS is to provide a way to create interactions between different behaviors in a consistent and modular way.
|
|
5
|
+
SkandhaJS is a library that provides data containers with generic (reusable) behaviors, or "facets," that are implemented as classes. Examples of facets are selection, highlighting, filtering, drag-and-drop, etc. These facets provide interfaces for specific behaviors and rely on callback functions provided by the programmer to specify the details of those behaviors. The goal of SkandhaJS is to provide a way to create interactions between different behaviors in a consistent and modular way.
|
|
6
6
|
|
|
7
7
|
As a brief illustration, consider the example of selecting a todo in a list of todos:
|
|
8
8
|
|
|
9
9
|
1. The application has a local state with a list of todos. The state contains a `todoSelection` facet so that clients can make a selection.
|
|
10
|
-
2. When the user clicks on a todo, the todo-list component calls `todoSelection.selectItem(todo.id, shift, ctrl)`.
|
|
10
|
+
2. When the user clicks on a todo, the todo-list component calls `todoSelection.selectItem({id: todo.id, shift, ctrl})`.
|
|
11
11
|
3. The `todoSelection.selectItem` function runs the `select` function from its `callbackMap`.
|
|
12
12
|
4. The `select` callback performs the selection and a side-effect: it highlights the item as well.
|
|
13
13
|
|
|
14
14
|
## Benefits
|
|
15
15
|
|
|
16
|
-
There are three ways in which facets help you to write
|
|
16
|
+
There are three ways in which facets help you to write reusable code:
|
|
17
17
|
|
|
18
18
|
1. They can be reused in any container, regardless of what data-type is stored in the container. This means that selection, filtering and drag-and-drop work the same everywhere in the application.
|
|
19
19
|
2. They allow you to capture the interaction between behaviours in reusable functions.
|
|
20
|
-
3. They allow you to handle user-interaction in UI components in a uniform way. All UI components that receive a selection facet will use the same interface to make a selection (and in theory the component can remain agnostic of how selection works, or even of the type of items that are being selected).
|
|
20
|
+
3. They allow you to handle user-interaction in UI components in a uniform way. All UI components that receive a selection facet will use the same interface to make a selection (and in theory the component can remain agnostic of how selection works, or even of the type of the items that are being selected).
|
|
21
21
|
|
|
22
22
|
## Links
|
|
23
23
|
|
|
@@ -30,9 +30,10 @@ There are three ways in which facets help you to write generic code:
|
|
|
30
30
|
## Glossary (conceptual)
|
|
31
31
|
|
|
32
32
|
- callback function: a function that is called by an operation to help implement that operation
|
|
33
|
-
-
|
|
33
|
+
- callbacks object: a collection of callback functions that is used by an operation.
|
|
34
|
+
- callbackMap: a dictionary that contains a callbacks object for every operation of a facet
|
|
34
35
|
- container: an object that contains data and facets. Typically, these facets are made to work together using callback functions.
|
|
35
|
-
- facet: a class that implements a
|
|
36
|
+
- facet: a class that implements a generic behaviour, such as selection.
|
|
36
37
|
- operation: a member function of a facet that changes the data of that facet.
|
|
37
38
|
|
|
38
39
|
## Glossary (technical)
|
|
@@ -40,11 +41,11 @@ There are three ways in which facets help you to write generic code:
|
|
|
40
41
|
- aspiration: a library that implements a form of Aspect Oriented Programming using callback functions.
|
|
41
42
|
- mapDataToProps: a helper function that maps data from one object to another. It's used to map data between facets.
|
|
42
43
|
- registerCtr: the function that tells SkandhaJS which containers and facets exist.
|
|
43
|
-
- setCallbackMap: the function that installs the callbackMap
|
|
44
|
+
- setCallbackMap: the function that installs the callbackMap for a facet.
|
|
44
45
|
|
|
45
46
|
## Example: a Selection facet
|
|
46
47
|
|
|
47
|
-
In Skandha, a behaviour such as selection is implemented in a "facet" class. The purpose of the selection facet is to store (and manipulate) the
|
|
48
|
+
In Skandha, a behaviour such as selection is implemented in a "facet" class. The purpose of the selection facet is to store (and manipulate) the selection state. In this example, we will show a simplified `Selection` facet class.
|
|
48
49
|
|
|
49
50
|
```
|
|
50
51
|
// file: Selection.ts
|
|
@@ -76,7 +77,7 @@ export type SelectionCbs<T = any> = DefineCbs<Selection<T>, Cbs<T>>;
|
|
|
76
77
|
|
|
77
78
|
## Facets have @input, @data and @output members
|
|
78
79
|
|
|
79
|
-
In `Selection.ts` we see the Selection facet class. It has a `selectableIds` field that stores the list of ids from which we can select. The `@input` decorator marks this field as an input. The `ids`, `anchorId` and `items` fields store the result of calling `selectItem()` and are marked as `@output` fields. Fields that are decorated with `@data` are used both as output and input. These decorators help with logging, and they also make it possible to bind the facets to state-management solutions such as MobX.
|
|
80
|
+
In `Selection.ts` we see the `Selection` facet class. It has a `selectableIds` field that stores the list of ids from which we can select. The `@input` decorator marks this field as an input. The `ids`, `anchorId` and `items` fields store the result of calling `selectItem()` and are marked as `@output` fields. Fields that are decorated with `@data` are used both as output and input. These decorators help with logging, and they also make it possible to bind the facets to state-management solutions such as MobX (this is explained later).
|
|
80
81
|
|
|
81
82
|
## Operations are decorated with `@operation`.
|
|
82
83
|
|
|
@@ -84,37 +85,35 @@ The `Selection` facet has a `selectItem()` operation that allows you to select f
|
|
|
84
85
|
|
|
85
86
|
## Operations are logged
|
|
86
87
|
|
|
87
|
-
As a side effect of calling an operation, the entire container is logged to the console (if logging is enabled); this includes the Selection facet, and any other facets that the container may have (such as filtering, or highlight). The log message contains all
|
|
88
|
+
As a side effect of calling an operation, the entire container is logged to the console (if logging is enabled); this includes the `Selection` facet, and any other facets that the container may have (such as filtering, or highlight). The log message contains all `@input`, `@data` and `@output` fields of all facets in the container.
|
|
88
89
|
|
|
89
90
|
## The `@withCbs` decorator is used to enable callbacks in an operation.
|
|
90
91
|
|
|
91
92
|
The `@withCbs` decorator comes from the `Aspiration` library. When you call a function `f` that is decorated with `@withCbs` then the following happens:
|
|
92
93
|
|
|
93
|
-
- Aspiration looks up the
|
|
94
|
-
-
|
|
95
|
-
-
|
|
96
|
-
-
|
|
94
|
+
- Aspiration looks up the callbacks object (that was installed with `setCallbackMap()`) for the given operation;
|
|
95
|
+
- It copies the operation arguments to the callbacks object, so that callback functions can access these arguments;
|
|
96
|
+
- It calls `f(arguments)`, which internally will use the installed callback functions to do its job;
|
|
97
|
+
- It restores the callbacks object to its previous state. This ensures that nested calls work correctly.
|
|
97
98
|
|
|
98
|
-
If the original function `f` wants to access the
|
|
99
|
+
If the original function `f` wants to access the callbacks object then it uses the `getCallbacks` helper function. Note that the client code calls `f` in the usual way; it remains agnostic of the fact that callbacks are used to help implement `f`.
|
|
99
100
|
|
|
100
101
|
## Facets have a static `className()` function
|
|
101
102
|
|
|
102
|
-
To allow SkandhaJS to log the container, you need to add the `className()` function to each facet class, so that Skandha knows the name of the facet.
|
|
103
|
-
|
|
104
|
-
not override the `className()`. By keeping the original `className()` function, you will be able to replace facets with their subclassed versions without affecting the facet lookup mechanism.
|
|
103
|
+
To allow SkandhaJS to log the container, you need to add the `className()` function to each facet class, so that Skandha knows the name of the facet. The `className()` function is also used to look up facets in containers with the `getf` function (e.g. `getf(Selection, ctr)` returns the selection facet in `ctr`).
|
|
104
|
+
|
|
105
|
+
Note that if you subclass a facet class then you should (in most cases) not override the `className()`. By keeping the original `className()` function, you will be able to replace facets with their subclassed versions without affecting the facet lookup mechanism. However, if you subclass the facet then you must add the `skandhaSymbol` member, as explained below.
|
|
105
106
|
|
|
106
107
|
## Facets that use inheritance must have a static `skandhaSymbol` member
|
|
107
108
|
|
|
108
|
-
|
|
109
|
-
`experimentalDecorators` feature. Add this member if the facet class inherits from some
|
|
110
|
-
other class.
|
|
109
|
+
Skandha will store class-level information about a facet class in the class itself. If you subclass a facet class then you must add a `skandhaSymbol` member so that Skandha will keep the information for the subclass separate from the information for the parent class:
|
|
111
110
|
|
|
112
111
|
```
|
|
113
112
|
// file: Selection.ts
|
|
114
113
|
import { Selection } from 'skandha-facets';
|
|
115
114
|
|
|
116
115
|
export class MySelection extends Selection {
|
|
117
|
-
static className = () => '
|
|
116
|
+
static className = () => 'Selection';
|
|
118
117
|
static skandhaSymbol = Symbol('MySelection');
|
|
119
118
|
}
|
|
120
119
|
```
|
|
@@ -204,7 +203,7 @@ We call `setCallbackMap()` to install a callbackMap object for each operation of
|
|
|
204
203
|
|
|
205
204
|
## The callback function can access the callbackMap object through `this`.
|
|
206
205
|
|
|
207
|
-
When the clients calls `todosCtr.selection.selectItem()` and `select()` is called back, then all arguments of the operation are available through `this`. In our case, we see that the `select()` callback function is accessing `this.args`. See the `aspiration` library documentation for details.
|
|
206
|
+
When the clients calls `todosCtr.selection.selectItem()` and `select()` is called back, then all arguments of the `selectItem` operation are available through `this`. In our case, we see that the `select()` callback function is accessing `this.args`. See the `aspiration` library documentation for details.
|
|
208
207
|
|
|
209
208
|
## Callbacks can take care of side effects
|
|
210
209
|
|
|
@@ -216,7 +215,7 @@ In `createContainer.ts` we also have a `Filtering` facet that has an `apply(f)`
|
|
|
216
215
|
|
|
217
216
|
## The `registerCtr()` allows SkandhaJS to introspect the container
|
|
218
217
|
|
|
219
|
-
At the end of `createContainer.ts` we call `registerCtr()` so that SkandhaJS can introspect the container. This is necessary for two reasons. First, it makes it possible to look up a facet by its name or class, e.g. `getf(
|
|
218
|
+
At the end of `createContainer.ts` we call `registerCtr()` so that SkandhaJS can introspect the container. This is necessary for two reasons. First, it makes it possible to look up a facet by its name or class, e.g. `getf(Selection, todosCtr)` returns `todosCtr.selection`. Second, it informs SkandhaJS how to log all facets of the container.
|
|
220
219
|
|
|
221
220
|
## Example: `mapDataToProps()` maps data onto each facet
|
|
222
221
|
|
|
@@ -285,7 +284,7 @@ If you want to render the facets with React then we need to let React know when
|
|
|
285
284
|
## Conclusion
|
|
286
285
|
|
|
287
286
|
SkandhaJS introduces a lot of new patterns and ideas (that are of course not in fact new) to allow you to
|
|
288
|
-
put
|
|
287
|
+
put generic behaviours on top of your existing data-structures. The difference to a more standard approach
|
|
289
288
|
can feel overwhelming, but it's important to point out the beneficial side effect of
|
|
290
289
|
separating concerns in a clear and understandable way. We could add a `Editing` facet to our code and install
|
|
291
290
|
a policy that says that changing the highlight disables editing. By using facets, we will not be tempted to put
|
package/internal/logging.js
CHANGED
|
@@ -56,5 +56,5 @@ export function defaultGetCtrState(ctr) {
|
|
|
56
56
|
}
|
|
57
57
|
export function getCtrState(ctr) {
|
|
58
58
|
var _a, _b;
|
|
59
|
-
return ((_b = (_a = getCtrAdmin(ctr)) === null || _a === void 0 ? void 0 : _a.
|
|
59
|
+
return ((_b = (_a = getCtrAdmin(ctr)) === null || _a === void 0 ? void 0 : _a.getCtrState) !== null && _b !== void 0 ? _b : defaultGetCtrState)(ctr);
|
|
60
60
|
}
|
package/lib/registerCtr.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
export type OptionsT = {
|
|
2
2
|
name: string;
|
|
3
3
|
facets?: string[];
|
|
4
|
-
|
|
4
|
+
getCtrState?: Function;
|
|
5
5
|
};
|
|
6
|
-
export
|
|
6
|
+
export type ArgsT = {
|
|
7
|
+
ctr: any;
|
|
8
|
+
options: OptionsT;
|
|
9
|
+
initCtr?: Function;
|
|
10
|
+
};
|
|
11
|
+
export declare function registerCtr({ ctr, options, initCtr }: ArgsT): void;
|
package/lib/registerCtr.js
CHANGED
|
@@ -3,13 +3,13 @@ import { options as skandhaOptions } from '../internal/options';
|
|
|
3
3
|
import { getCtrAdmin, getFacetAdmin } from '../internal/utils';
|
|
4
4
|
import { setCtr } from './ctr';
|
|
5
5
|
import { facetClassName } from './facet';
|
|
6
|
-
export function registerCtr(ctr, options) {
|
|
6
|
+
export function registerCtr({ ctr, options, initCtr }) {
|
|
7
7
|
var _a, _b, _c;
|
|
8
8
|
const ctrAdmin = getCtrAdmin(ctr);
|
|
9
9
|
ctrAdmin.logName = options.name;
|
|
10
10
|
ctrAdmin.facetMembers = (_a = ctrAdmin.facetMembers) !== null && _a !== void 0 ? _a : [];
|
|
11
11
|
ctrAdmin.facetByFacetClassName = (_b = ctrAdmin.facetByFacetClassName) !== null && _b !== void 0 ? _b : {};
|
|
12
|
-
ctrAdmin.
|
|
12
|
+
ctrAdmin.getCtrState = options.getCtrState;
|
|
13
13
|
((_c = options.facets) !== null && _c !== void 0 ? _c : Object.getOwnPropertyNames(ctr)).forEach((facetName) => {
|
|
14
14
|
const facet = ctr[facetName];
|
|
15
15
|
setCtr(facet, ctr);
|
|
@@ -28,4 +28,7 @@ export function registerCtr(ctr, options) {
|
|
|
28
28
|
if (skandhaOptions.logging) {
|
|
29
29
|
console.log('%c %s ctr initialized', 'color: gray', ctrAdmin.logName, getCtrState(ctr));
|
|
30
30
|
}
|
|
31
|
+
if (initCtr) {
|
|
32
|
+
initCtr(ctr);
|
|
33
|
+
}
|
|
31
34
|
}
|