clapton 0.0.23 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5a917233f15421e530f894af3f7fa67b46b79c78d8568cd158566610f6c2db5
4
- data.tar.gz: 2935aea66e3fc3e7442606c0fcf55ffcea56b61bb36864c4fc77101680cc29ea
3
+ metadata.gz: f15ceb3c247b4a620ea3508dc66511a2d7beb4f2e1bc32a24dd8e69bd0d70332
4
+ data.tar.gz: 329b1b8107968efdcce936a4f71deb904e5a609d846ca245d8c90cc7040ba144
5
5
  SHA512:
6
- metadata.gz: 458297f32dca2583bfb675fb74bba00b781bef37d825b9a76e9116cea3c3bf0f98b18d1d6867d62b67d47638335c2f99d0ef3885358281f5fad63245e53e6d2f
7
- data.tar.gz: 586febac7acaf53ed42c0dadaa696c5e136bee87de707214343cf36104f2b56625f3aef3cfa4dfd71bd34c8108243813b26f8ea8f8fa7d7aea7ac2b686e9c25a
6
+ metadata.gz: 24846c65443543d58f3e8f6f6018b5ace3af64166be9b9e2bde2bd0aa5056c7bd2f148ba43b7171a7e43756d0eb485bebaad9fca5ad57a913b038bead70baf8e
7
+ data.tar.gz: 60aaeaae14b6277cd5323f5cf1e36201e94f2887e4c1655838ce37a13a58282de3b05c729e2a8212b2e1705f7e4e538f0b2f2b78e57b872f089d9956acc58b57
data/README.md CHANGED
@@ -177,6 +177,30 @@ class TaskListComponent < Clapton::Component
177
177
  end
178
178
  ```
179
179
 
180
+ ### Effect
181
+
182
+ The `effect` method is a method that is triggered when the state is changed.
183
+
184
+ ```ruby
185
+ # app/components/task_list_component.rb
186
+ class TaskListComponent < Clapton::Component
187
+ effect [:tasks] do |state|
188
+ puts state[:tasks]
189
+ end
190
+ end
191
+ ```
192
+
193
+ If dependencies are not specified, the effect will be triggered on the first render.
194
+
195
+ ```ruby
196
+ # app/components/video_player_component.rb
197
+ class VideoPlayerComponent < Clapton::Component
198
+ effect [] do
199
+ puts "First render"
200
+ end
201
+ end
202
+ ```
203
+
180
204
  ### Preset Components Classes
181
205
 
182
206
  ```ruby
@@ -405,7 +405,28 @@ var c = (function () {
405
405
  }
406
406
  return root.renderWrapper;
407
407
  }
408
+ static effect(dependencies, callback) {
409
+ this._effects.push({ dependencies, callback });
410
+ }
411
+ get effects() {
412
+ return this.constructor._effects;
413
+ }
414
+ runEffects() {
415
+ this.effects.forEach((effect) => {
416
+ if (effect.dependencies.some((dependency) => this._state[dependency] !== undefined)) {
417
+ effect.callback(this._state);
418
+ }
419
+ });
420
+ }
421
+ runEffectOnFirstRender() {
422
+ this.effects.forEach((effect) => {
423
+ if (effect.dependencies.length === 0) {
424
+ effect.callback(this._state);
425
+ }
426
+ });
427
+ }
408
428
  }
429
+ Component._effects = [];
409
430
 
410
431
  class TextField {
411
432
  constructor(state, attribute, attributes = {}) {
@@ -402,7 +402,28 @@ class Component {
402
402
  }
403
403
  return root.renderWrapper;
404
404
  }
405
+ static effect(dependencies, callback) {
406
+ this._effects.push({ dependencies, callback });
407
+ }
408
+ get effects() {
409
+ return this.constructor._effects;
410
+ }
411
+ runEffects() {
412
+ this.effects.forEach((effect) => {
413
+ if (effect.dependencies.some((dependency) => this._state[dependency] !== undefined)) {
414
+ effect.callback(this._state);
415
+ }
416
+ });
417
+ }
418
+ runEffectOnFirstRender() {
419
+ this.effects.forEach((effect) => {
420
+ if (effect.dependencies.length === 0) {
421
+ effect.callback(this._state);
422
+ }
423
+ });
424
+ }
405
425
  }
426
+ Component._effects = [];
406
427
 
407
428
  class TextField {
408
429
  constructor(state, attribute, attributes = {}) {
@@ -1292,6 +1292,7 @@ const updateComponent = async (component, state, property, target) => {
1292
1292
  const ComponentClass = module[componentName];
1293
1293
  const instance = new ComponentClass(state, component.dataset.id);
1294
1294
  morphdom(component, instance.renderWrapper);
1295
+ instance.runEffects();
1295
1296
  };
1296
1297
 
1297
1298
  const initializeInputs = () => {
@@ -1333,6 +1334,7 @@ const claptonChannel = consumer.subscriptions.create("Clapton::ClaptonChannel",
1333
1334
 
1334
1335
  initializeInputs();
1335
1336
  initializeActions();
1337
+ instance.runEffects();
1336
1338
  }
1337
1339
  });
1338
1340
 
@@ -1440,6 +1442,7 @@ const createAndAppendComponent = async (component, element) => {
1440
1442
  element.outerHTML = firstChild.outerHTML;
1441
1443
  }
1442
1444
  }
