skandha 0.10.2 → 0.11.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 CHANGED
@@ -2,22 +2,14 @@
2
2
 
3
3
  ## Introduction
4
4
 
5
- The goal of SkandhaJS is to provide data containers that have reusable behaviors. These behaviours are implemented in classes called "facets". Examples of facets are selection, highlighting, filtering, drag-and-drop, etc. By connecting these facets, you can create interactions between different behaviours.
6
-
7
- Every facet provides an interface that supports a particular behaviour. This interface is used throughout the application, and ensures that the basic mechanism (e.g. for doing selection) is the same everywhere. The details of the behaviour depend on callback functions that the programmer provides:
8
-
9
- 1. Callbacks decide what exactly happens when an item is selected, highlighted, deleted etc.
10
- 2. Callbacks implement interactions. For example: when selecting an item, also higlight it.
11
-
12
- The second point is important for limiting the mixing of concerns that happens when you are combining multiple behaviours. This mixing of concerns is unavoidable, but should be limited to specific places that can be easily inspected.
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.
13
6
 
14
7
  As a brief illustration, consider the example of selecting a todo in a list of todos:
15
8
 
16
- 1. The application creates a local state to store todos. This state contains a `todoSelection` facet.
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.
17
10
  2. When the user clicks on a todo, the todo-list component calls `todoSelection.selectItem(todo.id, shift, ctrl)`.
18
- 3. The `aspiration` library intercepts the call in order to prepare a so-called `callbackMap`.
19
- 4. Next, the `todoSelection.selectItem` function runs. It uses the `select` function from the `callbackMap` to do its work.
20
- 5. The `select` callback performs the selection and a side-effect: it highlights the item as well.
11
+ 3. The `todoSelection.selectItem` function runs the `select` function from its `callbackMap`.
12
+ 4. The `select` callback performs the selection and a side-effect: it highlights the item as well.
21
13
 
22
14
  ## Benefits
23
15
 
@@ -25,7 +17,7 @@ There are three ways in which facets help you to write generic code:
25
17
 
26
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.
27
19
  2. They allow you to capture the interaction between behaviours in reusable functions.
28
- 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 the data-type of the selected items).
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).
29
21
 
30
22
  ## Links
31
23
 
@@ -57,8 +49,7 @@ In Skandha, a behaviour such as selection is implemented in a "facet" class. The
57
49
  ```
58
50
  // file: Selection.ts
59
51
  import { input, output, operation } from 'skandha';
60
- import { host, stub } from 'aspiration';
61
- import { Selection_selectItem, SelectionParamsT } from 'SelectionCbs';
52
+ import { DefineCbs, getCallbacks, withCbs, stub } from 'aspiration';
62
53
 
63
54
  export class Selection<ValueT = any> {
64
55
  static className = () => 'Selection';
@@ -68,30 +59,24 @@ export class Selection<ValueT = any> {
68
59
  @output anchorId?: string;
69
60
  @output items: Array<ValueT> = stub();
70
61
 
71
- @operation @host(['selectionParams']) selectItem(
72
- selectionParams: SelectionParamsT
73
- ) {
74
- const cbs = getCallbacks<Selection_selectItem>(this);
62
+ @operation @withCbs() selectItem(args: SelectionParamsT) {
63
+ const cbs = getCallbacks(this) as SelectionCbs['selectItem'];
75
64
  cbs.select();
76
65
  }
77
66
  }
78
- ```
79
-
80
- ```
81
- // file: SelectionCbs.ts
82
- import { stub } from 'aspiration';
83
67
 
84
- export type SelectionParamsT = { itemId?: string; isShift?: boolean; isCtrl?: boolean; };
68
+ type Cbs<T> = {
69
+ selectItem: {
70
+ selectItem(): void;
71
+ };
72
+ };
85
73
 
86
- export class Selection_selectItem extends Cbs {
87
- selectionParams: SelectionParamsT = stub();
88
- select() {}
89
- }
74
+ export type SelectionCbs<T = any> = DefineCbs<Selection<T>, Cbs<T>>;
90
75
  ```
91
76
 
92
77
  ## Facets have @input, @data and @output members
93
78
 
94
- 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 are marked as `@output` fields. They are used to store the result of calling `selectItem()`. 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.
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.
95
80
 
96
81
  ## Operations are decorated with `@operation`.
97
82
 
