@tko/lifecycle 4.0.0-beta1.3 → 4.0.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/dist/LifeCycle.js +19 -10
- package/dist/LifeCycle.js.map +2 -2
- package/dist/index.cjs +425 -315
- package/dist/index.cjs.map +4 -4
- package/dist/index.js +2 -1
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +2 -2
- package/package.json +5 -6
- package/LICENSE +0 -22
package/dist/LifeCycle.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
// @tko/lifecycle 🥊 4.0.0 ESM
|
|
1
2
|
"use strict";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
addDisposeCallback,
|
|
5
|
-
createSymbolOrString
|
|
6
|
-
} from "@tko/utils";
|
|
7
|
-
import {
|
|
8
|
-
computed
|
|
9
|
-
} from "@tko/computed";
|
|
3
|
+
import { addDisposeCallback, createSymbolOrString } from "@tko/utils";
|
|
4
|
+
import { computed } from "@tko/computed";
|
|
10
5
|
const SUBSCRIPTIONS = createSymbolOrString("LifeCycle Subscriptions List");
|
|
11
6
|
const ANCHOR_NODE = createSymbolOrString("LifeCycle Anchor Node");
|
|
12
7
|
export default class LifeCycle {
|
|
8
|
+
// NOTE: For more advanced integration as an ES6 mixin, see e.g.:
|
|
9
|
+
// http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
|
|
10
|
+
/**
|
|
11
|
+
* Copy the properties of the LifeCycle class to the target (or its prototype)
|
|
12
|
+
*
|
|
13
|
+
* NOTE: getOwnPropertyNames is needed to copy the non-enumerable properties.
|
|
14
|
+
*/
|
|
13
15
|
static mixInto(Constructor) {
|
|
14
16
|
const target = Constructor.prototype || Constructor;
|
|
15
17
|
const mixin = LifeCycle.prototype;
|
|
16
|
-
for (
|
|
18
|
+
for (const prop of Object.getOwnPropertyNames(mixin)) {
|
|
17
19
|
target[prop] = mixin[prop];
|
|
18
20
|
}
|
|
19
21
|
}
|
|
@@ -41,6 +43,13 @@ export default class LifeCycle {
|
|
|
41
43
|
params.disposeWhenNodeIsRemoved = this[ANCHOR_NODE];
|
|
42
44
|
return this.addDisposable(computed(params));
|
|
43
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Add an event listener for the given or anchored node.
|
|
48
|
+
* @param {node} [node] (optional) The target node (otherwise the anchored node)
|
|
49
|
+
* @param {string} [type] Event type
|
|
50
|
+
* @param {function|string} [action] Either call the given function or `this[action]`
|
|
51
|
+
* @param {object} [options] (optional) Passed as `options` to `node.addEventListener`
|
|
52
|
+
*/
|
|
44
53
|
addEventListener(...args) {
|
|
45
54
|
const node = args[0].nodeType ? args.shift() : this[ANCHOR_NODE];
|
|
46
55
|
const [type, act, options] = args;
|
|
@@ -67,7 +76,7 @@ export default class LifeCycle {
|
|
|
67
76
|
dispose() {
|
|
68
77
|
const subscriptions = this[SUBSCRIPTIONS] || [];
|
|
69
78
|
subscriptions.forEach((s) => s.dispose());
|
|
70
|
-
this[SUBSCRIPTIONS] =
|
|
79
|
+
this[SUBSCRIPTIONS] = new Array();
|
|
71
80
|
this[ANCHOR_NODE] = null;
|
|
72
81
|
}
|
|
73
82
|
addDisposable(subscription) {
|
package/dist/LifeCycle.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/LifeCycle.ts"],
|
|
4
|
-
"sourcesContent": ["'use strict'\n\nimport {
|
|
5
|
-
"mappings": ";;AAEA;
|
|
4
|
+
"sourcesContent": ["'use strict'\n\nimport { addDisposeCallback, createSymbolOrString } from '@tko/utils'\n\nimport { computed } from '@tko/computed'\n\nconst SUBSCRIPTIONS = createSymbolOrString('LifeCycle Subscriptions List')\nconst ANCHOR_NODE = createSymbolOrString('LifeCycle Anchor Node')\n\nexport default class LifeCycle {\n // NOTE: For more advanced integration as an ES6 mixin, see e.g.:\n // http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/\n\n /**\n * Copy the properties of the LifeCycle class to the target (or its prototype)\n *\n * NOTE: getOwnPropertyNames is needed to copy the non-enumerable properties.\n */\n static mixInto(Constructor) {\n const target = Constructor.prototype || Constructor\n const mixin = LifeCycle.prototype\n for (const prop of Object.getOwnPropertyNames(mixin)) {\n target[prop] = mixin[prop]\n }\n }\n\n subscribe(observable, action, subscriptionType) {\n if (typeof action === 'string') {\n action = this[action]\n }\n this.addDisposable(observable.subscribe(action, this, subscriptionType))\n }\n\n computed(params) {\n if (typeof params === 'string') {\n params = { read: this[params], write: this[params] }\n }\n\n if (typeof params === 'object') {\n params = Object.assign({ owner: this }, params)\n } else if (typeof params === 'function') {\n const proto = Object.getPrototypeOf(this)\n if (proto && proto[params.name] === params) {\n params = params.bind(this)\n }\n params = { read: params, write: params }\n } else {\n throw new Error('LifeCycle::computed not given a valid type.')\n }\n\n params.disposeWhenNodeIsRemoved = this[ANCHOR_NODE]\n return this.addDisposable(computed(params))\n }\n\n /**\n * Add an event listener for the given or anchored node.\n * @param {node} [node] (optional) The target node (otherwise the anchored node)\n * @param {string} [type] Event type\n * @param {function|string} [action] Either call the given function or `this[action]`\n * @param {object} [options] (optional) Passed as `options` to `node.addEventListener`\n */\n addEventListener(...args) {\n const node = args[0].nodeType ? args.shift() : this[ANCHOR_NODE]\n const [type, act, options] = args\n const handler = typeof act === 'string' ? this[act].bind(this) : act\n this.__addEventListener(node, type, handler, options)\n }\n\n __addEventListener(node, eventType, handler, options) {\n node.addEventListener(eventType, handler, options)\n function dispose() {\n node.removeEventListener(eventType, handler)\n }\n addDisposeCallback(node, dispose)\n this.addDisposable({ dispose })\n }\n\n anchorTo(nodeOrLifeCycle: Node | LifeCycle) {\n if ('addDisposable' in nodeOrLifeCycle) {\n nodeOrLifeCycle.addDisposable(this)\n this[ANCHOR_NODE] = null // re-anchor on `anchorTo` calls\n } else {\n this[ANCHOR_NODE] = nodeOrLifeCycle\n addDisposeCallback(nodeOrLifeCycle, () => this[ANCHOR_NODE] === nodeOrLifeCycle && this.dispose())\n }\n }\n\n dispose() {\n const subscriptions = this[SUBSCRIPTIONS] || []\n subscriptions.forEach(s => s.dispose())\n this[SUBSCRIPTIONS] = new Array()\n this[ANCHOR_NODE] = null\n }\n\n addDisposable(subscription) {\n const subscriptions = this[SUBSCRIPTIONS] || []\n if (!this[SUBSCRIPTIONS]) {\n this[SUBSCRIPTIONS] = subscriptions\n }\n if (typeof subscription.dispose !== 'function') {\n throw new Error('Lifecycle::addDisposable argument missing `dispose`.')\n }\n subscriptions.push(subscription)\n return subscription\n }\n}\n"],
|
|
5
|
+
"mappings": ";;AAEA,SAAS,oBAAoB,4BAA4B;AAEzD,SAAS,gBAAgB;AAEzB,MAAM,gBAAgB,qBAAqB,8BAA8B;AACzE,MAAM,cAAc,qBAAqB,uBAAuB;AAEhE,qBAAqB,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS7B,OAAO,QAAQ,aAAa;AAC1B,UAAM,SAAS,YAAY,aAAa;AACxC,UAAM,QAAQ,UAAU;AACxB,eAAW,QAAQ,OAAO,oBAAoB,KAAK,GAAG;AACpD,aAAO,IAAI,IAAI,MAAM,IAAI;AAAA,IAC3B;AAAA,EACF;AAAA,EAEA,UAAU,YAAY,QAAQ,kBAAkB;AAC9C,QAAI,OAAO,WAAW,UAAU;AAC9B,eAAS,KAAK,MAAM;AAAA,IACtB;AACA,SAAK,cAAc,WAAW,UAAU,QAAQ,MAAM,gBAAgB,CAAC;AAAA,EACzE;AAAA,EAEA,SAAS,QAAQ;AACf,QAAI,OAAO,WAAW,UAAU;AAC9B,eAAS,EAAE,MAAM,KAAK,MAAM,GAAG,OAAO,KAAK,MAAM,EAAE;AAAA,IACrD;AAEA,QAAI,OAAO,WAAW,UAAU;AAC9B,eAAS,OAAO,OAAO,EAAE,OAAO,KAAK,GAAG,MAAM;AAAA,IAChD,WAAW,OAAO,WAAW,YAAY;AACvC,YA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