1445
+ instance.runEffects();
1443
1446
  };
1444
1447
  document.addEventListener("DOMContentLoaded", async () => {
1445
1448
  await initializeComponents();
@@ -405,7 +405,28 @@ var Clapton = (function (exports) {
405
405
  }
406
406
  return root.renderWrapper;
407
407
  }
408
+ static effect(dependencies, callback) {
409
+ this._effects.push({ dependencies, callback });
410
+ }
411
+ get effects() {
412
+ return this.constructor._effects;
413
+ }
414
+ runEffects() {
415
+ this.effects.forEach((effect) => {
416
+ if (effect.dependencies.some((dependency) => this._state[dependency] !== undefined)) {
417
+ effect.callback(this._state);
418
+ }
419
+ });
420
+ }
421
+ runEffectOnFirstRender() {
422
+ this.effects.forEach((effect) => {
423
+ if (effect.dependencies.length === 0) {
424
+ effect.callback(this._state);
425
+ }
426
+ });
427
+ }
408
428
  }
429
+ Component._effects = [];
409
430
 
410
431
  class TextField {
411
432
  constructor(state, attribute, attributes = {}) {
@@ -402,7 +402,28 @@ class Component {
402
402
  }
403
403
  return root.renderWrapper;
404
404
  }
405
+ static effect(dependencies, callback) {
406
+ this._effects.push({ dependencies, callback });
407
+ }
408
+ get effects() {
409
+ return this.constructor._effects;
410
+ }
411
+ runEffects() {
412
+ this.effects.forEach((effect) => {
413
+ if (effect.dependencies.some((dependency) => this._state[dependency] !== undefined)) {
414
+ effect.callback(this._state);
415
+ }
416
+ });
417
+ }
418
+ runEffectOnFirstRender() {
419
+ this.effects.forEach((effect) => {
420
+ if (effect.dependencies.length === 0) {
421
+ effect.callback(this._state);
422
+ }
423
+ });
424
+ }
405
425
  }
426
+ Component._effects = [];
406
427
 
407
428
  class TextField {
408
429
  constructor(state, attribute, attributes = {}) {
@@ -25,5 +25,6 @@ export const claptonChannel = consumer.subscriptions.create("Clapton::ClaptonCha
25
25
 
26
26
  initializeInputs();
27
27
  initializeActions();
28
+ instance.runEffects();
28
29
  }
29
30
  })
@@ -36,6 +36,7 @@ const createAndAppendComponent = async (component: ComponentDefinition, element:
36
36
  element.outerHTML = firstChild.outerHTML;
37
37
  }
38
38
  }
39
+ instance.runEffects();
39
40
  };
40
41
 
41
42
  document.addEventListener("DOMContentLoaded", async () => {
@@ -5,6 +5,8 @@ export class Component {
5
5
  _state: any;
6
6
  _errors: any[];
7
7
 
8
+ static _effects: any[] = [];
9
+
8
10
  constructor(state: any = {}, id: string = Math.random().toString(36).substring(2, 10), errors: any[] = []) {
9
11
  this._state = state;
10
12
  this.id = id;
@@ -24,4 +26,28 @@ export class Component {
24
26
  }
25
27
  return root.renderWrapper;
26
28
  }
29
+
30
+ static effect(dependencies: any[], callback: () => void) {
31
+ this._effects.push({ dependencies, callback });
32
+ }
33
+
34
+ get effects(): any[] {
35
+ return (this.constructor as typeof Component)._effects;
36
+ }
37
+
38
+ runEffects() {
39
+ this.effects.forEach((effect) => {
40
+ if (effect.dependencies.some((dependency: any) => this._state[dependency] !== undefined)) {
41
+ effect.callback(this._state);
42
+ }
43
+ });
44
+ }
45
+
46
+ runEffectOnFirstRender() {
47
+ this.effects.forEach((effect) => {
48
+ if (effect.dependencies.length === 0) {
49
+ effect.callback(this._state);
50
+ }
51
+ });
52
+ }
27
53
  }
@@ -7,4 +7,5 @@ export const updateComponent = async (component: HTMLElement, state: any, proper
7
7
  const ComponentClass = module[componentName] as any;
8
8
  const instance = new ComponentClass(state, component.dataset.id);
9
9
  morphdom(component, instance.renderWrapper);
10
+ instance.runEffects();
10
11
  };
@@ -1,3 +1,3 @@
1
1
  module Clapton
2
- VERSION = '0.0.23'
2
+ VERSION = '0.0.24'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clapton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moeki Kawakami