@@ -101,24 +86,22 @@ The `Selection` facet has a `selectItem()` operation that allows you to select f
101
86
 
102
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 @input, @data and @output fields of all facets in the container.
103
88
 
104
- ## The `@host` decorator is used to enable callbacks in an operation.
89
+ ## The `@withCbs` decorator is used to enable callbacks in an operation.
105
90
 
106
- The `@host` decorator comes from the `Aspiration` library. When you call a function `f` that is decorated with `@host` then the following happens:
91
+ The `@withCbs` decorator comes from the `Aspiration` library. When you call a function `f` that is decorated with `@withCbs` then the following happens:
107
92
 
108
93
  - Aspiration looks up the callbackMap object (that was installed with `setCallbackMap()`) for the given operation;
109
94
  - it copies the operation arguments to the callbackMap object, so that callback functions can access these arguments;
110
- - if calls `f(arguments)`;
95
+ - if calls `f(arguments)`, which internally calls the installed callback functions;
111
96
  - it restores the callbackMap object to its previous state. This ensures that nested calls work correctly.
112
97
 
113
98
  If the original function `f` wants to access the callbackMap 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`.
114
99
 
115
- ## Callback functions have access to all operation arguments
116
-
117
- Each callback function of the callbackMap object has access to all arguments of the operation. In our example, the argument of the `selectItem` operation is named `selectionParams`. Therefore, a `selectionParams` field also exists in the `Selection_selectItem` class that stores the callbackMap. We must manually pass the list of argument names to the `@host` decorator, so that SkandhaJS can copy these values (unfortunately, introspection in typescript is not good enough to automate this).
118
-
119
100
  ## Facets have a static `className()` function
120
101
 
121
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
+ The `className()` function is also used to look up facets in containers. Note that if you subclass a facet class then you should (in most cases)
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.
122
105
 
123
106
  ## Example: a container with Selection, Highlight and Filtering
124
107
 
@@ -127,7 +110,7 @@ In this example we will take a look at a todos container. It uses the `Selection
127
110
 
128
111
  ```
129
112
  // file: createContainer.ts
130
- import { Selection_selectItem } from 'SelectionCbs';
113
+ import { SelectionCbs } from 'SelectionCbs';
131
114
  import * as R from 'ramda';
132
115
 
133
116
  const createContainer() {
@@ -139,9 +122,9 @@ const createContainer() {
139
122
 
140
123
  setCallbackMap(todosCtr.selection, {
141
124
  selectItem: {
142
- select(this: Selection_selectItem): {
143
- handleSelectItem(todosCtr.selection, this.selectionParams);
144
- highlightFollowsSelection(todosCtr, this.selectionParams);
125
+ select(this: SelectionCbs['selectItem']): {
126
+ handleSelectItem(todosCtr.selection, this.args);
127
+ highlightFollowsSelection(todosCtr, this.args);
145
128
  },
146
129
  }
147
130
  });
@@ -203,13 +186,13 @@ export function handleSelectItem(
203
186
 
204
187
  We call `setCallbackMap()` to install a callbackMap object for each operation of the `Selection` facet. In our case we only have one operation (`selectItem()`) that has only one callback (`select()`). This callback function calls `handleSelectItem()` to take care of doing the selection. This is a typical pattern: you want the call to `setCallbackMap()` to be as short and readable as possible, so that it's easy to understand what happens in each operation of each facet.
205
188
 
206
- ## Callbacks can take care of side effects
189
+ ## The callback function can access the callbackMap object through `this`.
207
190
 
208
- An important advantage of using callbacks is that they can also take care of side effects. In this case, it calls `highlightFollowsSelection()` to ensure that selected items are also highlighted. Note that the callback function is able to connect selection and highlight because it has access to the entire container. Keeping the code of `setCallbackMap()` short also helps the reader to understand which interactions take place. This is vital, because we are purposely mixing concerns in this part of the code, which means that there is a higher risk of confusing the reader.
191
+ 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.
209
192
 
210
- ## The callback function can access the callbackMap object through `this`.
193
+ ## Callbacks can take care of side effects
211
194
 
212
- 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.selectionParams`.
195
+ An important advantage of using callbacks is that they can also take care of side effects. In this case, it calls `highlightFollowsSelection()` to ensure that selected items are also highlighted. Note that the callback function is able to connect selection and highlight because it has access to the entire container. Keeping the code of `setCallbackMap()` short also helps the reader to understand which interactions take place. This is vital, because we are purposely mixing concerns in this part of the code, which means that there is a higher risk of confusing the reader.
213
196
 
