@tko/lifecycle 4.0.0-alpha8.0 → 4.0.0-beta1.3

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.
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ // @tko/lifecycle 🥊 4.0.0-beta1.3 ESM
3
+ import {
4
+ addDisposeCallback,
5
+ createSymbolOrString
6
+ } from "@tko/utils";
7
+ import {
8
+ computed
9
+ } from "@tko/computed";
10
+ const SUBSCRIPTIONS = createSymbolOrString("LifeCycle Subscriptions List");
11
+ const ANCHOR_NODE = createSymbolOrString("LifeCycle Anchor Node");
12
+ export default class LifeCycle {
13
+ static mixInto(Constructor) {
14
+ const target = Constructor.prototype || Constructor;
15
+ const mixin = LifeCycle.prototype;
16
+ for (let prop of Object.getOwnPropertyNames(mixin)) {
17
+ target[prop] = mixin[prop];
18
+ }
19
+ }
20
+ subscribe(observable, action, subscriptionType) {
21
+ if (typeof action === "string") {
22
+ action = this[action];
23
+ }
24
+ this.addDisposable(observable.subscribe(action, this, subscriptionType));
25
+ }
26
+ computed(params) {
27
+ if (typeof params === "string") {
28
+ params = { read: this[params], write: this[params] };
29
+ }
30
+ if (typeof params === "object") {
31
+ params = Object.assign({ owner: this }, params);
32
+ } else if (typeof params === "function") {
33
+ const proto = Object.getPrototypeOf(this);
34
+ if (proto && proto[params.name] === params) {
35
+ params = params.bind(this);
36
+ }
37
+ params = { read: params, write: params };
38
+ } else {
39
+ throw new Error("LifeCycle::computed not given a valid type.");
40
+ }
41
+ params.disposeWhenNodeIsRemoved = this[ANCHOR_NODE];
42
+ return this.addDisposable(computed(params));
43
+ }
44
+ addEventListener(...args) {
45
+ const node = args[0].nodeType ? args.shift() : this[ANCHOR_NODE];
46
+ const [type, act, options] = args;
47
+ const handler = typeof act === "string" ? this[act].bind(this) : act;
48
+ this.__addEventListener(node, type, handler, options);
49
+ }
50
+ __addEventListener(node, eventType, handler, options) {
51
+ node.addEventListener(eventType, handler, options);
52
+ function dispose() {
53
+ node.removeEventListener(eventType, handler);
54
+ }
55
+ addDisposeCallback(node, dispose);
56
+ this.addDisposable({ dispose });
57
+ }
58
+ anchorTo(nodeOrLifeCycle) {
59
+ if ("addDisposable" in nodeOrLifeCycle) {
60
+ nodeOrLifeCycle.addDisposable(this);
61
+ this[ANCHOR_NODE] = null;
62
+ } else {
63
+ this[ANCHOR_NODE] = nodeOrLifeCycle;
64
+ addDisposeCallback(nodeOrLifeCycle, () => this[ANCHOR_NODE] === nodeOrLifeCycle && this.dispose());
65
+ }
66
+ }
67
+ dispose() {
68
+ const subscriptions = this[SUBSCRIPTIONS] || [];
69
+ subscriptions.forEach((s) => s.dispose());
70
+ this[SUBSCRIPTIONS] = [];
71
+ this[ANCHOR_NODE] = null;
72
+ }
73
+ addDisposable(subscription) {
74
+ const subscriptions = this[SUBSCRIPTIONS] || [];
75
+ if (!this[SUBSCRIPTIONS]) {
76
+ this[SUBSCRIPTIONS] = subscriptions;
77
+ }
78
+ if (typeof subscription.dispose !== "function") {
79
+ throw new Error("Lifecycle::addDisposable argument missing `dispose`.");
80
+ }
81
+ subscriptions.push(subscription);
82
+ return subscription;
83
+ }
84
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/LifeCycle.ts"],
4
+ "sourcesContent": ["'use strict'\n\nimport {\n addDisposeCallback, createSymbolOrString\n} from '@tko/utils'\n\nimport {\n computed\n} 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 (let prop of Object.getOwnPropertyNames(mixin)) {\n target[prop] = mixin[prop]\n }\n }\n\n subscribe (observable, action, subscriptionType) {\n if (typeof action === 'string') { action = this[action] }\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) { params = params.bind(this) }\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 () { node.removeEventListener(eventType, handler) }\n addDisposeCallback(node, dispose)\n this.addDisposable({ dispose })\n }\n\n anchorTo (nodeOrLifeCycle) {\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] = []\n this[ANCHOR_NODE] = null\n }\n\n addDisposable (subscription) {\n const subscriptions = this[SUBSCRIPTIONS] || []\n if (!this[SUBSCRIPTIONS]) { this[SUBSCRIPTIONS] = subscriptions }\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;AAAA;AAAA;AAAA;AAIA;AAAA;AAAA;AAIA,MAAM,gBAAgB,qBAAqB,8BAA8B;AACzE,MAAM,cAAc,qBAAqB,uBAAuB;AAEhE,qBAAqB,UAAU;AAAA,SAStB,QAAS,aAAa;AAC3B,UAAM,SAAS,YAAY,aAAa;AACxC,UAAM,QAAQ,UAAU;AACxB,aAAS,QAAQ,OAAO,oBAAoB,KAAK,GAAG;AAClD,aAAO,QAAQ,MAAM;AAAA,IACvB;AAAA,EACF;AAAA,EAEA,UAAW,YAAY,QAAQ,kBAAkB;AAC/C,QAAI,OAAO,WAAW,UAAU;AAAE,eAAS,KAAK;AAAA,IAAQ;AACxD,SAAK,cAAc,WAAW,UAAU,QAAQ,MAAM,gBAAgB,CAAC;AAAA,EACzE;AAAA,EAEA,SAAU,QAAQ;AAChB,QAAI,OAAO,WAAW,UAAU;AAC9B,eAAS,EAAE,MAAM,KAAK,SAAS,OAAO,KAAK,QAAQ;AAAA,IACrD;AAEA,QAAI,OAAO,WAAW,UAAU;AAC9B,eAAS,OAAO,OAAO,EAAE,OAAO,KAAK,GAAG,MAAM;AAAA,IAChD,WAAW,OAAO,WAAW,YAAY;AACvC,YAAM,QAAQ,OAAO,eAAe,IAAI;AACxC,UAAI,SAAS,MAAM,OAAO,UAAU,QAAQ;AAAE,iBAAS,OAAO,KAAK,IAAI;AAAA,MAAE;AACzE,eAAS,EAAE,MAAM,QAAQ,OAAO,OAAO;AAAA,IACzC,OAAO;AACL,YAAM,IAAI,MAAM,6CAA6C;AAAA,IAC/D;AAEA,WAAO,2BAA2B,KAAK;AACvC,WAAO,KAAK,cAAc,SAAS,MAAM,CAAC;AAAA,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