214
197
  ## The `exit()` callback function is called at the end of an operation
215
198
 
@@ -226,7 +209,7 @@ We've seen how facets are implemented, and how they can be added to a container
226
209
  ```
227
210
  // file: createContainer.ts
228
211
  import * from 'ramda' as R;
229
- import { mapDataToProps, pmap } from 'skandha';
212
+ import { mapDataToProps } from 'skandha';
230
213
 
231
214
  class TodosData {
232
215
  static className = () => 'Inputs';
@@ -247,30 +230,20 @@ const createContainer = () => {
247
230
  filtering: new Filtering(),
248
231
  }
249
232
  todosCtr.data.todos = loadTodos();
250
-
251
233
  const getTodoById = (id?: string) => id ? todosCtr.data.todoById[id] : undefined;
252
234
 
253
- mapDataToProps(
254
- pmap(
255
- [todosCtr.filtering, 'inputItems'],
256
- () => todosCtr.data.todos
257
- ),
258
- pmap(
259
- [todosCtr.data, 'filteredTodos'],
260
- () => todosCtr.filtering.filteredItems
261
- ),
262
- pmap(
263
- [todosCtr.selection, 'selectableIds'],
264
- () => R.map(R.prop('id'), todosCtr.data.filteredTodos)
265
- ),
266
- pmap(
267
- [todosCtr.selection, 'items'],
268
- () => R.map(getTodoById, todosCtr.selection.ids)
269
- ),
270
- pmap(
271
- [todosCtr.highlight, 'item'],
272
- () => getTodoById(todosCtr.highlight.id)
273
- ),
235
+ mapDataToProps(todosCtr,
236
+ filtering: {
237
+ inputItems: () => todosCtr.data.todos,
238
+ filteredItems: () => todosCtr.filtering.filteredItems,
239
+ },
240
+ selection: {
241
+ selectableIds: () => R.map(R.prop('id'), todosCtr.data.filteredTodos),
242
+ items: () => R.map(getTodoById, todosCtr.selection.ids),
243
+ },
244
+ highlight: {
245
+ item: () => getTodoById(todosCtr.highlight.id)
246
+ }
274
247
  )
275
248
  }
276
249
 
@@ -279,15 +252,11 @@ setOptions({logging: true});
279
252
 
280
253
  ## The `mapDataToProps()` function maps data between objects
281
254
 
282
- The `mapDataToProp()` function takes a container, field name and function and installs a `get` property (on the given container, using the given field name) that executes the given function. The `mapDataToProps()` is a small convenience function that calls `mapDataToProp()` on each of its arguments. In our example, we see that the `todosCtr.data.todos` field is mapped onto the `todosCtr.filtering.inputItems` field.
283
-
284
- ## The `pmap()` function adds type-checking
285
-
286
- The `pmap` function does not really do anything at run-time, but it adds type-checking at compile-time. It takes two arguments. The first argument is an array that has a container and a container-member. The second argument is a function. The `pmap` function checks that the output of this function can be stored in the container-member (if not, then a compile-time error results). It returns an array that contains its two arguments (because that is the format that `mapDataToProps` expects).
255
+ The `mapDataToProp()` function takes a container, field name and function and installs a `get` property (on the given container, using the given field name) that executes the given function. In our example, we see that the `todosCtr.data.todos` field is mapped onto the `todosCtr.filtering.inputItems` field.
287
256
 
288
257
  ## Logging can be enabled using `setOptions()`
289
258
 
290
- If you call `setOptions({logging: true})` then the SkandhaJS library logs each facet before and after each operation. All facet members that are decorated with `@data` (or `@input`, or `@output`) are logged in a way that looks similar to what you are used to from Redux:
259
+ If you call `setOptions({logging: true})` then the SkandhaJS library logs each facet before and after each operation. All facet members that are decorated with `@data` (or `@input`, or `@output`) are logged in a way similar to Redux:
291
260
 
292
261
  - before and after each operation, the entire container that holds the facet is logged
293
262
  - all data members of each facet in the container are included in the log
package/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  export { getCtrState } from './internal/logging';
2
2
  export { decorateCb, getOptions, setOptions } from './internal/options';
3
- export { getc, getFacetMemberNames } from './lib/ctr';
3
+ export { getFacetMemberNames, getc } from './lib/ctr';
4
4
  export { data, getDataMemberNames, getLoggedMemberNames, input, log, output, } from './lib/data';
5
5
  export { getf } from './lib/facet';
6
6
  export { installPolicies } from './lib/install';
7
7
  export { getOperationMemberNames, operation } from './lib/operation';
8
- export { mapDataToProp, mapDataToProps, pmap } from './lib/patch';
8
+ export { mapDataToProp, mapDataToProps } from './lib/patch';
9
9
  export { registerCtr } from './lib/registerCtr';
10
10
  export type { OptionsT as RegisterCtrOptionsT } from './lib/registerCtr';
11
- export declare type ClassT = any;
11
+ export type ClassT = any;
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  export { getCtrState } from './internal/logging';
2
2
  export { decorateCb, getOptions, setOptions } from './internal/options';
3
- export { getc, getFacetMemberNames } from './lib/ctr';
3
+ export { getFacetMemberNames, getc } from './lib/ctr';
4
4
  export { data, getDataMemberNames, getLoggedMemberNames, input, log, output, } from './lib/data';
5
5
  export { getf } from './lib/facet';
6
6
  export { installPolicies } from './lib/install';
7
7
  export { getOperationMemberNames, operation } from './lib/operation';
8
- export { mapDataToProp, mapDataToProps, pmap } from './lib/patch';
8
+ export { mapDataToProp, mapDataToProps } from './lib/patch';
9
9
  export { registerCtr } from './lib/registerCtr';
package/lib/operation.js CHANGED
@@ -13,7 +13,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
13
13
  function verb(n) { return function (v) { return step([n, v]); }; }
14
14
  function step(op) {
15
15
  if (f) throw new TypeError("Generator is already executing.");
16
- while (_) try {
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
17
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
18
  if (y = 0, t) op = [op[0] & 2, t.value];
19
19
  switch (op[0]) {
package/lib/patch.d.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  export declare function mapDataToProp<F, P extends keyof F>(facet: F, prop: P, getter: () => F[P]): void;
2
- export declare function mapDataToProps(...mappings: any[]): void;
3
- export declare function pmap<F, P extends keyof F>(member: [F, P], getter: () => F[P]): ([F, P] | (() => F[P]))[];
2
+ type Mappings<F> = {
3
+ [K in keyof F]?: {
4
+ [P in keyof F[K]]?: () => F[K][P];
5
+ };
6
+ };
7
+ export declare function mapDataToProps<T>(target: T, mappings: Mappings<T>): void;
8
+ export {};
package/lib/patch.js CHANGED
@@ -6,16 +6,21 @@ export function mapDataToProp(facet, prop, getter) {
6
6
  configurable: true,
7
7
  });
8
8
  }
9
- export function mapDataToProps() {
10
- var mappings = [];
11
- for (var _i = 0; _i < arguments.length; _i++) {
12
- mappings[_i] = arguments[_i];
9
+ export function mapDataToProps(target, mappings) {
10
+ for (var key in mappings) {
11
+ if (mappings.hasOwnProperty(key)) {
12
+ var facet = target[key];
13
+ var propMappings = mappings[key];
14
+ if (facet && propMappings) {
15
+ for (var prop in propMappings) {
16
+ if (propMappings.hasOwnProperty(prop)) {
17
+ var getter = propMappings[prop];
18
+ if (getter) {
19
+ mapDataToProp(facet, prop, getter);
20
+ }
21
+ }
22
+ }
23
+ }
24
+ }
13
25
  }
14
- for (var _a = 0, mappings_1 = mappings; _a < mappings_1.length; _a++) {
15
- var mapping = mappings_1[_a];
16
- mapDataToProp(mapping[0][0], mapping[0][1], mapping[1]);
17
- }
18
- }
19
- export function pmap(member, getter) {
20
- return [member, getter];
21
26
  }
@@ -1,4 +1,4 @@
1
- export declare type OptionsT = {
1
+ export type OptionsT = {
2
2
  name: string;
3
3
  facets?: string[];
4
4
  ctrState?: Function;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skandha",
3
- "version": "0.10.2",
3
+ "version": "0.11.0",
4
4
  "description": "Create data containers with modular behaviours",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/mnieber/skandha.git",